
(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 (- 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%
+-commutative100.0%
fma-define100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* x (- z))))
(if (<= z -1.55e+269)
(* y z)
(if (<= z -2.15e+242)
t_0
(if (<= z -1.65e+175)
(* y z)
(if (<= z -6.2e+79)
t_0
(if (<= z -3e-20)
(* y z)
(if (<= z 1.0) x (if (<= z 1.35e+142) t_0 (* y z))))))))))
double code(double x, double y, double z) {
double t_0 = x * -z;
double tmp;
if (z <= -1.55e+269) {
tmp = y * z;
} else if (z <= -2.15e+242) {
tmp = t_0;
} else if (z <= -1.65e+175) {
tmp = y * z;
} else if (z <= -6.2e+79) {
tmp = t_0;
} else if (z <= -3e-20) {
tmp = y * z;
} else if (z <= 1.0) {
tmp = x;
} else if (z <= 1.35e+142) {
tmp = t_0;
} else {
tmp = y * 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) :: t_0
real(8) :: tmp
t_0 = x * -z
if (z <= (-1.55d+269)) then
tmp = y * z
else if (z <= (-2.15d+242)) then
tmp = t_0
else if (z <= (-1.65d+175)) then
tmp = y * z
else if (z <= (-6.2d+79)) then
tmp = t_0
else if (z <= (-3d-20)) then
tmp = y * z
else if (z <= 1.0d0) then
tmp = x
else if (z <= 1.35d+142) then
tmp = t_0
else
tmp = y * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = x * -z;
double tmp;
if (z <= -1.55e+269) {
tmp = y * z;
} else if (z <= -2.15e+242) {
tmp = t_0;
} else if (z <= -1.65e+175) {
tmp = y * z;
} else if (z <= -6.2e+79) {
tmp = t_0;
} else if (z <= -3e-20) {
tmp = y * z;
} else if (z <= 1.0) {
tmp = x;
} else if (z <= 1.35e+142) {
tmp = t_0;
} else {
tmp = y * z;
}
return tmp;
}
def code(x, y, z): t_0 = x * -z tmp = 0 if z <= -1.55e+269: tmp = y * z elif z <= -2.15e+242: tmp = t_0 elif z <= -1.65e+175: tmp = y * z elif z <= -6.2e+79: tmp = t_0 elif z <= -3e-20: tmp = y * z elif z <= 1.0: tmp = x elif z <= 1.35e+142: tmp = t_0 else: tmp = y * z return tmp
function code(x, y, z) t_0 = Float64(x * Float64(-z)) tmp = 0.0 if (z <= -1.55e+269) tmp = Float64(y * z); elseif (z <= -2.15e+242) tmp = t_0; elseif (z <= -1.65e+175) tmp = Float64(y * z); elseif (z <= -6.2e+79) tmp = t_0; elseif (z <= -3e-20) tmp = Float64(y * z); elseif (z <= 1.0) tmp = x; elseif (z <= 1.35e+142) tmp = t_0; else tmp = Float64(y * z); end return tmp end
function tmp_2 = code(x, y, z) t_0 = x * -z; tmp = 0.0; if (z <= -1.55e+269) tmp = y * z; elseif (z <= -2.15e+242) tmp = t_0; elseif (z <= -1.65e+175) tmp = y * z; elseif (z <= -6.2e+79) tmp = t_0; elseif (z <= -3e-20) tmp = y * z; elseif (z <= 1.0) tmp = x; elseif (z <= 1.35e+142) tmp = t_0; else tmp = y * z; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * (-z)), $MachinePrecision]}, If[LessEqual[z, -1.55e+269], N[(y * z), $MachinePrecision], If[LessEqual[z, -2.15e+242], t$95$0, If[LessEqual[z, -1.65e+175], N[(y * z), $MachinePrecision], If[LessEqual[z, -6.2e+79], t$95$0, If[LessEqual[z, -3e-20], N[(y * z), $MachinePrecision], If[LessEqual[z, 1.0], x, If[LessEqual[z, 1.35e+142], t$95$0, N[(y * z), $MachinePrecision]]]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(-z\right)\\
\mathbf{if}\;z \leq -1.55 \cdot 10^{+269}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq -2.15 \cdot 10^{+242}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq -1.65 \cdot 10^{+175}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq -6.2 \cdot 10^{+79}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq -3 \cdot 10^{-20}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq 1:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq 1.35 \cdot 10^{+142}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;y \cdot z\\
\end{array}
\end{array}
if z < -1.55000000000000001e269 or -2.1500000000000002e242 < z < -1.6500000000000001e175 or -6.1999999999999998e79 < z < -3.00000000000000029e-20 or 1.34999999999999991e142 < z Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in97.2%
Applied egg-rr97.2%
associate-+r+97.2%
distribute-rgt-neg-out97.2%
unsub-neg97.2%
+-commutative97.2%
Applied egg-rr97.2%
Taylor expanded in y around inf 70.9%
*-commutative70.9%
Simplified70.9%
if -1.55000000000000001e269 < z < -2.1500000000000002e242 or -1.6500000000000001e175 < z < -6.1999999999999998e79 or 1 < z < 1.34999999999999991e142Initial program 100.0%
Taylor expanded in x around inf 65.5%
mul-1-neg65.5%
unsub-neg65.5%
Simplified65.5%
sub-neg65.5%
distribute-rgt-in65.5%
*-un-lft-identity65.5%
distribute-lft-neg-in65.5%
unsub-neg65.5%
Applied egg-rr65.5%
Taylor expanded in z around inf 64.0%
mul-1-neg64.0%
distribute-rgt-neg-out64.0%
Simplified64.0%
if -3.00000000000000029e-20 < z < 1Initial program 100.0%
Taylor expanded in z around 0 70.6%
Final simplification68.8%
(FPCore (x y z)
:precision binary64
(if (or (<= y -1.28e+109)
(not
(or (<= y -5.4e+74) (and (not (<= y -1.8e+24)) (<= y 1.02e-20)))))
(* y z)
(* x (- 1.0 z))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -1.28e+109) || !((y <= -5.4e+74) || (!(y <= -1.8e+24) && (y <= 1.02e-20)))) {
tmp = y * z;
} else {
tmp = x * (1.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 ((y <= (-1.28d+109)) .or. (.not. (y <= (-5.4d+74)) .or. (.not. (y <= (-1.8d+24))) .and. (y <= 1.02d-20))) then
tmp = y * z
else
tmp = x * (1.0d0 - z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -1.28e+109) || !((y <= -5.4e+74) || (!(y <= -1.8e+24) && (y <= 1.02e-20)))) {
tmp = y * z;
} else {
tmp = x * (1.0 - z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -1.28e+109) or not ((y <= -5.4e+74) or (not (y <= -1.8e+24) and (y <= 1.02e-20))): tmp = y * z else: tmp = x * (1.0 - z) return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -1.28e+109) || !((y <= -5.4e+74) || (!(y <= -1.8e+24) && (y <= 1.02e-20)))) tmp = Float64(y * z); else tmp = Float64(x * Float64(1.0 - z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -1.28e+109) || ~(((y <= -5.4e+74) || (~((y <= -1.8e+24)) && (y <= 1.02e-20))))) tmp = y * z; else tmp = x * (1.0 - z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -1.28e+109], N[Not[Or[LessEqual[y, -5.4e+74], And[N[Not[LessEqual[y, -1.8e+24]], $MachinePrecision], LessEqual[y, 1.02e-20]]]], $MachinePrecision]], N[(y * z), $MachinePrecision], N[(x * N[(1.0 - z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.28 \cdot 10^{+109} \lor \neg \left(y \leq -5.4 \cdot 10^{+74} \lor \neg \left(y \leq -1.8 \cdot 10^{+24}\right) \land y \leq 1.02 \cdot 10^{-20}\right):\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\end{array}
\end{array}
if y < -1.28e109 or -5.3999999999999996e74 < y < -1.79999999999999992e24 or 1.02000000000000001e-20 < y Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in97.2%
Applied egg-rr97.2%
associate-+r+97.2%
distribute-rgt-neg-out97.2%
unsub-neg97.2%
+-commutative97.2%
Applied egg-rr97.2%
Taylor expanded in y around inf 76.4%
*-commutative76.4%
Simplified76.4%
if -1.28e109 < y < -5.3999999999999996e74 or -1.79999999999999992e24 < y < 1.02000000000000001e-20Initial program 100.0%
Taylor expanded in x around inf 83.2%
mul-1-neg83.2%
unsub-neg83.2%
Simplified83.2%
Final simplification80.4%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.5e-108) (not (<= z 62.0))) (* (- y x) z) (* x (- 1.0 z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.5e-108) || !(z <= 62.0)) {
tmp = (y - x) * z;
} else {
tmp = x * (1.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 ((z <= (-1.5d-108)) .or. (.not. (z <= 62.0d0))) then
tmp = (y - x) * z
else
tmp = x * (1.0d0 - z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -1.5e-108) || !(z <= 62.0)) {
tmp = (y - x) * z;
} else {
tmp = x * (1.0 - z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -1.5e-108) or not (z <= 62.0): tmp = (y - x) * z else: tmp = x * (1.0 - z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1.5e-108) || !(z <= 62.0)) tmp = Float64(Float64(y - x) * z); else tmp = Float64(x * Float64(1.0 - z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -1.5e-108) || ~((z <= 62.0))) tmp = (y - x) * z; else tmp = x * (1.0 - z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -1.5e-108], N[Not[LessEqual[z, 62.0]], $MachinePrecision]], N[(N[(y - x), $MachinePrecision] * z), $MachinePrecision], N[(x * N[(1.0 - z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.5 \cdot 10^{-108} \lor \neg \left(z \leq 62\right):\\
\;\;\;\;\left(y - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\end{array}
\end{array}
if z < -1.49999999999999996e-108 or 62 < z Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in98.1%
Applied egg-rr98.1%
associate-+r+98.1%
distribute-rgt-neg-out98.1%
unsub-neg98.1%
+-commutative98.1%
Applied egg-rr98.1%
Taylor expanded in z around inf 92.3%
if -1.49999999999999996e-108 < z < 62Initial program 100.0%
Taylor expanded in x around inf 75.9%
mul-1-neg75.9%
unsub-neg75.9%
Simplified75.9%
Final simplification86.2%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.5e+24) (not (<= z 1.0))) (* (- y x) z) (+ x (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.5e+24) || !(z <= 1.0)) {
tmp = (y - x) * z;
} else {
tmp = x + (y * 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 ((z <= (-1.5d+24)) .or. (.not. (z <= 1.0d0))) then
tmp = (y - x) * z
else
tmp = x + (y * z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -1.5e+24) || !(z <= 1.0)) {
tmp = (y - x) * z;
} else {
tmp = x + (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -1.5e+24) or not (z <= 1.0): tmp = (y - x) * z else: tmp = x + (y * z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1.5e+24) || !(z <= 1.0)) tmp = Float64(Float64(y - x) * z); else tmp = Float64(x + Float64(y * z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -1.5e+24) || ~((z <= 1.0))) tmp = (y - x) * z; else tmp = x + (y * z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -1.5e+24], N[Not[LessEqual[z, 1.0]], $MachinePrecision]], N[(N[(y - x), $MachinePrecision] * z), $MachinePrecision], N[(x + N[(y * z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.5 \cdot 10^{+24} \lor \neg \left(z \leq 1\right):\\
\;\;\;\;\left(y - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;x + y \cdot z\\
\end{array}
\end{array}
if z < -1.49999999999999997e24 or 1 < z Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in97.7%
Applied egg-rr97.7%
associate-+r+97.7%
distribute-rgt-neg-out97.7%
unsub-neg97.7%
+-commutative97.7%
Applied egg-rr97.7%
Taylor expanded in z around inf 99.2%
if -1.49999999999999997e24 < z < 1Initial program 100.0%
Taylor expanded in y around inf 97.8%
*-commutative97.8%
Simplified97.8%
Final simplification98.5%
(FPCore (x y z) :precision binary64 (if (or (<= z -6e-20) (not (<= z 1e-37))) (* y z) x))
double code(double x, double y, double z) {
double tmp;
if ((z <= -6e-20) || !(z <= 1e-37)) {
tmp = y * z;
} else {
tmp = 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 <= (-6d-20)) .or. (.not. (z <= 1d-37))) then
tmp = y * z
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -6e-20) || !(z <= 1e-37)) {
tmp = y * z;
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -6e-20) or not (z <= 1e-37): tmp = y * z else: tmp = x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -6e-20) || !(z <= 1e-37)) tmp = Float64(y * z); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -6e-20) || ~((z <= 1e-37))) tmp = y * z; else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -6e-20], N[Not[LessEqual[z, 1e-37]], $MachinePrecision]], N[(y * z), $MachinePrecision], x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -6 \cdot 10^{-20} \lor \neg \left(z \leq 10^{-37}\right):\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if z < -6.00000000000000057e-20 or 1.00000000000000007e-37 < z Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in97.9%
Applied egg-rr97.9%
associate-+r+97.9%
distribute-rgt-neg-out97.9%
unsub-neg97.9%
+-commutative97.9%
Applied egg-rr97.9%
Taylor expanded in y around inf 53.3%
*-commutative53.3%
Simplified53.3%
if -6.00000000000000057e-20 < z < 1.00000000000000007e-37Initial program 100.0%
Taylor expanded in z around 0 73.0%
Final simplification61.7%
(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}
Initial program 100.0%
Final simplification100.0%
(FPCore (x y z) :precision binary64 x)
double code(double x, double y, double z) {
return x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = x
end function
public static double code(double x, double y, double z) {
return x;
}
def code(x, y, z): return x
function code(x, y, z) return x end
function tmp = code(x, y, z) tmp = x; end
code[x_, y_, z_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 100.0%
Taylor expanded in z around 0 33.7%
Final simplification33.7%
herbie shell --seed 2024054
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))