
(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 8 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 (* (+ z y) x)))
double code(double x, double y, double z) {
return fma(z, 5.0, ((z + y) * x));
}
function code(x, y, z) return fma(z, 5.0, Float64(Float64(z + y) * x)) end
code[x_, y_, z_] := N[(z * 5.0 + N[(N[(z + y), $MachinePrecision] * x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, 5, \left(z + y\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%
(FPCore (x y z) :precision binary64 (if (<= x -3.05e+141) (* x z) (if (or (<= x -2.7e-14) (not (<= x 3e-109))) (* y x) (* 5.0 z))))
double code(double x, double y, double z) {
double tmp;
if (x <= -3.05e+141) {
tmp = x * z;
} else if ((x <= -2.7e-14) || !(x <= 3e-109)) {
tmp = y * x;
} else {
tmp = 5.0 * z;
}
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 <= (-3.05d+141)) then
tmp = x * z
else if ((x <= (-2.7d-14)) .or. (.not. (x <= 3d-109))) then
tmp = y * x
else
tmp = 5.0d0 * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (x <= -3.05e+141) {
tmp = x * z;
} else if ((x <= -2.7e-14) || !(x <= 3e-109)) {
tmp = y * x;
} else {
tmp = 5.0 * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -3.05e+141: tmp = x * z elif (x <= -2.7e-14) or not (x <= 3e-109): tmp = y * x else: tmp = 5.0 * z return tmp
function code(x, y, z) tmp = 0.0 if (x <= -3.05e+141) tmp = Float64(x * z); elseif ((x <= -2.7e-14) || !(x <= 3e-109)) tmp = Float64(y * x); else tmp = Float64(5.0 * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -3.05e+141) tmp = x * z; elseif ((x <= -2.7e-14) || ~((x <= 3e-109))) tmp = y * x; else tmp = 5.0 * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -3.05e+141], N[(x * z), $MachinePrecision], If[Or[LessEqual[x, -2.7e-14], N[Not[LessEqual[x, 3e-109]], $MachinePrecision]], N[(y * x), $MachinePrecision], N[(5.0 * z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.05 \cdot 10^{+141}:\\
\;\;\;\;x \cdot z\\
\mathbf{elif}\;x \leq -2.7 \cdot 10^{-14} \lor \neg \left(x \leq 3 \cdot 10^{-109}\right):\\
\;\;\;\;y \cdot x\\
\mathbf{else}:\\
\;\;\;\;5 \cdot z\\
\end{array}
\end{array}
if x < -3.04999999999999996e141Initial program 100.0%
Taylor expanded in y around 0
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
lower-+.f6465.8
Applied rewrites65.8%
Taylor expanded in x around inf
Applied rewrites65.8%
if -3.04999999999999996e141 < x < -2.6999999999999999e-14 or 3.00000000000000021e-109 < x Initial program 100.0%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6462.4
Applied rewrites62.4%
if -2.6999999999999999e-14 < x < 3.00000000000000021e-109Initial program 99.9%
Taylor expanded in x around 0
lower-*.f6482.4
Applied rewrites82.4%
Final simplification70.7%
(FPCore (x y z) :precision binary64 (if (or (<= x -3.7e-8) (not (<= x 3e-109))) (* (+ y z) x) (fma z 5.0 (* x z))))
double code(double x, double y, double z) {
double tmp;
if ((x <= -3.7e-8) || !(x <= 3e-109)) {
tmp = (y + z) * x;
} else {
tmp = fma(z, 5.0, (x * z));
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -3.7e-8) || !(x <= 3e-109)) tmp = Float64(Float64(y + z) * x); else tmp = fma(z, 5.0, Float64(x * z)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -3.7e-8], N[Not[LessEqual[x, 3e-109]], $MachinePrecision]], N[(N[(y + z), $MachinePrecision] * x), $MachinePrecision], N[(z * 5.0 + N[(x * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.7 \cdot 10^{-8} \lor \neg \left(x \leq 3 \cdot 10^{-109}\right):\\
\;\;\;\;\left(y + z\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(z, 5, x \cdot z\right)\\
\end{array}
\end{array}
if x < -3.7e-8 or 3.00000000000000021e-109 < x Initial program 100.0%
lift-+.f64N/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lift-*.f64N/A
distribute-lft-outN/A
lower-*.f64N/A
lower-+.f6498.1
Applied rewrites98.1%
Taylor expanded in x around -inf
mul-1-negN/A
distribute-lft-inN/A
*-commutativeN/A
distribute-lft-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
*-lft-identityN/A
lower-*.f64N/A
lower-+.f6496.0
Applied rewrites96.0%
if -3.7e-8 < x < 3.00000000000000021e-109Initial program 99.9%
Taylor expanded in y around 0
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
lower-+.f6481.8
Applied rewrites81.8%
Applied rewrites81.9%
Final simplification90.4%
(FPCore (x y z) :precision binary64 (if (or (<= x -3.7e-8) (not (<= x 3e-109))) (* (+ y z) x) (* (+ 5.0 x) z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -3.7e-8) || !(x <= 3e-109)) {
tmp = (y + z) * x;
} else {
tmp = (5.0 + x) * z;
}
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 <= (-3.7d-8)) .or. (.not. (x <= 3d-109))) then
tmp = (y + z) * x
else
tmp = (5.0d0 + x) * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -3.7e-8) || !(x <= 3e-109)) {
tmp = (y + z) * x;
} else {
tmp = (5.0 + x) * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -3.7e-8) or not (x <= 3e-109): tmp = (y + z) * x else: tmp = (5.0 + x) * z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -3.7e-8) || !(x <= 3e-109)) tmp = Float64(Float64(y + z) * x); else tmp = Float64(Float64(5.0 + x) * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -3.7e-8) || ~((x <= 3e-109))) tmp = (y + z) * x; else tmp = (5.0 + x) * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -3.7e-8], N[Not[LessEqual[x, 3e-109]], $MachinePrecision]], N[(N[(y + z), $MachinePrecision] * x), $MachinePrecision], N[(N[(5.0 + x), $MachinePrecision] * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -3.7 \cdot 10^{-8} \lor \neg \left(x \leq 3 \cdot 10^{-109}\right):\\
\;\;\;\;\left(y + z\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;\left(5 + x\right) \cdot z\\
\end{array}
\end{array}
if x < -3.7e-8 or 3.00000000000000021e-109 < x Initial program 100.0%
lift-+.f64N/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lift-*.f64N/A
distribute-lft-outN/A
lower-*.f64N/A
lower-+.f6498.1
Applied rewrites98.1%
Taylor expanded in x around -inf
mul-1-negN/A
distribute-lft-inN/A
*-commutativeN/A
distribute-lft-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
*-lft-identityN/A
lower-*.f64N/A
lower-+.f6496.0
Applied rewrites96.0%
if -3.7e-8 < x < 3.00000000000000021e-109Initial program 99.9%
Taylor expanded in y around 0
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
lower-+.f6481.8
Applied rewrites81.8%
Final simplification90.4%
(FPCore (x y z) :precision binary64 (if (or (<= z -4.2e-85) (not (<= z 5.4e-98))) (* (+ 5.0 x) z) (* y x)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -4.2e-85) || !(z <= 5.4e-98)) {
tmp = (5.0 + x) * z;
} else {
tmp = y * x;
}
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 ((z <= (-4.2d-85)) .or. (.not. (z <= 5.4d-98))) then
tmp = (5.0d0 + x) * z
else
tmp = y * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -4.2e-85) || !(z <= 5.4e-98)) {
tmp = (5.0 + x) * z;
} else {
tmp = y * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -4.2e-85) or not (z <= 5.4e-98): tmp = (5.0 + x) * z else: tmp = y * x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -4.2e-85) || !(z <= 5.4e-98)) tmp = Float64(Float64(5.0 + x) * z); else tmp = Float64(y * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -4.2e-85) || ~((z <= 5.4e-98))) tmp = (5.0 + x) * z; else tmp = y * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -4.2e-85], N[Not[LessEqual[z, 5.4e-98]], $MachinePrecision]], N[(N[(5.0 + x), $MachinePrecision] * z), $MachinePrecision], N[(y * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -4.2 \cdot 10^{-85} \lor \neg \left(z \leq 5.4 \cdot 10^{-98}\right):\\
\;\;\;\;\left(5 + x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;y \cdot x\\
\end{array}
\end{array}
if z < -4.2e-85 or 5.3999999999999997e-98 < z Initial program 99.9%
Taylor expanded in y around 0
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
lower-+.f6479.5
Applied rewrites79.5%
if -4.2e-85 < z < 5.3999999999999997e-98Initial program 99.9%
Taylor expanded in y around inf
*-commutativeN/A
lower-*.f6475.6
Applied rewrites75.6%
Final simplification78.2%
(FPCore (x y z) :precision binary64 (if (or (<= x -4.5e+16) (not (<= x 5.0))) (* x z) (* 5.0 z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -4.5e+16) || !(x <= 5.0)) {
tmp = x * z;
} else {
tmp = 5.0 * z;
}
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 <= (-4.5d+16)) .or. (.not. (x <= 5.0d0))) then
tmp = x * z
else
tmp = 5.0d0 * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -4.5e+16) || !(x <= 5.0)) {
tmp = x * z;
} else {
tmp = 5.0 * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -4.5e+16) or not (x <= 5.0): tmp = x * z else: tmp = 5.0 * z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -4.5e+16) || !(x <= 5.0)) tmp = Float64(x * z); else tmp = Float64(5.0 * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -4.5e+16) || ~((x <= 5.0))) tmp = x * z; else tmp = 5.0 * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -4.5e+16], N[Not[LessEqual[x, 5.0]], $MachinePrecision]], N[(x * z), $MachinePrecision], N[(5.0 * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.5 \cdot 10^{+16} \lor \neg \left(x \leq 5\right):\\
\;\;\;\;x \cdot z\\
\mathbf{else}:\\
\;\;\;\;5 \cdot z\\
\end{array}
\end{array}
if x < -4.5e16 or 5 < x Initial program 100.0%
Taylor expanded in y around 0
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
lower-+.f6451.1
Applied rewrites51.1%
Taylor expanded in x around inf
Applied rewrites50.7%
if -4.5e16 < x < 5Initial program 99.9%
Taylor expanded in x around 0
lower-*.f6471.7
Applied rewrites71.7%
Final simplification60.9%
(FPCore (x y z) :precision binary64 (fma y x (* z (+ x 5.0))))
double code(double x, double y, double z) {
return fma(y, x, (z * (x + 5.0)));
}
function code(x, y, z) return fma(y, x, Float64(z * Float64(x + 5.0))) end
code[x_, y_, z_] := N[(y * x + N[(z * N[(x + 5.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y, x, z \cdot \left(x + 5\right)\right)
\end{array}
Initial program 99.9%
lift-+.f64N/A
lift-*.f64N/A
lift-+.f64N/A
distribute-rgt-inN/A
associate-+l+N/A
lower-fma.f64N/A
lift-*.f64N/A
distribute-lft-outN/A
lower-*.f64N/A
lower-+.f6498.8
Applied rewrites98.8%
(FPCore (x y z) :precision binary64 (* x z))
double code(double x, double y, double z) {
return x * z;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = x * z
end function
public static double code(double x, double y, double z) {
return x * z;
}
def code(x, y, z): return x * z
function code(x, y, z) return Float64(x * z) end
function tmp = code(x, y, z) tmp = x * z; end
code[x_, y_, z_] := N[(x * z), $MachinePrecision]
\begin{array}{l}
\\
x \cdot z
\end{array}
Initial program 99.9%
Taylor expanded in y around 0
distribute-rgt-inN/A
*-commutativeN/A
lower-*.f64N/A
lower-+.f6461.3
Applied rewrites61.3%
Taylor expanded in x around inf
Applied rewrites27.5%
Final simplification27.5%
(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 2024337
(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)))