
(FPCore (x y z) :precision binary64 (+ (* x (+ y z)) (* z 5.0)))
double code(double x, double y, double z) {
return (x * (y + z)) + (z * 5.0);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * (y + z)) + (z * 5.0d0)
end function
public static double code(double x, double y, double z) {
return (x * (y + z)) + (z * 5.0);
}
def code(x, y, z): return (x * (y + z)) + (z * 5.0)
function code(x, y, z) return Float64(Float64(x * Float64(y + z)) + Float64(z * 5.0)) end
function tmp = code(x, y, z) tmp = (x * (y + z)) + (z * 5.0); end
code[x_, y_, z_] := N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] + N[(z * 5.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(y + z\right) + z \cdot 5
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 9 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ (* x (+ y z)) (* z 5.0)))
double code(double x, double y, double z) {
return (x * (y + z)) + (z * 5.0);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (x * (y + z)) + (z * 5.0d0)
end function
public static double code(double x, double y, double z) {
return (x * (y + z)) + (z * 5.0);
}
def code(x, y, z): return (x * (y + z)) + (z * 5.0)
function code(x, y, z) return Float64(Float64(x * Float64(y + z)) + Float64(z * 5.0)) end
function tmp = code(x, y, z) tmp = (x * (y + z)) + (z * 5.0); end
code[x_, y_, z_] := N[(N[(x * N[(y + z), $MachinePrecision]), $MachinePrecision] + N[(z * 5.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x \cdot \left(y + z\right) + z \cdot 5
\end{array}
(FPCore (x y z) :precision binary64 (fma z 5.0 (* (+ y z) x)))
double code(double x, double y, double z) {
return fma(z, 5.0, ((y + z) * x));
}
function code(x, y, z) return fma(z, 5.0, Float64(Float64(y + z) * x)) end
code[x_, y_, z_] := N[(z * 5.0 + N[(N[(y + z), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, 5, \left(y + z\right) \cdot x\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
lift-*.f64N/A
*-commutativeN/A
lower-*.f64100.0
lift-+.f64N/A
+-commutativeN/A
lower-+.f64100.0
Applied rewrites100.0%
Final simplification100.0%
(FPCore (x y z) :precision binary64 (if (<= x -1.2e+21) (* x z) (if (<= x -6e-117) (* x y) (if (<= x 5.4e-13) (* 5.0 z) (* x y)))))
double code(double x, double y, double z) {
double tmp;
if (x <= -1.2e+21) {
tmp = x * z;
} else if (x <= -6e-117) {
tmp = x * y;
} else if (x <= 5.4e-13) {
tmp = 5.0 * z;
} else {
tmp = x * y;
}
return tmp;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
real(8) :: tmp
if (x <= (-1.2d+21)) then
tmp = x * z
else if (x <= (-6d-117)) then
tmp = x * y
else if (x <= 5.4d-13) then
tmp = 5.0d0 * z
else
tmp = x * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (x <= -1.2e+21) {
tmp = x * z;
} else if (x <= -6e-117) {
tmp = x * y;
} else if (x <= 5.4e-13) {
tmp = 5.0 * z;
} else {
tmp = x * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -1.2e+21: tmp = x * z elif x <= -6e-117: tmp = x * y elif x <= 5.4e-13: tmp = 5.0 * z else: tmp = x * y return tmp
function code(x, y, z) tmp = 0.0 if (x <= -1.2e+21) tmp = Float64(x * z); elseif (x <= -6e-117) tmp = Float64(x * y); elseif (x <= 5.4e-13) tmp = Float64(5.0 * z); else tmp = Float64(x * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -1.2e+21) tmp = x * z; elseif (x <= -6e-117) tmp = x * y; elseif (x <= 5.4e-13) tmp = 5.0 * z; else tmp = x * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -1.2e+21], N[(x * z), $MachinePrecision], If[LessEqual[x, -6e-117], N[(x * y), $MachinePrecision], If[LessEqual[x, 5.4e-13], N[(5.0 * z), $MachinePrecision], N[(x * y), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.2 \cdot 10^{+21}:\\
\;\;\;\;x \cdot z\\
\mathbf{elif}\;x \leq -6 \cdot 10^{-117}:\\
\;\;\;\;x \cdot y\\
\mathbf{elif}\;x \leq 5.4 \cdot 10^{-13}:\\
\;\;\;\;5 \cdot z\\
\mathbf{else}:\\
\;\;\;\;x \cdot y\\
\end{array}
\end{array}
if x < -1.2e21Initial program 99.8%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
lower-+.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
Applied rewrites54.6%
if -1.2e21 < x < -5.99999999999999982e-117 or 5.40000000000000021e-13 < x Initial program 99.9%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6449.3
Applied rewrites49.3%
if -5.99999999999999982e-117 < x < 5.40000000000000021e-13Initial program 99.9%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6475.0
Applied rewrites75.0%
Final simplification60.6%
(FPCore (x y z) :precision binary64 (+ (* (+ x 5.0) z) (* x y)))
double code(double x, double y, double z) {
return ((x + 5.0) * z) + (x * y);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = ((x + 5.0d0) * z) + (x * y)
end function
public static double code(double x, double y, double z) {
return ((x + 5.0) * z) + (x * y);
}
def code(x, y, z): return ((x + 5.0) * z) + (x * y)
function code(x, y, z) return Float64(Float64(Float64(x + 5.0) * z) + Float64(x * y)) end
function tmp = code(x, y, z) tmp = ((x + 5.0) * z) + (x * y); end
code[x_, y_, z_] := N[(N[(N[(x + 5.0), $MachinePrecision] * z), $MachinePrecision] + N[(x * y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(x + 5\right) \cdot z + x \cdot y
\end{array}
herbie shell --seed 2024230
(FPCore (x y z)
:name "Graphics.Rendering.Plot.Render.Plot.Legend:renderLegendOutside from plot-0.2.3.4, C"
:precision binary64
:alt
(! :herbie-platform default (+ (* (+ x 5) z) (* x y)))
(+ (* x (+ y z)) (* z 5.0)))