
(FPCore (x) :precision binary64 (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))))
double code(double x) {
return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x / (x + 1.0d0)) - ((x + 1.0d0) / (x - 1.0d0))
end function
public static double code(double x) {
return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0));
}
def code(x): return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))
function code(x) return Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) end
function tmp = code(x) tmp = (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0)); end
code[x_] := N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{x + 1} - \frac{x + 1}{x - 1}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))))
double code(double x) {
return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x / (x + 1.0d0)) - ((x + 1.0d0) / (x - 1.0d0))
end function
public static double code(double x) {
return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0));
}
def code(x): return (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))
function code(x) return Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) end
function tmp = code(x) tmp = (x / (x + 1.0)) - ((x + 1.0) / (x - 1.0)); end
code[x_] := N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x}{x + 1} - \frac{x + 1}{x - 1}
\end{array}
(FPCore (x) :precision binary64 (if (<= (+ (/ x (+ x 1.0)) (/ (- -1.0 x) (+ x -1.0))) 0.0002) (* (/ (+ 3.0 (/ 1.0 x)) x) (+ -1.0 (/ -1.0 (* x x)))) (fma x (+ x 3.0) 1.0)))
double code(double x) {
double tmp;
if (((x / (x + 1.0)) + ((-1.0 - x) / (x + -1.0))) <= 0.0002) {
tmp = ((3.0 + (1.0 / x)) / x) * (-1.0 + (-1.0 / (x * x)));
} else {
tmp = fma(x, (x + 3.0), 1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x / Float64(x + 1.0)) + Float64(Float64(-1.0 - x) / Float64(x + -1.0))) <= 0.0002) tmp = Float64(Float64(Float64(3.0 + Float64(1.0 / x)) / x) * Float64(-1.0 + Float64(-1.0 / Float64(x * x)))); else tmp = fma(x, Float64(x + 3.0), 1.0); end return tmp end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] + N[(N[(-1.0 - x), $MachinePrecision] / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0002], N[(N[(N[(3.0 + N[(1.0 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision] * N[(-1.0 + N[(-1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(x * N[(x + 3.0), $MachinePrecision] + 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} + \frac{-1 - x}{x + -1} \leq 0.0002:\\
\;\;\;\;\frac{3 + \frac{1}{x}}{x} \cdot \left(-1 + \frac{-1}{x \cdot x}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, x + 3, 1\right)\\
\end{array}
\end{array}
if (-.f64 (/.f64 x (+.f64 x #s(literal 1 binary64))) (/.f64 (+.f64 x #s(literal 1 binary64)) (-.f64 x #s(literal 1 binary64)))) < 2.0000000000000001e-4Initial program 9.3%
Taylor expanded in x around inf
div-subN/A
sub-negN/A
mul-1-negN/A
+-commutativeN/A
associate-*r/N/A
unpow2N/A
times-fracN/A
metadata-evalN/A
distribute-neg-fracN/A
associate-*l/N/A
distribute-rgt-outN/A
lower-*.f64N/A
lower-/.f64N/A
lower-+.f64N/A
lower-/.f64N/A
Simplified99.4%
if 2.0000000000000001e-4 < (-.f64 (/.f64 x (+.f64 x #s(literal 1 binary64))) (/.f64 (+.f64 x #s(literal 1 binary64)) (-.f64 x #s(literal 1 binary64)))) Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
lower-+.f64100.0
Simplified100.0%
Final simplification99.7%
(FPCore (x) :precision binary64 (if (<= (+ (/ x (+ x 1.0)) (/ (- -1.0 x) (+ x -1.0))) 0.0) (/ (- -3.0 (/ (+ x 3.0) (* x x))) x) (/ (fma x -3.0 -1.0) (fma x x -1.0))))
double code(double x) {
double tmp;
if (((x / (x + 1.0)) + ((-1.0 - x) / (x + -1.0))) <= 0.0) {
tmp = (-3.0 - ((x + 3.0) / (x * x))) / x;
} else {
tmp = fma(x, -3.0, -1.0) / fma(x, x, -1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x / Float64(x + 1.0)) + Float64(Float64(-1.0 - x) / Float64(x + -1.0))) <= 0.0) tmp = Float64(Float64(-3.0 - Float64(Float64(x + 3.0) / Float64(x * x))) / x); else tmp = Float64(fma(x, -3.0, -1.0) / fma(x, x, -1.0)); end return tmp end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] + N[(N[(-1.0 - x), $MachinePrecision] / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(-3.0 - N[(N[(x + 3.0), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], N[(N[(x * -3.0 + -1.0), $MachinePrecision] / N[(x * x + -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} + \frac{-1 - x}{x + -1} \leq 0:\\
\;\;\;\;\frac{-3 - \frac{x + 3}{x \cdot x}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, -3, -1\right)}{\mathsf{fma}\left(x, x, -1\right)}\\
\end{array}
\end{array}
if (-.f64 (/.f64 x (+.f64 x #s(literal 1 binary64))) (/.f64 (+.f64 x #s(literal 1 binary64)) (-.f64 x #s(literal 1 binary64)))) < 0.0Initial program 7.8%
Taylor expanded in x around inf
Simplified99.3%
Taylor expanded in x around 0
metadata-evalN/A
lft-mult-inverseN/A
associate-*l*N/A
*-lft-identityN/A
distribute-rgt-inN/A
+-commutativeN/A
lower-/.f64N/A
+-commutativeN/A
distribute-rgt-inN/A
associate-*l*N/A
lft-mult-inverseN/A
metadata-evalN/A
*-lft-identityN/A
lower-+.f64N/A
unpow2N/A
lower-*.f6499.3
Simplified99.3%
if 0.0 < (-.f64 (/.f64 x (+.f64 x #s(literal 1 binary64))) (/.f64 (+.f64 x #s(literal 1 binary64)) (-.f64 x #s(literal 1 binary64)))) Initial program 98.6%
lift-+.f64N/A
lift-/.f64N/A
lift-+.f64N/A
lift--.f64N/A
lift-/.f64N/A
sub-negN/A
lift-/.f64N/A
lift-/.f64N/A
distribute-neg-fracN/A
frac-addN/A
lift-+.f64N/A
lift--.f64N/A
difference-of-sqr-1N/A
metadata-evalN/A
lower-/.f64N/A
Applied egg-rr98.8%
Taylor expanded in x around 0
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64100.0
Simplified100.0%
Final simplification99.6%
(FPCore (x) :precision binary64 (if (<= (+ (/ x (+ x 1.0)) (/ (- -1.0 x) (+ x -1.0))) 0.0) (/ (+ -3.0 (/ -1.0 x)) x) (/ (fma x -3.0 -1.0) (fma x x -1.0))))
double code(double x) {
double tmp;
if (((x / (x + 1.0)) + ((-1.0 - x) / (x + -1.0))) <= 0.0) {
tmp = (-3.0 + (-1.0 / x)) / x;
} else {
tmp = fma(x, -3.0, -1.0) / fma(x, x, -1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x / Float64(x + 1.0)) + Float64(Float64(-1.0 - x) / Float64(x + -1.0))) <= 0.0) tmp = Float64(Float64(-3.0 + Float64(-1.0 / x)) / x); else tmp = Float64(fma(x, -3.0, -1.0) / fma(x, x, -1.0)); end return tmp end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] + N[(N[(-1.0 - x), $MachinePrecision] / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(-3.0 + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], N[(N[(x * -3.0 + -1.0), $MachinePrecision] / N[(x * x + -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} + \frac{-1 - x}{x + -1} \leq 0:\\
\;\;\;\;\frac{-3 + \frac{-1}{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, -3, -1\right)}{\mathsf{fma}\left(x, x, -1\right)}\\
\end{array}
\end{array}
if (-.f64 (/.f64 x (+.f64 x #s(literal 1 binary64))) (/.f64 (+.f64 x #s(literal 1 binary64)) (-.f64 x #s(literal 1 binary64)))) < 0.0Initial program 7.8%
Taylor expanded in x around inf
associate-*r/N/A
lower-/.f64N/A
neg-mul-1N/A
distribute-neg-inN/A
metadata-evalN/A
lower-+.f64N/A
distribute-neg-fracN/A
metadata-evalN/A
lower-/.f6498.8
Simplified98.8%
if 0.0 < (-.f64 (/.f64 x (+.f64 x #s(literal 1 binary64))) (/.f64 (+.f64 x #s(literal 1 binary64)) (-.f64 x #s(literal 1 binary64)))) Initial program 98.6%
lift-+.f64N/A
lift-/.f64N/A
lift-+.f64N/A
lift--.f64N/A
lift-/.f64N/A
sub-negN/A
lift-/.f64N/A
lift-/.f64N/A
distribute-neg-fracN/A
frac-addN/A
lift-+.f64N/A
lift--.f64N/A
difference-of-sqr-1N/A
metadata-evalN/A
lower-/.f64N/A
Applied egg-rr98.8%
Taylor expanded in x around 0
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64100.0
Simplified100.0%
Final simplification99.3%
(FPCore (x) :precision binary64 (if (<= (+ (/ x (+ x 1.0)) (/ (- -1.0 x) (+ x -1.0))) 0.0) (/ -3.0 x) (/ (fma x -3.0 -1.0) (fma x x -1.0))))
double code(double x) {
double tmp;
if (((x / (x + 1.0)) + ((-1.0 - x) / (x + -1.0))) <= 0.0) {
tmp = -3.0 / x;
} else {
tmp = fma(x, -3.0, -1.0) / fma(x, x, -1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x / Float64(x + 1.0)) + Float64(Float64(-1.0 - x) / Float64(x + -1.0))) <= 0.0) tmp = Float64(-3.0 / x); else tmp = Float64(fma(x, -3.0, -1.0) / fma(x, x, -1.0)); end return tmp end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] + N[(N[(-1.0 - x), $MachinePrecision] / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(-3.0 / x), $MachinePrecision], N[(N[(x * -3.0 + -1.0), $MachinePrecision] / N[(x * x + -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} + \frac{-1 - x}{x + -1} \leq 0:\\
\;\;\;\;\frac{-3}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(x, -3, -1\right)}{\mathsf{fma}\left(x, x, -1\right)}\\
\end{array}
\end{array}
if (-.f64 (/.f64 x (+.f64 x #s(literal 1 binary64))) (/.f64 (+.f64 x #s(literal 1 binary64)) (-.f64 x #s(literal 1 binary64)))) < 0.0Initial program 7.8%
Taylor expanded in x around inf
lower-/.f6498.2
Simplified98.2%
if 0.0 < (-.f64 (/.f64 x (+.f64 x #s(literal 1 binary64))) (/.f64 (+.f64 x #s(literal 1 binary64)) (-.f64 x #s(literal 1 binary64)))) Initial program 98.6%
lift-+.f64N/A
lift-/.f64N/A
lift-+.f64N/A
lift--.f64N/A
lift-/.f64N/A
sub-negN/A
lift-/.f64N/A
lift-/.f64N/A
distribute-neg-fracN/A
frac-addN/A
lift-+.f64N/A
lift--.f64N/A
difference-of-sqr-1N/A
metadata-evalN/A
lower-/.f64N/A
Applied egg-rr98.8%
Taylor expanded in x around 0
sub-negN/A
*-commutativeN/A
metadata-evalN/A
lower-fma.f64100.0
Simplified100.0%
Final simplification99.1%
(FPCore (x) :precision binary64 (if (<= (+ (/ x (+ x 1.0)) (/ (- -1.0 x) (+ x -1.0))) 0.0002) (/ -3.0 x) (fma x (+ x 3.0) 1.0)))
double code(double x) {
double tmp;
if (((x / (x + 1.0)) + ((-1.0 - x) / (x + -1.0))) <= 0.0002) {
tmp = -3.0 / x;
} else {
tmp = fma(x, (x + 3.0), 1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x / Float64(x + 1.0)) + Float64(Float64(-1.0 - x) / Float64(x + -1.0))) <= 0.0002) tmp = Float64(-3.0 / x); else tmp = fma(x, Float64(x + 3.0), 1.0); end return tmp end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] + N[(N[(-1.0 - x), $MachinePrecision] / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0002], N[(-3.0 / x), $MachinePrecision], N[(x * N[(x + 3.0), $MachinePrecision] + 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} + \frac{-1 - x}{x + -1} \leq 0.0002:\\
\;\;\;\;\frac{-3}{x}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, x + 3, 1\right)\\
\end{array}
\end{array}
if (-.f64 (/.f64 x (+.f64 x #s(literal 1 binary64))) (/.f64 (+.f64 x #s(literal 1 binary64)) (-.f64 x #s(literal 1 binary64)))) < 2.0000000000000001e-4Initial program 9.3%
Taylor expanded in x around inf
lower-/.f6497.3
Simplified97.3%
if 2.0000000000000001e-4 < (-.f64 (/.f64 x (+.f64 x #s(literal 1 binary64))) (/.f64 (+.f64 x #s(literal 1 binary64)) (-.f64 x #s(literal 1 binary64)))) Initial program 100.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
lower-+.f64100.0
Simplified100.0%
Final simplification98.5%
(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 51.1%
Taylor expanded in x around 0
Simplified47.5%
herbie shell --seed 2024215
(FPCore (x)
:name "Asymptote C"
:precision binary64
(- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))))