
(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 7 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 84.9%
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
Simplified99.9%
(FPCore (x y z) :precision binary64 (let* ((t_0 (fma (/ x z) (- y) y))) (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 = fma((x / z), -y, y);
double tmp;
if (y <= -1.0) {
tmp = t_0;
} else if (y <= 1.0) {
tmp = y + (x / z);
} else {
tmp = t_0;
}
return tmp;
}
function code(x, y, z) t_0 = fma(Float64(x / z), Float64(-y), y) 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
code[x_, y_, z_] := Block[{t$95$0 = N[(N[(x / z), $MachinePrecision] * (-y) + y), $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 := \mathsf{fma}\left(\frac{x}{z}, -y, y\right)\\
\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 70.5%
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
Simplified99.9%
Taylor expanded in y around inf
+-commutativeN/A
distribute-rgt-inN/A
*-lft-identityN/A
*-commutativeN/A
associate-*l*N/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
lower-neg.f6498.5
Simplified98.5%
if -1 < y < 1Initial program 99.9%
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
Simplified100.0%
Taylor expanded in y around 0
Simplified99.3%
lift-/.f64N/A
*-lft-identityN/A
lower-fma.f64N/A
*-lft-identity99.3
lift-fma.f64N/A
lower-+.f64N/A
*-lft-identity99.3
Applied egg-rr99.3%
Final simplification98.9%
(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 70.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.7
Simplified91.7%
if -1 < y < 1Initial program 99.9%
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
Simplified100.0%
Taylor expanded in y around 0
Simplified99.3%
lift-/.f64N/A
*-lft-identityN/A
lower-fma.f64N/A
*-lft-identity99.3
lift-fma.f64N/A
lower-+.f64N/A
*-lft-identity99.3
Applied egg-rr99.3%
Final simplification95.4%
(FPCore (x y z) :precision binary64 (let* ((t_0 (+ y (/ x z)))) (if (<= z -0.00065) t_0 (if (<= z 4.3e-136) (* (- 1.0 y) (/ x z)) t_0))))
double code(double x, double y, double z) {
double t_0 = y + (x / z);
double tmp;
if (z <= -0.00065) {
tmp = t_0;
} else if (z <= 4.3e-136) {
tmp = (1.0 - 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 + (x / z)
if (z <= (-0.00065d0)) then
tmp = t_0
else if (z <= 4.3d-136) then
tmp = (1.0d0 - 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 + (x / z);
double tmp;
if (z <= -0.00065) {
tmp = t_0;
} else if (z <= 4.3e-136) {
tmp = (1.0 - y) * (x / z);
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = y + (x / z) tmp = 0 if z <= -0.00065: tmp = t_0 elif z <= 4.3e-136: tmp = (1.0 - y) * (x / z) else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(y + Float64(x / z)) tmp = 0.0 if (z <= -0.00065) tmp = t_0; elseif (z <= 4.3e-136) tmp = Float64(Float64(1.0 - y) * Float64(x / z)); else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = y + (x / z); tmp = 0.0; if (z <= -0.00065) tmp = t_0; elseif (z <= 4.3e-136) tmp = (1.0 - y) * (x / z); else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(y + N[(x / z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[z, -0.00065], t$95$0, If[LessEqual[z, 4.3e-136], N[(N[(1.0 - y), $MachinePrecision] * N[(x / z), $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := y + \frac{x}{z}\\
\mathbf{if}\;z \leq -0.00065:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 4.3 \cdot 10^{-136}:\\
\;\;\;\;\left(1 - y\right) \cdot \frac{x}{z}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < -6.4999999999999997e-4 or 4.3e-136 < z Initial program 75.2%
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
Simplified99.9%
Taylor expanded in y around 0
Simplified89.1%
lift-/.f64N/A
*-lft-identityN/A
lower-fma.f64N/A
*-lft-identity89.1
lift-fma.f64N/A
lower-+.f64N/A
*-lft-identity89.1
Applied egg-rr89.1%
if -6.4999999999999997e-4 < z < 4.3e-136Initial program 99.9%
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-*.f6487.6
Simplified87.6%
cancel-sign-sub-invN/A
distribute-rgt1-inN/A
+-commutativeN/A
sub-negN/A
lift--.f64N/A
*-commutativeN/A
associate-*l/N/A
lift-/.f64N/A
lower-*.f6487.6
Applied egg-rr87.6%
Final simplification88.5%
(FPCore (x y z) :precision binary64 (if (<= z -4.8e-74) y (if (<= z 3.3e+18) (/ x z) y)))
double code(double x, double y, double z) {
double tmp;
if (z <= -4.8e-74) {
tmp = y;
} else if (z <= 3.3e+18) {
tmp = x / z;
} else {
tmp = 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 (z <= (-4.8d-74)) then
tmp = y
else if (z <= 3.3d+18) then
tmp = x / z
else
tmp = y
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= -4.8e-74) {
tmp = y;
} else if (z <= 3.3e+18) {
tmp = x / z;
} else {
tmp = y;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= -4.8e-74: tmp = y elif z <= 3.3e+18: tmp = x / z else: tmp = y return tmp
function code(x, y, z) tmp = 0.0 if (z <= -4.8e-74) tmp = y; elseif (z <= 3.3e+18) tmp = Float64(x / z); else tmp = y; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= -4.8e-74) tmp = y; elseif (z <= 3.3e+18) tmp = x / z; else tmp = y; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, -4.8e-74], y, If[LessEqual[z, 3.3e+18], N[(x / z), $MachinePrecision], y]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -4.8 \cdot 10^{-74}:\\
\;\;\;\;y\\
\mathbf{elif}\;z \leq 3.3 \cdot 10^{+18}:\\
\;\;\;\;\frac{x}{z}\\
\mathbf{else}:\\
\;\;\;\;y\\
\end{array}
\end{array}
if z < -4.7999999999999998e-74 or 3.3e18 < z Initial program 74.4%
lift--.f64N/A
lift-*.f64N/A
flip3-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip3-+N/A
lift-+.f64N/A
lower-/.f6474.3
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6474.3
Applied egg-rr74.3%
Taylor expanded in z around inf
*-commutativeN/A
lower-*.f6447.1
Simplified47.1%
*-commutativeN/A
associate-/l*N/A
*-inversesN/A
*-rgt-identity67.7
Applied egg-rr67.7%
if -4.7999999999999998e-74 < z < 3.3e18Initial program 99.9%
Taylor expanded in y around 0
lower-/.f6462.3
Simplified62.3%
(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 84.9%
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
Simplified99.9%
Taylor expanded in y around 0
Simplified80.8%
lift-/.f64N/A
*-lft-identityN/A
lower-fma.f64N/A
*-lft-identity80.8
lift-fma.f64N/A
lower-+.f64N/A
*-lft-identity80.8
Applied egg-rr80.8%
Final simplification80.8%
(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 84.9%
lift--.f64N/A
lift-*.f64N/A
flip3-+N/A
clear-numN/A
lower-/.f64N/A
clear-numN/A
flip3-+N/A
lift-+.f64N/A
lower-/.f6484.8
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
lower-fma.f6484.8
Applied egg-rr84.8%
Taylor expanded in z around inf
*-commutativeN/A
lower-*.f6433.6
Simplified33.6%
*-commutativeN/A
associate-/l*N/A
*-inversesN/A
*-rgt-identity45.7
Applied egg-rr45.7%
(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 2024207
(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))