
(FPCore (x y z) :precision binary64 (/ (+ x (* y (- z x))) z))
double code(double x, double y, double z) {
return (x + (y * (z - 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 * (z - x))) / z
end function
public static double code(double x, double y, double z) {
return (x + (y * (z - x))) / z;
}
def code(x, y, z): return (x + (y * (z - x))) / z
function code(x, y, z) return Float64(Float64(x + Float64(y * Float64(z - x))) / z) end
function tmp = code(x, y, z) tmp = (x + (y * (z - x))) / z; end
code[x_, y_, z_] := N[(N[(x + N[(y * N[(z - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y \cdot \left(z - x\right)}{z}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (/ (+ x (* y (- z x))) z))
double code(double x, double y, double z) {
return (x + (y * (z - 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 * (z - x))) / z
end function
public static double code(double x, double y, double z) {
return (x + (y * (z - x))) / z;
}
def code(x, y, z): return (x + (y * (z - x))) / z
function code(x, y, z) return Float64(Float64(x + Float64(y * Float64(z - x))) / z) end
function tmp = code(x, y, z) tmp = (x + (y * (z - x))) / z; end
code[x_, y_, z_] := N[(N[(x + N[(y * N[(z - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / z), $MachinePrecision]
\begin{array}{l}
\\
\frac{x + y \cdot \left(z - x\right)}{z}
\end{array}
(FPCore (x y z) :precision binary64 (fma (- 1.0 y) (/ x z) y))
double code(double x, double y, double z) {
return fma((1.0 - y), (x / z), y);
}
function code(x, y, z) return fma(Float64(1.0 - y), Float64(x / z), y) end
code[x_, y_, z_] := N[(N[(1.0 - y), $MachinePrecision] * N[(x / z), $MachinePrecision] + y), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(1 - y, \frac{x}{z}, y\right)
\end{array}
Initial program 88.7%
Taylor expanded in x around 0
distribute-lft-inN/A
mul-1-negN/A
distribute-rgt-neg-inN/A
associate-/l*N/A
mul-1-negN/A
associate-+r+N/A
associate-*r/N/A
*-rgt-identityN/A
associate-+r+N/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
div-subN/A
unsub-negN/A
mul-1-negN/A
+-commutativeN/A
Applied rewrites99.9%
(FPCore (x y z) :precision binary64 (if (<= y -1.0) (- y (/ (* y x) z)) (if (<= y 1.0) (+ y (/ x z)) (fma (- x) (/ y z) y))))
double code(double x, double y, double z) {
double tmp;
if (y <= -1.0) {
tmp = y - ((y * x) / z);
} else if (y <= 1.0) {
tmp = y + (x / z);
} else {
tmp = fma(-x, (y / z), y);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= -1.0) tmp = Float64(y - Float64(Float64(y * x) / z)); elseif (y <= 1.0) tmp = Float64(y + Float64(x / z)); else tmp = fma(Float64(-x), Float64(y / z), y); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, -1.0], N[(y - N[(N[(y * x), $MachinePrecision] / z), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.0], N[(y + N[(x / z), $MachinePrecision]), $MachinePrecision], N[((-x) * N[(y / z), $MachinePrecision] + y), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -1:\\
\;\;\;\;y - \frac{y \cdot x}{z}\\
\mathbf{elif}\;y \leq 1:\\
\;\;\;\;y + \frac{x}{z}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(-x, \frac{y}{z}, y\right)\\
\end{array}
\end{array}
if y < -1Initial program 79.8%
Taylor expanded in y around inf
associate-/l*N/A
div-subN/A
sub-negN/A
*-inversesN/A
sub-negN/A
distribute-lft-out--N/A
*-rgt-identityN/A
associate-/l*N/A
*-commutativeN/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6493.0
Applied rewrites93.0%
if -1 < y < 1Initial program 100.0%
Taylor expanded in z around inf
lower-*.f6498.4
Applied rewrites98.4%
Taylor expanded in x around 0
+-commutativeN/A
lower-+.f64N/A
lower-/.f6498.4
Applied rewrites98.4%
if 1 < y Initial program 73.5%
Taylor expanded in y around inf
associate-/l*N/A
div-subN/A
sub-negN/A
*-inversesN/A
sub-negN/A
distribute-lft-out--N/A
*-rgt-identityN/A
associate-/l*N/A
*-commutativeN/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6491.6
Applied rewrites91.6%
lift-*.f64N/A
lift-/.f64N/A
sub-negN/A
+-commutativeN/A
lift-/.f64N/A
lift-*.f64N/A
*-commutativeN/A
associate-/l*N/A
distribute-lft-neg-inN/A
lower-fma.f64N/A
lower-neg.f64N/A
lower-/.f6494.6
Applied rewrites94.6%
Final simplification96.2%
(FPCore (x y z) :precision binary64 (let* ((t_0 (- y (/ (* y x) z)))) (if (<= y -1.0) t_0 (if (<= y 1.0) (+ y (/ x z)) t_0))))
double code(double x, double y, double z) {
double t_0 = y - ((y * x) / z);
double tmp;
if (y <= -1.0) {
tmp = t_0;
} else if (y <= 1.0) {
tmp = y + (x / 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 = y - ((y * x) / z)
if (y <= (-1.0d0)) then
tmp = t_0
else if (y <= 1.0d0) then
tmp = y + (x / z)
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = y - ((y * x) / z);
double tmp;
if (y <= -1.0) {
tmp = t_0;
} else if (y <= 1.0) {
tmp = y + (x / z);
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = y - ((y * x) / z) tmp = 0 if y <= -1.0: tmp = t_0 elif y <= 1.0: tmp = y + (x / z) else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(y - Float64(Float64(y * x) / z)) tmp = 0.0 if (y <= -1.0) tmp = t_0; elseif (y <= 1.0) tmp = Float64(y + Float64(x / z)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = y - ((y * x) / z); tmp = 0.0; if (y <= -1.0) tmp = t_0; elseif (y <= 1.0) tmp = y + (x / z); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(y - N[(N[(y * x), $MachinePrecision] / z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -1.0], t$95$0, If[LessEqual[y, 1.0], N[(y + N[(x / z), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y - \frac{y \cdot x}{z}\\
\mathbf{if}\;y \leq -1:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 1:\\
\;\;\;\;y + \frac{x}{z}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if y < -1 or 1 < y Initial program 76.6%
Taylor expanded in y around inf
associate-/l*N/A
div-subN/A
sub-negN/A
*-inversesN/A
sub-negN/A
distribute-lft-out--N/A
*-rgt-identityN/A
associate-/l*N/A
*-commutativeN/A
lower--.f64N/A
lower-/.f64N/A
*-commutativeN/A
lower-*.f6492.3
Applied rewrites92.3%
if -1 < y < 1Initial program 100.0%
Taylor expanded in z around inf
lower-*.f6498.4
Applied rewrites98.4%
Taylor expanded in x around 0
+-commutativeN/A
lower-+.f64N/A
lower-/.f6498.4
Applied rewrites98.4%
Final simplification95.5%
(FPCore (x y z) :precision binary64 (if (<= x -4e-15) (/ x z) (if (<= x 7.4e-37) y (/ x z))))
double code(double x, double y, double z) {
double tmp;
if (x <= -4e-15) {
tmp = x / z;
} else if (x <= 7.4e-37) {
tmp = y;
} else {
tmp = x / 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 <= (-4d-15)) then
tmp = x / z
else if (x <= 7.4d-37) then
tmp = y
else
tmp = x / z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (x <= -4e-15) {
tmp = x / z;
} else if (x <= 7.4e-37) {
tmp = y;
} else {
tmp = x / z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -4e-15: tmp = x / z elif x <= 7.4e-37: tmp = y else: tmp = x / z return tmp
function code(x, y, z) tmp = 0.0 if (x <= -4e-15) tmp = Float64(x / z); elseif (x <= 7.4e-37) tmp = y; else tmp = Float64(x / z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -4e-15) tmp = x / z; elseif (x <= 7.4e-37) tmp = y; else tmp = x / z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -4e-15], N[(x / z), $MachinePrecision], If[LessEqual[x, 7.4e-37], y, N[(x / z), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -4 \cdot 10^{-15}:\\
\;\;\;\;\frac{x}{z}\\
\mathbf{elif}\;x \leq 7.4 \cdot 10^{-37}:\\
\;\;\;\;y\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{z}\\
\end{array}
\end{array}
if x < -4.0000000000000003e-15 or 7.4e-37 < x Initial program 92.2%
Taylor expanded in y around 0
lower-/.f6461.2
Applied rewrites61.2%
if -4.0000000000000003e-15 < x < 7.4e-37Initial program 84.5%
Taylor expanded in z around inf
lower-*.f6474.3
Applied rewrites74.3%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6460.8
Applied rewrites60.8%
*-commutativeN/A
associate-/l*N/A
*-inversesN/A
*-rgt-identity76.4
Applied rewrites76.4%
(FPCore (x y z) :precision binary64 (+ y (/ x z)))
double code(double x, double y, double z) {
return 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 = y + (x / z)
end function
public static double code(double x, double y, double z) {
return y + (x / z);
}
def code(x, y, z): return y + (x / z)
function code(x, y, z) return Float64(y + Float64(x / z)) end
function tmp = code(x, y, z) tmp = y + (x / z); end
code[x_, y_, z_] := N[(y + N[(x / z), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
y + \frac{x}{z}
\end{array}
Initial program 88.7%
Taylor expanded in z around inf
lower-*.f6470.8
Applied rewrites70.8%
Taylor expanded in x around 0
+-commutativeN/A
lower-+.f64N/A
lower-/.f6479.7
Applied rewrites79.7%
Final simplification79.7%
(FPCore (x y z) :precision binary64 y)
double code(double x, double y, double z) {
return y;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = y
end function
public static double code(double x, double y, double z) {
return y;
}
def code(x, y, z): return y
function code(x, y, z) return y end
function tmp = code(x, y, z) tmp = y; end
code[x_, y_, z_] := y
\begin{array}{l}
\\
y
\end{array}
Initial program 88.7%
Taylor expanded in z around inf
lower-*.f6470.8
Applied rewrites70.8%
Taylor expanded in x around 0
*-commutativeN/A
lower-*.f6432.3
Applied rewrites32.3%
*-commutativeN/A
associate-/l*N/A
*-inversesN/A
*-rgt-identity41.2
Applied rewrites41.2%
(FPCore (x y z) :precision binary64 (- (+ y (/ x z)) (/ y (/ z x))))
double code(double x, double y, double z) {
return (y + (x / z)) - (y / (z / x));
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = (y + (x / z)) - (y / (z / x))
end function
public static double code(double x, double y, double z) {
return (y + (x / z)) - (y / (z / x));
}
def code(x, y, z): return (y + (x / z)) - (y / (z / x))
function code(x, y, z) return Float64(Float64(y + Float64(x / z)) - Float64(y / Float64(z / x))) end
function tmp = code(x, y, z) tmp = (y + (x / z)) - (y / (z / x)); end
code[x_, y_, z_] := N[(N[(y + N[(x / z), $MachinePrecision]), $MachinePrecision] - N[(y / N[(z / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(y + \frac{x}{z}\right) - \frac{y}{\frac{z}{x}}
\end{array}
herbie shell --seed 2024219
(FPCore (x y z)
:name "Diagrams.Backend.Rasterific:rasterificRadialGradient from diagrams-rasterific-1.3.1.3"
:precision binary64
:alt
(! :herbie-platform default (- (+ y (/ x z)) (/ y (/ z x))))
(/ (+ x (* y (- z x))) z))