
(FPCore (x y z) :precision binary64 (/ (- x y) (- z y)))
double code(double x, double y, double z) {
return (x - y) / (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 = (x - y) / (z - y)
end function
public static double code(double x, double y, double z) {
return (x - y) / (z - y);
}
def code(x, y, z): return (x - y) / (z - y)
function code(x, y, z) return Float64(Float64(x - y) / Float64(z - y)) end
function tmp = code(x, y, z) tmp = (x - y) / (z - y); end
code[x_, y_, z_] := N[(N[(x - y), $MachinePrecision] / N[(z - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - y}{z - y}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 8 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x y z) :precision binary64 (/ (- x y) (- z y)))
double code(double x, double y, double z) {
return (x - y) / (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 = (x - y) / (z - y)
end function
public static double code(double x, double y, double z) {
return (x - y) / (z - y);
}
def code(x, y, z): return (x - y) / (z - y)
function code(x, y, z) return Float64(Float64(x - y) / Float64(z - y)) end
function tmp = code(x, y, z) tmp = (x - y) / (z - y); end
code[x_, y_, z_] := N[(N[(x - y), $MachinePrecision] / N[(z - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - y}{z - y}
\end{array}
(FPCore (x y z) :precision binary64 (/ (- x y) (- z y)))
double code(double x, double y, double z) {
return (x - y) / (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 = (x - y) / (z - y)
end function
public static double code(double x, double y, double z) {
return (x - y) / (z - y);
}
def code(x, y, z): return (x - y) / (z - y)
function code(x, y, z) return Float64(Float64(x - y) / Float64(z - y)) end
function tmp = code(x, y, z) tmp = (x - y) / (z - y); end
code[x_, y_, z_] := N[(N[(x - y), $MachinePrecision] / N[(z - y), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - y}{z - y}
\end{array}
Initial program 100.0%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (/ y (- y z))))
(if (<= y -9.2e-53)
t_0
(if (<= y 30500000.0)
(/ x (- z y))
(if (<= y 2.1e+132) t_0 (- 1.0 (/ x y)))))))
double code(double x, double y, double z) {
double t_0 = y / (y - z);
double tmp;
if (y <= -9.2e-53) {
tmp = t_0;
} else if (y <= 30500000.0) {
tmp = x / (z - y);
} else if (y <= 2.1e+132) {
tmp = t_0;
} else {
tmp = 1.0 - (x / 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) :: t_0
real(8) :: tmp
t_0 = y / (y - z)
if (y <= (-9.2d-53)) then
tmp = t_0
else if (y <= 30500000.0d0) then
tmp = x / (z - y)
else if (y <= 2.1d+132) then
tmp = t_0
else
tmp = 1.0d0 - (x / y)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = y / (y - z);
double tmp;
if (y <= -9.2e-53) {
tmp = t_0;
} else if (y <= 30500000.0) {
tmp = x / (z - y);
} else if (y <= 2.1e+132) {
tmp = t_0;
} else {
tmp = 1.0 - (x / y);
}
return tmp;
}
def code(x, y, z): t_0 = y / (y - z) tmp = 0 if y <= -9.2e-53: tmp = t_0 elif y <= 30500000.0: tmp = x / (z - y) elif y <= 2.1e+132: tmp = t_0 else: tmp = 1.0 - (x / y) return tmp
function code(x, y, z) t_0 = Float64(y / Float64(y - z)) tmp = 0.0 if (y <= -9.2e-53) tmp = t_0; elseif (y <= 30500000.0) tmp = Float64(x / Float64(z - y)); elseif (y <= 2.1e+132) tmp = t_0; else tmp = Float64(1.0 - Float64(x / y)); end return tmp end
function tmp_2 = code(x, y, z) t_0 = y / (y - z); tmp = 0.0; if (y <= -9.2e-53) tmp = t_0; elseif (y <= 30500000.0) tmp = x / (z - y); elseif (y <= 2.1e+132) tmp = t_0; else tmp = 1.0 - (x / y); end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(y / N[(y - z), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[y, -9.2e-53], t$95$0, If[LessEqual[y, 30500000.0], N[(x / N[(z - y), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 2.1e+132], t$95$0, N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{y}{y - z}\\
\mathbf{if}\;y \leq -9.2 \cdot 10^{-53}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;y \leq 30500000:\\
\;\;\;\;\frac{x}{z - y}\\
\mathbf{elif}\;y \leq 2.1 \cdot 10^{+132}:\\
\;\;\;\;t\_0\\
\mathbf{else}:\\
\;\;\;\;1 - \frac{x}{y}\\
\end{array}
\end{array}
if y < -9.2000000000000005e-53 or 3.05e7 < y < 2.09999999999999993e132Initial program 100.0%
Taylor expanded in x around 0 79.9%
neg-mul-179.9%
distribute-neg-frac279.9%
sub-neg79.9%
+-commutative79.9%
distribute-neg-in79.9%
remove-double-neg79.9%
sub-neg79.9%
Simplified79.9%
if -9.2000000000000005e-53 < y < 3.05e7Initial program 100.0%
Taylor expanded in x around inf 86.8%
if 2.09999999999999993e132 < y Initial program 100.0%
Taylor expanded in z around 0 95.1%
associate-*r/95.1%
neg-mul-195.1%
sub-neg95.1%
+-commutative95.1%
distribute-neg-in95.1%
remove-double-neg95.1%
sub-neg95.1%
div-sub95.1%
*-inverses95.1%
Simplified95.1%
(FPCore (x y z) :precision binary64 (if (<= y -4.7e-12) (/ y (- y z)) (if (<= y 1.5e-9) (- (/ x z) (/ y z)) (- 1.0 (/ x y)))))
double code(double x, double y, double z) {
double tmp;
if (y <= -4.7e-12) {
tmp = y / (y - z);
} else if (y <= 1.5e-9) {
tmp = (x / z) - (y / z);
} else {
tmp = 1.0 - (x / 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 <= (-4.7d-12)) then
tmp = y / (y - z)
else if (y <= 1.5d-9) then
tmp = (x / z) - (y / z)
else
tmp = 1.0d0 - (x / y)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= -4.7e-12) {
tmp = y / (y - z);
} else if (y <= 1.5e-9) {
tmp = (x / z) - (y / z);
} else {
tmp = 1.0 - (x / y);
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -4.7e-12: tmp = y / (y - z) elif y <= 1.5e-9: tmp = (x / z) - (y / z) else: tmp = 1.0 - (x / y) return tmp
function code(x, y, z) tmp = 0.0 if (y <= -4.7e-12) tmp = Float64(y / Float64(y - z)); elseif (y <= 1.5e-9) tmp = Float64(Float64(x / z) - Float64(y / z)); else tmp = Float64(1.0 - Float64(x / y)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -4.7e-12) tmp = y / (y - z); elseif (y <= 1.5e-9) tmp = (x / z) - (y / z); else tmp = 1.0 - (x / y); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -4.7e-12], N[(y / N[(y - z), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 1.5e-9], N[(N[(x / z), $MachinePrecision] - N[(y / z), $MachinePrecision]), $MachinePrecision], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -4.7 \cdot 10^{-12}:\\
\;\;\;\;\frac{y}{y - z}\\
\mathbf{elif}\;y \leq 1.5 \cdot 10^{-9}:\\
\;\;\;\;\frac{x}{z} - \frac{y}{z}\\
\mathbf{else}:\\
\;\;\;\;1 - \frac{x}{y}\\
\end{array}
\end{array}
if y < -4.69999999999999976e-12Initial program 100.0%
Taylor expanded in x around 0 82.7%
neg-mul-182.7%
distribute-neg-frac282.7%
sub-neg82.7%
+-commutative82.7%
distribute-neg-in82.7%
remove-double-neg82.7%
sub-neg82.7%
Simplified82.7%
if -4.69999999999999976e-12 < y < 1.49999999999999999e-9Initial program 100.0%
Taylor expanded in z around inf 87.3%
Taylor expanded in x around 0 87.3%
neg-mul-187.3%
+-commutative87.3%
sub-neg87.3%
Simplified87.3%
if 1.49999999999999999e-9 < y Initial program 100.0%
Taylor expanded in z around 0 80.6%
associate-*r/80.6%
neg-mul-180.6%
sub-neg80.6%
+-commutative80.6%
distribute-neg-in80.6%
remove-double-neg80.6%
sub-neg80.6%
div-sub80.6%
*-inverses80.6%
Simplified80.6%
(FPCore (x y z) :precision binary64 (if (or (<= y -2.6e-12) (not (<= y 3800000000.0))) (- 1.0 (/ x y)) (/ x (- z y))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -2.6e-12) || !(y <= 3800000000.0)) {
tmp = 1.0 - (x / y);
} else {
tmp = x / (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 <= (-2.6d-12)) .or. (.not. (y <= 3800000000.0d0))) then
tmp = 1.0d0 - (x / y)
else
tmp = x / (z - y)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -2.6e-12) || !(y <= 3800000000.0)) {
tmp = 1.0 - (x / y);
} else {
tmp = x / (z - y);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -2.6e-12) or not (y <= 3800000000.0): tmp = 1.0 - (x / y) else: tmp = x / (z - y) return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -2.6e-12) || !(y <= 3800000000.0)) tmp = Float64(1.0 - Float64(x / y)); else tmp = Float64(x / Float64(z - y)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -2.6e-12) || ~((y <= 3800000000.0))) tmp = 1.0 - (x / y); else tmp = x / (z - y); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -2.6e-12], N[Not[LessEqual[y, 3800000000.0]], $MachinePrecision]], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(x / N[(z - y), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.6 \cdot 10^{-12} \lor \neg \left(y \leq 3800000000\right):\\
\;\;\;\;1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{z - y}\\
\end{array}
\end{array}
if y < -2.59999999999999983e-12 or 3.8e9 < y Initial program 100.0%
Taylor expanded in z around 0 78.3%
associate-*r/78.3%
neg-mul-178.3%
sub-neg78.3%
+-commutative78.3%
distribute-neg-in78.3%
remove-double-neg78.3%
sub-neg78.3%
div-sub78.3%
*-inverses78.3%
Simplified78.3%
if -2.59999999999999983e-12 < y < 3.8e9Initial program 100.0%
Taylor expanded in x around inf 82.7%
Final simplification80.6%
(FPCore (x y z) :precision binary64 (if (or (<= y -2.95e-12) (not (<= y 7.2e-14))) (- 1.0 (/ x y)) (/ x z)))
double code(double x, double y, double z) {
double tmp;
if ((y <= -2.95e-12) || !(y <= 7.2e-14)) {
tmp = 1.0 - (x / 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 <= (-2.95d-12)) .or. (.not. (y <= 7.2d-14))) then
tmp = 1.0d0 - (x / 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 <= -2.95e-12) || !(y <= 7.2e-14)) {
tmp = 1.0 - (x / y);
} else {
tmp = x / z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -2.95e-12) or not (y <= 7.2e-14): tmp = 1.0 - (x / y) else: tmp = x / z return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -2.95e-12) || !(y <= 7.2e-14)) tmp = Float64(1.0 - Float64(x / y)); else tmp = Float64(x / z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -2.95e-12) || ~((y <= 7.2e-14))) tmp = 1.0 - (x / y); else tmp = x / z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -2.95e-12], N[Not[LessEqual[y, 7.2e-14]], $MachinePrecision]], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision], N[(x / z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.95 \cdot 10^{-12} \lor \neg \left(y \leq 7.2 \cdot 10^{-14}\right):\\
\;\;\;\;1 - \frac{x}{y}\\
\mathbf{else}:\\
\;\;\;\;\frac{x}{z}\\
\end{array}
\end{array}
if y < -2.95e-12 or 7.1999999999999996e-14 < y Initial program 100.0%
Taylor expanded in z around 0 77.2%
associate-*r/77.2%
neg-mul-177.2%
sub-neg77.2%
+-commutative77.2%
distribute-neg-in77.2%
remove-double-neg77.2%
sub-neg77.2%
div-sub77.2%
*-inverses77.2%
Simplified77.2%
if -2.95e-12 < y < 7.1999999999999996e-14Initial program 100.0%
Taylor expanded in y around 0 76.4%
Final simplification76.8%
(FPCore (x y z) :precision binary64 (if (<= y -2.9e-12) (/ y (- y z)) (if (<= y 8e-12) (/ (- x y) z) (- 1.0 (/ x y)))))
double code(double x, double y, double z) {
double tmp;
if (y <= -2.9e-12) {
tmp = y / (y - z);
} else if (y <= 8e-12) {
tmp = (x - y) / z;
} else {
tmp = 1.0 - (x / 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 <= (-2.9d-12)) then
tmp = y / (y - z)
else if (y <= 8d-12) then
tmp = (x - y) / z
else
tmp = 1.0d0 - (x / y)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= -2.9e-12) {
tmp = y / (y - z);
} else if (y <= 8e-12) {
tmp = (x - y) / z;
} else {
tmp = 1.0 - (x / y);
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -2.9e-12: tmp = y / (y - z) elif y <= 8e-12: tmp = (x - y) / z else: tmp = 1.0 - (x / y) return tmp
function code(x, y, z) tmp = 0.0 if (y <= -2.9e-12) tmp = Float64(y / Float64(y - z)); elseif (y <= 8e-12) tmp = Float64(Float64(x - y) / z); else tmp = Float64(1.0 - Float64(x / y)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -2.9e-12) tmp = y / (y - z); elseif (y <= 8e-12) tmp = (x - y) / z; else tmp = 1.0 - (x / y); end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -2.9e-12], N[(y / N[(y - z), $MachinePrecision]), $MachinePrecision], If[LessEqual[y, 8e-12], N[(N[(x - y), $MachinePrecision] / z), $MachinePrecision], N[(1.0 - N[(x / y), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -2.9 \cdot 10^{-12}:\\
\;\;\;\;\frac{y}{y - z}\\
\mathbf{elif}\;y \leq 8 \cdot 10^{-12}:\\
\;\;\;\;\frac{x - y}{z}\\
\mathbf{else}:\\
\;\;\;\;1 - \frac{x}{y}\\
\end{array}
\end{array}
if y < -2.9000000000000002e-12Initial program 100.0%
Taylor expanded in x around 0 82.7%
neg-mul-182.7%
distribute-neg-frac282.7%
sub-neg82.7%
+-commutative82.7%
distribute-neg-in82.7%
remove-double-neg82.7%
sub-neg82.7%
Simplified82.7%
if -2.9000000000000002e-12 < y < 7.99999999999999984e-12Initial program 100.0%
Taylor expanded in z around inf 87.3%
if 7.99999999999999984e-12 < y Initial program 100.0%
Taylor expanded in z around 0 80.6%
associate-*r/80.6%
neg-mul-180.6%
sub-neg80.6%
+-commutative80.6%
distribute-neg-in80.6%
remove-double-neg80.6%
sub-neg80.6%
div-sub80.6%
*-inverses80.6%
Simplified80.6%
(FPCore (x y z) :precision binary64 (if (<= y -3e-12) 1.0 (if (<= y 80000000.0) (/ x z) 1.0)))
double code(double x, double y, double z) {
double tmp;
if (y <= -3e-12) {
tmp = 1.0;
} else if (y <= 80000000.0) {
tmp = x / z;
} else {
tmp = 1.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) :: tmp
if (y <= (-3d-12)) then
tmp = 1.0d0
else if (y <= 80000000.0d0) then
tmp = x / z
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (y <= -3e-12) {
tmp = 1.0;
} else if (y <= 80000000.0) {
tmp = x / z;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x, y, z): tmp = 0 if y <= -3e-12: tmp = 1.0 elif y <= 80000000.0: tmp = x / z else: tmp = 1.0 return tmp
function code(x, y, z) tmp = 0.0 if (y <= -3e-12) tmp = 1.0; elseif (y <= 80000000.0) tmp = Float64(x / z); else tmp = 1.0; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (y <= -3e-12) tmp = 1.0; elseif (y <= 80000000.0) tmp = x / z; else tmp = 1.0; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[y, -3e-12], 1.0, If[LessEqual[y, 80000000.0], N[(x / z), $MachinePrecision], 1.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -3 \cdot 10^{-12}:\\
\;\;\;\;1\\
\mathbf{elif}\;y \leq 80000000:\\
\;\;\;\;\frac{x}{z}\\
\mathbf{else}:\\
\;\;\;\;1\\
\end{array}
\end{array}
if y < -3.0000000000000001e-12 or 8e7 < y Initial program 100.0%
Taylor expanded in y around inf 63.6%
if -3.0000000000000001e-12 < y < 8e7Initial program 100.0%
Taylor expanded in y around 0 73.3%
(FPCore (x y z) :precision binary64 1.0)
double code(double x, double y, double z) {
return 1.0;
}
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
end function
public static double code(double x, double y, double z) {
return 1.0;
}
def code(x, y, z): return 1.0
function code(x, y, z) return 1.0 end
function tmp = code(x, y, z) tmp = 1.0; end
code[x_, y_, z_] := 1.0
\begin{array}{l}
\\
1
\end{array}
Initial program 100.0%
Taylor expanded in y around inf 34.6%
(FPCore (x y z) :precision binary64 (- (/ x (- z y)) (/ y (- z y))))
double code(double x, double y, double z) {
return (x / (z - y)) - (y / (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 = (x / (z - y)) - (y / (z - y))
end function
public static double code(double x, double y, double z) {
return (x / (z - y)) - (y / (z - y));
}
def code(x, y, z): return (x / (z - y)) - (y / (z - y))
function code(x, y, z) return Float64(Float64(x / Float64(z - y)) - Float64(y / Float64(z - y))) end
function tmp = code(x, y, z) tmp = (x / (z - y)) - (y / (z - y)); end
code[x_, y_, z_] := N[(N[(x / N[(z - y), $MachinePrecision]), $MachinePrecision] - N[(y / N[(z - y), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{z - y} - \frac{y}{z - y}
\end{array}
herbie shell --seed 2024186
(FPCore (x y z)
:name "Graphics.Rasterific.Shading:$sgradientColorAt from Rasterific-0.6.1"
:precision binary64
:alt
(! :herbie-platform default (- (/ x (- z y)) (/ y (- z y))))
(/ (- x y) (- z y)))