
(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 8 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 (or (<= z -4.5e+16) (not (<= z 5.1e-135))) (* z (- y x)) (* (- 1.0 z) x)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -4.5e+16) || !(z <= 5.1e-135)) {
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 <= (-4.5d+16)) .or. (.not. (z <= 5.1d-135))) 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 <= -4.5e+16) || !(z <= 5.1e-135)) {
tmp = z * (y - x);
} else {
tmp = (1.0 - z) * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -4.5e+16) or not (z <= 5.1e-135): tmp = z * (y - x) else: tmp = (1.0 - z) * x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -4.5e+16) || !(z <= 5.1e-135)) 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 <= -4.5e+16) || ~((z <= 5.1e-135))) tmp = z * (y - x); else tmp = (1.0 - z) * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -4.5e+16], N[Not[LessEqual[z, 5.1e-135]], $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 -4.5 \cdot 10^{+16} \lor \neg \left(z \leq 5.1 \cdot 10^{-135}\right):\\
\;\;\;\;z \cdot \left(y - x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 - z\right) \cdot x\\
\end{array}
\end{array}
if z < -4.5e16 or 5.1000000000000001e-135 < 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--.f6490.4
Applied rewrites90.4%
if -4.5e16 < z < 5.1000000000000001e-135Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6468.6
Applied rewrites68.6%
Final simplification81.5%
(FPCore (x y z) :precision binary64 (if (or (<= y -0.000335) (not (<= y 9.2e+39))) (* z y) (fma (- x) z x)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -0.000335) || !(y <= 9.2e+39)) {
tmp = z * y;
} else {
tmp = fma(-x, z, x);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -0.000335) || !(y <= 9.2e+39)) tmp = Float64(z * y); else tmp = fma(Float64(-x), z, x); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -0.000335], N[Not[LessEqual[y, 9.2e+39]], $MachinePrecision]], N[(z * y), $MachinePrecision], N[((-x) * z + x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -0.000335 \lor \neg \left(y \leq 9.2 \cdot 10^{+39}\right):\\
\;\;\;\;z \cdot y\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-x, z, x\right)\\
\end{array}
\end{array}
if y < -3.3500000000000001e-4 or 9.20000000000000047e39 < y Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6471.5
Applied rewrites71.5%
if -3.3500000000000001e-4 < y < 9.20000000000000047e39Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6481.9
Applied rewrites81.9%
Applied rewrites81.9%
Final simplification76.8%
(FPCore (x y z) :precision binary64 (if (or (<= y -0.000335) (not (<= y 9.2e+39))) (* z y) (* (- 1.0 z) x)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -0.000335) || !(y <= 9.2e+39)) {
tmp = z * y;
} 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 ((y <= (-0.000335d0)) .or. (.not. (y <= 9.2d+39))) then
tmp = z * y
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 ((y <= -0.000335) || !(y <= 9.2e+39)) {
tmp = z * y;
} else {
tmp = (1.0 - z) * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -0.000335) or not (y <= 9.2e+39): tmp = z * y else: tmp = (1.0 - z) * x return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -0.000335) || !(y <= 9.2e+39)) tmp = Float64(z * y); else tmp = Float64(Float64(1.0 - z) * x); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -0.000335) || ~((y <= 9.2e+39))) tmp = z * y; else tmp = (1.0 - z) * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -0.000335], N[Not[LessEqual[y, 9.2e+39]], $MachinePrecision]], N[(z * y), $MachinePrecision], N[(N[(1.0 - z), $MachinePrecision] * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -0.000335 \lor \neg \left(y \leq 9.2 \cdot 10^{+39}\right):\\
\;\;\;\;z \cdot y\\
\mathbf{else}:\\
\;\;\;\;\left(1 - z\right) \cdot x\\
\end{array}
\end{array}
if y < -3.3500000000000001e-4 or 9.20000000000000047e39 < y Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6471.5
Applied rewrites71.5%
if -3.3500000000000001e-4 < y < 9.20000000000000047e39Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6481.9
Applied rewrites81.9%
Final simplification76.8%
(FPCore (x y z) :precision binary64 (if (or (<= z -2.2e-68) (not (<= z 5e-135))) (* z y) (* 1.0 x)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -2.2e-68) || !(z <= 5e-135)) {
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 <= (-2.2d-68)) .or. (.not. (z <= 5d-135))) 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 <= -2.2e-68) || !(z <= 5e-135)) {
tmp = z * y;
} else {
tmp = 1.0 * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -2.2e-68) or not (z <= 5e-135): tmp = z * y else: tmp = 1.0 * x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -2.2e-68) || !(z <= 5e-135)) 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 <= -2.2e-68) || ~((z <= 5e-135))) tmp = z * y; else tmp = 1.0 * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -2.2e-68], N[Not[LessEqual[z, 5e-135]], $MachinePrecision]], N[(z * y), $MachinePrecision], N[(1.0 * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -2.2 \cdot 10^{-68} \lor \neg \left(z \leq 5 \cdot 10^{-135}\right):\\
\;\;\;\;z \cdot y\\
\mathbf{else}:\\
\;\;\;\;1 \cdot x\\
\end{array}
\end{array}
if z < -2.20000000000000002e-68 or 5.0000000000000002e-135 < z Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6451.7
Applied rewrites51.7%
if -2.20000000000000002e-68 < z < 5.0000000000000002e-135Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6470.1
Applied rewrites70.1%
Taylor expanded in z around 0
Applied rewrites70.1%
Final simplification58.2%
(FPCore (x y z) :precision binary64 (if (<= z -1.0) (* (- z) x) (if (<= z 5e-135) (* 1.0 x) (* z y))))
double code(double x, double y, double z) {
double tmp;
if (z <= -1.0) {
tmp = -z * x;
} else if (z <= 5e-135) {
tmp = 1.0 * 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 <= (-1.0d0)) then
tmp = -z * x
else if (z <= 5d-135) then
tmp = 1.0d0 * 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 <= -1.0) {
tmp = -z * x;
} else if (z <= 5e-135) {
tmp = 1.0 * x;
} else {
tmp = z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= -1.0: tmp = -z * x elif z <= 5e-135: tmp = 1.0 * x else: tmp = z * y return tmp
function code(x, y, z) tmp = 0.0 if (z <= -1.0) tmp = Float64(Float64(-z) * x); elseif (z <= 5e-135) tmp = Float64(1.0 * x); else tmp = Float64(z * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= -1.0) tmp = -z * x; elseif (z <= 5e-135) tmp = 1.0 * x; else tmp = z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, -1.0], N[((-z) * x), $MachinePrecision], If[LessEqual[z, 5e-135], N[(1.0 * x), $MachinePrecision], N[(z * y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1:\\
\;\;\;\;\left(-z\right) \cdot x\\
\mathbf{elif}\;z \leq 5 \cdot 10^{-135}:\\
\;\;\;\;1 \cdot x\\
\mathbf{else}:\\
\;\;\;\;z \cdot y\\
\end{array}
\end{array}
if z < -1Initial program 99.9%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6459.2
Applied rewrites59.2%
Taylor expanded in z around inf
Applied rewrites56.3%
if -1 < z < 5.0000000000000002e-135Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6467.0
Applied rewrites67.0%
Taylor expanded in z around 0
Applied rewrites66.3%
if 5.0000000000000002e-135 < z Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6456.5
Applied rewrites56.5%
Final simplification60.2%
(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-*.f6444.8
Applied rewrites44.8%
Final simplification44.8%
herbie shell --seed 2024314
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))