
(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 8 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 (/ x z) (- 1.0 y) y))
double code(double x, double y, double z) {
return fma((x / z), (1.0 - y), y);
}
function code(x, y, z) return fma(Float64(x / z), Float64(1.0 - y), y) end
code[x_, y_, z_] := N[(N[(x / z), $MachinePrecision] * N[(1.0 - y), $MachinePrecision] + y), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{x}{z}, 1 - y, y\right)
\end{array}
Initial program 87.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6487.9
Applied rewrites87.9%
Taylor expanded in x around 0
+-commutativeN/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
div-subN/A
associate-/l*N/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
(FPCore (x y z) :precision binary64 (if (or (<= y -14800.0) (not (<= y 5.4e-8))) (* (- 1.0 (/ x z)) y) (fma (/ x z) 1.0 y)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -14800.0) || !(y <= 5.4e-8)) {
tmp = (1.0 - (x / z)) * y;
} else {
tmp = fma((x / z), 1.0, y);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -14800.0) || !(y <= 5.4e-8)) tmp = Float64(Float64(1.0 - Float64(x / z)) * y); else tmp = fma(Float64(x / z), 1.0, y); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -14800.0], N[Not[LessEqual[y, 5.4e-8]], $MachinePrecision]], N[(N[(1.0 - N[(x / z), $MachinePrecision]), $MachinePrecision] * y), $MachinePrecision], N[(N[(x / z), $MachinePrecision] * 1.0 + y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -14800 \lor \neg \left(y \leq 5.4 \cdot 10^{-8}\right):\\
\;\;\;\;\left(1 - \frac{x}{z}\right) \cdot y\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{z}, 1, y\right)\\
\end{array}
\end{array}
if y < -14800 or 5.40000000000000005e-8 < y Initial program 74.4%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6474.4
Applied rewrites74.4%
Taylor expanded in y around inf
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
lower--.f6499.5
Applied rewrites99.5%
Applied rewrites99.5%
if -14800 < y < 5.40000000000000005e-8Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
Applied rewrites99.9%
Taylor expanded in x around 0
+-commutativeN/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
div-subN/A
associate-/l*N/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
Applied rewrites99.2%
Final simplification99.4%
(FPCore (x y z) :precision binary64 (if (or (<= y -100000.0) (not (<= y 5.4e-8))) (/ (* (- z x) y) z) (fma (/ x z) 1.0 y)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -100000.0) || !(y <= 5.4e-8)) {
tmp = ((z - x) * y) / z;
} else {
tmp = fma((x / z), 1.0, y);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((y <= -100000.0) || !(y <= 5.4e-8)) tmp = Float64(Float64(Float64(z - x) * y) / z); else tmp = fma(Float64(x / z), 1.0, y); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[y, -100000.0], N[Not[LessEqual[y, 5.4e-8]], $MachinePrecision]], N[(N[(N[(z - x), $MachinePrecision] * y), $MachinePrecision] / z), $MachinePrecision], N[(N[(x / z), $MachinePrecision] * 1.0 + y), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -100000 \lor \neg \left(y \leq 5.4 \cdot 10^{-8}\right):\\
\;\;\;\;\frac{\left(z - x\right) \cdot y}{z}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{z}, 1, y\right)\\
\end{array}
\end{array}
if y < -1e5 or 5.40000000000000005e-8 < y Initial program 74.4%
Taylor expanded in y around inf
lower-/.f64N/A
*-commutativeN/A
lower-*.f64N/A
lower--.f6474.1
Applied rewrites74.1%
if -1e5 < y < 5.40000000000000005e-8Initial program 99.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6499.9
Applied rewrites99.9%
Taylor expanded in x around 0
+-commutativeN/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
div-subN/A
associate-/l*N/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
Applied rewrites99.2%
Final simplification87.3%
(FPCore (x y z) :precision binary64 (if (or (<= z -2e-81) (not (<= z 0.0148))) (fma (/ x z) 1.0 y) (* (- 1.0 y) (/ x z))))
double code(double x, double y, double z) {
double tmp;
if ((z <= -2e-81) || !(z <= 0.0148)) {
tmp = fma((x / z), 1.0, y);
} else {
tmp = (1.0 - y) * (x / z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if ((z <= -2e-81) || !(z <= 0.0148)) tmp = fma(Float64(x / z), 1.0, y); else tmp = Float64(Float64(1.0 - y) * Float64(x / z)); end return tmp end
code[x_, y_, z_] := If[Or[LessEqual[z, -2e-81], N[Not[LessEqual[z, 0.0148]], $MachinePrecision]], N[(N[(x / z), $MachinePrecision] * 1.0 + y), $MachinePrecision], N[(N[(1.0 - y), $MachinePrecision] * N[(x / z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -2 \cdot 10^{-81} \lor \neg \left(z \leq 0.0148\right):\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{z}, 1, y\right)\\
\mathbf{else}:\\
\;\;\;\;\left(1 - y\right) \cdot \frac{x}{z}\\
\end{array}
\end{array}
if z < -1.9999999999999999e-81 or 0.014800000000000001 < z Initial program 80.4%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6480.4
Applied rewrites80.4%
Taylor expanded in x around 0
+-commutativeN/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
div-subN/A
associate-/l*N/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
Applied rewrites85.0%
if -1.9999999999999999e-81 < z < 0.014800000000000001Initial program 99.9%
Taylor expanded in x around inf
*-commutativeN/A
associate-/l*N/A
+-commutativeN/A
metadata-evalN/A
distribute-lft-inN/A
metadata-evalN/A
sub-negN/A
neg-mul-1N/A
lower-*.f64N/A
sub-negN/A
metadata-evalN/A
+-commutativeN/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f64N/A
lower-/.f6488.1
Applied rewrites88.1%
Final simplification86.2%
(FPCore (x y z) :precision binary64 (if (<= y 3.5e+20) (fma (/ x z) 1.0 y) (* (- y) (/ x z))))
double code(double x, double y, double z) {
double tmp;
if (y <= 3.5e+20) {
tmp = fma((x / z), 1.0, y);
} else {
tmp = -y * (x / z);
}
return tmp;
}
function code(x, y, z) tmp = 0.0 if (y <= 3.5e+20) tmp = fma(Float64(x / z), 1.0, y); else tmp = Float64(Float64(-y) * Float64(x / z)); end return tmp end
code[x_, y_, z_] := If[LessEqual[y, 3.5e+20], N[(N[(x / z), $MachinePrecision] * 1.0 + y), $MachinePrecision], N[((-y) * N[(x / z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq 3.5 \cdot 10^{+20}:\\
\;\;\;\;\mathsf{fma}\left(\frac{x}{z}, 1, y\right)\\
\mathbf{else}:\\
\;\;\;\;\left(-y\right) \cdot \frac{x}{z}\\
\end{array}
\end{array}
if y < 3.5e20Initial program 91.6%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6491.6
Applied rewrites91.6%
Taylor expanded in x around 0
+-commutativeN/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
div-subN/A
associate-/l*N/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
Applied rewrites88.7%
if 3.5e20 < y Initial program 76.3%
Taylor expanded in x around inf
*-commutativeN/A
associate-/l*N/A
+-commutativeN/A
metadata-evalN/A
distribute-lft-inN/A
metadata-evalN/A
sub-negN/A
neg-mul-1N/A
lower-*.f64N/A
sub-negN/A
metadata-evalN/A
+-commutativeN/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f64N/A
lower-/.f6460.3
Applied rewrites60.3%
Taylor expanded in y around inf
Applied rewrites60.3%
Final simplification81.7%
(FPCore (x y z) :precision binary64 (if (or (<= y -4.5e-25) (not (<= y 1.25e-39))) (* 1.0 y) (/ x z)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -4.5e-25) || !(y <= 1.25e-39)) {
tmp = 1.0 * 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 ((y <= (-4.5d-25)) .or. (.not. (y <= 1.25d-39))) then
tmp = 1.0d0 * y
else
tmp = x / z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -4.5e-25) || !(y <= 1.25e-39)) {
tmp = 1.0 * y;
} else {
tmp = x / z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -4.5e-25) or not (y <= 1.25e-39): tmp = 1.0 * y else: tmp = x / z return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -4.5e-25) || !(y <= 1.25e-39)) tmp = Float64(1.0 * y); else tmp = Float64(x / z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -4.5e-25) || ~((y <= 1.25e-39))) tmp = 1.0 * y; else tmp = x / z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -4.5e-25], N[Not[LessEqual[y, 1.25e-39]], $MachinePrecision]], N[(1.0 * y), $MachinePrecision], N[(x / z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -4.5 \cdot 10^{-25} \lor \neg \left(y \leq 1.25 \cdot 10^{-39}\right):\\
\;\;\;\;1 \cdot y\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{z}\\
\end{array}
\end{array}
if y < -4.5000000000000001e-25 or 1.25e-39 < y Initial program 77.1%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6477.1
Applied rewrites77.1%
Taylor expanded in y around inf
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
lower--.f6497.6
Applied rewrites97.6%
Taylor expanded in x around 0
Applied rewrites52.4%
if -4.5000000000000001e-25 < y < 1.25e-39Initial program 99.9%
Taylor expanded in y around 0
lower-/.f6477.7
Applied rewrites77.7%
Final simplification64.4%
(FPCore (x y z) :precision binary64 (fma (/ x z) 1.0 y))
double code(double x, double y, double z) {
return fma((x / z), 1.0, y);
}
function code(x, y, z) return fma(Float64(x / z), 1.0, y) end
code[x_, y_, z_] := N[(N[(x / z), $MachinePrecision] * 1.0 + y), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(\frac{x}{z}, 1, y\right)
\end{array}
Initial program 87.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6487.9
Applied rewrites87.9%
Taylor expanded in x around 0
+-commutativeN/A
+-commutativeN/A
mul-1-negN/A
unsub-negN/A
div-subN/A
associate-/l*N/A
*-commutativeN/A
sub-negN/A
mul-1-negN/A
associate-/l*N/A
*-commutativeN/A
lower-fma.f64N/A
lower-/.f64N/A
mul-1-negN/A
sub-negN/A
lower--.f64100.0
Applied rewrites100.0%
Taylor expanded in y around 0
Applied rewrites77.0%
(FPCore (x y z) :precision binary64 (* 1.0 y))
double code(double x, double y, double z) {
return 1.0 * y;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = 1.0d0 * y
end function
public static double code(double x, double y, double z) {
return 1.0 * y;
}
def code(x, y, z): return 1.0 * y
function code(x, y, z) return Float64(1.0 * y) end
function tmp = code(x, y, z) tmp = 1.0 * y; end
code[x_, y_, z_] := N[(1.0 * y), $MachinePrecision]
\begin{array}{l}
\\
1 \cdot y
\end{array}
Initial program 87.9%
lift-+.f64N/A
+-commutativeN/A
lift-*.f64N/A
*-commutativeN/A
lower-fma.f6487.9
Applied rewrites87.9%
Taylor expanded in y around inf
associate-/l*N/A
*-commutativeN/A
lower-*.f64N/A
lower-/.f64N/A
lower--.f6466.9
Applied rewrites66.9%
Taylor expanded in x around 0
Applied rewrites39.1%
(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 2024317
(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))