
(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 -5.5e-182) (* (fabs (- y x)) 0.5) (/ (+ x y) 2.0)))
double code(double x, double y) {
double tmp;
if (y <= -5.5e-182) {
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 <= (-5.5d-182)) 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 <= -5.5e-182) {
tmp = Math.abs((y - x)) * 0.5;
} else {
tmp = (x + y) / 2.0;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -5.5e-182: tmp = math.fabs((y - x)) * 0.5 else: tmp = (x + y) / 2.0 return tmp
function code(x, y) tmp = 0.0 if (y <= -5.5e-182) 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 <= -5.5e-182) tmp = abs((y - x)) * 0.5; else tmp = (x + y) / 2.0; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -5.5e-182], 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 -5.5 \cdot 10^{-182}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;\frac{x + y}{2}\\
\end{array}
\end{array}
if y < -5.49999999999999993e-182Initial program 100.0%
Taylor expanded in x around 0 63.0%
if -5.49999999999999993e-182 < y Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt75.2%
fabs-sqr75.2%
add-sqr-sqrt79.8%
metadata-eval79.8%
Applied egg-rr79.8%
fma-udef79.8%
flip-+45.3%
pow245.3%
pow245.3%
Applied egg-rr45.3%
unpow245.3%
unpow245.3%
difference-of-squares46.9%
fma-def46.9%
associate-/l*79.8%
fma-def79.8%
*-commutative79.8%
sub-neg79.8%
+-commutative79.8%
distribute-lft-in79.8%
distribute-rgt-neg-in79.8%
distribute-lft-neg-in79.8%
metadata-eval79.8%
+-commutative79.8%
associate-+r+79.8%
distribute-rgt1-in79.8%
metadata-eval79.8%
distribute-lft-out79.8%
*-inverses79.8%
Simplified79.8%
Final simplification73.0%
(FPCore (x y) :precision binary64 (if (<= y 2.25e-140) x (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 2.25e-140) {
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 <= 2.25d-140) 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 <= 2.25e-140) {
tmp = x;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 2.25e-140: tmp = x else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 2.25e-140) tmp = x; else tmp = Float64(y * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 2.25e-140) tmp = x; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 2.25e-140], x, N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.25 \cdot 10^{-140}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 2.25000000000000002e-140Initial program 99.9%
Taylor expanded in x around inf 13.1%
if 2.25000000000000002e-140 < y Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt89.6%
fabs-sqr89.6%
add-sqr-sqrt92.0%
metadata-eval92.0%
Applied egg-rr92.0%
Taylor expanded in y around inf 68.0%
Final simplification32.8%
(FPCore (x y) :precision binary64 (if (<= y 2.85e-105) (* x 0.5) (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 2.85e-105) {
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 <= 2.85d-105) 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 <= 2.85e-105) {
tmp = x * 0.5;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 2.85e-105: tmp = x * 0.5 else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 2.85e-105) 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 <= 2.85e-105) tmp = x * 0.5; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 2.85e-105], N[(x * 0.5), $MachinePrecision], N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.85 \cdot 10^{-105}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 2.84999999999999981e-105Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt30.0%
fabs-sqr30.0%
add-sqr-sqrt36.3%
metadata-eval36.3%
Applied egg-rr36.3%
Taylor expanded in y around 0 34.3%
distribute-rgt1-in34.3%
metadata-eval34.3%
*-commutative34.3%
Simplified34.3%
if 2.84999999999999981e-105 < y Initial program 100.0%
+-commutative100.0%
div-inv100.0%
fma-def100.0%
add-sqr-sqrt92.2%
fabs-sqr92.2%
add-sqr-sqrt94.1%
metadata-eval94.1%
Applied egg-rr94.1%
Taylor expanded in y around inf 71.9%
Final simplification46.6%
(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-sqrt50.4%
fabs-sqr50.4%
add-sqr-sqrt55.3%
metadata-eval55.3%
Applied egg-rr55.3%
fma-udef55.3%
flip-+29.3%
pow229.3%
pow229.3%
Applied egg-rr29.3%
unpow229.3%
unpow229.3%
difference-of-squares31.0%
fma-def31.0%
associate-/l*55.3%
fma-def55.3%
*-commutative55.3%
sub-neg55.3%
+-commutative55.3%
distribute-lft-in55.3%
distribute-rgt-neg-in55.3%
distribute-lft-neg-in55.3%
metadata-eval55.3%
+-commutative55.3%
associate-+r+55.3%
distribute-rgt1-in55.3%
metadata-eval55.3%
distribute-lft-out55.3%
*-inverses55.3%
Simplified55.3%
Final simplification55.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 99.9%
Taylor expanded in x around inf 11.3%
Final simplification11.3%
herbie shell --seed 2023322
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))