
(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 -3.2e+242)
(* y z)
(if (<= z -1.75e+130)
t_0
(if (<= z -3.5e+70)
(* y z)
(if (<= z -3700.0)
t_0
(if (<= z 0.065) x (if (<= z 2.7e+219) (* y z) t_0))))))))
double code(double x, double y, double z) {
double t_0 = x * -z;
double tmp;
if (z <= -3.2e+242) {
tmp = y * z;
} else if (z <= -1.75e+130) {
tmp = t_0;
} else if (z <= -3.5e+70) {
tmp = y * z;
} else if (z <= -3700.0) {
tmp = t_0;
} else if (z <= 0.065) {
tmp = x;
} else if (z <= 2.7e+219) {
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 <= (-3.2d+242)) then
tmp = y * z
else if (z <= (-1.75d+130)) then
tmp = t_0
else if (z <= (-3.5d+70)) then
tmp = y * z
else if (z <= (-3700.0d0)) then
tmp = t_0
else if (z <= 0.065d0) then
tmp = x
else if (z <= 2.7d+219) 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 <= -3.2e+242) {
tmp = y * z;
} else if (z <= -1.75e+130) {
tmp = t_0;
} else if (z <= -3.5e+70) {
tmp = y * z;
} else if (z <= -3700.0) {
tmp = t_0;
} else if (z <= 0.065) {
tmp = x;
} else if (z <= 2.7e+219) {
tmp = y * z;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = x * -z tmp = 0 if z <= -3.2e+242: tmp = y * z elif z <= -1.75e+130: tmp = t_0 elif z <= -3.5e+70: tmp = y * z elif z <= -3700.0: tmp = t_0 elif z <= 0.065: tmp = x elif z <= 2.7e+219: 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 <= -3.2e+242) tmp = Float64(y * z); elseif (z <= -1.75e+130) tmp = t_0; elseif (z <= -3.5e+70) tmp = Float64(y * z); elseif (z <= -3700.0) tmp = t_0; elseif (z <= 0.065) tmp = x; elseif (z <= 2.7e+219) 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 <= -3.2e+242) tmp = y * z; elseif (z <= -1.75e+130) tmp = t_0; elseif (z <= -3.5e+70) tmp = y * z; elseif (z <= -3700.0) tmp = t_0; elseif (z <= 0.065) tmp = x; elseif (z <= 2.7e+219) 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, -3.2e+242], N[(y * z), $MachinePrecision], If[LessEqual[z, -1.75e+130], t$95$0, If[LessEqual[z, -3.5e+70], N[(y * z), $MachinePrecision], If[LessEqual[z, -3700.0], t$95$0, If[LessEqual[z, 0.065], x, If[LessEqual[z, 2.7e+219], N[(y * z), $MachinePrecision], t$95$0]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(-z\right)\\
\mathbf{if}\;z \leq -3.2 \cdot 10^{+242}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq -1.75 \cdot 10^{+130}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq -3.5 \cdot 10^{+70}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq -3700:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 0.065:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq 2.7 \cdot 10^{+219}:\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < -3.2000000000000002e242 or -1.75e130 < z < -3.50000000000000002e70 or 0.065000000000000002 < z < 2.6999999999999999e219Initial program 100.0%
Taylor expanded in x around 0 96.2%
fma-define98.7%
mul-1-neg98.7%
*-commutative98.7%
Simplified98.7%
Taylor expanded in x around 0 64.6%
if -3.2000000000000002e242 < z < -1.75e130 or -3.50000000000000002e70 < z < -3700 or 2.6999999999999999e219 < z Initial program 100.0%
Taylor expanded in x around inf 68.6%
mul-1-neg68.6%
unsub-neg68.6%
Simplified68.6%
Taylor expanded in z around inf 65.6%
associate-*r*65.6%
mul-1-neg65.6%
Simplified65.6%
if -3700 < z < 0.065000000000000002Initial program 100.0%
Taylor expanded in z around 0 79.8%
Final simplification71.7%
(FPCore (x y z) :precision binary64 (if (or (<= x -9.5e-40) (not (<= x 1.6e-52))) (* x (- 1.0 z)) (* y z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -9.5e-40) || !(x <= 1.6e-52)) {
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 <= (-9.5d-40)) .or. (.not. (x <= 1.6d-52))) 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 <= -9.5e-40) || !(x <= 1.6e-52)) {
tmp = x * (1.0 - z);
} else {
tmp = y * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -9.5e-40) or not (x <= 1.6e-52): tmp = x * (1.0 - z) else: tmp = y * z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -9.5e-40) || !(x <= 1.6e-52)) 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 <= -9.5e-40) || ~((x <= 1.6e-52))) tmp = x * (1.0 - z); else tmp = y * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -9.5e-40], N[Not[LessEqual[x, 1.6e-52]], $MachinePrecision]], N[(x * N[(1.0 - z), $MachinePrecision]), $MachinePrecision], N[(y * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.5 \cdot 10^{-40} \lor \neg \left(x \leq 1.6 \cdot 10^{-52}\right):\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot z\\
\end{array}
\end{array}
if x < -9.5000000000000006e-40 or 1.60000000000000005e-52 < x Initial program 100.0%
Taylor expanded in x around inf 87.1%
mul-1-neg87.1%
unsub-neg87.1%
Simplified87.1%
if -9.5000000000000006e-40 < x < 1.60000000000000005e-52Initial program 100.0%
Taylor expanded in x around 0 99.9%
fma-define99.9%
mul-1-neg99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in x around 0 63.4%
Final simplification77.0%
(FPCore (x y z) :precision binary64 (if (or (<= z -6e-12) (not (<= z 70.0))) (* (- y x) z) (* x (- 1.0 z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -6e-12) || !(z <= 70.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 <= (-6d-12)) .or. (.not. (z <= 70.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 <= -6e-12) || !(z <= 70.0)) {
tmp = (y - x) * z;
} else {
tmp = x * (1.0 - z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -6e-12) or not (z <= 70.0): tmp = (y - x) * z else: tmp = x * (1.0 - z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -6e-12) || !(z <= 70.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 <= -6e-12) || ~((z <= 70.0))) tmp = (y - x) * z; else tmp = x * (1.0 - z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -6e-12], N[Not[LessEqual[z, 70.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 -6 \cdot 10^{-12} \lor \neg \left(z \leq 70\right):\\
\;\;\;\;\left(y - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\end{array}
\end{array}
if z < -6.0000000000000003e-12 or 70 < z Initial program 100.0%
Taylor expanded in x around 0 96.4%
fma-define97.9%
mul-1-neg97.9%
*-commutative97.9%
Simplified97.9%
Taylor expanded in z around inf 98.2%
mul-1-neg98.2%
unsub-neg98.2%
Simplified98.2%
if -6.0000000000000003e-12 < z < 70Initial program 100.0%
Taylor expanded in x around inf 82.6%
mul-1-neg82.6%
unsub-neg82.6%
Simplified82.6%
Final simplification91.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%
Taylor expanded in x around 0 96.4%
fma-define97.9%
mul-1-neg97.9%
*-commutative97.9%
Simplified97.9%
Taylor expanded in z around inf 98.2%
mul-1-neg98.2%
unsub-neg98.2%
Simplified98.2%
if -1 < z < 1Initial program 100.0%
Taylor expanded in y around inf 98.5%
*-commutative98.5%
Simplified98.5%
Final simplification98.3%
(FPCore (x y z) :precision binary64 (if (or (<= z -7e-11) (not (<= z 0.065))) (* y z) x))
double code(double x, double y, double z) {
double tmp;
if ((z <= -7e-11) || !(z <= 0.065)) {
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 <= (-7d-11)) .or. (.not. (z <= 0.065d0))) 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 <= -7e-11) || !(z <= 0.065)) {
tmp = y * z;
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -7e-11) or not (z <= 0.065): tmp = y * z else: tmp = x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -7e-11) || !(z <= 0.065)) tmp = Float64(y * z); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -7e-11) || ~((z <= 0.065))) tmp = y * z; else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -7e-11], N[Not[LessEqual[z, 0.065]], $MachinePrecision]], N[(y * z), $MachinePrecision], x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -7 \cdot 10^{-11} \lor \neg \left(z \leq 0.065\right):\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if z < -7.00000000000000038e-11 or 0.065000000000000002 < z Initial program 100.0%
Taylor expanded in x around 0 96.4%
fma-define97.9%
mul-1-neg97.9%
*-commutative97.9%
Simplified97.9%
Taylor expanded in x around 0 52.4%
if -7.00000000000000038e-11 < z < 0.065000000000000002Initial program 100.0%
Taylor expanded in z around 0 81.2%
Final simplification65.2%
(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 37.9%
Final simplification37.9%
herbie shell --seed 2024040
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))