
(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-def100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (* x (- z))))
(if (<= z -3.5e+104)
t_0
(if (<= z -1.1e-12)
(* y z)
(if (<= z 1.25e-21)
x
(if (or (<= z 8.5e+32) (not (<= z 3e+159))) (* y z) t_0))))))
double code(double x, double y, double z) {
double t_0 = x * -z;
double tmp;
if (z <= -3.5e+104) {
tmp = t_0;
} else if (z <= -1.1e-12) {
tmp = y * z;
} else if (z <= 1.25e-21) {
tmp = x;
} else if ((z <= 8.5e+32) || !(z <= 3e+159)) {
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.5d+104)) then
tmp = t_0
else if (z <= (-1.1d-12)) then
tmp = y * z
else if (z <= 1.25d-21) then
tmp = x
else if ((z <= 8.5d+32) .or. (.not. (z <= 3d+159))) 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.5e+104) {
tmp = t_0;
} else if (z <= -1.1e-12) {
tmp = y * z;
} else if (z <= 1.25e-21) {
tmp = x;
} else if ((z <= 8.5e+32) || !(z <= 3e+159)) {
tmp = y * z;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = x * -z tmp = 0 if z <= -3.5e+104: tmp = t_0 elif z <= -1.1e-12: tmp = y * z elif z <= 1.25e-21: tmp = x elif (z <= 8.5e+32) or not (z <= 3e+159): 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.5e+104) tmp = t_0; elseif (z <= -1.1e-12) tmp = Float64(y * z); elseif (z <= 1.25e-21) tmp = x; elseif ((z <= 8.5e+32) || !(z <= 3e+159)) 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.5e+104) tmp = t_0; elseif (z <= -1.1e-12) tmp = y * z; elseif (z <= 1.25e-21) tmp = x; elseif ((z <= 8.5e+32) || ~((z <= 3e+159))) 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.5e+104], t$95$0, If[LessEqual[z, -1.1e-12], N[(y * z), $MachinePrecision], If[LessEqual[z, 1.25e-21], x, If[Or[LessEqual[z, 8.5e+32], N[Not[LessEqual[z, 3e+159]], $MachinePrecision]], 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.5 \cdot 10^{+104}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;z \leq -1.1 \cdot 10^{-12}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq 1.25 \cdot 10^{-21}:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq 8.5 \cdot 10^{+32} \lor \neg \left(z \leq 3 \cdot 10^{+159}\right):\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if z < -3.5000000000000002e104 or 8.4999999999999998e32 < z < 3.0000000000000002e159Initial program 100.0%
Taylor expanded in x around 0 94.3%
fma-def95.8%
mul-1-neg95.8%
*-commutative95.8%
Simplified95.8%
Taylor expanded in x around 0 94.3%
Taylor expanded in z around inf 100.0%
mul-1-neg100.0%
unsub-neg100.0%
Simplified100.0%
Taylor expanded in y around 0 62.0%
mul-1-neg62.0%
distribute-lft-neg-out62.0%
*-commutative62.0%
Simplified62.0%
if -3.5000000000000002e104 < z < -1.09999999999999996e-12 or 1.24999999999999993e-21 < z < 8.4999999999999998e32 or 3.0000000000000002e159 < z Initial program 100.0%
Taylor expanded in x around 0 96.7%
fma-def99.9%
mul-1-neg99.9%
*-commutative99.9%
Simplified99.9%
Taylor expanded in x around 0 96.7%
Taylor expanded in x around 0 65.4%
if -1.09999999999999996e-12 < z < 1.24999999999999993e-21Initial program 100.0%
Taylor expanded in z around 0 75.4%
Final simplification69.3%
(FPCore (x y z) :precision binary64 (if (or (<= y -1.45e+16) (not (<= y 1.45e+52))) (* y z) (* x (- 1.0 z))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -1.45e+16) || !(y <= 1.45e+52)) {
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.45d+16)) .or. (.not. (y <= 1.45d+52))) 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.45e+16) || !(y <= 1.45e+52)) {
tmp = y * z;
} else {
tmp = x * (1.0 - z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -1.45e+16) or not (y <= 1.45e+52): tmp = y * z else: tmp = x * (1.0 - z) return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -1.45e+16) || !(y <= 1.45e+52)) 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.45e+16) || ~((y <= 1.45e+52))) tmp = y * z; else tmp = x * (1.0 - z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -1.45e+16], N[Not[LessEqual[y, 1.45e+52]], $MachinePrecision]], N[(y * z), $MachinePrecision], N[(x * N[(1.0 - z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.45 \cdot 10^{+16} \lor \neg \left(y \leq 1.45 \cdot 10^{+52}\right):\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\end{array}
\end{array}
if y < -1.45e16 or 1.45e52 < y Initial program 100.0%
Taylor expanded in x around 0 94.9%
fma-def97.9%
mul-1-neg97.9%
*-commutative97.9%
Simplified97.9%
Taylor expanded in x around 0 94.9%
Taylor expanded in x around 0 73.5%
if -1.45e16 < y < 1.45e52Initial program 100.0%
Taylor expanded in x around inf 82.3%
mul-1-neg82.3%
unsub-neg82.3%
Simplified82.3%
Final simplification79.0%
(FPCore (x y z) :precision binary64 (if (or (<= z -2.3e-5) (not (<= z 2.3e-21))) (* (- y x) z) (* x (- 1.0 z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -2.3e-5) || !(z <= 2.3e-21)) {
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 <= (-2.3d-5)) .or. (.not. (z <= 2.3d-21))) 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 <= -2.3e-5) || !(z <= 2.3e-21)) {
tmp = (y - x) * z;
} else {
tmp = x * (1.0 - z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -2.3e-5) or not (z <= 2.3e-21): tmp = (y - x) * z else: tmp = x * (1.0 - z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -2.3e-5) || !(z <= 2.3e-21)) 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 <= -2.3e-5) || ~((z <= 2.3e-21))) tmp = (y - x) * z; else tmp = x * (1.0 - z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -2.3e-5], N[Not[LessEqual[z, 2.3e-21]], $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 -2.3 \cdot 10^{-5} \lor \neg \left(z \leq 2.3 \cdot 10^{-21}\right):\\
\;\;\;\;\left(y - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\end{array}
\end{array}
if z < -2.3e-5 or 2.29999999999999999e-21 < z Initial program 100.0%
Taylor expanded in x around 0 95.5%
fma-def97.7%
mul-1-neg97.7%
*-commutative97.7%
Simplified97.7%
Taylor expanded in x around 0 95.5%
Taylor expanded in z around inf 98.7%
mul-1-neg98.7%
unsub-neg98.7%
Simplified98.7%
if -2.3e-5 < z < 2.29999999999999999e-21Initial program 100.0%
Taylor expanded in x around inf 75.5%
mul-1-neg75.5%
unsub-neg75.5%
Simplified75.5%
Final simplification87.6%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.0) (not (<= z 4.6e-6))) (* (- y x) z) (+ x (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.0) || !(z <= 4.6e-6)) {
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 <= 4.6d-6))) 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 <= 4.6e-6)) {
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 <= 4.6e-6): tmp = (y - x) * z else: tmp = x + (y * z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1.0) || !(z <= 4.6e-6)) 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 <= 4.6e-6))) 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, 4.6e-6]], $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 4.6 \cdot 10^{-6}\right):\\
\;\;\;\;\left(y - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;x + y \cdot z\\
\end{array}
\end{array}
if z < -1 or 4.6e-6 < z Initial program 100.0%
Taylor expanded in x around 0 95.2%
fma-def97.6%
mul-1-neg97.6%
*-commutative97.6%
Simplified97.6%
Taylor expanded in x around 0 95.2%
Taylor expanded in z around inf 99.4%
mul-1-neg99.4%
unsub-neg99.4%
Simplified99.4%
if -1 < z < 4.6e-6Initial program 100.0%
Taylor expanded in y around inf 99.5%
*-commutative99.5%
Simplified99.5%
Final simplification99.4%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.8e-12) (not (<= z 3.8e-21))) (* y z) x))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.8e-12) || !(z <= 3.8e-21)) {
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.8d-12)) .or. (.not. (z <= 3.8d-21))) 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.8e-12) || !(z <= 3.8e-21)) {
tmp = y * z;
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -1.8e-12) or not (z <= 3.8e-21): tmp = y * z else: tmp = x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1.8e-12) || !(z <= 3.8e-21)) tmp = Float64(y * z); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -1.8e-12) || ~((z <= 3.8e-21))) tmp = y * z; else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -1.8e-12], N[Not[LessEqual[z, 3.8e-21]], $MachinePrecision]], N[(y * z), $MachinePrecision], x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.8 \cdot 10^{-12} \lor \neg \left(z \leq 3.8 \cdot 10^{-21}\right):\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if z < -1.8e-12 or 3.7999999999999998e-21 < z Initial program 100.0%
Taylor expanded in x around 0 95.5%
fma-def97.7%
mul-1-neg97.7%
*-commutative97.7%
Simplified97.7%
Taylor expanded in x around 0 95.5%
Taylor expanded in x around 0 52.2%
if -1.8e-12 < z < 3.7999999999999998e-21Initial program 100.0%
Taylor expanded in z around 0 75.4%
Final simplification63.3%
(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 38.0%
Final simplification38.0%
herbie shell --seed 2023322
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))