
(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 8 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
(let* ((t_0 (* 0.5 (+ x y))))
(if (<= x -9e-19)
t_0
(if (<= x 5.2e-87) (* (fabs (- y x)) 0.5) (+ x t_0)))))
double code(double x, double y) {
double t_0 = 0.5 * (x + y);
double tmp;
if (x <= -9e-19) {
tmp = t_0;
} else if (x <= 5.2e-87) {
tmp = fabs((y - x)) * 0.5;
} else {
tmp = x + t_0;
}
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 = 0.5d0 * (x + y)
if (x <= (-9d-19)) then
tmp = t_0
else if (x <= 5.2d-87) then
tmp = abs((y - x)) * 0.5d0
else
tmp = x + t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = 0.5 * (x + y);
double tmp;
if (x <= -9e-19) {
tmp = t_0;
} else if (x <= 5.2e-87) {
tmp = Math.abs((y - x)) * 0.5;
} else {
tmp = x + t_0;
}
return tmp;
}
def code(x, y): t_0 = 0.5 * (x + y) tmp = 0 if x <= -9e-19: tmp = t_0 elif x <= 5.2e-87: tmp = math.fabs((y - x)) * 0.5 else: tmp = x + t_0 return tmp
function code(x, y) t_0 = Float64(0.5 * Float64(x + y)) tmp = 0.0 if (x <= -9e-19) tmp = t_0; elseif (x <= 5.2e-87) tmp = Float64(abs(Float64(y - x)) * 0.5); else tmp = Float64(x + t_0); end return tmp end
function tmp_2 = code(x, y) t_0 = 0.5 * (x + y); tmp = 0.0; if (x <= -9e-19) tmp = t_0; elseif (x <= 5.2e-87) tmp = abs((y - x)) * 0.5; else tmp = x + t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(0.5 * N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -9e-19], t$95$0, If[LessEqual[x, 5.2e-87], N[(N[Abs[N[(y - x), $MachinePrecision]], $MachinePrecision] * 0.5), $MachinePrecision], N[(x + t$95$0), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(x + y\right)\\
\mathbf{if}\;x \leq -9 \cdot 10^{-19}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;x \leq 5.2 \cdot 10^{-87}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;x + t_0\\
\end{array}
\end{array}
if x < -9.00000000000000026e-19Initial program 100.0%
expm1-log1p-u28.7%
expm1-udef26.1%
+-commutative26.1%
div-inv26.1%
fma-def26.1%
add-sqr-sqrt16.7%
fabs-sqr16.7%
add-sqr-sqrt16.7%
metadata-eval16.7%
Applied egg-rr16.7%
expm1-def19.4%
expm1-log1p89.6%
Simplified89.6%
Taylor expanded in y around 0 89.6%
associate-+r+89.6%
distribute-rgt1-in89.6%
metadata-eval89.6%
distribute-lft-out89.6%
Simplified89.6%
if -9.00000000000000026e-19 < x < 5.20000000000000005e-87Initial program 99.9%
Taylor expanded in x around 0 81.7%
if 5.20000000000000005e-87 < x Initial program 99.9%
expm1-log1p-u92.4%
expm1-udef80.6%
+-commutative80.6%
div-inv80.6%
fma-def80.6%
add-sqr-sqrt17.0%
fabs-sqr17.0%
add-sqr-sqrt27.8%
metadata-eval27.8%
Applied egg-rr27.8%
expm1-def30.0%
expm1-log1p31.9%
Simplified31.9%
fma-udef31.9%
*-commutative31.9%
sub-neg31.9%
add-sqr-sqrt0.0%
sqrt-unprod54.6%
sqr-neg54.6%
sqrt-unprod84.2%
add-sqr-sqrt84.4%
Applied egg-rr84.4%
Final simplification84.8%
(FPCore (x y) :precision binary64 (if (<= x -2.9e-47) (* x 0.5) (if (<= x 4.5e-81) (* y 0.5) (* x 1.5))))
double code(double x, double y) {
double tmp;
if (x <= -2.9e-47) {
tmp = x * 0.5;
} else if (x <= 4.5e-81) {
tmp = y * 0.5;
} else {
tmp = x * 1.5;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-2.9d-47)) then
tmp = x * 0.5d0
else if (x <= 4.5d-81) then
tmp = y * 0.5d0
else
tmp = x * 1.5d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -2.9e-47) {
tmp = x * 0.5;
} else if (x <= 4.5e-81) {
tmp = y * 0.5;
} else {
tmp = x * 1.5;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -2.9e-47: tmp = x * 0.5 elif x <= 4.5e-81: tmp = y * 0.5 else: tmp = x * 1.5 return tmp
function code(x, y) tmp = 0.0 if (x <= -2.9e-47) tmp = Float64(x * 0.5); elseif (x <= 4.5e-81) tmp = Float64(y * 0.5); else tmp = Float64(x * 1.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -2.9e-47) tmp = x * 0.5; elseif (x <= 4.5e-81) tmp = y * 0.5; else tmp = x * 1.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -2.9e-47], N[(x * 0.5), $MachinePrecision], If[LessEqual[x, 4.5e-81], N[(y * 0.5), $MachinePrecision], N[(x * 1.5), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.9 \cdot 10^{-47}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{elif}\;x \leq 4.5 \cdot 10^{-81}:\\
\;\;\;\;y \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;x \cdot 1.5\\
\end{array}
\end{array}
if x < -2.9e-47Initial program 100.0%
expm1-log1p-u34.2%
expm1-udef29.1%
+-commutative29.1%
div-inv29.1%
fma-def29.1%
add-sqr-sqrt15.5%
fabs-sqr15.5%
add-sqr-sqrt15.5%
metadata-eval15.5%
Applied egg-rr15.5%
expm1-def20.5%
expm1-log1p85.0%
Simplified85.0%
Taylor expanded in y around 0 67.7%
distribute-rgt1-in67.7%
metadata-eval67.7%
*-commutative67.7%
Simplified67.7%
if -2.9e-47 < x < 4.5e-81Initial program 99.9%
expm1-log1p-u95.2%
expm1-udef49.8%
+-commutative49.8%
div-inv49.8%
fma-def49.8%
add-sqr-sqrt34.7%
fabs-sqr34.7%
add-sqr-sqrt35.6%
metadata-eval35.6%
Applied egg-rr35.6%
expm1-def62.1%
expm1-log1p65.5%
Simplified65.5%
Taylor expanded in y around inf 54.3%
if 4.5e-81 < x Initial program 99.9%
expm1-log1p-u92.4%
expm1-udef80.4%
+-commutative80.4%
div-inv80.4%
fma-def80.4%
add-sqr-sqrt15.4%
fabs-sqr15.4%
add-sqr-sqrt26.5%
metadata-eval26.5%
Applied egg-rr26.5%
expm1-def28.7%
expm1-log1p30.4%
Simplified30.4%
fma-udef30.4%
*-commutative30.4%
sub-neg30.4%
add-sqr-sqrt0.0%
sqrt-unprod53.7%
sqr-neg53.7%
sqrt-unprod83.9%
add-sqr-sqrt84.1%
Applied egg-rr84.1%
Taylor expanded in y around 0 69.3%
distribute-rgt1-in69.3%
metadata-eval69.3%
*-commutative69.3%
Simplified69.3%
Final simplification63.6%
(FPCore (x y) :precision binary64 (let* ((t_0 (* 0.5 (+ x y)))) (if (<= x -2e-264) t_0 (+ x t_0))))
double code(double x, double y) {
double t_0 = 0.5 * (x + y);
double tmp;
if (x <= -2e-264) {
tmp = t_0;
} else {
tmp = x + t_0;
}
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 = 0.5d0 * (x + y)
if (x <= (-2d-264)) then
tmp = t_0
else
tmp = x + t_0
end if
code = tmp
end function
public static double code(double x, double y) {
double t_0 = 0.5 * (x + y);
double tmp;
if (x <= -2e-264) {
tmp = t_0;
} else {
tmp = x + t_0;
}
return tmp;
}
def code(x, y): t_0 = 0.5 * (x + y) tmp = 0 if x <= -2e-264: tmp = t_0 else: tmp = x + t_0 return tmp
function code(x, y) t_0 = Float64(0.5 * Float64(x + y)) tmp = 0.0 if (x <= -2e-264) tmp = t_0; else tmp = Float64(x + t_0); end return tmp end
function tmp_2 = code(x, y) t_0 = 0.5 * (x + y); tmp = 0.0; if (x <= -2e-264) tmp = t_0; else tmp = x + t_0; end tmp_2 = tmp; end
code[x_, y_] := Block[{t$95$0 = N[(0.5 * N[(x + y), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[x, -2e-264], t$95$0, N[(x + t$95$0), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(x + y\right)\\
\mathbf{if}\;x \leq -2 \cdot 10^{-264}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;x + t_0\\
\end{array}
\end{array}
if x < -2e-264Initial program 100.0%
expm1-log1p-u56.5%
expm1-udef36.1%
+-commutative36.1%
div-inv36.1%
fma-def36.1%
add-sqr-sqrt22.6%
fabs-sqr22.6%
add-sqr-sqrt22.8%
metadata-eval22.8%
Applied egg-rr22.8%
expm1-def39.7%
expm1-log1p81.9%
Simplified81.9%
Taylor expanded in y around 0 81.9%
associate-+r+81.9%
distribute-rgt1-in81.9%
metadata-eval81.9%
distribute-lft-out81.9%
Simplified81.9%
if -2e-264 < x Initial program 99.9%
expm1-log1p-u93.4%
expm1-udef70.6%
+-commutative70.6%
div-inv70.6%
fma-def70.6%
add-sqr-sqrt21.7%
fabs-sqr21.7%
add-sqr-sqrt29.5%
metadata-eval29.5%
Applied egg-rr29.5%
expm1-def36.6%
expm1-log1p38.8%
Simplified38.8%
fma-udef38.8%
*-commutative38.8%
sub-neg38.8%
add-sqr-sqrt3.6%
sqrt-unprod55.8%
sqr-neg55.8%
sqrt-unprod73.7%
add-sqr-sqrt77.4%
Applied egg-rr77.4%
Final simplification79.4%
(FPCore (x y) :precision binary64 (if (<= x 2.7e-55) (* 0.5 (+ x y)) (* x 1.5)))
double code(double x, double y) {
double tmp;
if (x <= 2.7e-55) {
tmp = 0.5 * (x + y);
} else {
tmp = x * 1.5;
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= 2.7d-55) then
tmp = 0.5d0 * (x + y)
else
tmp = x * 1.5d0
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= 2.7e-55) {
tmp = 0.5 * (x + y);
} else {
tmp = x * 1.5;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 2.7e-55: tmp = 0.5 * (x + y) else: tmp = x * 1.5 return tmp
function code(x, y) tmp = 0.0 if (x <= 2.7e-55) tmp = Float64(0.5 * Float64(x + y)); else tmp = Float64(x * 1.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= 2.7e-55) tmp = 0.5 * (x + y); else tmp = x * 1.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 2.7e-55], N[(0.5 * N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x * 1.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2.7 \cdot 10^{-55}:\\
\;\;\;\;0.5 \cdot \left(x + y\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 1.5\\
\end{array}
\end{array}
if x < 2.70000000000000004e-55Initial program 100.0%
expm1-log1p-u68.9%
expm1-udef40.5%
+-commutative40.5%
div-inv40.5%
fma-def40.5%
add-sqr-sqrt26.6%
fabs-sqr26.6%
add-sqr-sqrt27.1%
metadata-eval27.1%
Applied egg-rr27.1%
expm1-def43.5%
expm1-log1p73.3%
Simplified73.3%
Taylor expanded in y around 0 73.3%
associate-+r+73.3%
distribute-rgt1-in73.3%
metadata-eval73.3%
distribute-lft-out73.3%
Simplified73.3%
if 2.70000000000000004e-55 < x Initial program 99.9%
expm1-log1p-u92.1%
expm1-udef83.5%
+-commutative83.5%
div-inv83.5%
fma-def83.5%
add-sqr-sqrt13.5%
fabs-sqr13.5%
add-sqr-sqrt25.3%
metadata-eval25.3%
Applied egg-rr25.3%
expm1-def27.2%
expm1-log1p28.8%
Simplified28.8%
fma-udef28.8%
*-commutative28.8%
sub-neg28.8%
add-sqr-sqrt0.0%
sqrt-unprod51.1%
sqr-neg51.1%
sqrt-unprod83.7%
add-sqr-sqrt83.9%
Applied egg-rr83.9%
Taylor expanded in y around 0 71.1%
distribute-rgt1-in71.1%
metadata-eval71.1%
*-commutative71.1%
Simplified71.1%
Final simplification72.6%
(FPCore (x y) :precision binary64 (if (<= y 1.3e-239) x (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 1.3e-239) {
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 <= 1.3d-239) 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 <= 1.3e-239) {
tmp = x;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 1.3e-239: tmp = x else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 1.3e-239) tmp = x; else tmp = Float64(y * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 1.3e-239) tmp = x; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 1.3e-239], x, N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.3 \cdot 10^{-239}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 1.30000000000000001e-239Initial program 99.9%
Taylor expanded in x around inf 13.3%
if 1.30000000000000001e-239 < y Initial program 100.0%
expm1-log1p-u80.3%
expm1-udef62.5%
+-commutative62.5%
div-inv62.5%
fma-def62.5%
add-sqr-sqrt42.6%
fabs-sqr42.6%
add-sqr-sqrt46.7%
metadata-eval46.7%
Applied egg-rr46.7%
expm1-def60.1%
expm1-log1p78.3%
Simplified78.3%
Taylor expanded in y around inf 57.8%
Final simplification35.7%
(FPCore (x y) :precision binary64 (if (<= y 1.4e-27) (* x 0.5) (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 1.4e-27) {
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 <= 1.4d-27) 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 <= 1.4e-27) {
tmp = x * 0.5;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 1.4e-27: tmp = x * 0.5 else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 1.4e-27) 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 <= 1.4e-27) tmp = x * 0.5; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 1.4e-27], N[(x * 0.5), $MachinePrecision], N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 1.4 \cdot 10^{-27}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 1.4e-27Initial program 99.9%
expm1-log1p-u74.4%
expm1-udef45.1%
+-commutative45.1%
div-inv45.1%
fma-def45.1%
add-sqr-sqrt1.3%
fabs-sqr1.3%
add-sqr-sqrt6.9%
metadata-eval6.9%
Applied egg-rr6.9%
expm1-def21.6%
expm1-log1p43.2%
Simplified43.2%
Taylor expanded in y around 0 37.2%
distribute-rgt1-in37.2%
metadata-eval37.2%
*-commutative37.2%
Simplified37.2%
if 1.4e-27 < y Initial program 100.0%
expm1-log1p-u82.2%
expm1-udef77.7%
+-commutative77.7%
div-inv77.7%
fma-def77.7%
add-sqr-sqrt68.7%
fabs-sqr68.7%
add-sqr-sqrt70.5%
metadata-eval70.5%
Applied egg-rr70.5%
expm1-def74.7%
expm1-log1p91.6%
Simplified91.6%
Taylor expanded in y around inf 77.5%
Final simplification49.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.8%
Final simplification11.8%
herbie shell --seed 2023336
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))