
(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 -1.95e-63) (* (fabs (- y x)) 0.5) (* 0.5 (+ x y))))
double code(double x, double y) {
double tmp;
if (y <= -1.95e-63) {
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 <= (-1.95d-63)) 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 <= -1.95e-63) {
tmp = Math.abs((y - x)) * 0.5;
} else {
tmp = 0.5 * (x + y);
}
return tmp;
}
def code(x, y): tmp = 0 if y <= -1.95e-63: tmp = math.fabs((y - x)) * 0.5 else: tmp = 0.5 * (x + y) return tmp
function code(x, y) tmp = 0.0 if (y <= -1.95e-63) 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 <= -1.95e-63) tmp = abs((y - x)) * 0.5; else tmp = 0.5 * (x + y); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, -1.95e-63], 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 -1.95 \cdot 10^{-63}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(x + y\right)\\
\end{array}
\end{array}
if y < -1.95000000000000011e-63Initial program 100.0%
Taylor expanded in x around 0 72.6%
if -1.95000000000000011e-63 < y Initial program 99.9%
flip-+56.3%
div-sub56.3%
pow256.3%
div-inv56.3%
add-sqr-sqrt34.4%
fabs-sqr34.4%
add-sqr-sqrt39.6%
metadata-eval39.6%
Applied egg-rr38.1%
div-sub38.1%
unpow238.1%
fma-neg38.2%
distribute-rgt-neg-in38.2%
metadata-eval38.2%
sub-neg38.2%
*-commutative38.2%
distribute-rgt-neg-in38.2%
sub-neg38.2%
+-commutative38.2%
distribute-neg-in38.2%
unsub-neg38.2%
remove-double-neg38.2%
Simplified38.2%
Taylor expanded in x around 0 67.0%
distribute-lft-out67.0%
+-commutative67.0%
Simplified67.0%
Final simplification69.0%
(FPCore (x y) :precision binary64 (if (<= x -1.85e-33) (* x 0.5) (if (<= x 1.5e+106) (* y 0.5) x)))
double code(double x, double y) {
double tmp;
if (x <= -1.85e-33) {
tmp = x * 0.5;
} else if (x <= 1.5e+106) {
tmp = y * 0.5;
} else {
tmp = x;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1.85d-33)) then
tmp = x * 0.5d0
else if (x <= 1.5d+106) then
tmp = y * 0.5d0
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1.85e-33) {
tmp = x * 0.5;
} else if (x <= 1.5e+106) {
tmp = y * 0.5;
} else {
tmp = x;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.85e-33: tmp = x * 0.5 elif x <= 1.5e+106: tmp = y * 0.5 else: tmp = x return tmp
function code(x, y) tmp = 0.0 if (x <= -1.85e-33) tmp = Float64(x * 0.5); elseif (x <= 1.5e+106) tmp = Float64(y * 0.5); else tmp = x; end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1.85e-33) tmp = x * 0.5; elseif (x <= 1.5e+106) tmp = y * 0.5; else tmp = x; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.85e-33], N[(x * 0.5), $MachinePrecision], If[LessEqual[x, 1.5e+106], N[(y * 0.5), $MachinePrecision], x]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.85 \cdot 10^{-33}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{elif}\;x \leq 1.5 \cdot 10^{+106}:\\
\;\;\;\;y \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -1.85000000000000007e-33Initial program 100.0%
flip-+38.0%
div-sub37.9%
pow237.9%
div-inv37.9%
add-sqr-sqrt32.3%
fabs-sqr32.3%
add-sqr-sqrt36.9%
metadata-eval36.9%
Applied egg-rr32.4%
div-sub32.4%
unpow232.4%
fma-neg32.5%
distribute-rgt-neg-in32.5%
metadata-eval32.5%
sub-neg32.5%
*-commutative32.5%
distribute-rgt-neg-in32.5%
sub-neg32.5%
+-commutative32.5%
distribute-neg-in32.5%
unsub-neg32.5%
remove-double-neg32.5%
Simplified32.5%
Taylor expanded in x around inf 74.8%
if -1.85000000000000007e-33 < x < 1.5e106Initial program 99.9%
flip-+70.6%
div-sub70.5%
pow270.5%
div-inv70.5%
add-sqr-sqrt24.9%
fabs-sqr24.9%
add-sqr-sqrt52.8%
metadata-eval52.8%
Applied egg-rr29.2%
div-sub29.2%
unpow229.2%
fma-neg29.2%
distribute-rgt-neg-in29.2%
metadata-eval29.2%
sub-neg29.2%
*-commutative29.2%
distribute-rgt-neg-in29.2%
sub-neg29.2%
+-commutative29.2%
distribute-neg-in29.2%
unsub-neg29.2%
remove-double-neg29.2%
Simplified29.2%
Taylor expanded in x around 0 36.5%
if 1.5e106 < x Initial program 99.9%
Taylor expanded in x around inf 19.8%
Final simplification42.6%
(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 99.9%
flip-+55.0%
div-sub55.0%
pow255.0%
div-inv55.0%
add-sqr-sqrt22.5%
fabs-sqr22.5%
add-sqr-sqrt41.0%
metadata-eval41.0%
Applied egg-rr25.8%
div-sub25.8%
unpow225.8%
fma-neg25.8%
distribute-rgt-neg-in25.8%
metadata-eval25.8%
sub-neg25.8%
*-commutative25.8%
distribute-rgt-neg-in25.8%
sub-neg25.8%
+-commutative25.8%
distribute-neg-in25.8%
unsub-neg25.8%
remove-double-neg25.8%
Simplified25.8%
Taylor expanded in x around 0 48.5%
distribute-lft-out48.5%
+-commutative48.5%
Simplified48.5%
Final simplification48.5%
(FPCore (x y) :precision binary64 (* x 0.5))
double code(double x, double y) {
return x * 0.5;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
code = x * 0.5d0
end function
public static double code(double x, double y) {
return x * 0.5;
}
def code(x, y): return x * 0.5
function code(x, y) return Float64(x * 0.5) end
function tmp = code(x, y) tmp = x * 0.5; end
code[x_, y_] := N[(x * 0.5), $MachinePrecision]
\begin{array}{l}
\\
x \cdot 0.5
\end{array}
Initial program 99.9%
flip-+55.0%
div-sub55.0%
pow255.0%
div-inv55.0%
add-sqr-sqrt22.5%
fabs-sqr22.5%
add-sqr-sqrt41.0%
metadata-eval41.0%
Applied egg-rr25.8%
div-sub25.8%
unpow225.8%
fma-neg25.8%
distribute-rgt-neg-in25.8%
metadata-eval25.8%
sub-neg25.8%
*-commutative25.8%
distribute-rgt-neg-in25.8%
sub-neg25.8%
+-commutative25.8%
distribute-neg-in25.8%
unsub-neg25.8%
remove-double-neg25.8%
Simplified25.8%
Taylor expanded in x around inf 25.6%
Final simplification25.6%
(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.0%
Final simplification11.0%
herbie shell --seed 2024021
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))