
(FPCore (x y) :precision binary64 (+ x (/ (fabs (- y x)) 2.0)))
double code(double x, double y) {
return x + (fabs((y - x)) / 2.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x + (abs((y - x)) / 2.0d0)
end function
public static double code(double x, double y) {
return x + (Math.abs((y - x)) / 2.0);
}
def code(x, y): return x + (math.fabs((y - x)) / 2.0)
function code(x, y) return Float64(x + Float64(abs(Float64(y - x)) / 2.0)) end
function tmp = code(x, y) tmp = x + (abs((y - x)) / 2.0); end
code[x_, y_] := N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \frac{\left|y - x\right|}{2}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y) :precision binary64 (+ x (/ (fabs (- y x)) 2.0)))
double code(double x, double y) {
return x + (fabs((y - x)) / 2.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x + (abs((y - x)) / 2.0d0)
end function
public static double code(double x, double y) {
return x + (Math.abs((y - x)) / 2.0);
}
def code(x, y): return x + (math.fabs((y - x)) / 2.0)
function code(x, y) return Float64(x + Float64(abs(Float64(y - x)) / 2.0)) end
function tmp = code(x, y) tmp = x + (abs((y - x)) / 2.0); end
code[x_, y_] := N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \frac{\left|y - x\right|}{2}
\end{array}
(FPCore (x y) :precision binary64 (+ x (/ (fabs (- y x)) 2.0)))
double code(double x, double y) {
return x + (fabs((y - x)) / 2.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x + (abs((y - x)) / 2.0d0)
end function
public static double code(double x, double y) {
return x + (Math.abs((y - x)) / 2.0);
}
def code(x, y): return x + (math.fabs((y - x)) / 2.0)
function code(x, y) return Float64(x + Float64(abs(Float64(y - x)) / 2.0)) end
function tmp = code(x, y) tmp = x + (abs((y - x)) / 2.0); end
code[x_, y_] := N[(x + N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \frac{\left|y - x\right|}{2}
\end{array}
Initial program 100.0%
Final simplification100.0%
(FPCore (x y) :precision binary64 (if (or (<= y -57000.0) (and (not (<= y -3.5e-124)) (<= y -1.42e-185))) (* (fabs (- y x)) 0.5) (* 0.5 (+ x y))))
double code(double x, double y) {
double tmp;
if ((y <= -57000.0) || (!(y <= -3.5e-124) && (y <= -1.42e-185))) {
tmp = fabs((y - x)) * 0.5;
} else {
tmp = 0.5 * (x + y);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if ((y <= (-57000.0d0)) .or. (.not. (y <= (-3.5d-124))) .and. (y <= (-1.42d-185))) then
tmp = abs((y - x)) * 0.5d0
else
tmp = 0.5d0 * (x + y)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if ((y <= -57000.0) || (!(y <= -3.5e-124) && (y <= -1.42e-185))) {
tmp = Math.abs((y - x)) * 0.5;
} else {
tmp = 0.5 * (x + y);
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -57000.0) or (not (y <= -3.5e-124) and (y <= -1.42e-185)): tmp = math.fabs((y - x)) * 0.5 else: tmp = 0.5 * (x + y) return tmp
function code(x, y) tmp = 0.0 if ((y <= -57000.0) || (!(y <= -3.5e-124) && (y <= -1.42e-185))) tmp = Float64(abs(Float64(y - x)) * 0.5); else tmp = Float64(0.5 * Float64(x + y)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if ((y <= -57000.0) || (~((y <= -3.5e-124)) && (y <= -1.42e-185))) tmp = abs((y - x)) * 0.5; else tmp = 0.5 * (x + y); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -57000.0], And[N[Not[LessEqual[y, -3.5e-124]], $MachinePrecision], LessEqual[y, -1.42e-185]]], N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(0.5 * N[(x + y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -57000 \lor \neg \left(y \leq -3.5 \cdot 10^{-124}\right) \land y \leq -1.42 \cdot 10^{-185}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(x + y\right)\\
\end{array}
\end{array}
if y < -57000 or -3.4999999999999999e-124 < y < -1.42000000000000003e-185Initial program 100.0%
Taylor expanded in x around 0 81.0%
if -57000 < y < -3.4999999999999999e-124 or -1.42000000000000003e-185 < y Initial program 100.0%
+-commutative100.0%
div-inv100.0%
fma-def100.0%
add-sqr-sqrt69.4%
fabs-sqr69.4%
add-sqr-sqrt74.4%
metadata-eval74.4%
Applied egg-rr74.4%
Taylor expanded in y around 0 74.4%
associate-+r+74.4%
distribute-rgt1-in74.4%
metadata-eval74.4%
distribute-lft-out74.4%
Simplified74.4%
Final simplification76.1%
(FPCore (x y) :precision binary64 (if (<= y 1.75e-255) x (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 1.75e-255) {
tmp = x;
} else {
tmp = y * 0.5;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 1.75d-255) then
tmp = x
else
tmp = y * 0.5d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 1.75e-255) {
tmp = x;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 1.75e-255: tmp = x else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 1.75e-255) tmp = x; else tmp = Float64(y * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 1.75e-255) tmp = x; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 1.75e-255], x, N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.75 \cdot 10^{-255}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 1.74999999999999989e-255Initial program 100.0%
Taylor expanded in x around inf 11.5%
if 1.74999999999999989e-255 < y Initial program 100.0%
+-commutative100.0%
div-inv100.0%
fma-def100.0%
add-sqr-sqrt78.9%
fabs-sqr78.9%
add-sqr-sqrt83.1%
metadata-eval83.1%
Applied egg-rr83.1%
Taylor expanded in y around inf 55.3%
Final simplification33.2%
(FPCore (x y) :precision binary64 (if (<= y 1.08e-114) (* x 0.5) (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 1.08e-114) {
tmp = x * 0.5;
} else {
tmp = y * 0.5;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= 1.08d-114) then
tmp = x * 0.5d0
else
tmp = y * 0.5d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 1.08e-114) {
tmp = x * 0.5;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 1.08e-114: tmp = x * 0.5 else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 1.08e-114) tmp = Float64(x * 0.5); else tmp = Float64(y * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 1.08e-114) tmp = x * 0.5; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 1.08e-114], N[(x * 0.5), $MachinePrecision], N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.08 \cdot 10^{-114}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 1.08e-114Initial program 100.0%
+-commutative100.0%
div-inv100.0%
fma-def100.0%
add-sqr-sqrt33.7%
fabs-sqr33.7%
add-sqr-sqrt39.0%
metadata-eval39.0%
Applied egg-rr39.0%
Taylor expanded in y around 0 35.9%
distribute-rgt1-in35.9%
metadata-eval35.9%
*-commutative35.9%
Simplified35.9%
if 1.08e-114 < y Initial program 100.0%
+-commutative100.0%
div-inv100.0%
fma-def100.0%
add-sqr-sqrt83.8%
fabs-sqr83.8%
add-sqr-sqrt87.2%
metadata-eval87.2%
Applied egg-rr87.2%
Taylor expanded in y around inf 65.4%
Final simplification47.1%
(FPCore (x y) :precision binary64 (* 0.5 (+ x y)))
double code(double x, double y) {
return 0.5 * (x + y);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = 0.5d0 * (x + y)
end function
public static double code(double x, double y) {
return 0.5 * (x + y);
}
def code(x, y): return 0.5 * (x + y)
function code(x, y) return Float64(0.5 * Float64(x + y)) end
function tmp = code(x, y) tmp = 0.5 * (x + y); end
code[x_, y_] := N[(0.5 * N[(x + y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
0.5 \cdot \left(x + y\right)
\end{array}
Initial program 100.0%
+-commutative100.0%
div-inv100.0%
fma-def100.0%
add-sqr-sqrt52.7%
fabs-sqr52.7%
add-sqr-sqrt57.3%
metadata-eval57.3%
Applied egg-rr57.3%
Taylor expanded in y around 0 57.3%
associate-+r+57.3%
distribute-rgt1-in57.3%
metadata-eval57.3%
distribute-lft-out57.3%
Simplified57.3%
Final simplification57.3%
(FPCore (x y) :precision binary64 x)
double code(double x, double y) {
return x;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x
end function
public static double code(double x, double y) {
return x;
}
def code(x, y): return x
function code(x, y) return x end
function tmp = code(x, y) tmp = x; end
code[x_, y_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 100.0%
Taylor expanded in x around inf 10.9%
Final simplification10.9%
herbie shell --seed 2024031
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))