
(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 99.9%
Final simplification99.9%
(FPCore (x y) :precision binary64 (if (<= y -2.2e-48) (* (fabs (- y x)) 0.5) (/ (+ x y) 2.0)))
double code(double x, double y) {
double tmp;
if (y <= -2.2e-48) {
tmp = fabs((y - x)) * 0.5;
} else {
tmp = (x + y) / 2.0;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (y <= (-2.2d-48)) then
tmp = abs((y - x)) * 0.5d0
else
tmp = (x + y) / 2.0d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= -2.2e-48) {
tmp = Math.abs((y - x)) * 0.5;
} else {
tmp = (x + y) / 2.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -2.2e-48: tmp = math.fabs((y - x)) * 0.5 else: tmp = (x + y) / 2.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -2.2e-48) tmp = Float64(abs(Float64(y - x)) * 0.5); else tmp = Float64(Float64(x + y) / 2.0); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= -2.2e-48) tmp = abs((y - x)) * 0.5; else tmp = (x + y) / 2.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -2.2e-48], N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(N[(x + y), $MachinePrecision] / 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.2 \cdot 10^{-48}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{x + y}{2}\\
\end{array}
\end{array}
if y < -2.20000000000000013e-48Initial program 99.9%
Taylor expanded in x around 0 76.6%
if -2.20000000000000013e-48 < y Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt61.8%
fabs-sqr61.8%
add-sqr-sqrt68.8%
metadata-eval68.8%
Applied egg-rr68.8%
fma-udef68.8%
flip-+37.7%
pow237.7%
pow237.7%
Applied egg-rr37.7%
unpow237.7%
unpow237.7%
difference-of-squares39.9%
fma-def39.9%
associate-/l*68.8%
fma-def68.8%
+-commutative68.8%
*-commutative68.8%
sub-neg68.8%
+-commutative68.8%
distribute-lft-in68.8%
distribute-rgt-neg-in68.8%
distribute-lft-neg-in68.8%
metadata-eval68.8%
associate-+r+68.8%
distribute-rgt1-in68.8%
metadata-eval68.8%
distribute-lft-out68.8%
*-inverses68.8%
Simplified68.8%
Final simplification71.3%
(FPCore (x y) :precision binary64 (if (<= y 1.7e-176) x (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 1.7e-176) {
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.7d-176) 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.7e-176) {
tmp = x;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 1.7e-176: tmp = x else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 1.7e-176) tmp = x; else tmp = Float64(y * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 1.7e-176) tmp = x; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 1.7e-176], x, N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.7 \cdot 10^{-176}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 1.6999999999999999e-176Initial program 99.9%
Taylor expanded in x around inf 13.2%
if 1.6999999999999999e-176 < y Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt77.7%
fabs-sqr77.7%
add-sqr-sqrt82.1%
metadata-eval82.1%
Applied egg-rr82.1%
Taylor expanded in y around inf 58.8%
Final simplification29.6%
(FPCore (x y) :precision binary64 (if (<= y 1.95e-50) (* x 0.5) (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 1.95e-50) {
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.95d-50) 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.95e-50) {
tmp = x * 0.5;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 1.95e-50: tmp = x * 0.5 else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 1.95e-50) 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.95e-50) tmp = x * 0.5; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 1.95e-50], N[(x * 0.5), $MachinePrecision], N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.95 \cdot 10^{-50}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 1.9500000000000001e-50Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt34.0%
fabs-sqr34.0%
add-sqr-sqrt40.1%
metadata-eval40.1%
Applied egg-rr40.1%
Taylor expanded in y around 0 36.5%
distribute-rgt1-in36.5%
metadata-eval36.5%
*-commutative36.5%
Simplified36.5%
if 1.9500000000000001e-50 < y Initial program 100.0%
+-commutative100.0%
div-inv100.0%
fma-def100.0%
add-sqr-sqrt79.1%
fabs-sqr79.1%
add-sqr-sqrt83.3%
metadata-eval83.3%
Applied egg-rr83.3%
Taylor expanded in y around inf 67.7%
Final simplification44.9%
(FPCore (x y) :precision binary64 (/ (+ x y) 2.0))
double code(double x, double y) {
return (x + y) / 2.0;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = (x + y) / 2.0d0
end function
public static double code(double x, double y) {
return (x + y) / 2.0;
}
def code(x, y): return (x + y) / 2.0
function code(x, y) return Float64(Float64(x + y) / 2.0) end
function tmp = code(x, y) tmp = (x + y) / 2.0; end
code[x_, y_] := N[(N[(x + y), $MachinePrecision] / 2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y}{2}
\end{array}
Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt46.1%
fabs-sqr46.1%
add-sqr-sqrt51.7%
metadata-eval51.7%
Applied egg-rr51.7%
fma-udef51.7%
flip-+28.2%
pow228.2%
pow228.2%
Applied egg-rr28.2%
unpow228.2%
unpow228.2%
difference-of-squares30.0%
fma-def30.0%
associate-/l*51.7%
fma-def51.7%
+-commutative51.7%
*-commutative51.7%
sub-neg51.7%
+-commutative51.7%
distribute-lft-in51.7%
distribute-rgt-neg-in51.7%
distribute-lft-neg-in51.7%
metadata-eval51.7%
associate-+r+51.7%
distribute-rgt1-in51.7%
metadata-eval51.7%
distribute-lft-out51.7%
*-inverses51.7%
Simplified51.7%
Final simplification51.7%
(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 99.9%
Taylor expanded in x around inf 12.0%
Final simplification12.0%
herbie shell --seed 2024036
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))