
(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 + \frac{y - x}{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 + \frac{y - x}{z}
\end{array}
(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 + \frac{y - x}{z}
\end{array}
Initial program 100.0%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (/ x (- z))))
(if (<= z -1.0)
x
(if (<= z 1.5e-300)
t_0
(if (<= z 3.15e-246)
(/ y z)
(if (<= z 8.5e-40) t_0 (if (<= z 6e+68) (/ y z) x)))))))
double code(double x, double y, double z) {
double t_0 = x / -z;
double tmp;
if (z <= -1.0) {
tmp = x;
} else if (z <= 1.5e-300) {
tmp = t_0;
} else if (z <= 3.15e-246) {
tmp = y / z;
} else if (z <= 8.5e-40) {
tmp = t_0;
} else if (z <= 6e+68) {
tmp = y / z;
} else {
tmp = x;
}
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 = x / -z
if (z <= (-1.0d0)) then
tmp = x
else if (z <= 1.5d-300) then
tmp = t_0
else if (z <= 3.15d-246) then
tmp = y / z
else if (z <= 8.5d-40) then
tmp = t_0
else if (z <= 6d+68) then
tmp = y / z
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = x / -z;
double tmp;
if (z <= -1.0) {
tmp = x;
} else if (z <= 1.5e-300) {
tmp = t_0;
} else if (z <= 3.15e-246) {
tmp = y / z;
} else if (z <= 8.5e-40) {
tmp = t_0;
} else if (z <= 6e+68) {
tmp = y / z;
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): t_0 = x / -z tmp = 0 if z <= -1.0: tmp = x elif z <= 1.5e-300: tmp = t_0 elif z <= 3.15e-246: tmp = y / z elif z <= 8.5e-40: tmp = t_0 elif z <= 6e+68: tmp = y / z else: tmp = x return tmp
function code(x, y, z) t_0 = Float64(x / Float64(-z)) tmp = 0.0 if (z <= -1.0) tmp = x; elseif (z <= 1.5e-300) tmp = t_0; elseif (z <= 3.15e-246) tmp = Float64(y / z); elseif (z <= 8.5e-40) tmp = t_0; elseif (z <= 6e+68) tmp = Float64(y / z); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) t_0 = x / -z; tmp = 0.0; if (z <= -1.0) tmp = x; elseif (z <= 1.5e-300) tmp = t_0; elseif (z <= 3.15e-246) tmp = y / z; elseif (z <= 8.5e-40) tmp = t_0; elseif (z <= 6e+68) tmp = y / z; else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(x / (-z)), $MachinePrecision]}, If[LessEqual[z, -1.0], x, If[LessEqual[z, 1.5e-300], t$95$0, If[LessEqual[z, 3.15e-246], N[(y / z), $MachinePrecision], If[LessEqual[z, 8.5e-40], t$95$0, If[LessEqual[z, 6e+68], N[(y / z), $MachinePrecision], x]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x}{-z}\\
\mathbf{if}\;z \leq -1:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq 1.5 \cdot 10^{-300}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 3.15 \cdot 10^{-246}:\\
\;\;\;\;\frac{y}{z}\\
\mathbf{elif}\;z \leq 8.5 \cdot 10^{-40}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 6 \cdot 10^{+68}:\\
\;\;\;\;\frac{y}{z}\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if z < -1 or 6.0000000000000004e68 < z Initial program 100.0%
div-sub100.0%
sub-neg100.0%
distribute-frac-neg100.0%
+-commutative100.0%
associate-+r+100.0%
distribute-frac-neg100.0%
sub-neg100.0%
associate--r-100.0%
div-sub100.0%
Simplified100.0%
Taylor expanded in z around inf 79.9%
if -1 < z < 1.50000000000000012e-300 or 3.15000000000000004e-246 < z < 8.4999999999999998e-40Initial program 100.0%
div-sub97.3%
sub-neg97.3%
distribute-frac-neg97.3%
+-commutative97.3%
associate-+r+97.3%
distribute-frac-neg97.3%
sub-neg97.3%
associate--r-97.3%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around inf 63.3%
Taylor expanded in z around 0 62.1%
associate-*r/62.1%
neg-mul-162.1%
Simplified62.1%
if 1.50000000000000012e-300 < z < 3.15000000000000004e-246 or 8.4999999999999998e-40 < z < 6.0000000000000004e68Initial program 100.0%
div-sub91.1%
sub-neg91.1%
distribute-frac-neg91.1%
+-commutative91.1%
associate-+r+91.1%
distribute-frac-neg91.1%
sub-neg91.1%
associate--r-91.1%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around 0 68.2%
Final simplification70.6%
(FPCore (x y z)
:precision binary64
(let* ((t_0 (+ x (/ y z))) (t_1 (/ x (- z))))
(if (<= z -3.1e-10)
t_0
(if (<= z 1.32e-304)
t_1
(if (<= z 1e-243) (/ y z) (if (<= z 1.5e-41) t_1 t_0))))))
double code(double x, double y, double z) {
double t_0 = x + (y / z);
double t_1 = x / -z;
double tmp;
if (z <= -3.1e-10) {
tmp = t_0;
} else if (z <= 1.32e-304) {
tmp = t_1;
} else if (z <= 1e-243) {
tmp = y / z;
} else if (z <= 1.5e-41) {
tmp = t_1;
} 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) :: t_1
real(8) :: tmp
t_0 = x + (y / z)
t_1 = x / -z
if (z <= (-3.1d-10)) then
tmp = t_0
else if (z <= 1.32d-304) then
tmp = t_1
else if (z <= 1d-243) then
tmp = y / z
else if (z <= 1.5d-41) then
tmp = t_1
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double t_0 = x + (y / z);
double t_1 = x / -z;
double tmp;
if (z <= -3.1e-10) {
tmp = t_0;
} else if (z <= 1.32e-304) {
tmp = t_1;
} else if (z <= 1e-243) {
tmp = y / z;
} else if (z <= 1.5e-41) {
tmp = t_1;
} else {
tmp = t_0;
}
return tmp;
}
def code(x, y, z): t_0 = x + (y / z) t_1 = x / -z tmp = 0 if z <= -3.1e-10: tmp = t_0 elif z <= 1.32e-304: tmp = t_1 elif z <= 1e-243: tmp = y / z elif z <= 1.5e-41: tmp = t_1 else: tmp = t_0 return tmp
function code(x, y, z) t_0 = Float64(x + Float64(y / z)) t_1 = Float64(x / Float64(-z)) tmp = 0.0 if (z <= -3.1e-10) tmp = t_0; elseif (z <= 1.32e-304) tmp = t_1; elseif (z <= 1e-243) tmp = Float64(y / z); elseif (z <= 1.5e-41) tmp = t_1; else tmp = t_0; end return tmp end
function tmp_2 = code(x, y, z) t_0 = x + (y / z); t_1 = x / -z; tmp = 0.0; if (z <= -3.1e-10) tmp = t_0; elseif (z <= 1.32e-304) tmp = t_1; elseif (z <= 1e-243) tmp = y / z; elseif (z <= 1.5e-41) tmp = t_1; else tmp = t_0; end tmp_2 = tmp; end
code[x_, y_, z_] := Block[{t$95$0 = N[(x + N[(y / z), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(x / (-z)), $MachinePrecision]}, If[LessEqual[z, -3.1e-10], t$95$0, If[LessEqual[z, 1.32e-304], t$95$1, If[LessEqual[z, 1e-243], N[(y / z), $MachinePrecision], If[LessEqual[z, 1.5e-41], t$95$1, t$95$0]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := x + \frac{y}{z}\\
t_1 := \frac{x}{-z}\\
\mathbf{if}\;z \leq -3.1 \cdot 10^{-10}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 1.32 \cdot 10^{-304}:\\
\;\;\;\;t\_1\\
\mathbf{elif}\;z \leq 10^{-243}:\\
\;\;\;\;\frac{y}{z}\\
\mathbf{elif}\;z \leq 1.5 \cdot 10^{-41}:\\
\;\;\;\;t\_1\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if z < -3.10000000000000015e-10 or 1.49999999999999994e-41 < z Initial program 100.0%
div-sub100.0%
sub-neg100.0%
distribute-frac-neg100.0%
+-commutative100.0%
associate-+r+100.0%
distribute-frac-neg100.0%
sub-neg100.0%
associate--r-100.0%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around 0 97.0%
neg-mul-197.0%
distribute-neg-frac297.0%
Simplified97.0%
Taylor expanded in y around 0 97.0%
if -3.10000000000000015e-10 < z < 1.31999999999999993e-304 or 9.99999999999999995e-244 < z < 1.49999999999999994e-41Initial program 100.0%
div-sub97.1%
sub-neg97.1%
distribute-frac-neg97.1%
+-commutative97.1%
associate-+r+97.1%
distribute-frac-neg97.1%
sub-neg97.1%
associate--r-97.1%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around inf 64.3%
Taylor expanded in z around 0 63.9%
associate-*r/63.9%
neg-mul-163.9%
Simplified63.9%
if 1.31999999999999993e-304 < z < 9.99999999999999995e-244Initial program 99.9%
div-sub79.9%
sub-neg79.9%
distribute-frac-neg79.9%
+-commutative79.9%
associate-+r+79.9%
distribute-frac-neg79.9%
sub-neg79.9%
associate--r-79.9%
div-sub99.9%
Simplified99.9%
Taylor expanded in x around 0 66.5%
Final simplification81.5%
(FPCore (x y z) :precision binary64 (if (or (<= z -1.0) (not (<= z 1.0))) (+ x (/ y z)) (/ (- y x) z)))
double code(double x, double y, double z) {
double tmp;
if ((z <= -1.0) || !(z <= 1.0)) {
tmp = x + (y / z);
} else {
tmp = (y - 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 ((z <= (-1.0d0)) .or. (.not. (z <= 1.0d0))) then
tmp = x + (y / z)
else
tmp = (y - x) / z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((z <= -1.0) || !(z <= 1.0)) {
tmp = x + (y / z);
} else {
tmp = (y - x) / z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (z <= -1.0) or not (z <= 1.0): tmp = x + (y / z) else: tmp = (y - x) / z return tmp
function code(x, y, z) tmp = 0.0 if ((z <= -1.0) || !(z <= 1.0)) tmp = Float64(x + Float64(y / z)); else tmp = Float64(Float64(y - x) / z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((z <= -1.0) || ~((z <= 1.0))) tmp = x + (y / z); else tmp = (y - x) / z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[z, -1.0], N[Not[LessEqual[z, 1.0]], $MachinePrecision]], N[(x + N[(y / z), $MachinePrecision]), $MachinePrecision], N[(N[(y - x), $MachinePrecision] / z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1 \lor \neg \left(z \leq 1\right):\\
\;\;\;\;x + \frac{y}{z}\\
\mathbf{else}:\\
\;\;\;\;\frac{y - x}{z}\\
\end{array}
\end{array}
if z < -1 or 1 < z Initial program 100.0%
div-sub100.0%
sub-neg100.0%
distribute-frac-neg100.0%
+-commutative100.0%
associate-+r+100.0%
distribute-frac-neg100.0%
sub-neg100.0%
associate--r-100.0%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around 0 99.8%
neg-mul-199.8%
distribute-neg-frac299.8%
Simplified99.8%
Taylor expanded in y around 0 99.8%
if -1 < z < 1Initial program 100.0%
div-sub95.5%
sub-neg95.5%
distribute-frac-neg95.5%
+-commutative95.5%
associate-+r+95.5%
distribute-frac-neg95.5%
sub-neg95.5%
associate--r-95.5%
div-sub100.0%
Simplified100.0%
Taylor expanded in z around 0 98.4%
Final simplification99.1%
(FPCore (x y z) :precision binary64 (if (or (<= y -5e+19) (not (<= y 4.5e+30))) (+ x (/ y z)) (- x (/ x z))))
double code(double x, double y, double z) {
double tmp;
if ((y <= -5e+19) || !(y <= 4.5e+30)) {
tmp = x + (y / z);
} else {
tmp = x - (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 <= (-5d+19)) .or. (.not. (y <= 4.5d+30))) then
tmp = x + (y / z)
else
tmp = x - (x / z)
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((y <= -5e+19) || !(y <= 4.5e+30)) {
tmp = x + (y / z);
} else {
tmp = x - (x / z);
}
return tmp;
}
def code(x, y, z): tmp = 0 if (y <= -5e+19) or not (y <= 4.5e+30): tmp = x + (y / z) else: tmp = x - (x / z) return tmp
function code(x, y, z) tmp = 0.0 if ((y <= -5e+19) || !(y <= 4.5e+30)) tmp = Float64(x + Float64(y / z)); else tmp = Float64(x - Float64(x / z)); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((y <= -5e+19) || ~((y <= 4.5e+30))) tmp = x + (y / z); else tmp = x - (x / z); end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[y, -5e+19], N[Not[LessEqual[y, 4.5e+30]], $MachinePrecision]], N[(x + N[(y / z), $MachinePrecision]), $MachinePrecision], N[(x - N[(x / z), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;y \leq -5 \cdot 10^{+19} \lor \neg \left(y \leq 4.5 \cdot 10^{+30}\right):\\
\;\;\;\;x + \frac{y}{z}\\
\mathbf{else}:\\
\;\;\;\;x - \frac{x}{z}\\
\end{array}
\end{array}
if y < -5e19 or 4.49999999999999995e30 < y Initial program 100.0%
div-sub94.9%
sub-neg94.9%
distribute-frac-neg94.9%
+-commutative94.9%
associate-+r+94.9%
distribute-frac-neg94.9%
sub-neg94.9%
associate--r-94.9%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around 0 92.9%
neg-mul-192.9%
distribute-neg-frac292.9%
Simplified92.9%
Taylor expanded in y around 0 92.9%
if -5e19 < y < 4.49999999999999995e30Initial program 100.0%
div-sub100.0%
sub-neg100.0%
distribute-frac-neg100.0%
+-commutative100.0%
associate-+r+100.0%
distribute-frac-neg100.0%
sub-neg100.0%
associate--r-100.0%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around inf 86.4%
Final simplification89.3%
(FPCore (x y z) :precision binary64 (if (<= z -1.22e+64) x (if (<= z 5e+68) (/ y z) x)))
double code(double x, double y, double z) {
double tmp;
if (z <= -1.22e+64) {
tmp = x;
} else if (z <= 5e+68) {
tmp = y / z;
} else {
tmp = x;
}
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 <= (-1.22d+64)) then
tmp = x
else if (z <= 5d+68) then
tmp = y / z
else
tmp = x
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if (z <= -1.22e+64) {
tmp = x;
} else if (z <= 5e+68) {
tmp = y / z;
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if z <= -1.22e+64: tmp = x elif z <= 5e+68: tmp = y / z else: tmp = x return tmp
function code(x, y, z) tmp = 0.0 if (z <= -1.22e+64) tmp = x; elseif (z <= 5e+68) tmp = Float64(y / z); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (z <= -1.22e+64) tmp = x; elseif (z <= 5e+68) tmp = y / z; else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[z, -1.22e+64], x, If[LessEqual[z, 5e+68], N[(y / z), $MachinePrecision], x]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;z \leq -1.22 \cdot 10^{+64}:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq 5 \cdot 10^{+68}:\\
\;\;\;\;\frac{y}{z}\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if z < -1.21999999999999994e64 or 5.0000000000000004e68 < z Initial program 100.0%
div-sub100.0%
sub-neg100.0%
distribute-frac-neg100.0%
+-commutative100.0%
associate-+r+100.0%
distribute-frac-neg100.0%
sub-neg100.0%
associate--r-100.0%
div-sub100.0%
Simplified100.0%
Taylor expanded in z around inf 84.3%
if -1.21999999999999994e64 < z < 5.0000000000000004e68Initial program 100.0%
div-sub96.2%
sub-neg96.2%
distribute-frac-neg96.2%
+-commutative96.2%
associate-+r+96.2%
distribute-frac-neg96.2%
sub-neg96.2%
associate--r-96.2%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around 0 49.1%
(FPCore (x y z) :precision binary64 x)
double code(double x, double y, double z) {
return x;
}
real(8) function code(x, y, z)
real(8), intent (in) :: x
real(8), intent (in) :: y
real(8), intent (in) :: z
code = x
end function
public static double code(double x, double y, double z) {
return x;
}
def code(x, y, z): return x
function code(x, y, z) return x end
function tmp = code(x, y, z) tmp = x; end
code[x_, y_, z_] := x
\begin{array}{l}
\\
x
\end{array}
Initial program 100.0%
div-sub97.6%
sub-neg97.6%
distribute-frac-neg97.6%
+-commutative97.6%
associate-+r+97.6%
distribute-frac-neg97.6%
sub-neg97.6%
associate--r-97.6%
div-sub100.0%
Simplified100.0%
Taylor expanded in z around inf 38.1%
herbie shell --seed 2024087
(FPCore (x y z)
:name "Statistics.Sample:$swelfordMean from math-functions-0.1.5.2"
:precision binary64
(+ x (/ (- y x) z)))