
(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.1e+71)
(* y z)
(if (<= z -2.1e+29)
t_0
(if (<= z -7.5e-17)
(* y z)
(if (<= z 3.2e-78) x (if (<= z 2.3e+126) (* y z) t_0)))))))
double code(double x, double y, double z) {
double t_0 = x * -z;
double tmp;
if (z <= -1.1e+71) {
tmp = y * z;
} else if (z <= -2.1e+29) {
tmp = t_0;
} else if (z <= -7.5e-17) {
tmp = y * z;
} else if (z <= 3.2e-78) {
tmp = x;
} else if (z <= 2.3e+126) {
tmp = y * z;
} 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 = x * -z
if (z <= (-1.1d+71)) then
tmp = y * z
else if (z <= (-2.1d+29)) then
tmp = t_0
else if (z <= (-7.5d-17)) then
tmp = y * z
else if (z <= 3.2d-78) then
tmp = x
else if (z <= 2.3d+126) then
tmp = y * z
else
tmp = t_0
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.1e+71) {
tmp = y * z;
} else if (z <= -2.1e+29) {
tmp = t_0;
} else if (z <= -7.5e-17) {
tmp = y * z;
} else if (z <= 3.2e-78) {
tmp = x;
} else if (z <= 2.3e+126) {
tmp = y * z;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = x * -z tmp = 0 if z <= -1.1e+71: tmp = y * z elif z <= -2.1e+29: tmp = t_0 elif z <= -7.5e-17: tmp = y * z elif z <= 3.2e-78: tmp = x elif z <= 2.3e+126: tmp = y * z else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(x * Float64(-z)) tmp = 0.0 if (z <= -1.1e+71) tmp = Float64(y * z); elseif (z <= -2.1e+29) tmp = t_0; elseif (z <= -7.5e-17) tmp = Float64(y * z); elseif (z <= 3.2e-78) tmp = x; elseif (z <= 2.3e+126) tmp = Float64(y * z); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = x * -z; tmp = 0.0; if (z <= -1.1e+71) tmp = y * z; elseif (z <= -2.1e+29) tmp = t_0; elseif (z <= -7.5e-17) tmp = y * z; elseif (z <= 3.2e-78) tmp = x; elseif (z <= 2.3e+126) tmp = y * z; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(x * (-z)), $MachinePrecision]}, If[LessEqual[z, -1.1e+71], N[(y * z), $MachinePrecision], If[LessEqual[z, -2.1e+29], t$95$0, If[LessEqual[z, -7.5e-17], N[(y * z), $MachinePrecision], If[LessEqual[z, 3.2e-78], x, If[LessEqual[z, 2.3e+126], N[(y * z), $MachinePrecision], t$95$0]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(-z\right)\\
\mathbf{if}\;z \leq -1.1 \cdot 10^{+71}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq -2.1 \cdot 10^{+29}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq -7.5 \cdot 10^{-17}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq 3.2 \cdot 10^{-78}:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq 2.3 \cdot 10^{+126}:\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < -1.09999999999999997e71 or -2.1000000000000002e29 < z < -7.49999999999999984e-17 or 3.2e-78 < z < 2.3000000000000001e126Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in95.7%
Applied egg-rr95.7%
Taylor expanded in x around -inf 95.7%
+-commutative95.7%
*-commutative95.7%
mul-1-neg95.7%
unsub-neg95.7%
sub-neg95.7%
metadata-eval95.7%
Simplified95.7%
Taylor expanded in y around inf 65.5%
*-commutative65.5%
Simplified65.5%
if -1.09999999999999997e71 < z < -2.1000000000000002e29 or 2.3000000000000001e126 < z Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in95.1%
Applied egg-rr95.1%
Taylor expanded in x around -inf 95.1%
+-commutative95.1%
*-commutative95.1%
mul-1-neg95.1%
unsub-neg95.1%
sub-neg95.1%
metadata-eval95.1%
Simplified95.1%
Taylor expanded in z around inf 100.0%
Taylor expanded in y around 0 66.6%
associate-*r*66.6%
neg-mul-166.6%
*-commutative66.6%
Simplified66.6%
if -7.49999999999999984e-17 < z < 3.2e-78Initial program 100.0%
Taylor expanded in z around 0 80.4%
Final simplification72.7%
(FPCore (x y z) :precision binary64 (if (or (<= x -4.5e-45) (not (<= x 1.8e-85))) (* x (- 1.0 z)) (* y z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -4.5e-45) || !(x <= 1.8e-85)) {
tmp = x * (1.0 - z);
} 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) :: tmp
if ((x <= (-4.5d-45)) .or. (.not. (x <= 1.8d-85))) then
tmp = x * (1.0d0 - z)
else
tmp = y * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -4.5e-45) || !(x <= 1.8e-85)) {
tmp = x * (1.0 - z);
} else {
tmp = y * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -4.5e-45) or not (x <= 1.8e-85): tmp = x * (1.0 - z) else: tmp = y * z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -4.5e-45) || !(x <= 1.8e-85)) tmp = Float64(x * Float64(1.0 - z)); else tmp = Float64(y * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -4.5e-45) || ~((x <= 1.8e-85))) tmp = x * (1.0 - z); else tmp = y * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -4.5e-45], N[Not[LessEqual[x, 1.8e-85]], $MachinePrecision]], N[(x * N[(1.0 - z), $MachinePrecision]), $MachinePrecision], N[(y * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4.5 \cdot 10^{-45} \lor \neg \left(x \leq 1.8 \cdot 10^{-85}\right):\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot z\\
\end{array}
\end{array}
if x < -4.4999999999999999e-45 or 1.7999999999999999e-85 < x Initial program 100.0%
Taylor expanded in x around inf 82.6%
mul-1-neg82.6%
unsub-neg82.6%
Simplified82.6%
if -4.4999999999999999e-45 < x < 1.7999999999999999e-85Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in100.0%
Applied egg-rr100.0%
Taylor expanded in x around -inf 100.0%
+-commutative100.0%
*-commutative100.0%
mul-1-neg100.0%
unsub-neg100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in y around inf 71.2%
*-commutative71.2%
Simplified71.2%
Final simplification78.3%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.02e-16) (not (<= z 1.12e-79))) (* (- y x) z) (* x (- 1.0 z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.02e-16) || !(z <= 1.12e-79)) {
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.02d-16)) .or. (.not. (z <= 1.12d-79))) 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.02e-16) || !(z <= 1.12e-79)) {
tmp = (y - x) * z;
} else {
tmp = x * (1.0 - z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -1.02e-16) or not (z <= 1.12e-79): tmp = (y - x) * z else: tmp = x * (1.0 - z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1.02e-16) || !(z <= 1.12e-79)) 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.02e-16) || ~((z <= 1.12e-79))) tmp = (y - x) * z; else tmp = x * (1.0 - z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -1.02e-16], N[Not[LessEqual[z, 1.12e-79]], $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.02 \cdot 10^{-16} \lor \neg \left(z \leq 1.12 \cdot 10^{-79}\right):\\
\;\;\;\;\left(y - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\end{array}
\end{array}
if z < -1.0200000000000001e-16 or 1.11999999999999996e-79 < z Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in95.6%
Applied egg-rr95.6%
Taylor expanded in x around -inf 95.5%
+-commutative95.5%
*-commutative95.5%
mul-1-neg95.5%
unsub-neg95.5%
sub-neg95.5%
metadata-eval95.5%
Simplified95.5%
Taylor expanded in z around inf 93.5%
if -1.0200000000000001e-16 < z < 1.11999999999999996e-79Initial program 100.0%
Taylor expanded in x around inf 80.4%
mul-1-neg80.4%
unsub-neg80.4%
Simplified80.4%
Final simplification87.3%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.0) (not (<= z 1.0))) (* (- y x) z) (+ x (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.0) || !(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.0d0)) .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.0) || !(z <= 1.0)) {
tmp = (y - x) * z;
} else {
tmp = x + (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -1.0) 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.0) || !(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.0) || ~((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.0], 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 \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 or 1 < z Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in94.7%
Applied egg-rr94.7%
Taylor expanded in x around -inf 94.7%
+-commutative94.7%
*-commutative94.7%
mul-1-neg94.7%
unsub-neg94.7%
sub-neg94.7%
metadata-eval94.7%
Simplified94.7%
Taylor expanded in z around inf 99.2%
if -1 < z < 1Initial program 100.0%
Taylor expanded in y around inf 99.3%
*-commutative99.3%
Simplified99.3%
Final simplification99.2%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.5e-16) (not (<= z 9.6e-80))) (* y z) x))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.5e-16) || !(z <= 9.6e-80)) {
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 <= (-1.5d-16)) .or. (.not. (z <= 9.6d-80))) 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 <= -1.5e-16) || !(z <= 9.6e-80)) {
tmp = y * z;
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -1.5e-16) or not (z <= 9.6e-80): tmp = y * z else: tmp = x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1.5e-16) || !(z <= 9.6e-80)) tmp = Float64(y * z); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -1.5e-16) || ~((z <= 9.6e-80))) tmp = y * z; else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -1.5e-16], N[Not[LessEqual[z, 9.6e-80]], $MachinePrecision]], N[(y * z), $MachinePrecision], x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.5 \cdot 10^{-16} \lor \neg \left(z \leq 9.6 \cdot 10^{-80}\right):\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if z < -1.49999999999999997e-16 or 9.5999999999999996e-80 < z Initial program 100.0%
*-commutative100.0%
sub-neg100.0%
distribute-lft-in95.6%
Applied egg-rr95.6%
Taylor expanded in x around -inf 95.5%
+-commutative95.5%
*-commutative95.5%
mul-1-neg95.5%
unsub-neg95.5%
sub-neg95.5%
metadata-eval95.5%
Simplified95.5%
Taylor expanded in y around inf 58.7%
*-commutative58.7%
Simplified58.7%
if -1.49999999999999997e-16 < z < 9.5999999999999996e-80Initial program 100.0%
Taylor expanded in z around 0 80.4%
Final simplification69.0%
(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 42.2%
Final simplification42.2%
herbie shell --seed 2024034
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))