
(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 -7e+156)
t_0
(if (<= z -4.2e-10)
(* y z)
(if (<= z 7.2e-38)
x
(if (<= z 2.45e+44) (* y z) (if (<= z 6e+280) t_0 (* y z))))))))
double code(double x, double y, double z) {
double t_0 = x * -z;
double tmp;
if (z <= -7e+156) {
tmp = t_0;
} else if (z <= -4.2e-10) {
tmp = y * z;
} else if (z <= 7.2e-38) {
tmp = x;
} else if (z <= 2.45e+44) {
tmp = y * z;
} else if (z <= 6e+280) {
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 <= (-7d+156)) then
tmp = t_0
else if (z <= (-4.2d-10)) then
tmp = y * z
else if (z <= 7.2d-38) then
tmp = x
else if (z <= 2.45d+44) then
tmp = y * z
else if (z <= 6d+280) 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 <= -7e+156) {
tmp = t_0;
} else if (z <= -4.2e-10) {
tmp = y * z;
} else if (z <= 7.2e-38) {
tmp = x;
} else if (z <= 2.45e+44) {
tmp = y * z;
} else if (z <= 6e+280) {
tmp = t_0;
} else {
tmp = y * z;
}
return tmp;
}
def code(x, y, z): t_0 = x * -z tmp = 0 if z <= -7e+156: tmp = t_0 elif z <= -4.2e-10: tmp = y * z elif z <= 7.2e-38: tmp = x elif z <= 2.45e+44: tmp = y * z elif z <= 6e+280: 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 <= -7e+156) tmp = t_0; elseif (z <= -4.2e-10) tmp = Float64(y * z); elseif (z <= 7.2e-38) tmp = x; elseif (z <= 2.45e+44) tmp = Float64(y * z); elseif (z <= 6e+280) 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 <= -7e+156) tmp = t_0; elseif (z <= -4.2e-10) tmp = y * z; elseif (z <= 7.2e-38) tmp = x; elseif (z <= 2.45e+44) tmp = y * z; elseif (z <= 6e+280) 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, -7e+156], t$95$0, If[LessEqual[z, -4.2e-10], N[(y * z), $MachinePrecision], If[LessEqual[z, 7.2e-38], x, If[LessEqual[z, 2.45e+44], N[(y * z), $MachinePrecision], If[LessEqual[z, 6e+280], t$95$0, N[(y * z), $MachinePrecision]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x \cdot \left(-z\right)\\
\mathbf{if}\;z \leq -7 \cdot 10^{+156}:\\
\;\;\;\;t_0\\
\mathbf{elif}\;z \leq -4.2 \cdot 10^{-10}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq 7.2 \cdot 10^{-38}:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq 2.45 \cdot 10^{+44}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq 6 \cdot 10^{+280}:\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;y \cdot z\\
\end{array}
\end{array}
if z < -7.0000000000000006e156 or 2.45000000000000018e44 < z < 6.0000000000000002e280Initial program 100.0%
Taylor expanded in x around inf 64.8%
Taylor expanded in z around inf 64.8%
associate-*r*64.8%
mul-1-neg64.8%
Simplified64.8%
if -7.0000000000000006e156 < z < -4.2e-10 or 7.2000000000000001e-38 < z < 2.45000000000000018e44 or 6.0000000000000002e280 < z Initial program 100.0%
Taylor expanded in x around 0 62.1%
if -4.2e-10 < z < 7.2000000000000001e-38Initial program 100.0%
Taylor expanded in z around 0 72.0%
Final simplification67.7%
(FPCore (x y z)
:precision binary64
(if (or (<= x -2.5e-49)
(not
(or (<= x -3.8e-187) (and (not (<= x -7e-240)) (<= x 3.2e-112)))))
(* x (- 1.0 z))
(* y z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -2.5e-49) || !((x <= -3.8e-187) || (!(x <= -7e-240) && (x <= 3.2e-112)))) {
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.5d-49)) .or. (.not. (x <= (-3.8d-187)) .or. (.not. (x <= (-7d-240))) .and. (x <= 3.2d-112))) 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.5e-49) || !((x <= -3.8e-187) || (!(x <= -7e-240) && (x <= 3.2e-112)))) {
tmp = x * (1.0 - z);
} else {
tmp = y * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -2.5e-49) or not ((x <= -3.8e-187) or (not (x <= -7e-240) and (x <= 3.2e-112))): tmp = x * (1.0 - z) else: tmp = y * z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -2.5e-49) || !((x <= -3.8e-187) || (!(x <= -7e-240) && (x <= 3.2e-112)))) 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.5e-49) || ~(((x <= -3.8e-187) || (~((x <= -7e-240)) && (x <= 3.2e-112))))) tmp = x * (1.0 - z); else tmp = y * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -2.5e-49], N[Not[Or[LessEqual[x, -3.8e-187], And[N[Not[LessEqual[x, -7e-240]], $MachinePrecision], LessEqual[x, 3.2e-112]]]], $MachinePrecision]], N[(x * N[(1.0 - z), $MachinePrecision]), $MachinePrecision], N[(y * z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -2.5 \cdot 10^{-49} \lor \neg \left(x \leq -3.8 \cdot 10^{-187} \lor \neg \left(x \leq -7 \cdot 10^{-240}\right) \land x \leq 3.2 \cdot 10^{-112}\right):\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\mathbf{else}:\\
\;\;\;\;y \cdot z\\
\end{array}
\end{array}
if x < -2.4999999999999999e-49 or -3.80000000000000025e-187 < x < -7.00000000000000032e-240 or 3.19999999999999993e-112 < x Initial program 100.0%
Taylor expanded in x around inf 80.4%
distribute-rgt1-in80.4%
mul-1-neg80.4%
cancel-sign-sub-inv80.4%
Simplified80.4%
Taylor expanded in x around 0 80.4%
if -2.4999999999999999e-49 < x < -3.80000000000000025e-187 or -7.00000000000000032e-240 < x < 3.19999999999999993e-112Initial program 100.0%
Taylor expanded in x around 0 78.2%
Final simplification79.7%
(FPCore (x y z) :precision binary64 (if (or (<= z -5e-8) (not (<= z 13.5))) (* (- y x) z) (* x (- 1.0 z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -5e-8) || !(z <= 13.5)) {
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 <= (-5d-8)) .or. (.not. (z <= 13.5d0))) 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 <= -5e-8) || !(z <= 13.5)) {
tmp = (y - x) * z;
} else {
tmp = x * (1.0 - z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -5e-8) or not (z <= 13.5): tmp = (y - x) * z else: tmp = x * (1.0 - z) return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -5e-8) || !(z <= 13.5)) 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 <= -5e-8) || ~((z <= 13.5))) tmp = (y - x) * z; else tmp = x * (1.0 - z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -5e-8], N[Not[LessEqual[z, 13.5]], $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 -5 \cdot 10^{-8} \lor \neg \left(z \leq 13.5\right):\\
\;\;\;\;\left(y - x\right) \cdot z\\
\mathbf{else}:\\
\;\;\;\;x \cdot \left(1 - z\right)\\
\end{array}
\end{array}
if z < -4.9999999999999998e-8 or 13.5 < z Initial program 100.0%
Taylor expanded in z around inf 99.5%
if -4.9999999999999998e-8 < z < 13.5Initial program 100.0%
Taylor expanded in x around inf 70.8%
distribute-rgt1-in70.8%
mul-1-neg70.8%
cancel-sign-sub-inv70.8%
Simplified70.8%
Taylor expanded in x around 0 70.8%
Final simplification84.3%
(FPCore (x y z) :precision binary64 (if (or (<= z -6200000000.0) (not (<= z 1.0))) (* (- y x) z) (+ x (* y z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -6200000000.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 <= (-6200000000.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 <= -6200000000.0) || !(z <= 1.0)) {
tmp = (y - x) * z;
} else {
tmp = x + (y * z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -6200000000.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 <= -6200000000.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 <= -6200000000.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, -6200000000.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 -6200000000 \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 < -6.2e9 or 1 < z Initial program 100.0%
Taylor expanded in z around inf 98.8%
if -6.2e9 < z < 1Initial program 100.0%
*-commutative100.0%
flip--49.4%
associate-*r/49.4%
+-commutative49.4%
Applied egg-rr49.4%
associate-*l/47.0%
+-commutative47.0%
Simplified47.0%
Taylor expanded in y around inf 98.9%
*-commutative98.9%
Simplified98.9%
Final simplification98.9%
(FPCore (x y z) :precision binary64 (if (<= z -2.3e-8) (* y z) (if (<= z 5.3e-36) x (* y z))))
double code(double x, double y, double z) {
double tmp;
if (z <= -2.3e-8) {
tmp = y * z;
} else if (z <= 5.3e-36) {
tmp = x;
} 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 (z <= (-2.3d-8)) then
tmp = y * z
else if (z <= 5.3d-36) then
tmp = x
else
tmp = y * z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= -2.3e-8) {
tmp = y * z;
} else if (z <= 5.3e-36) {
tmp = x;
} else {
tmp = y * z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= -2.3e-8: tmp = y * z elif z <= 5.3e-36: tmp = x else: tmp = y * z return tmp
function code(x, y, z) tmp = 0.0 if (z <= -2.3e-8) tmp = Float64(y * z); elseif (z <= 5.3e-36) tmp = x; else tmp = Float64(y * z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= -2.3e-8) tmp = y * z; elseif (z <= 5.3e-36) tmp = x; else tmp = y * z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, -2.3e-8], N[(y * z), $MachinePrecision], If[LessEqual[z, 5.3e-36], x, N[(y * z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -2.3 \cdot 10^{-8}:\\
\;\;\;\;y \cdot z\\
\mathbf{elif}\;z \leq 5.3 \cdot 10^{-36}:\\
\;\;\;\;x\\
\mathbf{else}:\\
\;\;\;\;y \cdot z\\
\end{array}
\end{array}
if z < -2.3000000000000001e-8 or 5.2999999999999998e-36 < z Initial program 100.0%
Taylor expanded in x around 0 46.6%
if -2.3000000000000001e-8 < z < 5.2999999999999998e-36Initial program 100.0%
Taylor expanded in z around 0 72.0%
Final simplification58.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 37.8%
Final simplification37.8%
herbie shell --seed 2023230
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))