
(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 (- 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
(let* ((t_0 (* (- z) x)))
(if (<= z -1.3e+47)
t_0
(if (<= z -1e-136)
(* z y)
(if (<= z 7.5e-14) (* 1.0 x) (if (<= z 5e+85) (* z y) t_0))))))
double code(double x, double y, double z) {
double t_0 = -z * x;
double tmp;
if (z <= -1.3e+47) {
tmp = t_0;
} else if (z <= -1e-136) {
tmp = z * y;
} else if (z <= 7.5e-14) {
tmp = 1.0 * x;
} else if (z <= 5e+85) {
tmp = z * y;
} else {
tmp = t_0;
}
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) :: t_0
real(8) :: tmp
t_0 = -z * x
if (z <= (-1.3d+47)) then
tmp = t_0
else if (z <= (-1d-136)) then
tmp = z * y
else if (z <= 7.5d-14) then
tmp = 1.0d0 * x
else if (z <= 5d+85) then
tmp = z * y
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = -z * x;
double tmp;
if (z <= -1.3e+47) {
tmp = t_0;
} else if (z <= -1e-136) {
tmp = z * y;
} else if (z <= 7.5e-14) {
tmp = 1.0 * x;
} else if (z <= 5e+85) {
tmp = z * y;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = -z * x tmp = 0 if z <= -1.3e+47: tmp = t_0 elif z <= -1e-136: tmp = z * y elif z <= 7.5e-14: tmp = 1.0 * x elif z <= 5e+85: tmp = z * y else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(Float64(-z) * x) tmp = 0.0 if (z <= -1.3e+47) tmp = t_0; elseif (z <= -1e-136) tmp = Float64(z * y); elseif (z <= 7.5e-14) tmp = Float64(1.0 * x); elseif (z <= 5e+85) tmp = Float64(z * y); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = -z * x; tmp = 0.0; if (z <= -1.3e+47) tmp = t_0; elseif (z <= -1e-136) tmp = z * y; elseif (z <= 7.5e-14) tmp = 1.0 * x; elseif (z <= 5e+85) tmp = z * y; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[((-z) * x), $MachinePrecision]}, If[LessEqual[z, -1.3e+47], t$95$0, If[LessEqual[z, -1e-136], N[(z * y), $MachinePrecision], If[LessEqual[z, 7.5e-14], N[(1.0 * x), $MachinePrecision], If[LessEqual[z, 5e+85], N[(z * y), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(-z\right) \cdot x\\
\mathbf{if}\;z \leq -1.3 \cdot 10^{+47}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq -1 \cdot 10^{-136}:\\
\;\;\;\;z \cdot y\\
\mathbf{elif}\;z \leq 7.5 \cdot 10^{-14}:\\
\;\;\;\;1 \cdot x\\
\mathbf{elif}\;z \leq 5 \cdot 10^{+85}:\\
\;\;\;\;z \cdot y\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < -1.30000000000000002e47 or 5.0000000000000001e85 < z Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6465.0
Applied rewrites65.0%
Taylor expanded in z around inf
Applied rewrites65.0%
if -1.30000000000000002e47 < z < -1e-136 or 7.4999999999999996e-14 < z < 5.0000000000000001e85Initial program 99.9%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6462.6
Applied rewrites62.6%
if -1e-136 < z < 7.4999999999999996e-14Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6473.8
Applied rewrites73.8%
Taylor expanded in z around 0
Applied rewrites73.7%
Final simplification68.1%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.5e-7) (not (<= x 8.5e-19))) (fma (- z) x x) (* (- y x) z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.5e-7) || !(x <= 8.5e-19)) {
tmp = fma(-z, x, x);
} else {
tmp = (y - x) * z;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -1.5e-7) || !(x <= 8.5e-19)) tmp = fma(Float64(-z), x, x); else tmp = Float64(Float64(y - x) * z); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.5e-7], N[Not[LessEqual[x, 8.5e-19]], $MachinePrecision]], N[((-z) * x + x), $MachinePrecision], N[(N[(y - x), $MachinePrecision] * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.5 \cdot 10^{-7} \lor \neg \left(x \leq 8.5 \cdot 10^{-19}\right):\\
\;\;\;\;\mathsf{fma}\left(-z, x, x\right)\\
\mathbf{else}:\\
\;\;\;\;\left(y - x\right) \cdot z\\
\end{array}
\end{array}
if x < -1.4999999999999999e-7 or 8.50000000000000003e-19 < x Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6489.9
Applied rewrites89.9%
Applied rewrites89.9%
if -1.4999999999999999e-7 < x < 8.50000000000000003e-19Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
Applied rewrites100.0%
Taylor expanded in z around inf
*-commutativeN/A
lower-*.f64N/A
lower--.f6479.1
Applied rewrites79.1%
Final simplification84.6%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.45e-75) (not (<= x 1.35e-94))) (fma (- z) x x) (* z y)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.45e-75) || !(x <= 1.35e-94)) {
tmp = fma(-z, x, x);
} else {
tmp = z * y;
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((x <= -1.45e-75) || !(x <= 1.35e-94)) tmp = fma(Float64(-z), x, x); else tmp = Float64(z * y); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.45e-75], N[Not[LessEqual[x, 1.35e-94]], $MachinePrecision]], N[((-z) * x + x), $MachinePrecision], N[(z * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.45 \cdot 10^{-75} \lor \neg \left(x \leq 1.35 \cdot 10^{-94}\right):\\
\;\;\;\;\mathsf{fma}\left(-z, x, x\right)\\
\mathbf{else}:\\
\;\;\;\;z \cdot y\\
\end{array}
\end{array}
if x < -1.4500000000000001e-75 or 1.3500000000000001e-94 < x Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6483.0
Applied rewrites83.0%
Applied rewrites83.0%
if -1.4500000000000001e-75 < x < 1.3500000000000001e-94Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6472.9
Applied rewrites72.9%
Final simplification79.3%
(FPCore (x y z) :precision binary64 (if (or (<= x -1.45e-75) (not (<= x 1.35e-94))) (* (- 1.0 z) x) (* z y)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -1.45e-75) || !(x <= 1.35e-94)) {
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.45d-75)) .or. (.not. (x <= 1.35d-94))) 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.45e-75) || !(x <= 1.35e-94)) {
tmp = (1.0 - z) * x;
} else {
tmp = z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -1.45e-75) or not (x <= 1.35e-94): tmp = (1.0 - z) * x else: tmp = z * y return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -1.45e-75) || !(x <= 1.35e-94)) 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.45e-75) || ~((x <= 1.35e-94))) tmp = (1.0 - z) * x; else tmp = z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -1.45e-75], N[Not[LessEqual[x, 1.35e-94]], $MachinePrecision]], N[(N[(1.0 - z), $MachinePrecision] * x), $MachinePrecision], N[(z * y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.45 \cdot 10^{-75} \lor \neg \left(x \leq 1.35 \cdot 10^{-94}\right):\\
\;\;\;\;\left(1 - z\right) \cdot x\\
\mathbf{else}:\\
\;\;\;\;z \cdot y\\
\end{array}
\end{array}
if x < -1.4500000000000001e-75 or 1.3500000000000001e-94 < x Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6483.0
Applied rewrites83.0%
if -1.4500000000000001e-75 < x < 1.3500000000000001e-94Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6472.9
Applied rewrites72.9%
Final simplification79.3%
(FPCore (x y z) :precision binary64 (if (or (<= z -1e-136) (not (<= z 7.5e-14))) (* z y) (* 1.0 x)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1e-136) || !(z <= 7.5e-14)) {
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 <= (-1d-136)) .or. (.not. (z <= 7.5d-14))) 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 <= -1e-136) || !(z <= 7.5e-14)) {
tmp = z * y;
} else {
tmp = 1.0 * x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -1e-136) or not (z <= 7.5e-14): tmp = z * y else: tmp = 1.0 * x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1e-136) || !(z <= 7.5e-14)) 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 <= -1e-136) || ~((z <= 7.5e-14))) tmp = z * y; else tmp = 1.0 * x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -1e-136], N[Not[LessEqual[z, 7.5e-14]], $MachinePrecision]], N[(z * y), $MachinePrecision], N[(1.0 * x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1 \cdot 10^{-136} \lor \neg \left(z \leq 7.5 \cdot 10^{-14}\right):\\
\;\;\;\;z \cdot y\\
\mathbf{else}:\\
\;\;\;\;1 \cdot x\\
\end{array}
\end{array}
if z < -1e-136 or 7.4999999999999996e-14 < z Initial program 100.0%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6449.5
Applied rewrites49.5%
if -1e-136 < z < 7.4999999999999996e-14Initial program 100.0%
Taylor expanded in x around inf
*-commutativeN/A
lower-*.f64N/A
mul-1-negN/A
unsub-negN/A
lower--.f6473.8
Applied rewrites73.8%
Taylor expanded in z around 0
Applied rewrites73.7%
Final simplification59.6%
(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-*.f6441.4
Applied rewrites41.4%
Final simplification41.4%
herbie shell --seed 2024323
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))