
(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 4 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 (fma (fabs (- y x)) 0.5 x))
double code(double x, double y) {
return fma(fabs((y - x)), 0.5, x);
}
function code(x, y) return fma(abs(Float64(y - x)), 0.5, x) end
code[x_, y_] := N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] * 0.5 + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\left|y - x\right|, 0.5, x\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval99.9
Applied rewrites99.9%
(FPCore (x y) :precision binary64 (let* ((t_0 (fabs (- y x)))) (if (<= (+ x (/ t_0 2.0)) 1e-272) (* x 0.75) (* t_0 0.5))))
double code(double x, double y) {
double t_0 = fabs((y - x));
double tmp;
if ((x + (t_0 / 2.0)) <= 1e-272) {
tmp = x * 0.75;
} else {
tmp = t_0 * 0.5;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: t_0
real(8) :: tmp
t_0 = abs((y - x))
if ((x + (t_0 / 2.0d0)) <= 1d-272) then
tmp = x * 0.75d0
else
tmp = t_0 * 0.5d0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = Math.abs((y - x));
double tmp;
if ((x + (t_0 / 2.0)) <= 1e-272) {
tmp = x * 0.75;
} else {
tmp = t_0 * 0.5;
}
return tmp;
}
def code(x, y): t_0 = math.fabs((y - x)) tmp = 0 if (x + (t_0 / 2.0)) <= 1e-272: tmp = x * 0.75 else: tmp = t_0 * 0.5 return tmp
function code(x, y) t_0 = abs(Float64(y - x)) tmp = 0.0 if (Float64(x + Float64(t_0 / 2.0)) <= 1e-272) tmp = Float64(x * 0.75); else tmp = Float64(t_0 * 0.5); end return tmp end
function tmp_2 = code(x, y) t_0 = abs((y - x)); tmp = 0.0; if ((x + (t_0 / 2.0)) <= 1e-272) tmp = x * 0.75; else tmp = t_0 * 0.5; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(x + N[(t$95$0 / 2.0), $MachinePrecision]), $MachinePrecision], 1e-272], N[(x * 0.75), $MachinePrecision], N[(t$95$0 * 0.5), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left|y - x\right|\\
\mathbf{if}\;x + \frac{t\_0}{2} \leq 10^{-272}:\\
\;\;\;\;x \cdot 0.75\\
\mathbf{else}:\\
\;\;\;\;t\_0 \cdot 0.5\\
\end{array}
\end{array}
if (+.f64 x (/.f64 (fabs.f64 (-.f64 y x)) #s(literal 2 binary64))) < 9.9999999999999993e-273Initial program 100.0%
lift-+.f64N/A
flip-+N/A
lower-/.f64N/A
Applied rewrites50.5%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6419.9
Applied rewrites19.9%
if 9.9999999999999993e-273 < (+.f64 x (/.f64 (fabs.f64 (-.f64 y x)) #s(literal 2 binary64))) Initial program 99.9%
Taylor expanded in x around 0
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
remove-double-negN/A
mul-1-negN/A
distribute-neg-inN/A
+-commutativeN/A
lower-fabs.f64N/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
sub-negN/A
lower--.f6472.5
Applied rewrites72.5%
Final simplification61.4%
(FPCore (x y) :precision binary64 (let* ((t_0 (* (fabs (- y x)) 0.5))) (if (<= y -3.5e-97) t_0 (if (<= y 1.4e-17) (fma (fabs (- x)) 0.5 x) t_0))))
double code(double x, double y) {
double t_0 = fabs((y - x)) * 0.5;
double tmp;
if (y <= -3.5e-97) {
tmp = t_0;
} else if (y <= 1.4e-17) {
tmp = fma(fabs(-x), 0.5, x);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y) t_0 = Float64(abs(Float64(y - x)) * 0.5) tmp = 0.0 if (y <= -3.5e-97) tmp = t_0; elseif (y <= 1.4e-17) tmp = fma(abs(Float64(-x)), 0.5, x); else tmp = t_0; end return tmp end
code[x_, y_] := Block[{t$95$0 = N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]}, If[LessEqual[y, -3.5e-97], t$95$0, If[LessEqual[y, 1.4e-17], N[(N[Abs[(-x)], $MachinePrecision] * 0.5 + x), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left|y - x\right| \cdot 0.5\\
\mathbf{if}\;y \leq -3.5 \cdot 10^{-97}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 1.4 \cdot 10^{-17}:\\
\;\;\;\;\mathsf{fma}\left(\left|-x\right|, 0.5, x\right)\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -3.50000000000000019e-97 or 1.3999999999999999e-17 < y Initial program 99.9%
Taylor expanded in x around 0
sub-negN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
remove-double-negN/A
mul-1-negN/A
distribute-neg-inN/A
+-commutativeN/A
lower-fabs.f64N/A
+-commutativeN/A
distribute-neg-inN/A
mul-1-negN/A
remove-double-negN/A
sub-negN/A
lower--.f6479.2
Applied rewrites79.2%
if -3.50000000000000019e-97 < y < 1.3999999999999999e-17Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-/.f64N/A
div-invN/A
lower-fma.f64N/A
metadata-eval99.9
Applied rewrites99.9%
Taylor expanded in y around 0
mul-1-negN/A
lower-neg.f6482.5
Applied rewrites82.5%
Final simplification80.5%
(FPCore (x y) :precision binary64 (* x 0.75))
double code(double x, double y) {
return x * 0.75;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * 0.75d0
end function
public static double code(double x, double y) {
return x * 0.75;
}
def code(x, y): return x * 0.75
function code(x, y) return Float64(x * 0.75) end
function tmp = code(x, y) tmp = x * 0.75; end
code[x_, y_] := N[(x * 0.75), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.75
\end{array}
Initial program 99.9%
lift-+.f64N/A
flip-+N/A
lower-/.f64N/A
Applied rewrites51.9%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f6410.6
Applied rewrites10.6%
herbie shell --seed 2024221
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))