
(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 5 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%
(FPCore (x y) :precision binary64 (if (<= x -1e-98) (+ x (/ (- y x) 2.0)) (* (fabs (- y x)) 0.5)))
double code(double x, double y) {
double tmp;
if (x <= -1e-98) {
tmp = x + ((y - x) / 2.0);
} else {
tmp = fabs((y - x)) * 0.5;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1d-98)) then
tmp = x + ((y - x) / 2.0d0)
else
tmp = abs((y - x)) * 0.5d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1e-98) {
tmp = x + ((y - x) / 2.0);
} else {
tmp = Math.abs((y - x)) * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1e-98: tmp = x + ((y - x) / 2.0) else: tmp = math.fabs((y - x)) * 0.5 return tmp
function code(x, y) tmp = 0.0 if (x <= -1e-98) tmp = Float64(x + Float64(Float64(y - x) / 2.0)); else tmp = Float64(abs(Float64(y - x)) * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1e-98) tmp = x + ((y - x) / 2.0); else tmp = abs((y - x)) * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1e-98], N[(x + N[(N[(y - x), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision], N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{-98}:\\
\;\;\;\;x + \frac{y - x}{2}\\
\mathbf{else}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\end{array}
\end{array}
if x < -9.99999999999999939e-99Initial program 100.0%
+-commutative100.0%
div-inv100.0%
fma-define100.0%
add-sqr-sqrt90.3%
fabs-sqr90.3%
add-sqr-sqrt91.0%
fma-define91.0%
div-inv91.0%
add-sqr-sqrt90.3%
fabs-sqr90.3%
add-sqr-sqrt100.0%
add-cube-cbrt98.0%
associate-/l*98.0%
fma-define98.0%
Applied egg-rr89.2%
fma-undefine89.2%
+-commutative89.2%
associate-*r/89.2%
unpow289.2%
rem-3cbrt-lft91.0%
Simplified91.0%
if -9.99999999999999939e-99 < x Initial program 99.9%
Taylor expanded in x around 0 63.5%
Final simplification73.0%
(FPCore (x y) :precision binary64 (if (<= y 2.35e-119) x (* (- y x) 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 2.35e-119) {
tmp = x;
} else {
tmp = (y - x) * 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.35d-119) then
tmp = x
else
tmp = (y - x) * 0.5d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (y <= 2.35e-119) {
tmp = x;
} else {
tmp = (y - x) * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 2.35e-119: tmp = x else: tmp = (y - x) * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 2.35e-119) tmp = x; else tmp = Float64(Float64(y - x) * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 2.35e-119) tmp = x; else tmp = (y - x) * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 2.35e-119], x, N[(N[(y - x), $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.35 \cdot 10^{-119}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;\left(y - x\right) \cdot 0.5\\
\end{array}
\end{array}
if y < 2.35000000000000001e-119Initial program 99.9%
Taylor expanded in x around inf 13.1%
if 2.35000000000000001e-119 < y Initial program 100.0%
Taylor expanded in x around 0 64.4%
rem-square-sqrt61.1%
fabs-sqr61.1%
rem-square-sqrt61.6%
Simplified61.6%
Final simplification32.6%
(FPCore (x y) :precision binary64 (+ x (/ (- y x) 2.0)))
double code(double x, double y) {
return x + ((y - x) / 2.0);
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x + ((y - x) / 2.0d0)
end function
public static double code(double x, double y) {
return x + ((y - x) / 2.0);
}
def code(x, y): return x + ((y - x) / 2.0)
function code(x, y) return Float64(x + Float64(Float64(y - x) / 2.0)) end
function tmp = code(x, y) tmp = x + ((y - x) / 2.0); end
code[x_, y_] := N[(x + N[(N[(y - x), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \frac{y - x}{2}
\end{array}
Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-define99.9%
add-sqr-sqrt50.8%
fabs-sqr50.8%
add-sqr-sqrt56.0%
fma-define56.0%
div-inv56.0%
add-sqr-sqrt50.8%
fabs-sqr50.8%
add-sqr-sqrt99.9%
add-cube-cbrt98.2%
associate-/l*98.2%
fma-define98.3%
Applied egg-rr54.9%
fma-undefine54.9%
+-commutative54.9%
associate-*r/54.9%
unpow254.9%
rem-3cbrt-lft56.0%
Simplified56.0%
(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.4%
herbie shell --seed 2024103
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))