
(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 7 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 z y (fma z (- x) x)))
double code(double x, double y, double z) {
return fma(z, y, fma(z, -x, x));
}
function code(x, y, z) return fma(z, y, fma(z, Float64(-x), x)) end
code[x_, y_, z_] := N[(z * y + N[(z * (-x) + x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(z, y, \mathsf{fma}\left(z, -x, x\right)\right)
\end{array}
Initial program 100.0%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lift--.f64N/A
sub-negN/A
distribute-lft-inN/A
associate-+l+N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
lower-fma.f64N/A
lower-neg.f64100.0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* z (- y x)))) (if (<= z -1.0) t_0 (if (<= z 1.0) (+ x (* z y)) t_0))))
double code(double x, double y, double z) {
double t_0 = z * (y - x);
double tmp;
if (z <= -1.0) {
tmp = t_0;
} else if (z <= 1.0) {
tmp = x + (z * y);
} 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 = z * (y - x)
if (z <= (-1.0d0)) then
tmp = t_0
else if (z <= 1.0d0) then
tmp = x + (z * y)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = z * (y - x);
double tmp;
if (z <= -1.0) {
tmp = t_0;
} else if (z <= 1.0) {
tmp = x + (z * y);
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = z * (y - x) tmp = 0 if z <= -1.0: tmp = t_0 elif z <= 1.0: tmp = x + (z * y) else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(z * Float64(y - x)) tmp = 0.0 if (z <= -1.0) tmp = t_0; elseif (z <= 1.0) tmp = Float64(x + Float64(z * y)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = z * (y - x); tmp = 0.0; if (z <= -1.0) tmp = t_0; elseif (z <= 1.0) tmp = x + (z * y); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(z * N[(y - x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -1.0], t$95$0, If[LessEqual[z, 1.0], N[(x + N[(z * y), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := z \cdot \left(y - x\right)\\
\mathbf{if}\;z \leq -1:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 1:\\
\;\;\;\;x + z \cdot y\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < -1 or 1 < z Initial program 100.0%
Taylor expanded in z around inf
lower-*.f64N/A
lower--.f6497.4
Applied rewrites97.4%
if -1 < z < 1Initial program 100.0%
Taylor expanded in y around inf
lower-*.f6498.7
Applied rewrites98.7%
Final simplification98.1%
(FPCore (x y z) :precision binary64 (let* ((t_0 (* z (- y x)))) (if (<= z -1.06e-32) t_0 (if (<= z 6000.0) (- x (* z x)) t_0))))
double code(double x, double y, double z) {
double t_0 = z * (y - x);
double tmp;
if (z <= -1.06e-32) {
tmp = t_0;
} else if (z <= 6000.0) {
tmp = x - (z * x);
} 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 = z * (y - x)
if (z <= (-1.06d-32)) then
tmp = t_0
else if (z <= 6000.0d0) then
tmp = x - (z * x)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = z * (y - x);
double tmp;
if (z <= -1.06e-32) {
tmp = t_0;
} else if (z <= 6000.0) {
tmp = x - (z * x);
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = z * (y - x) tmp = 0 if z <= -1.06e-32: tmp = t_0 elif z <= 6000.0: tmp = x - (z * x) else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(z * Float64(y - x)) tmp = 0.0 if (z <= -1.06e-32) tmp = t_0; elseif (z <= 6000.0) tmp = Float64(x - Float64(z * x)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = z * (y - x); tmp = 0.0; if (z <= -1.06e-32) tmp = t_0; elseif (z <= 6000.0) tmp = x - (z * x); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(z * N[(y - x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -1.06e-32], t$95$0, If[LessEqual[z, 6000.0], N[(x - N[(z * x), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := z \cdot \left(y - x\right)\\
\mathbf{if}\;z \leq -1.06 \cdot 10^{-32}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 6000:\\
\;\;\;\;x - z \cdot x\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < -1.05999999999999994e-32 or 6e3 < z Initial program 100.0%
Taylor expanded in z around inf
lower-*.f64N/A
lower--.f6497.1
Applied rewrites97.1%
if -1.05999999999999994e-32 < z < 6e3Initial program 100.0%
Taylor expanded in x around inf
mul-1-negN/A
unsub-negN/A
distribute-lft-out--N/A
*-rgt-identityN/A
lower--.f64N/A
*-commutativeN/A
lower-*.f6472.4
Applied rewrites72.4%
(FPCore (x y z) :precision binary64 (if (<= y -1.6e-110) (* z y) (if (<= y 2.9e-20) (* z (- x)) (* z y))))
double code(double x, double y, double z) {
double tmp;
if (y <= -1.6e-110) {
tmp = z * y;
} else if (y <= 2.9e-20) {
tmp = z * -x;
} else {
tmp = z * y;
}
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.6d-110)) then
tmp = z * y
else if (y <= 2.9d-20) then
tmp = z * -x
else
tmp = z * y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= -1.6e-110) {
tmp = z * y;
} else if (y <= 2.9e-20) {
tmp = z * -x;
} else {
tmp = z * y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -1.6e-110: tmp = z * y elif y <= 2.9e-20: tmp = z * -x else: tmp = z * y return tmp
function code(x, y, z) tmp = 0.0 if (y <= -1.6e-110) tmp = Float64(z * y); elseif (y <= 2.9e-20) tmp = Float64(z * Float64(-x)); else tmp = Float64(z * y); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -1.6e-110) tmp = z * y; elseif (y <= 2.9e-20) tmp = z * -x; else tmp = z * y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -1.6e-110], N[(z * y), $MachinePrecision], If[LessEqual[y, 2.9e-20], N[(z * (-x)), $MachinePrecision], N[(z * y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1.6 \cdot 10^{-110}:\\
\;\;\;\;z \cdot y\\
\mathbf{elif}\;y \leq 2.9 \cdot 10^{-20}:\\
\;\;\;\;z \cdot \left(-x\right)\\
\mathbf{else}:\\
\;\;\;\;z \cdot y\\
\end{array}
\end{array}
if y < -1.60000000000000014e-110 or 2.9e-20 < y Initial program 99.9%
Taylor expanded in x around 0
lower-*.f6463.8
Applied rewrites63.8%
if -1.60000000000000014e-110 < y < 2.9e-20Initial program 100.0%
Taylor expanded in z around inf
lower-*.f64N/A
lower--.f6453.9
Applied rewrites53.9%
Taylor expanded in y around 0
Applied rewrites44.6%
Final simplification55.4%
(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%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f64100.0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (* z (- y x)))
double code(double x, double y, double z) {
return z * (y - x);
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = z * (y - x)
end function
public static double code(double x, double y, double z) {
return z * (y - x);
}
def code(x, y, z): return z * (y - x)
function code(x, y, z) return Float64(z * Float64(y - x)) end
function tmp = code(x, y, z) tmp = z * (y - x); end
code[x_, y_, z_] := N[(z * N[(y - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
z \cdot \left(y - x\right)
\end{array}
Initial program 100.0%
Taylor expanded in z around inf
lower-*.f64N/A
lower--.f6464.8
Applied rewrites64.8%
(FPCore (x y z) :precision binary64 (* z y))
double code(double x, double y, double z) {
return z * y;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = z * y
end function
public static double code(double x, double y, double z) {
return z * y;
}
def code(x, y, z): return z * y
function code(x, y, z) return Float64(z * y) end
function tmp = code(x, y, z) tmp = z * y; end
code[x_, y_, z_] := N[(z * y), $MachinePrecision]
\begin{array}{l}
\\
z \cdot y
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
lower-*.f6441.3
Applied rewrites41.3%
Final simplification41.3%
herbie shell --seed 2024238
(FPCore (x y z)
:name "Diagrams.ThreeD.Shapes:frustum from diagrams-lib-1.3.0.3, B"
:precision binary64
(+ x (* (- y x) z)))