
(FPCore (x) :precision binary64 (+ (/ 1.0 (- x 1.0)) (/ x (+ x 1.0))))
double code(double x) {
return (1.0 / (x - 1.0)) + (x / (x + 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x - 1.0d0)) + (x / (x + 1.0d0))
end function
public static double code(double x) {
return (1.0 / (x - 1.0)) + (x / (x + 1.0));
}
def code(x): return (1.0 / (x - 1.0)) + (x / (x + 1.0))
function code(x) return Float64(Float64(1.0 / Float64(x - 1.0)) + Float64(x / Float64(x + 1.0))) end
function tmp = code(x) tmp = (1.0 / (x - 1.0)) + (x / (x + 1.0)); end
code[x_] := N[(N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision] + N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x - 1} + \frac{x}{x + 1}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 7 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (+ (/ 1.0 (- x 1.0)) (/ x (+ x 1.0))))
double code(double x) {
return (1.0 / (x - 1.0)) + (x / (x + 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x - 1.0d0)) + (x / (x + 1.0d0))
end function
public static double code(double x) {
return (1.0 / (x - 1.0)) + (x / (x + 1.0));
}
def code(x): return (1.0 / (x - 1.0)) + (x / (x + 1.0))
function code(x) return Float64(Float64(1.0 / Float64(x - 1.0)) + Float64(x / Float64(x + 1.0))) end
function tmp = code(x) tmp = (1.0 / (x - 1.0)) + (x / (x + 1.0)); end
code[x_] := N[(N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision] + N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x - 1} + \frac{x}{x + 1}
\end{array}
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (+ x -1.0)) (/ x (+ 1.0 x))) -1.0) (/ (fma x x 1.0) (fma x x -1.0)) (+ 1.0 (/ 2.0 (* x x)))))
double code(double x) {
double tmp;
if (((1.0 / (x + -1.0)) + (x / (1.0 + x))) <= -1.0) {
tmp = fma(x, x, 1.0) / fma(x, x, -1.0);
} else {
tmp = 1.0 + (2.0 / (x * x));
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / Float64(x + -1.0)) + Float64(x / Float64(1.0 + x))) <= -1.0) tmp = Float64(fma(x, x, 1.0) / fma(x, x, -1.0)); else tmp = Float64(1.0 + Float64(2.0 / Float64(x * x))); end return tmp end
code[x_] := If[LessEqual[N[(N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision] + N[(x / N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0], N[(N[(x * x + 1.0), $MachinePrecision] / N[(x * x + -1.0), $MachinePrecision]), $MachinePrecision], N[(1.0 + N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{x + -1} + \frac{x}{1 + x} \leq -1:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, x, 1\right)}{\mathsf{fma}\left(x, x, -1\right)}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{2}{x \cdot x}\\
\end{array}
\end{array}
if (+.f64 (/.f64 #s(literal 1 binary64) (-.f64 x #s(literal 1 binary64))) (/.f64 x (+.f64 x #s(literal 1 binary64)))) < -1Initial program 100.0%
lift-+.f64N/A
lift-/.f64N/A
lift-/.f64N/A
frac-addN/A
*-commutativeN/A
lift-+.f64N/A
lift--.f64N/A
difference-of-sqr-1N/A
metadata-evalN/A
lower-/.f64N/A
Applied rewrites100.0%
Taylor expanded in x around 0
+-commutativeN/A
unpow2N/A
lower-fma.f64100.0
Applied rewrites100.0%
if -1 < (+.f64 (/.f64 #s(literal 1 binary64) (-.f64 x #s(literal 1 binary64))) (/.f64 x (+.f64 x #s(literal 1 binary64)))) Initial program 100.0%
Taylor expanded in x around inf
lower-+.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f64100.0
Applied rewrites100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (+ x -1.0)) (/ x (+ 1.0 x))) -1.0) (fma x (* -2.0 (fma x (* x x) x)) -1.0) (+ 1.0 (/ 2.0 (* x x)))))
double code(double x) {
double tmp;
if (((1.0 / (x + -1.0)) + (x / (1.0 + x))) <= -1.0) {
tmp = fma(x, (-2.0 * fma(x, (x * x), x)), -1.0);
} else {
tmp = 1.0 + (2.0 / (x * x));
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / Float64(x + -1.0)) + Float64(x / Float64(1.0 + x))) <= -1.0) tmp = fma(x, Float64(-2.0 * fma(x, Float64(x * x), x)), -1.0); else tmp = Float64(1.0 + Float64(2.0 / Float64(x * x))); end return tmp end
code[x_] := If[LessEqual[N[(N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision] + N[(x / N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], -1.0], N[(x * N[(-2.0 * N[(x * N[(x * x), $MachinePrecision] + x), $MachinePrecision]), $MachinePrecision] + -1.0), $MachinePrecision], N[(1.0 + N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{x + -1} + \frac{x}{1 + x} \leq -1:\\
\;\;\;\;\mathsf{fma}\left(x, -2 \cdot \mathsf{fma}\left(x, x \cdot x, x\right), -1\right)\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{2}{x \cdot x}\\
\end{array}
\end{array}
if (+.f64 (/.f64 #s(literal 1 binary64) (-.f64 x #s(literal 1 binary64))) (/.f64 x (+.f64 x #s(literal 1 binary64)))) < -1Initial program 100.0%
Taylor expanded in x around 0
sub-negN/A
*-commutativeN/A
metadata-evalN/A
unpow2N/A
associate-*r*N/A
*-commutativeN/A
lower-fma.f64N/A
*-commutativeN/A
sub-negN/A
*-commutativeN/A
metadata-evalN/A
distribute-lft1-inN/A
associate-*r*N/A
*-commutativeN/A
lower-*.f64N/A
distribute-lft-inN/A
*-rgt-identityN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6499.6
Applied rewrites99.6%
if -1 < (+.f64 (/.f64 #s(literal 1 binary64) (-.f64 x #s(literal 1 binary64))) (/.f64 x (+.f64 x #s(literal 1 binary64)))) Initial program 100.0%
Taylor expanded in x around inf
lower-+.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f6499.2
Applied rewrites99.2%
Final simplification99.4%
herbie shell --seed 2024227
(FPCore (x)
:name "Asymptote B"
:precision binary64
(+ (/ 1.0 (- x 1.0)) (/ x (+ x 1.0))))