
(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 8 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 (+ (/ 1.0 (+ x -1.0)) (/ x (+ 1.0 x))))
double code(double x) {
return (1.0 / (x + -1.0)) + (x / (1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x + (-1.0d0))) + (x / (1.0d0 + x))
end function
public static double code(double x) {
return (1.0 / (x + -1.0)) + (x / (1.0 + x));
}
def code(x): return (1.0 / (x + -1.0)) + (x / (1.0 + x))
function code(x) return Float64(Float64(1.0 / Float64(x + -1.0)) + Float64(x / Float64(1.0 + x))) end
function tmp = code(x) tmp = (1.0 / (x + -1.0)) + (x / (1.0 + x)); end
code[x_] := N[(N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision] + N[(x / N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + -1} + \frac{x}{1 + x}
\end{array}
Initial program 100.0%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (+ x -1.0)) (/ x (+ 1.0 x))) -1.0) (/ (+ x (fma x x (- 1.0 x))) (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 = (x + fma(x, x, (1.0 - x))) / 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(Float64(x + fma(x, x, Float64(1.0 - x))) / 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 + N[(x * x + N[(1.0 - x), $MachinePrecision]), $MachinePrecision]), $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{x + \mathsf{fma}\left(x, x, 1 - x\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%
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.7
Applied rewrites99.7%
Final simplification99.8%
(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-*.f6499.7
Applied rewrites99.7%
Final simplification99.8%
(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.9
Applied rewrites99.9%
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.7
Applied rewrites99.7%
Final simplification99.8%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (+ x -1.0)) (/ x (+ 1.0 x))) -1.0) (fma -2.0 (* 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(-2.0, (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(-2.0, Float64(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[(-2.0 * N[(x * x), $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(-2, x \cdot 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%
Taylor expanded in x around 0
sub-negN/A
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6499.7
Applied rewrites99.7%
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.7
Applied rewrites99.7%
Final simplification99.7%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (+ x -1.0)) (/ x (+ 1.0 x))) -1.0) (fma -2.0 (* x x) -1.0) 1.0))
double code(double x) {
double tmp;
if (((1.0 / (x + -1.0)) + (x / (1.0 + x))) <= -1.0) {
tmp = fma(-2.0, (x * x), -1.0);
} else {
tmp = 1.0;
}
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(-2.0, Float64(x * x), -1.0); else tmp = 1.0; 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[(-2.0 * N[(x * x), $MachinePrecision] + -1.0), $MachinePrecision], 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{x + -1} + \frac{x}{1 + x} \leq -1:\\
\;\;\;\;\mathsf{fma}\left(-2, x \cdot x, -1\right)\\
\mathbf{else}:\\
\;\;\;\;1\\
\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
metadata-evalN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6499.7
Applied rewrites99.7%
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
Applied rewrites99.5%
Final simplification99.6%
(FPCore (x) :precision binary64 (if (<= (+ (/ 1.0 (+ x -1.0)) (/ x (+ 1.0 x))) -1.0) -1.0 1.0))
double code(double x) {
double tmp;
if (((1.0 / (x + -1.0)) + (x / (1.0 + x))) <= -1.0) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((1.0d0 / (x + (-1.0d0))) + (x / (1.0d0 + x))) <= (-1.0d0)) then
tmp = -1.0d0
else
tmp = 1.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (((1.0 / (x + -1.0)) + (x / (1.0 + x))) <= -1.0) {
tmp = -1.0;
} else {
tmp = 1.0;
}
return tmp;
}
def code(x): tmp = 0 if ((1.0 / (x + -1.0)) + (x / (1.0 + x))) <= -1.0: tmp = -1.0 else: tmp = 1.0 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 = -1.0; else tmp = 1.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / (x + -1.0)) + (x / (1.0 + x))) <= -1.0) tmp = -1.0; else tmp = 1.0; end tmp_2 = 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], -1.0, 1.0]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{x + -1} + \frac{x}{1 + x} \leq -1:\\
\;\;\;\;-1\\
\mathbf{else}:\\
\;\;\;\;1\\
\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
Applied rewrites98.9%
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
Applied rewrites99.5%
Final simplification99.2%
(FPCore (x) :precision binary64 -1.0)
double code(double x) {
return -1.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = -1.0d0
end function
public static double code(double x) {
return -1.0;
}
def code(x): return -1.0
function code(x) return -1.0 end
function tmp = code(x) tmp = -1.0; end
code[x_] := -1.0
\begin{array}{l}
\\
-1
\end{array}
Initial program 100.0%
Taylor expanded in x around 0
Applied rewrites47.2%
herbie shell --seed 2024233
(FPCore (x)
:name "Asymptote B"
:precision binary64
(+ (/ 1.0 (- x 1.0)) (/ x (+ x 1.0))))