
(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 -2.6e+109)
x
(if (<= z -5.4e+42)
(/ y z)
(if (<= z -0.22)
x
(if (<= z 3.9e-292)
t_0
(if (<= z 1.75e-193)
(/ y z)
(if (<= z 9.5e-133) t_0 (if (<= z 2.7e+59) (/ y z) x)))))))))
double code(double x, double y, double z) {
double t_0 = x / -z;
double tmp;
if (z <= -2.6e+109) {
tmp = x;
} else if (z <= -5.4e+42) {
tmp = y / z;
} else if (z <= -0.22) {
tmp = x;
} else if (z <= 3.9e-292) {
tmp = t_0;
} else if (z <= 1.75e-193) {
tmp = y / z;
} else if (z <= 9.5e-133) {
tmp = t_0;
} else if (z <= 2.7e+59) {
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 <= (-2.6d+109)) then
tmp = x
else if (z <= (-5.4d+42)) then
tmp = y / z
else if (z <= (-0.22d0)) then
tmp = x
else if (z <= 3.9d-292) then
tmp = t_0
else if (z <= 1.75d-193) then
tmp = y / z
else if (z <= 9.5d-133) then
tmp = t_0
else if (z <= 2.7d+59) 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 <= -2.6e+109) {
tmp = x;
} else if (z <= -5.4e+42) {
tmp = y / z;
} else if (z <= -0.22) {
tmp = x;
} else if (z <= 3.9e-292) {
tmp = t_0;
} else if (z <= 1.75e-193) {
tmp = y / z;
} else if (z <= 9.5e-133) {
tmp = t_0;
} else if (z <= 2.7e+59) {
tmp = y / z;
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): t_0 = x / -z tmp = 0 if z <= -2.6e+109: tmp = x elif z <= -5.4e+42: tmp = y / z elif z <= -0.22: tmp = x elif z <= 3.9e-292: tmp = t_0 elif z <= 1.75e-193: tmp = y / z elif z <= 9.5e-133: tmp = t_0 elif z <= 2.7e+59: tmp = y / z else: tmp = x return tmp
function code(x, y, z) t_0 = Float64(x / Float64(-z)) tmp = 0.0 if (z <= -2.6e+109) tmp = x; elseif (z <= -5.4e+42) tmp = Float64(y / z); elseif (z <= -0.22) tmp = x; elseif (z <= 3.9e-292) tmp = t_0; elseif (z <= 1.75e-193) tmp = Float64(y / z); elseif (z <= 9.5e-133) tmp = t_0; elseif (z <= 2.7e+59) 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 <= -2.6e+109) tmp = x; elseif (z <= -5.4e+42) tmp = y / z; elseif (z <= -0.22) tmp = x; elseif (z <= 3.9e-292) tmp = t_0; elseif (z <= 1.75e-193) tmp = y / z; elseif (z <= 9.5e-133) tmp = t_0; elseif (z <= 2.7e+59) 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, -2.6e+109], x, If[LessEqual[z, -5.4e+42], N[(y / z), $MachinePrecision], If[LessEqual[z, -0.22], x, If[LessEqual[z, 3.9e-292], t$95$0, If[LessEqual[z, 1.75e-193], N[(y / z), $MachinePrecision], If[LessEqual[z, 9.5e-133], t$95$0, If[LessEqual[z, 2.7e+59], N[(y / z), $MachinePrecision], x]]]]]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{x}{-z}\\
\mathbf{if}\;z \leq -2.6 \cdot 10^{+109}:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq -5.4 \cdot 10^{+42}:\\
\;\;\;\;\frac{y}{z}\\
\mathbf{elif}\;z \leq -0.22:\\
\;\;\;\;x\\
\mathbf{elif}\;z \leq 3.9 \cdot 10^{-292}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 1.75 \cdot 10^{-193}:\\
\;\;\;\;\frac{y}{z}\\
\mathbf{elif}\;z \leq 9.5 \cdot 10^{-133}:\\
\;\;\;\;t\_0\\
\mathbf{elif}\;z \leq 2.7 \cdot 10^{+59}:\\
\;\;\;\;\frac{y}{z}\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if z < -2.5999999999999998e109 or -5.4000000000000001e42 < z < -0.220000000000000001 or 2.7000000000000001e59 < 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 72.2%
if -2.5999999999999998e109 < z < -5.4000000000000001e42 or 3.9e-292 < z < 1.75000000000000002e-193 or 9.4999999999999992e-133 < z < 2.7000000000000001e59Initial program 100.0%
div-sub98.4%
sub-neg98.4%
distribute-frac-neg98.4%
+-commutative98.4%
associate-+r+98.4%
distribute-frac-neg98.4%
sub-neg98.4%
associate--r-98.4%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around 0 68.1%
if -0.220000000000000001 < z < 3.9e-292 or 1.75000000000000002e-193 < z < 9.4999999999999992e-133Initial program 99.9%
div-sub98.7%
sub-neg98.7%
distribute-frac-neg98.7%
+-commutative98.7%
associate-+r+98.7%
distribute-frac-neg98.7%
sub-neg98.7%
associate--r-98.7%
div-sub99.9%
Simplified99.9%
Taylor expanded in x around inf 70.3%
distribute-rgt-out--70.3%
*-lft-identity70.3%
associate-*l/70.4%
*-lft-identity70.4%
Simplified70.4%
Taylor expanded in z around 0 67.3%
mul-1-neg67.3%
distribute-frac-neg267.3%
Simplified67.3%
(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 98.5%
neg-mul-198.5%
distribute-neg-frac298.5%
Simplified98.5%
if -1 < z < 1Initial program 99.9%
div-sub98.4%
sub-neg98.4%
distribute-frac-neg98.4%
+-commutative98.4%
associate-+r+98.4%
distribute-frac-neg98.4%
sub-neg98.4%
associate--r-98.4%
div-sub99.9%
Simplified99.9%
Taylor expanded in z around 0 96.3%
Final simplification97.4%
(FPCore (x y z) :precision binary64 (if (or (<= x -9.2e+35) (not (<= x 8e-151))) (- x (/ x z)) (/ (- y x) z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -9.2e+35) || !(x <= 8e-151)) {
tmp = x - (x / 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 ((x <= (-9.2d+35)) .or. (.not. (x <= 8d-151))) then
tmp = x - (x / 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 ((x <= -9.2e+35) || !(x <= 8e-151)) {
tmp = x - (x / z);
} else {
tmp = (y - x) / z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -9.2e+35) or not (x <= 8e-151): tmp = x - (x / z) else: tmp = (y - x) / z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -9.2e+35) || !(x <= 8e-151)) tmp = Float64(x - Float64(x / z)); else tmp = Float64(Float64(y - x) / z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -9.2e+35) || ~((x <= 8e-151))) tmp = x - (x / z); else tmp = (y - x) / z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -9.2e+35], N[Not[LessEqual[x, 8e-151]], $MachinePrecision]], N[(x - N[(x / z), $MachinePrecision]), $MachinePrecision], N[(N[(y - x), $MachinePrecision] / z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -9.2 \cdot 10^{+35} \lor \neg \left(x \leq 8 \cdot 10^{-151}\right):\\
\;\;\;\;x - \frac{x}{z}\\
\mathbf{else}:\\
\;\;\;\;\frac{y - x}{z}\\
\end{array}
\end{array}
if x < -9.1999999999999993e35 or 7.9999999999999995e-151 < x Initial program 100.0%
div-sub99.3%
sub-neg99.3%
distribute-frac-neg99.3%
+-commutative99.3%
associate-+r+99.3%
distribute-frac-neg99.3%
sub-neg99.3%
associate--r-99.3%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around inf 85.1%
distribute-rgt-out--85.1%
*-lft-identity85.1%
associate-*l/85.1%
*-lft-identity85.1%
Simplified85.1%
if -9.1999999999999993e35 < x < 7.9999999999999995e-151Initial program 100.0%
div-sub99.0%
sub-neg99.0%
distribute-frac-neg99.0%
+-commutative99.0%
associate-+r+99.0%
distribute-frac-neg99.0%
sub-neg99.0%
associate--r-99.0%
div-sub100.0%
Simplified100.0%
Taylor expanded in z around 0 84.1%
Final simplification84.7%
(FPCore (x y z) :precision binary64 (if (or (<= x -7.2e-117) (not (<= x 1.65e-150))) (- x (/ x z)) (/ y z)))
double code(double x, double y, double z) {
double tmp;
if ((x <= -7.2e-117) || !(x <= 1.65e-150)) {
tmp = x - (x / z);
} else {
tmp = y / 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 <= (-7.2d-117)) .or. (.not. (x <= 1.65d-150))) then
tmp = x - (x / z)
else
tmp = y / z
end if
code = tmp
end function
public static double code(double x, double y, double z) {
double tmp;
if ((x <= -7.2e-117) || !(x <= 1.65e-150)) {
tmp = x - (x / z);
} else {
tmp = y / z;
}
return tmp;
}
def code(x, y, z): tmp = 0 if (x <= -7.2e-117) or not (x <= 1.65e-150): tmp = x - (x / z) else: tmp = y / z return tmp
function code(x, y, z) tmp = 0.0 if ((x <= -7.2e-117) || !(x <= 1.65e-150)) tmp = Float64(x - Float64(x / z)); else tmp = Float64(y / z); end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if ((x <= -7.2e-117) || ~((x <= 1.65e-150))) tmp = x - (x / z); else tmp = y / z; end tmp_2 = tmp; end
code[x_, y_, z_] := If[Or[LessEqual[x, -7.2e-117], N[Not[LessEqual[x, 1.65e-150]], $MachinePrecision]], N[(x - N[(x / z), $MachinePrecision]), $MachinePrecision], N[(y / z), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -7.2 \cdot 10^{-117} \lor \neg \left(x \leq 1.65 \cdot 10^{-150}\right):\\
\;\;\;\;x - \frac{x}{z}\\
\mathbf{else}:\\
\;\;\;\;\frac{y}{z}\\
\end{array}
\end{array}
if x < -7.2000000000000001e-117 or 1.6500000000000001e-150 < x Initial program 100.0%
div-sub98.9%
sub-neg98.9%
distribute-frac-neg98.9%
+-commutative98.9%
associate-+r+98.9%
distribute-frac-neg98.9%
sub-neg98.9%
associate--r-98.9%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around inf 81.2%
distribute-rgt-out--81.2%
*-lft-identity81.2%
associate-*l/81.3%
*-lft-identity81.3%
Simplified81.3%
if -7.2000000000000001e-117 < x < 1.6500000000000001e-150Initial 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 82.9%
Final simplification81.7%
(FPCore (x y z) :precision binary64 (if (<= x -1.2e+36) x (if (<= x 1.65e-150) (/ y z) x)))
double code(double x, double y, double z) {
double tmp;
if (x <= -1.2e+36) {
tmp = x;
} else if (x <= 1.65e-150) {
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 (x <= (-1.2d+36)) then
tmp = x
else if (x <= 1.65d-150) 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 (x <= -1.2e+36) {
tmp = x;
} else if (x <= 1.65e-150) {
tmp = y / z;
} else {
tmp = x;
}
return tmp;
}
def code(x, y, z): tmp = 0 if x <= -1.2e+36: tmp = x elif x <= 1.65e-150: tmp = y / z else: tmp = x return tmp
function code(x, y, z) tmp = 0.0 if (x <= -1.2e+36) tmp = x; elseif (x <= 1.65e-150) tmp = Float64(y / z); else tmp = x; end return tmp end
function tmp_2 = code(x, y, z) tmp = 0.0; if (x <= -1.2e+36) tmp = x; elseif (x <= 1.65e-150) tmp = y / z; else tmp = x; end tmp_2 = tmp; end
code[x_, y_, z_] := If[LessEqual[x, -1.2e+36], x, If[LessEqual[x, 1.65e-150], N[(y / z), $MachinePrecision], x]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1.2 \cdot 10^{+36}:\\
\;\;\;\;x\\
\mathbf{elif}\;x \leq 1.65 \cdot 10^{-150}:\\
\;\;\;\;\frac{y}{z}\\
\mathbf{else}:\\
\;\;\;\;x\\
\end{array}
\end{array}
if x < -1.19999999999999996e36 or 1.6500000000000001e-150 < x Initial program 100.0%
div-sub99.3%
sub-neg99.3%
distribute-frac-neg99.3%
+-commutative99.3%
associate-+r+99.3%
distribute-frac-neg99.3%
sub-neg99.3%
associate--r-99.3%
div-sub100.0%
Simplified100.0%
Taylor expanded in z around inf 46.9%
if -1.19999999999999996e36 < x < 1.6500000000000001e-150Initial program 100.0%
div-sub99.0%
sub-neg99.0%
distribute-frac-neg99.0%
+-commutative99.0%
associate-+r+99.0%
distribute-frac-neg99.0%
sub-neg99.0%
associate--r-99.0%
div-sub100.0%
Simplified100.0%
Taylor expanded in x around 0 69.7%
(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-sub99.2%
sub-neg99.2%
distribute-frac-neg99.2%
+-commutative99.2%
associate-+r+99.2%
distribute-frac-neg99.2%
sub-neg99.2%
associate--r-99.2%
div-sub100.0%
Simplified100.0%
Taylor expanded in z around inf 34.8%
herbie shell --seed 2024108
(FPCore (x y z)
:name "Statistics.Sample:$swelfordMean from math-functions-0.1.5.2"
:precision binary64
(+ x (/ (- y x) z)))