
(FPCore (t) :precision binary64 (let* ((t_1 (- 2.0 (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t))))) (t_2 (* t_1 t_1))) (/ (+ 1.0 t_2) (+ 2.0 t_2))))
double code(double t) {
double t_1 = 2.0 - ((2.0 / t) / (1.0 + (1.0 / t)));
double t_2 = t_1 * t_1;
return (1.0 + t_2) / (2.0 + t_2);
}
real(8) function code(t)
real(8), intent (in) :: t
real(8) :: t_1
real(8) :: t_2
t_1 = 2.0d0 - ((2.0d0 / t) / (1.0d0 + (1.0d0 / t)))
t_2 = t_1 * t_1
code = (1.0d0 + t_2) / (2.0d0 + t_2)
end function
public static double code(double t) {
double t_1 = 2.0 - ((2.0 / t) / (1.0 + (1.0 / t)));
double t_2 = t_1 * t_1;
return (1.0 + t_2) / (2.0 + t_2);
}
def code(t): t_1 = 2.0 - ((2.0 / t) / (1.0 + (1.0 / t))) t_2 = t_1 * t_1 return (1.0 + t_2) / (2.0 + t_2)
function code(t) t_1 = Float64(2.0 - Float64(Float64(2.0 / t) / Float64(1.0 + Float64(1.0 / t)))) t_2 = Float64(t_1 * t_1) return Float64(Float64(1.0 + t_2) / Float64(2.0 + t_2)) end
function tmp = code(t) t_1 = 2.0 - ((2.0 / t) / (1.0 + (1.0 / t))); t_2 = t_1 * t_1; tmp = (1.0 + t_2) / (2.0 + t_2); end
code[t_] := Block[{t$95$1 = N[(2.0 - N[(N[(2.0 / t), $MachinePrecision] / N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 * t$95$1), $MachinePrecision]}, N[(N[(1.0 + t$95$2), $MachinePrecision] / N[(2.0 + t$95$2), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := 2 - \frac{\frac{2}{t}}{1 + \frac{1}{t}}\\
t_2 := t\_1 \cdot t\_1\\
\frac{1 + t\_2}{2 + t\_2}
\end{array}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (t) :precision binary64 (let* ((t_1 (- 2.0 (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t))))) (t_2 (* t_1 t_1))) (/ (+ 1.0 t_2) (+ 2.0 t_2))))
double code(double t) {
double t_1 = 2.0 - ((2.0 / t) / (1.0 + (1.0 / t)));
double t_2 = t_1 * t_1;
return (1.0 + t_2) / (2.0 + t_2);
}
real(8) function code(t)
real(8), intent (in) :: t
real(8) :: t_1
real(8) :: t_2
t_1 = 2.0d0 - ((2.0d0 / t) / (1.0d0 + (1.0d0 / t)))
t_2 = t_1 * t_1
code = (1.0d0 + t_2) / (2.0d0 + t_2)
end function
public static double code(double t) {
double t_1 = 2.0 - ((2.0 / t) / (1.0 + (1.0 / t)));
double t_2 = t_1 * t_1;
return (1.0 + t_2) / (2.0 + t_2);
}
def code(t): t_1 = 2.0 - ((2.0 / t) / (1.0 + (1.0 / t))) t_2 = t_1 * t_1 return (1.0 + t_2) / (2.0 + t_2)
function code(t) t_1 = Float64(2.0 - Float64(Float64(2.0 / t) / Float64(1.0 + Float64(1.0 / t)))) t_2 = Float64(t_1 * t_1) return Float64(Float64(1.0 + t_2) / Float64(2.0 + t_2)) end
function tmp = code(t) t_1 = 2.0 - ((2.0 / t) / (1.0 + (1.0 / t))); t_2 = t_1 * t_1; tmp = (1.0 + t_2) / (2.0 + t_2); end
code[t_] := Block[{t$95$1 = N[(2.0 - N[(N[(2.0 / t), $MachinePrecision] / N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 * t$95$1), $MachinePrecision]}, N[(N[(1.0 + t$95$2), $MachinePrecision] / N[(2.0 + t$95$2), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := 2 - \frac{\frac{2}{t}}{1 + \frac{1}{t}}\\
t_2 := t\_1 \cdot t\_1\\
\frac{1 + t\_2}{2 + t\_2}
\end{array}
\end{array}
(FPCore (t) :precision binary64 (let* ((t_1 (+ 2.0 (/ -2.0 (+ t 1.0)))) (t_2 (+ -2.0 (/ 2.0 (+ t 1.0))))) (/ (fma t_1 t_2 -1.0) (fma t_1 t_2 -2.0))))
double code(double t) {
double t_1 = 2.0 + (-2.0 / (t + 1.0));
double t_2 = -2.0 + (2.0 / (t + 1.0));
return fma(t_1, t_2, -1.0) / fma(t_1, t_2, -2.0);
}
function code(t) t_1 = Float64(2.0 + Float64(-2.0 / Float64(t + 1.0))) t_2 = Float64(-2.0 + Float64(2.0 / Float64(t + 1.0))) return Float64(fma(t_1, t_2, -1.0) / fma(t_1, t_2, -2.0)) end
code[t_] := Block[{t$95$1 = N[(2.0 + N[(-2.0 / N[(t + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(-2.0 + N[(2.0 / N[(t + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(N[(t$95$1 * t$95$2 + -1.0), $MachinePrecision] / N[(t$95$1 * t$95$2 + -2.0), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := 2 + \frac{-2}{t + 1}\\
t_2 := -2 + \frac{2}{t + 1}\\
\frac{\mathsf{fma}\left(t\_1, t\_2, -1\right)}{\mathsf{fma}\left(t\_1, t\_2, -2\right)}
\end{array}
\end{array}
Initial program 100.0%
Applied rewrites100.0%
lift-/.f64N/A
lift-fma.f64N/A
lift-/.f64N/A
lift-+.f64N/A
lift-/.f64N/A
lift-fma.f64N/A
lift-/.f64N/A
lift-+.f64N/A
lift-fma.f64N/A
Applied rewrites100.0%
(FPCore (t) :precision binary64 (if (<= (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t))) 2e-20) (+ 0.8333333333333334 (/ -0.2222222222222222 t)) (fma t (fma (* t t) (+ -2.0 t) t) 0.5)))
double code(double t) {
double tmp;
if (((2.0 / t) / (1.0 + (1.0 / t))) <= 2e-20) {
tmp = 0.8333333333333334 + (-0.2222222222222222 / t);
} else {
tmp = fma(t, fma((t * t), (-2.0 + t), t), 0.5);
}
return tmp;
}
function code(t) tmp = 0.0 if (Float64(Float64(2.0 / t) / Float64(1.0 + Float64(1.0 / t))) <= 2e-20) tmp = Float64(0.8333333333333334 + Float64(-0.2222222222222222 / t)); else tmp = fma(t, fma(Float64(t * t), Float64(-2.0 + t), t), 0.5); end return tmp end
code[t_] := If[LessEqual[N[(N[(2.0 / t), $MachinePrecision] / N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2e-20], N[(0.8333333333333334 + N[(-0.2222222222222222 / t), $MachinePrecision]), $MachinePrecision], N[(t * N[(N[(t * t), $MachinePrecision] * N[(-2.0 + t), $MachinePrecision] + t), $MachinePrecision] + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\frac{2}{t}}{1 + \frac{1}{t}} \leq 2 \cdot 10^{-20}:\\
\;\;\;\;0.8333333333333334 + \frac{-0.2222222222222222}{t}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t, \mathsf{fma}\left(t \cdot t, -2 + t, t\right), 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) < 1.99999999999999989e-20Initial program 100.0%
Taylor expanded in t around inf
sub-negN/A
lower-+.f64N/A
associate-*r/N/A
metadata-evalN/A
distribute-neg-fracN/A
lower-/.f64N/A
metadata-eval100.0
Applied rewrites100.0%
if 1.99999999999999989e-20 < (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) Initial program 100.0%
Taylor expanded in t around 0
+-commutativeN/A
unpow2N/A
associate-*l*N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
+-commutativeN/A
distribute-lft-inN/A
associate-*r*N/A
unpow2N/A
*-rgt-identityN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
sub-negN/A
metadata-evalN/A
+-commutativeN/A
lower-+.f6499.7
Applied rewrites99.7%
(FPCore (t) :precision binary64 (if (<= (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t))) 2e-20) (+ 0.8333333333333334 (/ -0.2222222222222222 t)) (fma (* t t) (fma -2.0 t 1.0) 0.5)))
double code(double t) {
double tmp;
if (((2.0 / t) / (1.0 + (1.0 / t))) <= 2e-20) {
tmp = 0.8333333333333334 + (-0.2222222222222222 / t);
} else {
tmp = fma((t * t), fma(-2.0, t, 1.0), 0.5);
}
return tmp;
}
function code(t) tmp = 0.0 if (Float64(Float64(2.0 / t) / Float64(1.0 + Float64(1.0 / t))) <= 2e-20) tmp = Float64(0.8333333333333334 + Float64(-0.2222222222222222 / t)); else tmp = fma(Float64(t * t), fma(-2.0, t, 1.0), 0.5); end return tmp end
code[t_] := If[LessEqual[N[(N[(2.0 / t), $MachinePrecision] / N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2e-20], N[(0.8333333333333334 + N[(-0.2222222222222222 / t), $MachinePrecision]), $MachinePrecision], N[(N[(t * t), $MachinePrecision] * N[(-2.0 * t + 1.0), $MachinePrecision] + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\frac{2}{t}}{1 + \frac{1}{t}} \leq 2 \cdot 10^{-20}:\\
\;\;\;\;0.8333333333333334 + \frac{-0.2222222222222222}{t}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t \cdot t, \mathsf{fma}\left(-2, t, 1\right), 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) < 1.99999999999999989e-20Initial program 100.0%
Taylor expanded in t around inf
sub-negN/A
lower-+.f64N/A
associate-*r/N/A
metadata-evalN/A
distribute-neg-fracN/A
lower-/.f64N/A
metadata-eval100.0
Applied rewrites100.0%
if 1.99999999999999989e-20 < (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) Initial program 100.0%
Taylor expanded in t around 0
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f64N/A
+-commutativeN/A
lower-fma.f6499.5
Applied rewrites99.5%
(FPCore (t) :precision binary64 (if (<= (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t))) 2e-20) (+ 0.8333333333333334 (/ -0.2222222222222222 t)) (fma t t 0.5)))
double code(double t) {
double tmp;
if (((2.0 / t) / (1.0 + (1.0 / t))) <= 2e-20) {
tmp = 0.8333333333333334 + (-0.2222222222222222 / t);
} else {
tmp = fma(t, t, 0.5);
}
return tmp;
}
function code(t) tmp = 0.0 if (Float64(Float64(2.0 / t) / Float64(1.0 + Float64(1.0 / t))) <= 2e-20) tmp = Float64(0.8333333333333334 + Float64(-0.2222222222222222 / t)); else tmp = fma(t, t, 0.5); end return tmp end
code[t_] := If[LessEqual[N[(N[(2.0 / t), $MachinePrecision] / N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2e-20], N[(0.8333333333333334 + N[(-0.2222222222222222 / t), $MachinePrecision]), $MachinePrecision], N[(t * t + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\frac{2}{t}}{1 + \frac{1}{t}} \leq 2 \cdot 10^{-20}:\\
\;\;\;\;0.8333333333333334 + \frac{-0.2222222222222222}{t}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t, t, 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) < 1.99999999999999989e-20Initial program 100.0%
Taylor expanded in t around inf
sub-negN/A
lower-+.f64N/A
associate-*r/N/A
metadata-evalN/A
distribute-neg-fracN/A
lower-/.f64N/A
metadata-eval100.0
Applied rewrites100.0%
if 1.99999999999999989e-20 < (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) Initial program 100.0%
Taylor expanded in t around 0
+-commutativeN/A
unpow2N/A
lower-fma.f6499.2
Applied rewrites99.2%
(FPCore (t) :precision binary64 (if (<= (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t))) 2e-20) 0.8333333333333334 (fma t t 0.5)))
double code(double t) {
double tmp;
if (((2.0 / t) / (1.0 + (1.0 / t))) <= 2e-20) {
tmp = 0.8333333333333334;
} else {
tmp = fma(t, t, 0.5);
}
return tmp;
}
function code(t) tmp = 0.0 if (Float64(Float64(2.0 / t) / Float64(1.0 + Float64(1.0 / t))) <= 2e-20) tmp = 0.8333333333333334; else tmp = fma(t, t, 0.5); end return tmp end
code[t_] := If[LessEqual[N[(N[(2.0 / t), $MachinePrecision] / N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 2e-20], 0.8333333333333334, N[(t * t + 0.5), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\frac{2}{t}}{1 + \frac{1}{t}} \leq 2 \cdot 10^{-20}:\\
\;\;\;\;0.8333333333333334\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(t, t, 0.5\right)\\
\end{array}
\end{array}
if (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) < 1.99999999999999989e-20Initial program 100.0%
Taylor expanded in t around inf
Applied rewrites99.7%
if 1.99999999999999989e-20 < (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) Initial program 100.0%
Taylor expanded in t around 0
+-commutativeN/A
unpow2N/A
lower-fma.f6499.2
Applied rewrites99.2%
(FPCore (t) :precision binary64 (if (<= (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t))) 1.0) 0.8333333333333334 0.5))
double code(double t) {
double tmp;
if (((2.0 / t) / (1.0 + (1.0 / t))) <= 1.0) {
tmp = 0.8333333333333334;
} else {
tmp = 0.5;
}
return tmp;
}
real(8) function code(t)
real(8), intent (in) :: t
real(8) :: tmp
if (((2.0d0 / t) / (1.0d0 + (1.0d0 / t))) <= 1.0d0) then
tmp = 0.8333333333333334d0
else
tmp = 0.5d0
end if
code = tmp
end function
public static double code(double t) {
double tmp;
if (((2.0 / t) / (1.0 + (1.0 / t))) <= 1.0) {
tmp = 0.8333333333333334;
} else {
tmp = 0.5;
}
return tmp;
}
def code(t): tmp = 0 if ((2.0 / t) / (1.0 + (1.0 / t))) <= 1.0: tmp = 0.8333333333333334 else: tmp = 0.5 return tmp
function code(t) tmp = 0.0 if (Float64(Float64(2.0 / t) / Float64(1.0 + Float64(1.0 / t))) <= 1.0) tmp = 0.8333333333333334; else tmp = 0.5; end return tmp end
function tmp_2 = code(t) tmp = 0.0; if (((2.0 / t) / (1.0 + (1.0 / t))) <= 1.0) tmp = 0.8333333333333334; else tmp = 0.5; end tmp_2 = tmp; end
code[t_] := If[LessEqual[N[(N[(2.0 / t), $MachinePrecision] / N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1.0], 0.8333333333333334, 0.5]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{\frac{2}{t}}{1 + \frac{1}{t}} \leq 1:\\
\;\;\;\;0.8333333333333334\\
\mathbf{else}:\\
\;\;\;\;0.5\\
\end{array}
\end{array}
if (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) < 1Initial program 100.0%
Taylor expanded in t around inf
Applied rewrites99.7%
if 1 < (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) Initial program 100.0%
Taylor expanded in t around 0
Applied rewrites98.6%
(FPCore (t) :precision binary64 0.5)
double code(double t) {
return 0.5;
}
real(8) function code(t)
real(8), intent (in) :: t
code = 0.5d0
end function
public static double code(double t) {
return 0.5;
}
def code(t): return 0.5
function code(t) return 0.5 end
function tmp = code(t) tmp = 0.5; end
code[t_] := 0.5
\begin{array}{l}
\\
0.5
\end{array}
Initial program 100.0%
Taylor expanded in t around 0
Applied rewrites59.4%
herbie shell --seed 2024214
(FPCore (t)
:name "Kahan p13 Example 2"
:precision binary64
(/ (+ 1.0 (* (- 2.0 (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t)))) (- 2.0 (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t)))))) (+ 2.0 (* (- 2.0 (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t)))) (- 2.0 (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t))))))))