
(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 8 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 (/ (- (/ (* -2.0 x) (+ 1.0 x)) 1.0) (- x 1.0)))
double code(double x) {
return (((-2.0 * x) / (1.0 + x)) - 1.0) / (x - 1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((((-2.0d0) * x) / (1.0d0 + x)) - 1.0d0) / (x - 1.0d0)
end function
public static double code(double x) {
return (((-2.0 * x) / (1.0 + x)) - 1.0) / (x - 1.0);
}
def code(x): return (((-2.0 * x) / (1.0 + x)) - 1.0) / (x - 1.0)
function code(x) return Float64(Float64(Float64(Float64(-2.0 * x) / Float64(1.0 + x)) - 1.0) / Float64(x - 1.0)) end
function tmp = code(x) tmp = (((-2.0 * x) / (1.0 + x)) - 1.0) / (x - 1.0); end
code[x_] := N[(N[(N[(N[(-2.0 * x), $MachinePrecision] / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{-2 \cdot x}{1 + x} - 1}{x - 1}
\end{array}
Initial program 58.1%
lift--.f64N/A
lift-/.f64N/A
lift-+.f64N/A
div-addN/A
associate--r+N/A
lift--.f64N/A
flip--N/A
lift-+.f64N/A
associate-/r/N/A
fp-cancel-sub-sign-invN/A
Applied rewrites62.7%
lift-fma.f64N/A
lift-*.f64N/A
lift-neg.f64N/A
fp-cancel-sub-signN/A
lift-/.f64N/A
associate-*l/N/A
lift-fma.f64N/A
difference-of-sqr--1N/A
lift--.f64N/A
+-commutativeN/A
lift-+.f64N/A
associate-/r*N/A
lift-pow.f64N/A
unpow-1N/A
associate-/r/N/A
lift-fma.f64N/A
difference-of-sqr--1N/A
difference-of-squares-revN/A
lift-+.f64N/A
+-commutativeN/A
Applied rewrites65.0%
Taylor expanded in x around 0
Applied rewrites99.6%
(FPCore (x) :precision binary64 (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.0) (/ (- -3.0 (pow x -1.0)) x) (/ (fma -3.0 x -1.0) (fma x x -1.0))))
double code(double x) {
double tmp;
if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.0) {
tmp = (-3.0 - pow(x, -1.0)) / x;
} else {
tmp = fma(-3.0, x, -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(x + 1.0) / Float64(x - 1.0))) <= 0.0) tmp = Float64(Float64(-3.0 - (x ^ -1.0)) / x); else tmp = Float64(fma(-3.0, x, -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[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(-3.0 - N[Power[x, -1.0], $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision], N[(N[(-3.0 * x + -1.0), $MachinePrecision] / N[(x * x + -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0:\\
\;\;\;\;\frac{-3 - {x}^{-1}}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(-3, x, -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 9.7%
Taylor expanded in x around inf
associate-*r/N/A
lower-/.f64N/A
distribute-lft-inN/A
metadata-evalN/A
fp-cancel-sign-sub-invN/A
metadata-evalN/A
*-lft-identityN/A
lower--.f64N/A
lower-/.f6498.2
Applied rewrites98.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.3%
lift--.f64N/A
lift-/.f64N/A
lift-+.f64N/A
div-addN/A
associate--r+N/A
lift--.f64N/A
flip--N/A
lift-+.f64N/A
associate-/r/N/A
fp-cancel-sub-sign-invN/A
Applied rewrites99.9%
lift-fma.f64N/A
lift-*.f64N/A
lift-neg.f64N/A
fp-cancel-sub-signN/A
lift-/.f64N/A
associate-*l/N/A
lift-pow.f64N/A
unpow-1N/A
associate-*l/N/A
sub-divN/A
lower-/.f64N/A
Applied rewrites99.9%
Taylor expanded in x around 0
metadata-evalN/A
distribute-lft-neg-inN/A
*-commutativeN/A
distribute-lft-neg-inN/A
rgt-mult-inverseN/A
fp-cancel-sub-signN/A
distribute-lft-inN/A
distribute-rgt-inN/A
distribute-rgt-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
distribute-rgt-neg-outN/A
lft-mult-inverseN/A
metadata-evalN/A
lower-fma.f64100.0
Applied rewrites100.0%
Final simplification99.1%
(FPCore (x) :precision binary64 (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.0) (/ -3.0 x) (/ (fma -3.0 x -1.0) (fma x x -1.0))))
double code(double x) {
double tmp;
if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.0) {
tmp = -3.0 / x;
} else {
tmp = fma(-3.0, x, -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(x + 1.0) / Float64(x - 1.0))) <= 0.0) tmp = Float64(-3.0 / x); else tmp = Float64(fma(-3.0, x, -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[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(-3.0 / x), $MachinePrecision], N[(N[(-3.0 * x + -1.0), $MachinePrecision] / N[(x * x + -1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0:\\
\;\;\;\;\frac{-3}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{\mathsf{fma}\left(-3, x, -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 9.7%
Taylor expanded in x around inf
lower-/.f6496.8
Applied rewrites96.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.3%
lift--.f64N/A
lift-/.f64N/A
lift-+.f64N/A
div-addN/A
associate--r+N/A
lift--.f64N/A
flip--N/A
lift-+.f64N/A
associate-/r/N/A
fp-cancel-sub-sign-invN/A
Applied rewrites99.9%
lift-fma.f64N/A
lift-*.f64N/A
lift-neg.f64N/A
fp-cancel-sub-signN/A
lift-/.f64N/A
associate-*l/N/A
lift-pow.f64N/A
unpow-1N/A
associate-*l/N/A
sub-divN/A
lower-/.f64N/A
Applied rewrites99.9%
Taylor expanded in x around 0
metadata-evalN/A
distribute-lft-neg-inN/A
*-commutativeN/A
distribute-lft-neg-inN/A
rgt-mult-inverseN/A
fp-cancel-sub-signN/A
distribute-lft-inN/A
distribute-rgt-inN/A
distribute-rgt-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
distribute-rgt-neg-outN/A
lft-mult-inverseN/A
metadata-evalN/A
lower-fma.f64100.0
Applied rewrites100.0%
(FPCore (x) :precision binary64 (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.6) (/ -3.0 x) (fma (fma x x 1.0) (* 3.0 x) (fma x x 1.0))))
double code(double x) {
double tmp;
if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.6) {
tmp = -3.0 / x;
} else {
tmp = fma(fma(x, x, 1.0), (3.0 * x), fma(x, x, 1.0));
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.6) tmp = Float64(-3.0 / x); else tmp = fma(fma(x, x, 1.0), Float64(3.0 * x), fma(x, x, 1.0)); end return tmp end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.6], N[(-3.0 / x), $MachinePrecision], N[(N[(x * x + 1.0), $MachinePrecision] * N[(3.0 * x), $MachinePrecision] + N[(x * x + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.6:\\
\;\;\;\;\frac{-3}{x}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(\mathsf{fma}\left(x, x, 1\right), 3 \cdot x, \mathsf{fma}\left(x, x, 1\right)\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.599999999999999978Initial program 12.2%
Taylor expanded in x around inf
lower-/.f6495.2
Applied rewrites95.2%
if 0.599999999999999978 < (-.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
distribute-lft-inN/A
*-commutativeN/A
associate-+r+N/A
associate-*r*N/A
unpow2N/A
distribute-rgt1-inN/A
+-commutativeN/A
unpow2N/A
fp-cancel-sign-subN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
fp-cancel-sign-subN/A
unpow2N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6498.7
Applied rewrites98.7%
Applied rewrites98.7%
(FPCore (x) :precision binary64 (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.6) (/ -3.0 x) (* (fma x x 1.0) (fma 3.0 x 1.0))))
double code(double x) {
double tmp;
if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.6) {
tmp = -3.0 / x;
} else {
tmp = fma(x, x, 1.0) * fma(3.0, x, 1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.6) tmp = Float64(-3.0 / x); else tmp = Float64(fma(x, x, 1.0) * fma(3.0, x, 1.0)); end return tmp end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.6], N[(-3.0 / x), $MachinePrecision], N[(N[(x * x + 1.0), $MachinePrecision] * N[(3.0 * x + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.6:\\
\;\;\;\;\frac{-3}{x}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, x, 1\right) \cdot \mathsf{fma}\left(3, 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.599999999999999978Initial program 12.2%
Taylor expanded in x around inf
lower-/.f6495.2
Applied rewrites95.2%
if 0.599999999999999978 < (-.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
distribute-lft-inN/A
*-commutativeN/A
associate-+r+N/A
associate-*r*N/A
unpow2N/A
distribute-rgt1-inN/A
+-commutativeN/A
unpow2N/A
fp-cancel-sign-subN/A
mul-1-negN/A
lower-*.f64N/A
mul-1-negN/A
fp-cancel-sign-subN/A
unpow2N/A
+-commutativeN/A
unpow2N/A
lower-fma.f64N/A
+-commutativeN/A
lower-fma.f6498.7
Applied rewrites98.7%
(FPCore (x) :precision binary64 (if (<= (- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))) 0.6) (/ -3.0 x) (fma (+ 3.0 x) x 1.0)))
double code(double x) {
double tmp;
if (((x / (x + 1.0)) - ((x + 1.0) / (x - 1.0))) <= 0.6) {
tmp = -3.0 / x;
} else {
tmp = fma((3.0 + x), x, 1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x / Float64(x + 1.0)) - Float64(Float64(x + 1.0) / Float64(x - 1.0))) <= 0.6) tmp = Float64(-3.0 / x); else tmp = fma(Float64(3.0 + x), x, 1.0); end return tmp end
code[x_] := If[LessEqual[N[(N[(x / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(N[(x + 1.0), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.6], N[(-3.0 / x), $MachinePrecision], N[(N[(3.0 + x), $MachinePrecision] * x + 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{x + 1} - \frac{x + 1}{x - 1} \leq 0.6:\\
\;\;\;\;\frac{-3}{x}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(3 + 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.599999999999999978Initial program 12.2%
Taylor expanded in x around inf
lower-/.f6495.2
Applied rewrites95.2%
if 0.599999999999999978 < (-.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
*-commutativeN/A
lower-fma.f64N/A
lower-+.f6498.5
Applied rewrites98.5%
(FPCore (x) :precision binary64 (/ (/ (fma -3.0 x -1.0) (+ 1.0 x)) (- x 1.0)))
double code(double x) {
return (fma(-3.0, x, -1.0) / (1.0 + x)) / (x - 1.0);
}
function code(x) return Float64(Float64(fma(-3.0, x, -1.0) / Float64(1.0 + x)) / Float64(x - 1.0)) end
code[x_] := N[(N[(N[(-3.0 * x + -1.0), $MachinePrecision] / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\mathsf{fma}\left(-3, x, -1\right)}{1 + x}}{x - 1}
\end{array}
Initial program 58.1%
lift--.f64N/A
lift-/.f64N/A
lift-+.f64N/A
div-addN/A
associate--r+N/A
lift--.f64N/A
flip--N/A
lift-+.f64N/A
associate-/r/N/A
fp-cancel-sub-sign-invN/A
Applied rewrites62.7%
lift-fma.f64N/A
lift-*.f64N/A
lift-neg.f64N/A
fp-cancel-sub-signN/A
lift-/.f64N/A
associate-*l/N/A
lift-pow.f64N/A
unpow-1N/A
associate-*l/N/A
sub-divN/A
lower-/.f64N/A
Applied rewrites62.7%
Taylor expanded in x around 0
metadata-evalN/A
distribute-lft-neg-inN/A
*-commutativeN/A
distribute-lft-neg-inN/A
rgt-mult-inverseN/A
fp-cancel-sub-signN/A
distribute-lft-inN/A
distribute-rgt-inN/A
distribute-rgt-neg-inN/A
distribute-lft-neg-inN/A
metadata-evalN/A
distribute-rgt-neg-outN/A
lft-mult-inverseN/A
metadata-evalN/A
lower-fma.f6480.9
Applied rewrites80.9%
lift-/.f64N/A
lift-fma.f64N/A
difference-of-sqr--1N/A
+-commutativeN/A
*-lft-identityN/A
lift--.f64N/A
associate-/r*N/A
lower-/.f64N/A
lower-/.f64N/A
*-lft-identityN/A
lift-+.f6499.4
Applied rewrites99.4%
(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 58.1%
Taylor expanded in x around 0
Applied rewrites52.8%
herbie shell --seed 2024332
(FPCore (x)
:name "Asymptote C"
:precision binary64
(- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))))