
(FPCore (x y z) :precision binary64 (+ x (* (- y x) z)))
double code(double x, double y, double z) {
return x + ((y - 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 + ((y - x) * z)
end function
public static double code(double x, double y, double z) {
return x + ((y - x) * z);
}
def code(x, y, z): return x + ((y - x) * z)
function code(x, y, z) return Float64(x + Float64(Float64(y - x) * z)) end
function tmp = code(x, y, z) tmp = x + ((y - x) * z); end
code[x_, y_, z_] := N[(x + N[(N[(y - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(y - x\right) \cdot z
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (+ x (* (- y x) z)))
double code(double x, double y, double z) {
return x + ((y - 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 + ((y - x) * z)
end function
public static double code(double x, double y, double z) {
return x + ((y - x) * z);
}
def code(x, y, z): return x + ((y - x) * z)
function code(x, y, z) return Float64(x + Float64(Float64(y - x) * z)) end
function tmp = code(x, y, z) tmp = x + ((y - x) * z); end
code[x_, y_, z_] := N[(x + N[(N[(y - x), $MachinePrecision] * z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
x + \left(y - x\right) \cdot z
\end{array}
(FPCore (x y z) :precision binary64 (fma z y (fma (- x) z x)))
double code(double x, double y, double z) {
return fma(z, y, fma(-x, z, x));
}
function code(x, y, z) return fma(z, y, fma(Float64(-x), z, x)) end
code[x_, y_, z_] := N[(z * y + N[((-x) * z + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, y, \mathsf{fma}\left(-x, z, x\right)\right)
\end{array}
Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift--.f64N/A
sub-negN/A
distribute-lft-inN/A
associate-+l+N/A
*-commutativeN/A
lower-fma.f64N/A
lower-fma.f64N/A
lower-neg.f64100.0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (if (<= z -6.9e-137) (* z y) (if (<= z 1.0) (* 1.0 x) (if (<= z 5.6e+77) (* (- z) x) (* z y)))))
double code(double x, double y, double z) {
double tmp;
if (z <= -6.9e-137) {
tmp = z * y;
} else if (z <= 1.0) {
tmp = 1.0 * x;
} else if (z <= 5.6e+77) {
tmp = -z * x;
} else {
tmp = z * 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 (z <= (-6.9d-137)) then
tmp = z * y
else if (z <= 1.0d0) then
tmp = 1.0d0 * x
else if (z <= 5.6d+77) then
tmp = -z * x
else
tmp = z * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= -6.9e-137) {
tmp = z * y;
} else if (z <= 1.0) {
tmp = 1.0 * x;
} else if (z <= 5.6e+77) {
tmp = -z * x;
} else {
tmp = z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= -6.9e-137: tmp = z * y elif z <= 1.0: tmp = 1.0 * x elif z <= 5.6e+77: tmp = -z * x else: tmp = z * y return tmp
function code(x, y, z) tmp = 0.0 if (z <= -6.9e-137) tmp = Float64(z * y); elseif (z <= 1.0) tmp = Float64(1.0 * x); elseif (z <= 5.6e+77) tmp = Float64(Float64(-z) * x); else tmp = Float64(z * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= -6.9e-137) tmp = z * y; elseif (z <= 1.0) tmp = 1.0 * x; elseif (z <= 5.6e+77) tmp = -z * x; else tmp = z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, -6.9e-137], N[(z * y), $MachinePrecision], If[LessEqual[z, 1.0], N[(1.0 * x), $MachinePrecision], If[LessEqual[z, 5.6e+77], N[((-z) * x), $MachinePrecision], N[(z * y), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -6.9 \cdot 10^{-137}:\\
\;\;\;\;z \cdot y\\
\mathbf{elif}\;z \leq 1:\\
\;\;\;\;1 \cdot x\\
\mathbf{elif}\;z \leq 5.6 \cdot 10^{+77}:\\
\;\;\;\;\left(-z\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;z \cdot y\\
\end{array}
\end{array}
if z < -6.89999999999999976e-137 or 5.60000000000000001e77 < z Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6463.4
Applied rewrites63.4%
if -6.89999999999999976e-137 < z < 1Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6475.8
Applied rewrites75.8%
Taylor expanded in z around 0
Applied rewrites75.1%
if 1 < z < 5.60000000000000001e77Initial program 99.9%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6463.2
Applied rewrites63.2%
Taylor expanded in z around inf
Applied rewrites59.2%
Final simplification67.7%
(FPCore (x y z) :precision binary64 (if (or (<= z -3.5e-143) (not (<= z 0.00175))) (* z (- y x)) (* (- 1.0 z) x)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -3.5e-143) || !(z <= 0.00175)) {
tmp = z * (y - x);
} else {
tmp = (1.0 - z) * 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 <= (-3.5d-143)) .or. (.not. (z <= 0.00175d0))) then
tmp = z * (y - x)
else
tmp = (1.0d0 - z) * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -3.5e-143) || !(z <= 0.00175)) {
tmp = z * (y - x);
} else {
tmp = (1.0 - z) * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -3.5e-143) or not (z <= 0.00175): tmp = z * (y - x) else: tmp = (1.0 - z) * x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -3.5e-143) || !(z <= 0.00175)) tmp = Float64(z * Float64(y - x)); else tmp = Float64(Float64(1.0 - z) * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -3.5e-143) || ~((z <= 0.00175))) tmp = z * (y - x); else tmp = (1.0 - z) * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -3.5e-143], N[Not[LessEqual[z, 0.00175]], $MachinePrecision]], N[(z * N[(y - x), $MachinePrecision]), $MachinePrecision], N[(N[(1.0 - z), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -3.5 \cdot 10^{-143} \lor \neg \left(z \leq 0.00175\right):\\
\;\;\;\;z \cdot \left(y - x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 - z\right) \cdot x\\
\end{array}
\end{array}
if z < -3.50000000000000005e-143 or 0.00175000000000000004 < z Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift--.f64N/A
sub-negN/A
distribute-lft-inN/A
associate-+l+N/A
*-commutativeN/A
lower-fma.f64N/A
lower-fma.f64N/A
lower-neg.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
mul-1-negN/A
sub-negN/A
lower-*.f64N/A
lower--.f6491.9
Applied rewrites91.9%
if -3.50000000000000005e-143 < z < 0.00175000000000000004Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6476.3
Applied rewrites76.3%
Final simplification85.7%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.3e-10) (not (<= x 3.2e-208))) (* (- 1.0 z) x) (* z y)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.3e-10) || !(x <= 3.2e-208)) {
tmp = (1.0 - z) * x;
} else {
tmp = z * 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.3d-10)) .or. (.not. (x <= 3.2d-208))) then
tmp = (1.0d0 - z) * x
else
tmp = z * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -1.3e-10) || !(x <= 3.2e-208)) {
tmp = (1.0 - z) * x;
} else {
tmp = z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -1.3e-10) or not (x <= 3.2e-208): tmp = (1.0 - z) * x else: tmp = z * y return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -1.3e-10) || !(x <= 3.2e-208)) tmp = Float64(Float64(1.0 - z) * x); else tmp = Float64(z * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -1.3e-10) || ~((x <= 3.2e-208))) tmp = (1.0 - z) * x; else tmp = z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.3e-10], N[Not[LessEqual[x, 3.2e-208]], $MachinePrecision]], N[(N[(1.0 - z), $MachinePrecision] * x), $MachinePrecision], N[(z * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.3 \cdot 10^{-10} \lor \neg \left(x \leq 3.2 \cdot 10^{-208}\right):\\
\;\;\;\;\left(1 - z\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;z \cdot y\\
\end{array}
\end{array}
if x < -1.29999999999999991e-10 or 3.2000000000000001e-208 < x Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6479.2
Applied rewrites79.2%
if -1.29999999999999991e-10 < x < 3.2000000000000001e-208Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6483.8
Applied rewrites83.8%
Final simplification80.8%
(FPCore (x y z) :precision binary64 (if (or (<= z -6.9e-137) (not (<= z 8.5e-8))) (* z y) (* 1.0 x)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -6.9e-137) || !(z <= 8.5e-8)) {
tmp = z * y;
} else {
tmp = 1.0 * 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 <= (-6.9d-137)) .or. (.not. (z <= 8.5d-8))) then
tmp = z * y
else
tmp = 1.0d0 * x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -6.9e-137) || !(z <= 8.5e-8)) {
tmp = z * y;
} else {
tmp = 1.0 * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -6.9e-137) or not (z <= 8.5e-8): tmp = z * y else: tmp = 1.0 * x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -6.9e-137) || !(z <= 8.5e-8)) tmp = Float64(z * y); else tmp = Float64(1.0 * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -6.9e-137) || ~((z <= 8.5e-8))) tmp = z * y; else tmp = 1.0 * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -6.9e-137], N[Not[LessEqual[z, 8.5e-8]], $MachinePrecision]], N[(z * y), $MachinePrecision], N[(1.0 * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -6.9 \cdot 10^{-137} \lor \neg \left(z \leq 8.5 \cdot 10^{-8}\right):\\
\;\;\;\;z \cdot y\\
\mathbf{else}:\\
\;\;\;\;1 \cdot x\\
\end{array}
\end{array}
if z < -6.89999999999999976e-137 or 8.49999999999999935e-8 < z Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6459.2
Applied rewrites59.2%
if -6.89999999999999976e-137 < z < 8.49999999999999935e-8Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6475.8
Applied rewrites75.8%
Taylor expanded in z around 0
Applied rewrites75.1%
Final simplification65.6%
(FPCore (x y z) :precision binary64 (fma (- y x) z x))
double code(double x, double y, double z) {
return fma((y - x), z, x);
}
function code(x, y, z) return fma(Float64(y - x), z, x) end
code[x_, y_, z_] := N[(N[(y - x), $MachinePrecision] * z + x), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(y - x, z, x\right)
\end{array}
Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (* z y))
double code(double x, double y, double z) {
return z * y;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = z * y
end function
public static double code(double x, double y, double z) {
return z * y;
}
def code(x, y, z): return z * y
function code(x, y, z) return Float64(z * y) end
function tmp = code(x, y, z) tmp = z * y; end
code[x_, y_, z_] := N[(z * y), $MachinePrecision]
\begin{array}{l}
\\
z \cdot y
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6445.7
Applied rewrites45.7%
Final simplification45.7%
herbie shell --seed 2024318
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))