
(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.35e+118)
(* y z)
(if (<= z -9.5e+74)
t_0
(if (<= z -2.7e-20)
(* y z)
(if (<= z 4.3e-41) x (if (<= z 1500000000000.0) (* y z) t_0)))))))
double code(double x, double y, double z) {
double t_0 = x * -z;
double tmp;
if (z <= -1.35e+118) {
tmp = y * z;
} else if (z <= -9.5e+74) {
tmp = t_0;
} else if (z <= -2.7e-20) {
tmp = y * z;
} else if (z <= 4.3e-41) {
tmp = x;
} else if (z <= 1500000000000.0) {
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.35d+118)) then
tmp = y * z
else if (z <= (-9.5d+74)) then
tmp = t_0
else if (z <= (-2.7d-20)) then
tmp = y * z
else if (z <= 4.3d-41) then
tmp = x
else if (z <= 1500000000000.0d0) 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.35e+118) {
tmp = y * z;
} else if (z <= -9.5e+74) {
tmp = t_0;
} else if (z <= -2.7e-20) {
tmp = y * z;
} else if (z <= 4.3e-41) {
tmp = x;
} else if (z <= 1500000000000.0) {
tmp = y * z;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = x * -z tmp = 0 if z <= -1.35e+118: tmp = y * z elif z <= -9.5e+74: tmp = t_0 elif z <= -2.7e-20: tmp = y * z elif z <= 4.3e-41: tmp = x elif z <= 1500000000000.0: 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.35e+118) tmp = Float64(y * z); elseif (z <= -9.5e+74) tmp = t_0; elseif (z <= -2.7e-20) tmp = Float64(y * z); elseif (z <= 4.3e-41) tmp = x; elseif (z <= 1500000000000.0) 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.35e+118) tmp = y * z; elseif (z <= -9.5e+74) tmp = t_0; elseif (z <= -2.7e-20) tmp = y * z; elseif (z <= 4.3e-41) tmp = x; elseif (z <= 1500000000000.0) 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.35e+118], N[(y * z), $MachinePrecision], If[LessEqual[z, -9.5e+74], t$95$0, If[LessEqual[z, -2.7e-20], N[(y * z), $MachinePrecision], If[LessEqual[z, 4.3e-41], x, If[LessEqual[z, 1500000000000.0], 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.35 \cdot 10^{+118}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq -9.5 \cdot 10^{+74}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq -2.7 \cdot 10^{-20}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq 4.3 \cdot 10^{-41}:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq 1500000000000:\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < -1.35e118 or -9.5000000000000006e74 < z < -2.7e-20 or 4.2999999999999999e-41 < z < 1.5e12Initial program 100.0%
Taylor expanded in x around 0 64.0%
if -1.35e118 < z < -9.5000000000000006e74 or 1.5e12 < z Initial program 100.0%
Taylor expanded in x around inf 62.6%
mul-1-neg62.6%
unsub-neg62.6%
Simplified62.6%
Taylor expanded in z around inf 62.3%
mul-1-neg62.3%
distribute-lft-neg-out62.3%
*-commutative62.3%
Simplified62.3%
if -2.7e-20 < z < 4.2999999999999999e-41Initial program 100.0%
Taylor expanded in z around 0 83.1%
Final simplification72.7%
(FPCore (x y z) :precision binary64 (if (or (<= x -2.35e-123) (not (<= x 1.65e-48))) (* x (- 1.0 z)) (* y z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -2.35e-123) || !(x <= 1.65e-48)) {
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 <= (-2.35d-123)) .or. (.not. (x <= 1.65d-48))) 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 <= -2.35e-123) || !(x <= 1.65e-48)) {
tmp = x * (1.0 - z);
} else {
tmp = y * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -2.35e-123) or not (x <= 1.65e-48): tmp = x * (1.0 - z) else: tmp = y * z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -2.35e-123) || !(x <= 1.65e-48)) 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 <= -2.35e-123) || ~((x <= 1.65e-48))) tmp = x * (1.0 - z); else tmp = y * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -2.35e-123], N[Not[LessEqual[x, 1.65e-48]], $MachinePrecision]], N[(x * N[(1.0 - z), $MachinePrecision]), $MachinePrecision], N[(y * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.35 \cdot 10^{-123} \lor \neg \left(x \leq 1.65 \cdot 10^{-48}\right):\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot z\\
\end{array}
\end{array}
if x < -2.3500000000000001e-123 or 1.65e-48 < x Initial program 100.0%
Taylor expanded in x around inf 83.7%
mul-1-neg83.7%
unsub-neg83.7%
Simplified83.7%
if -2.3500000000000001e-123 < x < 1.65e-48Initial program 100.0%
Taylor expanded in x around 0 73.7%
Final simplification80.5%
(FPCore (x y z) :precision binary64 (if (or (<= z -18000000.0) (not (<= z 2.6e-43))) (* (- y x) z) (* x (- 1.0 z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -18000000.0) || !(z <= 2.6e-43)) {
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 <= (-18000000.0d0)) .or. (.not. (z <= 2.6d-43))) 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 <= -18000000.0) || !(z <= 2.6e-43)) {
tmp = (y - x) * z;
} else {
tmp = x * (1.0 - z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -18000000.0) or not (z <= 2.6e-43): tmp = (y - x) * z else: tmp = x * (1.0 - z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -18000000.0) || !(z <= 2.6e-43)) 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 <= -18000000.0) || ~((z <= 2.6e-43))) tmp = (y - x) * z; else tmp = x * (1.0 - z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -18000000.0], N[Not[LessEqual[z, 2.6e-43]], $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 -18000000 \lor \neg \left(z \leq 2.6 \cdot 10^{-43}\right):\\
\;\;\;\;\left(y - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\end{array}
\end{array}
if z < -1.8e7 or 2.6e-43 < z Initial program 100.0%
Taylor expanded in z around inf 98.4%
if -1.8e7 < z < 2.6e-43Initial program 100.0%
Taylor expanded in x around inf 82.3%
mul-1-neg82.3%
unsub-neg82.3%
Simplified82.3%
Final simplification90.5%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.0) (not (<= z 0.065))) (* (- y x) z) (+ x (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.0) || !(z <= 0.065)) {
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 <= 0.065d0))) 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 <= 0.065)) {
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 <= 0.065): tmp = (y - x) * z else: tmp = x + (y * z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1.0) || !(z <= 0.065)) 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 <= 0.065))) 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, 0.065]], $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 0.065\right):\\
\;\;\;\;\left(y - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;x + y \cdot z\\
\end{array}
\end{array}
if z < -1 or 0.065000000000000002 < z Initial program 100.0%
Taylor expanded in z around inf 99.5%
if -1 < z < 0.065000000000000002Initial program 100.0%
Taylor expanded in x around inf 97.0%
Taylor expanded in x around 0 97.0%
mul-1-neg97.0%
distribute-lft-neg-out97.0%
distribute-rgt-out97.0%
+-commutative97.0%
sub-neg97.0%
Simplified97.0%
Taylor expanded in y around inf 96.2%
associate-/l*96.1%
Simplified96.1%
Taylor expanded in x around 0 99.1%
+-commutative99.1%
Simplified99.1%
Final simplification99.3%
(FPCore (x y z) :precision binary64 (if (or (<= z -8e-21) (not (<= z 4e-42))) (* y z) x))
double code(double x, double y, double z) {
double tmp;
if ((z <= -8e-21) || !(z <= 4e-42)) {
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 <= (-8d-21)) .or. (.not. (z <= 4d-42))) 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 <= -8e-21) || !(z <= 4e-42)) {
tmp = y * z;
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -8e-21) or not (z <= 4e-42): tmp = y * z else: tmp = x return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -8e-21) || !(z <= 4e-42)) tmp = Float64(y * z); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -8e-21) || ~((z <= 4e-42))) tmp = y * z; else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -8e-21], N[Not[LessEqual[z, 4e-42]], $MachinePrecision]], N[(y * z), $MachinePrecision], x]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -8 \cdot 10^{-21} \lor \neg \left(z \leq 4 \cdot 10^{-42}\right):\\
\;\;\;\;y \cdot z\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if z < -7.99999999999999926e-21 or 4.00000000000000015e-42 < z Initial program 100.0%
Taylor expanded in x around 0 54.2%
if -7.99999999999999926e-21 < z < 4.00000000000000015e-42Initial program 100.0%
Taylor expanded in z around 0 83.1%
Final simplification67.9%
(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.0%
Final simplification42.0%
herbie shell --seed 2024115
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))