
(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 (or (<= y -58000.0) (and (not (<= y -5.6e-112)) (<= y -1.05e-186))) (* (fabs (- y x)) 0.5) (* 0.5 (+ x y))))
double code(double x, double y) {
double tmp;
if ((y <= -58000.0) || (!(y <= -5.6e-112) && (y <= -1.05e-186))) {
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 <= (-58000.0d0)) .or. (.not. (y <= (-5.6d-112))) .and. (y <= (-1.05d-186))) 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 <= -58000.0) || (!(y <= -5.6e-112) && (y <= -1.05e-186))) {
tmp = Math.abs((y - x)) * 0.5;
} else {
tmp = 0.5 * (x + y);
}
return tmp;
}
def code(x, y): tmp = 0 if (y <= -58000.0) or (not (y <= -5.6e-112) and (y <= -1.05e-186)): tmp = math.fabs((y - x)) * 0.5 else: tmp = 0.5 * (x + y) return tmp
function code(x, y) tmp = 0.0 if ((y <= -58000.0) || (!(y <= -5.6e-112) && (y <= -1.05e-186))) 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 <= -58000.0) || (~((y <= -5.6e-112)) && (y <= -1.05e-186))) tmp = abs((y - x)) * 0.5; else tmp = 0.5 * (x + y); end tmp_2 = tmp; end
code[x_, y_] := If[Or[LessEqual[y, -58000.0], And[N[Not[LessEqual[y, -5.6e-112]], $MachinePrecision], LessEqual[y, -1.05e-186]]], 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 -58000 \lor \neg \left(y \leq -5.6 \cdot 10^{-112}\right) \land y \leq -1.05 \cdot 10^{-186}:\\
\;\;\;\;\left|y - x\right| \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;0.5 \cdot \left(x + y\right)\\
\end{array}
\end{array}
if y < -58000 or -5.60000000000000046e-112 < y < -1.0500000000000001e-186Initial program 100.0%
Taylor expanded in x around 0 78.0%
if -58000 < y < -5.60000000000000046e-112 or -1.0500000000000001e-186 < y Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt71.1%
fabs-sqr71.1%
add-sqr-sqrt76.0%
metadata-eval76.0%
Applied egg-rr76.0%
Taylor expanded in y around 0 76.0%
associate-+r+76.0%
distribute-rgt1-in76.0%
metadata-eval76.0%
distribute-lft-out76.0%
Simplified76.0%
Final simplification76.5%
(FPCore (x y) :precision binary64 (if (<= y 8.4e-257) x (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 8.4e-257) {
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 <= 8.4d-257) 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 <= 8.4e-257) {
tmp = x;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 8.4e-257: tmp = x else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 8.4e-257) tmp = x; else tmp = Float64(y * 0.5); end return tmp end
function tmp_2 = code(x, y) tmp = 0.0; if (y <= 8.4e-257) tmp = x; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 8.4e-257], x, N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 8.4 \cdot 10^{-257}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 8.4000000000000004e-257Initial program 99.9%
Taylor expanded in x around inf 11.8%
if 8.4000000000000004e-257 < y Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt79.1%
fabs-sqr79.1%
add-sqr-sqrt83.3%
metadata-eval83.3%
Applied egg-rr83.3%
Taylor expanded in y around inf 56.4%
Final simplification34.1%
(FPCore (x y) :precision binary64 (if (<= y 2.7e-117) (* x 0.5) (* y 0.5)))
double code(double x, double y) {
double tmp;
if (y <= 2.7e-117) {
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.7d-117) 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.7e-117) {
tmp = x * 0.5;
} else {
tmp = y * 0.5;
}
return tmp;
}
def code(x, y): tmp = 0 if y <= 2.7e-117: tmp = x * 0.5 else: tmp = y * 0.5 return tmp
function code(x, y) tmp = 0.0 if (y <= 2.7e-117) 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.7e-117) tmp = x * 0.5; else tmp = y * 0.5; end tmp_2 = tmp; end
code[x_, y_] := If[LessEqual[y, 2.7e-117], N[(x * 0.5), $MachinePrecision], N[(y * 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 2.7 \cdot 10^{-117}:\\
\;\;\;\;x \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;y \cdot 0.5\\
\end{array}
\end{array}
if y < 2.70000000000000003e-117Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt34.8%
fabs-sqr34.8%
add-sqr-sqrt40.1%
metadata-eval40.1%
Applied egg-rr40.1%
Taylor expanded in y around 0 37.0%
distribute-rgt1-in37.0%
metadata-eval37.0%
*-commutative37.0%
Simplified37.0%
if 2.70000000000000003e-117 < y Initial program 99.9%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt83.1%
fabs-sqr83.1%
add-sqr-sqrt86.7%
metadata-eval86.7%
Applied egg-rr86.7%
Taylor expanded in y around inf 66.2%
Final simplification48.3%
(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%
+-commutative99.9%
div-inv99.9%
fma-def99.9%
add-sqr-sqrt53.5%
fabs-sqr53.5%
add-sqr-sqrt58.1%
metadata-eval58.1%
Applied egg-rr58.1%
Taylor expanded in y around 0 58.1%
associate-+r+58.1%
distribute-rgt1-in58.1%
metadata-eval58.1%
distribute-lft-out58.1%
Simplified58.1%
Final simplification58.1%
(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 2024031
(FPCore (x y)
:name "Graphics.Rendering.Chart.Plot.AreaSpots:renderSpotLegend from Chart-1.5.3"
:precision binary64
(+ x (/ (fabs (- y x)) 2.0)))