
(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 8 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 (/ 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}
Initial program 100.0%
(FPCore (t)
:precision binary64
(let* ((t_1 (+ 1.0 (/ 1.0 t))) (t_2 (/ (/ 2.0 t) t_1)))
(if (<= t_2 0.1)
(+ 1.0 (- 2.0 (* (- 2.0 t_2) t_1)))
(/ t_2 (+ 2.0 (- 2.0 (/ t_2 t_1)))))))
double code(double t) {
double t_1 = 1.0 + (1.0 / t);
double t_2 = (2.0 / t) / t_1;
double tmp;
if (t_2 <= 0.1) {
tmp = 1.0 + (2.0 - ((2.0 - t_2) * t_1));
} else {
tmp = t_2 / (2.0 + (2.0 - (t_2 / t_1)));
}
return tmp;
}
real(8) function code(t)
real(8), intent (in) :: t
real(8) :: t_1
real(8) :: t_2
real(8) :: tmp
t_1 = 1.0d0 + (1.0d0 / t)
t_2 = (2.0d0 / t) / t_1
if (t_2 <= 0.1d0) then
tmp = 1.0d0 + (2.0d0 - ((2.0d0 - t_2) * t_1))
else
tmp = t_2 / (2.0d0 + (2.0d0 - (t_2 / t_1)))
end if
code = tmp
end function
public static double code(double t) {
double t_1 = 1.0 + (1.0 / t);
double t_2 = (2.0 / t) / t_1;
double tmp;
if (t_2 <= 0.1) {
tmp = 1.0 + (2.0 - ((2.0 - t_2) * t_1));
} else {
tmp = t_2 / (2.0 + (2.0 - (t_2 / t_1)));
}
return tmp;
}
def code(t): t_1 = 1.0 + (1.0 / t) t_2 = (2.0 / t) / t_1 tmp = 0 if t_2 <= 0.1: tmp = 1.0 + (2.0 - ((2.0 - t_2) * t_1)) else: tmp = t_2 / (2.0 + (2.0 - (t_2 / t_1))) return tmp
function code(t) t_1 = Float64(1.0 + Float64(1.0 / t)) t_2 = Float64(Float64(2.0 / t) / t_1) tmp = 0.0 if (t_2 <= 0.1) tmp = Float64(1.0 + Float64(2.0 - Float64(Float64(2.0 - t_2) * t_1))); else tmp = Float64(t_2 / Float64(2.0 + Float64(2.0 - Float64(t_2 / t_1)))); end return tmp end
function tmp_2 = code(t) t_1 = 1.0 + (1.0 / t); t_2 = (2.0 / t) / t_1; tmp = 0.0; if (t_2 <= 0.1) tmp = 1.0 + (2.0 - ((2.0 - t_2) * t_1)); else tmp = t_2 / (2.0 + (2.0 - (t_2 / t_1))); end tmp_2 = tmp; end
code[t_] := Block[{t$95$1 = N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(2.0 / t), $MachinePrecision] / t$95$1), $MachinePrecision]}, If[LessEqual[t$95$2, 0.1], N[(1.0 + N[(2.0 - N[(N[(2.0 - t$95$2), $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(t$95$2 / N[(2.0 + N[(2.0 - N[(t$95$2 / t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := 1 + \frac{1}{t}\\
t_2 := \frac{\frac{2}{t}}{t\_1}\\
\mathbf{if}\;t\_2 \leq 0.1:\\
\;\;\;\;1 + \left(2 - \left(2 - t\_2\right) \cdot t\_1\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{t\_2}{2 + \left(2 - \frac{t\_2}{t\_1}\right)}\\
\end{array}
\end{array}
if (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t))) < 0.10000000000000001Initial program 100.0%
Taylor expanded in t around 0
Applied rewrites16.6%
Taylor expanded in t around 0
Applied rewrites17.4%
Taylor expanded in t around 0
Applied rewrites21.2%
if 0.10000000000000001 < (/.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 rewrites18.7%
Taylor expanded in t around 0
Applied rewrites18.7%
Taylor expanded in t around 0
Applied rewrites96.9%
(FPCore (t) :precision binary64 (let* ((t_1 (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t))))) (+ 1.0 (* (- 2.0 t_1) t_1))))
double code(double t) {
double t_1 = (2.0 / t) / (1.0 + (1.0 / t));
return 1.0 + ((2.0 - t_1) * t_1);
}
real(8) function code(t)
real(8), intent (in) :: t
real(8) :: t_1
t_1 = (2.0d0 / t) / (1.0d0 + (1.0d0 / t))
code = 1.0d0 + ((2.0d0 - t_1) * t_1)
end function
public static double code(double t) {
double t_1 = (2.0 / t) / (1.0 + (1.0 / t));
return 1.0 + ((2.0 - t_1) * t_1);
}
def code(t): t_1 = (2.0 / t) / (1.0 + (1.0 / t)) return 1.0 + ((2.0 - t_1) * t_1)
function code(t) t_1 = Float64(Float64(2.0 / t) / Float64(1.0 + Float64(1.0 / t))) return Float64(1.0 + Float64(Float64(2.0 - t_1) * t_1)) end
function tmp = code(t) t_1 = (2.0 / t) / (1.0 + (1.0 / t)); tmp = 1.0 + ((2.0 - t_1) * t_1); end
code[t_] := Block[{t$95$1 = N[(N[(2.0 / t), $MachinePrecision] / N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, N[(1.0 + N[(N[(2.0 - t$95$1), $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := \frac{\frac{2}{t}}{1 + \frac{1}{t}}\\
1 + \left(2 - t\_1\right) \cdot t\_1
\end{array}
\end{array}
Initial program 100.0%
Taylor expanded in t around 0
Applied rewrites17.7%
Taylor expanded in t around 0
Applied rewrites20.0%
(FPCore (t) :precision binary64 (let* ((t_1 (+ 1.0 (/ 1.0 t))) (t_2 (/ (/ 2.0 t) t_1))) (if (<= (- 2.0 t_2) 0.002) t_2 t_1)))
double code(double t) {
double t_1 = 1.0 + (1.0 / t);
double t_2 = (2.0 / t) / t_1;
double tmp;
if ((2.0 - t_2) <= 0.002) {
tmp = t_2;
} else {
tmp = t_1;
}
return tmp;
}
real(8) function code(t)
real(8), intent (in) :: t
real(8) :: t_1
real(8) :: t_2
real(8) :: tmp
t_1 = 1.0d0 + (1.0d0 / t)
t_2 = (2.0d0 / t) / t_1
if ((2.0d0 - t_2) <= 0.002d0) then
tmp = t_2
else
tmp = t_1
end if
code = tmp
end function
public static double code(double t) {
double t_1 = 1.0 + (1.0 / t);
double t_2 = (2.0 / t) / t_1;
double tmp;
if ((2.0 - t_2) <= 0.002) {
tmp = t_2;
} else {
tmp = t_1;
}
return tmp;
}
def code(t): t_1 = 1.0 + (1.0 / t) t_2 = (2.0 / t) / t_1 tmp = 0 if (2.0 - t_2) <= 0.002: tmp = t_2 else: tmp = t_1 return tmp
function code(t) t_1 = Float64(1.0 + Float64(1.0 / t)) t_2 = Float64(Float64(2.0 / t) / t_1) tmp = 0.0 if (Float64(2.0 - t_2) <= 0.002) tmp = t_2; else tmp = t_1; end return tmp end
function tmp_2 = code(t) t_1 = 1.0 + (1.0 / t); t_2 = (2.0 / t) / t_1; tmp = 0.0; if ((2.0 - t_2) <= 0.002) tmp = t_2; else tmp = t_1; end tmp_2 = tmp; end
code[t_] := Block[{t$95$1 = N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(2.0 / t), $MachinePrecision] / t$95$1), $MachinePrecision]}, If[LessEqual[N[(2.0 - t$95$2), $MachinePrecision], 0.002], t$95$2, t$95$1]]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := 1 + \frac{1}{t}\\
t_2 := \frac{\frac{2}{t}}{t\_1}\\
\mathbf{if}\;2 - t\_2 \leq 0.002:\\
\;\;\;\;t\_2\\
\mathbf{else}:\\
\;\;\;\;t\_1\\
\end{array}
\end{array}
if (-.f64 #s(literal 2 binary64) (/.f64 (/.f64 #s(literal 2 binary64) t) (+.f64 #s(literal 1 binary64) (/.f64 #s(literal 1 binary64) t)))) < 2e-3Initial program 100.0%
Taylor expanded in t around 0
Applied rewrites3.8%
Taylor expanded in t around 0
Applied rewrites17.2%
if 2e-3 < (-.f64 #s(literal 2 binary64) (/.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 rewrites18.1%
Taylor expanded in t around 0
Applied rewrites21.2%
(FPCore (t) :precision binary64 (let* ((t_1 (+ 1.0 (/ 1.0 t)))) (+ 1.0 (- 2.0 (* (- 2.0 (/ (/ 2.0 t) t_1)) t_1)))))
double code(double t) {
double t_1 = 1.0 + (1.0 / t);
return 1.0 + (2.0 - ((2.0 - ((2.0 / t) / t_1)) * t_1));
}
real(8) function code(t)
real(8), intent (in) :: t
real(8) :: t_1
t_1 = 1.0d0 + (1.0d0 / t)
code = 1.0d0 + (2.0d0 - ((2.0d0 - ((2.0d0 / t) / t_1)) * t_1))
end function
public static double code(double t) {
double t_1 = 1.0 + (1.0 / t);
return 1.0 + (2.0 - ((2.0 - ((2.0 / t) / t_1)) * t_1));
}
def code(t): t_1 = 1.0 + (1.0 / t) return 1.0 + (2.0 - ((2.0 - ((2.0 / t) / t_1)) * t_1))
function code(t) t_1 = Float64(1.0 + Float64(1.0 / t)) return Float64(1.0 + Float64(2.0 - Float64(Float64(2.0 - Float64(Float64(2.0 / t) / t_1)) * t_1))) end
function tmp = code(t) t_1 = 1.0 + (1.0 / t); tmp = 1.0 + (2.0 - ((2.0 - ((2.0 / t) / t_1)) * t_1)); end
code[t_] := Block[{t$95$1 = N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]}, N[(1.0 + N[(2.0 - N[(N[(2.0 - N[(N[(2.0 / t), $MachinePrecision] / t$95$1), $MachinePrecision]), $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
t_1 := 1 + \frac{1}{t}\\
1 + \left(2 - \left(2 - \frac{\frac{2}{t}}{t\_1}\right) \cdot t\_1\right)
\end{array}
\end{array}
Initial program 100.0%
Taylor expanded in t around 0
Applied rewrites17.7%
Taylor expanded in t around 0
Applied rewrites18.1%
Taylor expanded in t around 0
Applied rewrites19.0%
(FPCore (t) :precision binary64 (+ 1.0 (/ (/ 2.0 t) (+ 1.0 (/ 1.0 t)))))
double code(double t) {
return 1.0 + ((2.0 / t) / (1.0 + (1.0 / t)));
}
real(8) function code(t)
real(8), intent (in) :: t
code = 1.0d0 + ((2.0d0 / t) / (1.0d0 + (1.0d0 / t)))
end function
public static double code(double t) {
return 1.0 + ((2.0 / t) / (1.0 + (1.0 / t)));
}
def code(t): return 1.0 + ((2.0 / t) / (1.0 + (1.0 / t)))
function code(t) return Float64(1.0 + Float64(Float64(2.0 / t) / Float64(1.0 + Float64(1.0 / t)))) end
function tmp = code(t) tmp = 1.0 + ((2.0 / t) / (1.0 + (1.0 / t))); end
code[t_] := N[(1.0 + N[(N[(2.0 / t), $MachinePrecision] / N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 + \frac{\frac{2}{t}}{1 + \frac{1}{t}}
\end{array}
Initial program 100.0%
Taylor expanded in t around 0
Applied rewrites17.7%
Taylor expanded in t around 0
Applied rewrites18.1%
Taylor expanded in t around 0
Applied rewrites19.0%
(FPCore (t) :precision binary64 (+ 1.0 (/ 1.0 t)))
double code(double t) {
return 1.0 + (1.0 / t);
}
real(8) function code(t)
real(8), intent (in) :: t
code = 1.0d0 + (1.0d0 / t)
end function
public static double code(double t) {
return 1.0 + (1.0 / t);
}
def code(t): return 1.0 + (1.0 / t)
function code(t) return Float64(1.0 + Float64(1.0 / t)) end
function tmp = code(t) tmp = 1.0 + (1.0 / t); end
code[t_] := N[(1.0 + N[(1.0 / t), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 + \frac{1}{t}
\end{array}
Initial program 100.0%
Taylor expanded in t around 0
Applied rewrites10.9%
Taylor expanded in t around 0
Applied rewrites12.4%
(FPCore (t) :precision binary64 (/ 2.0 t))
double code(double t) {
return 2.0 / t;
}
real(8) function code(t)
real(8), intent (in) :: t
code = 2.0d0 / t
end function
public static double code(double t) {
return 2.0 / t;
}
def code(t): return 2.0 / t
function code(t) return Float64(2.0 / t) end
function tmp = code(t) tmp = 2.0 / t; end
code[t_] := N[(2.0 / t), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{t}
\end{array}
Initial program 100.0%
Taylor expanded in t around 0
Applied rewrites10.4%
Taylor expanded in t around 0
Applied rewrites3.6%
herbie shell --seed 2024321
(FPCore (t)
:name "Kahan p13 Example 2"
:precision binary64
:pre (TRUE)
(/ (+ 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))))))))