
(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 9 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-8) (* 0.5 (+ x y)) (if (<= x 2.1e-48) (+ x (/ (fabs y) 2.0)) (+ x (/ (+ x y) 2.0)))))
double code(double x, double y) {
double tmp;
if (x <= -1e-8) {
tmp = 0.5 * (x + y);
} else if (x <= 2.1e-48) {
tmp = x + (fabs(y) / 2.0);
} else {
tmp = x + ((x + y) / 2.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-1d-8)) then
tmp = 0.5d0 * (x + y)
else if (x <= 2.1d-48) then
tmp = x + (abs(y) / 2.0d0)
else
tmp = x + ((x + y) / 2.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -1e-8) {
tmp = 0.5 * (x + y);
} else if (x <= 2.1e-48) {
tmp = x + (Math.abs(y) / 2.0);
} else {
tmp = x + ((x + y) / 2.0);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -1e-8: tmp = 0.5 * (x + y) elif x <= 2.1e-48: tmp = x + (math.fabs(y) / 2.0) else: tmp = x + ((x + y) / 2.0) return tmp
function code(x, y) tmp = 0.0 if (x <= -1e-8) tmp = Float64(0.5 * Float64(x + y)); elseif (x <= 2.1e-48) tmp = Float64(x + Float64(abs(y) / 2.0)); else tmp = Float64(x + Float64(Float64(x + y) / 2.0)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -1e-8) tmp = 0.5 * (x + y); elseif (x <= 2.1e-48) tmp = x + (abs(y) / 2.0); else tmp = x + ((x + y) / 2.0); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -1e-8], N[(0.5 * N[(x + y), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 2.1e-48], N[(x + N[(N[Abs[y], $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision], N[(x + N[(N[(x + y), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \cdot 10^{-8}:\\
\;\;\;\;0.5 \cdot \left(x + y\right)\\
\mathbf{elif}\;x \leq 2.1 \cdot 10^{-48}:\\
\;\;\;\;x + \frac{\left|y\right|}{2}\\
\mathbf{else}:\\
\;\;\;\;x + \frac{x + y}{2}\\
\end{array}
\end{array}
if x < -1e-8Initial program 100.0%
add-sqr-sqrt97.5%
fabs-sqr97.5%
add-sqr-sqrt98.3%
sub-neg98.3%
flip-+42.7%
pow242.7%
Applied egg-rr42.7%
Taylor expanded in x around 0 98.3%
+-commutative98.3%
distribute-lft-in98.3%
Simplified98.3%
if -1e-8 < x < 2.09999999999999989e-48Initial program 99.9%
Taylor expanded in y around inf 88.8%
if 2.09999999999999989e-48 < x Initial program 99.8%
add-cube-cbrt98.8%
fabs-mul98.8%
pow298.8%
Applied egg-rr98.8%
Applied egg-rr85.1%
fma-undefine85.1%
associate-*r/85.1%
unpow285.1%
rem-3cbrt-lft85.9%
Simplified85.9%
Final simplification90.2%
(FPCore (x y) :precision binary64 (if (<= x -6e-8) (* 0.5 (+ x y)) (if (<= x 5.8e-45) (* 0.5 (fabs y)) (+ x (/ (+ x y) 2.0)))))
double code(double x, double y) {
double tmp;
if (x <= -6e-8) {
tmp = 0.5 * (x + y);
} else if (x <= 5.8e-45) {
tmp = 0.5 * fabs(y);
} else {
tmp = x + ((x + y) / 2.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-6d-8)) then
tmp = 0.5d0 * (x + y)
else if (x <= 5.8d-45) then
tmp = 0.5d0 * abs(y)
else
tmp = x + ((x + y) / 2.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -6e-8) {
tmp = 0.5 * (x + y);
} else if (x <= 5.8e-45) {
tmp = 0.5 * Math.abs(y);
} else {
tmp = x + ((x + y) / 2.0);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -6e-8: tmp = 0.5 * (x + y) elif x <= 5.8e-45: tmp = 0.5 * math.fabs(y) else: tmp = x + ((x + y) / 2.0) return tmp
function code(x, y) tmp = 0.0 if (x <= -6e-8) tmp = Float64(0.5 * Float64(x + y)); elseif (x <= 5.8e-45) tmp = Float64(0.5 * abs(y)); else tmp = Float64(x + Float64(Float64(x + y) / 2.0)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -6e-8) tmp = 0.5 * (x + y); elseif (x <= 5.8e-45) tmp = 0.5 * abs(y); else tmp = x + ((x + y) / 2.0); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -6e-8], N[(0.5 * N[(x + y), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 5.8e-45], N[(0.5 * N[Abs[y], $MachinePrecision]), $MachinePrecision], N[(x + N[(N[(x + y), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -6 \cdot 10^{-8}:\\
\;\;\;\;0.5 \cdot \left(x + y\right)\\
\mathbf{elif}\;x \leq 5.8 \cdot 10^{-45}:\\
\;\;\;\;0.5 \cdot \left|y\right|\\
\mathbf{else}:\\
\;\;\;\;x + \frac{x + y}{2}\\
\end{array}
\end{array}
if x < -5.99999999999999946e-8Initial program 100.0%
add-sqr-sqrt97.5%
fabs-sqr97.5%
add-sqr-sqrt98.3%
sub-neg98.3%
flip-+42.7%
pow242.7%
Applied egg-rr42.7%
Taylor expanded in x around 0 98.3%
+-commutative98.3%
distribute-lft-in98.3%
Simplified98.3%
if -5.99999999999999946e-8 < x < 5.8e-45Initial program 99.9%
Taylor expanded in y around inf 88.8%
Taylor expanded in x around 0 87.0%
if 5.8e-45 < x Initial program 99.8%
add-cube-cbrt98.8%
fabs-mul98.8%
pow298.8%
Applied egg-rr98.8%
Applied egg-rr85.1%
fma-undefine85.1%
associate-*r/85.1%
unpow285.1%
rem-3cbrt-lft85.9%
Simplified85.9%
Final simplification89.3%
(FPCore (x y) :precision binary64 (if (<= x -0.85) (* x 0.5) (if (<= x 1.1e-125) (* y 0.5) (* x 1.5))))
double code(double x, double y) {
double tmp;
if (x <= -0.85) {
tmp = x * 0.5;
} else if (x <= 1.1e-125) {
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 <= (-0.85d0)) then
tmp = x * 0.5d0
else if (x <= 1.1d-125) 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 <= -0.85) {
tmp = x * 0.5;
} else if (x <= 1.1e-125) {
tmp = y * 0.5;
} else {
tmp = x * 1.5;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -0.85: tmp = x * 0.5 elif x <= 1.1e-125: tmp = y * 0.5 else: tmp = x * 1.5 return tmp
function code(x, y) tmp = 0.0 if (x <= -0.85) tmp = Float64(x * 0.5); elseif (x <= 1.1e-125) 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 <= -0.85) tmp = x * 0.5; elseif (x <= 1.1e-125) tmp = y * 0.5; else tmp = x * 1.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -0.85], N[(x * 0.5), $MachinePrecision], If[LessEqual[x, 1.1e-125], N[(y * 0.5), $MachinePrecision], N[(x * 1.5), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.85:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{elif}\;x \leq 1.1 \cdot 10^{-125}:\\
\;\;\;\;y \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;x \cdot 1.5\\
\end{array}
\end{array}
if x < -0.849999999999999978Initial program 100.0%
add-sqr-sqrt97.4%
fabs-sqr97.4%
add-sqr-sqrt98.2%
sub-neg98.2%
flip-+43.0%
pow243.0%
Applied egg-rr43.0%
Taylor expanded in x around inf 94.4%
if -0.849999999999999978 < x < 1.09999999999999997e-125Initial program 100.0%
Taylor expanded in y around inf 90.9%
Taylor expanded in x around 0 89.4%
rem-square-sqrt48.2%
fabs-sqr48.2%
rem-square-sqrt49.6%
Simplified49.6%
if 1.09999999999999997e-125 < x Initial program 99.8%
add-cube-cbrt98.7%
fabs-mul98.7%
pow298.7%
Applied egg-rr98.7%
mul-fabs98.7%
unpow298.7%
add-cube-cbrt99.8%
add-sqr-sqrt15.9%
fabs-sqr15.9%
add-sqr-sqrt27.4%
add-sqr-sqrt27.4%
sqrt-prod20.7%
sqr-neg20.7%
pow220.7%
sqrt-pow177.5%
metadata-eval77.5%
pow177.5%
add-cube-cbrt76.8%
pow376.9%
Applied egg-rr76.9%
Taylor expanded in x around inf 63.8%
*-commutative63.8%
Simplified63.8%
Final simplification64.2%
(FPCore (x y) :precision binary64 (if (<= x -5e-266) (* 0.5 (+ x y)) (+ x (/ (+ x y) 2.0))))
double code(double x, double y) {
double tmp;
if (x <= -5e-266) {
tmp = 0.5 * (x + y);
} else {
tmp = x + ((x + y) / 2.0);
}
return tmp;
}
real(8) function code(x, y)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8) :: tmp
if (x <= (-5d-266)) then
tmp = 0.5d0 * (x + y)
else
tmp = x + ((x + y) / 2.0d0)
end if
code = tmp
end function
public static double code(double x, double y) {
double tmp;
if (x <= -5e-266) {
tmp = 0.5 * (x + y);
} else {
tmp = x + ((x + y) / 2.0);
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -5e-266: tmp = 0.5 * (x + y) else: tmp = x + ((x + y) / 2.0) return tmp
function code(x, y) tmp = 0.0 if (x <= -5e-266) tmp = Float64(0.5 * Float64(x + y)); else tmp = Float64(x + Float64(Float64(x + y) / 2.0)); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (x <= -5e-266) tmp = 0.5 * (x + y); else tmp = x + ((x + y) / 2.0); end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -5e-266], N[(0.5 * N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x + N[(N[(x + y), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -5 \cdot 10^{-266}:\\
\;\;\;\;0.5 \cdot \left(x + y\right)\\
\mathbf{else}:\\
\;\;\;\;x + \frac{x + y}{2}\\
\end{array}
\end{array}
if x < -4.99999999999999992e-266Initial program 100.0%
add-sqr-sqrt78.8%
fabs-sqr78.8%
add-sqr-sqrt79.7%
sub-neg79.7%
flip-+41.5%
pow241.5%
Applied egg-rr41.5%
Taylor expanded in x around 0 79.7%
+-commutative79.7%
distribute-lft-in79.7%
Simplified79.7%
if -4.99999999999999992e-266 < x Initial program 99.8%
add-cube-cbrt98.3%
fabs-mul98.3%
pow298.3%
Applied egg-rr98.3%
Applied egg-rr68.3%
fma-undefine68.3%
associate-*r/68.3%
unpow268.3%
rem-3cbrt-lft69.2%
Simplified69.2%
Final simplification74.0%
(FPCore (x y) :precision binary64 (if (<= x 6.5e-126) (* 0.5 (+ x y)) (* x 1.5)))
double code(double x, double y) {
double tmp;
if (x <= 6.5e-126) {
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 <= 6.5d-126) 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 <= 6.5e-126) {
tmp = 0.5 * (x + y);
} else {
tmp = x * 1.5;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= 6.5e-126: tmp = 0.5 * (x + y) else: tmp = x * 1.5 return tmp
function code(x, y) tmp = 0.0 if (x <= 6.5e-126) 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 <= 6.5e-126) tmp = 0.5 * (x + y); else tmp = x * 1.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, 6.5e-126], N[(0.5 * N[(x + y), $MachinePrecision]), $MachinePrecision], N[(x * 1.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 6.5 \cdot 10^{-126}:\\
\;\;\;\;0.5 \cdot \left(x + y\right)\\
\mathbf{else}:\\
\;\;\;\;x \cdot 1.5\\
\end{array}
\end{array}
if x < 6.50000000000000014e-126Initial program 100.0%
add-sqr-sqrt69.3%
fabs-sqr69.3%
add-sqr-sqrt70.6%
sub-neg70.6%
flip-+37.6%
pow237.6%
Applied egg-rr37.6%
Taylor expanded in x around 0 70.6%
+-commutative70.6%
distribute-lft-in70.6%
Simplified70.6%
if 6.50000000000000014e-126 < x Initial program 99.8%
add-cube-cbrt98.7%
fabs-mul98.7%
pow298.7%
Applied egg-rr98.7%
mul-fabs98.7%
unpow298.7%
add-cube-cbrt99.8%
add-sqr-sqrt15.9%
fabs-sqr15.9%
add-sqr-sqrt27.4%
add-sqr-sqrt27.4%
sqrt-prod20.7%
sqr-neg20.7%
pow220.7%
sqrt-pow177.5%
metadata-eval77.5%
pow177.5%
add-cube-cbrt76.8%
pow376.9%
Applied egg-rr76.9%
Taylor expanded in x around inf 63.8%
*-commutative63.8%
Simplified63.8%
Final simplification68.3%
(FPCore (x y) :precision binary64 (if (<= x -0.45) (* x 0.5) (* y 0.5)))
double code(double x, double y) {
double tmp;
if (x <= -0.45) {
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 (x <= (-0.45d0)) 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 (x <= -0.45) {
tmp = x * 0.5;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if x <= -0.45: tmp = x * 0.5 else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (x <= -0.45) 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 (x <= -0.45) tmp = x * 0.5; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[x, -0.45], N[(x * 0.5), $MachinePrecision], N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -0.45:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if x < -0.450000000000000011Initial program 100.0%
add-sqr-sqrt97.4%
fabs-sqr97.4%
add-sqr-sqrt98.2%
sub-neg98.2%
flip-+43.0%
pow243.0%
Applied egg-rr43.0%
Taylor expanded in x around inf 94.4%
if -0.450000000000000011 < x Initial program 99.9%
Taylor expanded in y around inf 72.4%
Taylor expanded in x around 0 67.4%
rem-square-sqrt34.9%
fabs-sqr34.9%
rem-square-sqrt35.9%
Simplified35.9%
Final simplification48.7%
(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%
add-sqr-sqrt51.2%
fabs-sqr51.2%
add-sqr-sqrt55.9%
sub-neg55.9%
flip-+29.0%
pow229.0%
Applied egg-rr29.0%
Taylor expanded in x around inf 29.3%
Final simplification29.3%
(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 10.9%
herbie shell --seed 2024165
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))