
(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 (<= x -1.66e-215) (/ (+ x y) 2.0) (* (fabs (- y x)) 0.5)))
double code(double x, double y) {
double tmp;
if (x <= -1.66e-215) {
tmp = (x + y) / 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 <= (-1.66d-215)) then
tmp = (x + y) / 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 <= -1.66e-215) {
tmp = (x + y) / 2.0;
} else {
tmp = Math.abs((y - x)) * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1.66e-215: tmp = (x + y) / 2.0 else: tmp = math.fabs((y - x)) * 0.5 return tmp
function code(x, y) tmp = 0.0 if (x <= -1.66e-215) tmp = Float64(Float64(x + y) / 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 <= -1.66e-215) tmp = (x + y) / 2.0; else tmp = abs((y - x)) * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1.66e-215], N[(N[(x + y), $MachinePrecision] / 2.0), $MachinePrecision], N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.66 \cdot 10^{-215}:\\
\;\;\;\;\frac{x + y}{2}\\
\mathbf{else}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\end{array}
\end{array}
if x < -1.66e-215Initial program 100.0%
+-commutative100.0%
div-inv100.0%
fma-def100.0%
add-sqr-sqrt84.9%
fabs-sqr84.9%
add-sqr-sqrt85.5%
metadata-eval85.5%
Applied egg-rr85.5%
fma-udef85.5%
flip-+43.7%
pow243.7%
pow243.7%
Applied egg-rr43.7%
unpow243.7%
unpow243.7%
difference-of-squares45.7%
fma-def45.7%
associate-/l*85.5%
fma-def85.5%
+-commutative85.5%
*-commutative85.5%
sub-neg85.5%
+-commutative85.5%
distribute-lft-in85.5%
distribute-rgt-neg-in85.5%
distribute-lft-neg-in85.5%
metadata-eval85.5%
associate-+r+85.5%
distribute-rgt1-in85.5%
metadata-eval85.5%
distribute-lft-out85.5%
*-inverses85.5%
Simplified85.5%
if -1.66e-215 < x Initial program 99.9%
Taylor expanded in x around 0 64.8%
Final simplification72.6%
(FPCore (x y) :precision binary64 (if (<= y 2e-166) x (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 2e-166) {
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 <= 2d-166) 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 <= 2e-166) {
tmp = x;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 2e-166: tmp = x else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 2e-166) tmp = x; else tmp = Float64(y * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 2e-166) tmp = x; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 2e-166], x, N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2 \cdot 10^{-166}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 2.00000000000000008e-166Initial program 99.9%
Taylor expanded in x around inf 12.4%
if 2.00000000000000008e-166 < y Initial program 100.0%
+-commutative100.0%
div-inv100.0%
fma-def100.0%
add-sqr-sqrt79.9%
fabs-sqr79.9%
add-sqr-sqrt83.9%
metadata-eval83.9%
Applied egg-rr83.9%
Taylor expanded in y around inf 61.2%
Final simplification31.9%
(FPCore (x y) :precision binary64 (if (<= y 2.35e-88) (* x 0.5) (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 2.35e-88) {
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.35d-88) 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.35e-88) {
tmp = x * 0.5;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 2.35e-88: tmp = x * 0.5 else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 2.35e-88) 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.35e-88) tmp = x * 0.5; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 2.35e-88], N[(x * 0.5), $MachinePrecision], N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.35 \cdot 10^{-88}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 2.35e-88Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt29.6%
fabs-sqr29.6%
add-sqr-sqrt35.4%
metadata-eval35.4%
Applied egg-rr35.4%
Taylor expanded in y around 0 32.8%
distribute-rgt1-in32.8%
metadata-eval32.8%
*-commutative32.8%
Simplified32.8%
if 2.35e-88 < y Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt79.4%
fabs-sqr79.4%
add-sqr-sqrt83.5%
metadata-eval83.5%
Applied egg-rr83.5%
Taylor expanded in y around inf 65.1%
Final simplification43.5%
(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.2%
fabs-sqr46.2%
add-sqr-sqrt51.4%
metadata-eval51.4%
Applied egg-rr51.4%
fma-udef51.4%
flip-+27.8%
pow227.8%
pow227.8%
Applied egg-rr27.8%
unpow227.8%
unpow227.8%
difference-of-squares29.3%
fma-def29.3%
associate-/l*51.4%
fma-def51.4%
+-commutative51.4%
*-commutative51.4%
sub-neg51.4%
+-commutative51.4%
distribute-lft-in51.4%
distribute-rgt-neg-in51.4%
distribute-lft-neg-in51.4%
metadata-eval51.4%
associate-+r+51.4%
distribute-rgt1-in51.4%
metadata-eval51.4%
distribute-lft-out51.4%
*-inverses51.4%
Simplified51.4%
Final simplification51.4%
(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.2%
Final simplification11.2%
herbie shell --seed 2024027
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))