
(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 (/ (/ (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 59.1%
lift--.f64N/A
sub-negN/A
+-commutativeN/A
lift-/.f64N/A
distribute-neg-fracN/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 rewrites57.5%
Taylor expanded in x around 0
sub-negN/A
metadata-evalN/A
lower-fma.f6477.7
Applied rewrites77.7%
lift-/.f64N/A
lift-fma.f64N/A
difference-of-sqr--1N/A
associate-/r*N/A
lower-/.f64N/A
lower-/.f64N/A
+-commutativeN/A
lower-+.f64N/A
lift--.f6499.5
Applied rewrites99.5%
(FPCore (x) :precision binary64 (if (<= (- (/ x (+ 1.0 x)) (/ (+ 1.0 x) (- x 1.0))) 0.0) (/ (- (/ -1.0 x) 3.0) x) (/ (fma -3.0 x -1.0) (fma x x -1.0))))
double code(double x) {
double tmp;
if (((x / (1.0 + x)) - ((1.0 + x) / (x - 1.0))) <= 0.0) {
tmp = ((-1.0 / x) - 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(1.0 + x)) - Float64(Float64(1.0 + x) / Float64(x - 1.0))) <= 0.0) tmp = Float64(Float64(Float64(-1.0 / x) - 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[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(N[(1.0 + x), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(N[(-1.0 / x), $MachinePrecision] - 3.0), $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}{1 + x} - \frac{1 + x}{x - 1} \leq 0:\\
\;\;\;\;\frac{\frac{-1}{x} - 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 8.1%
Taylor expanded in x around inf
associate-*r/N/A
lower-/.f64N/A
neg-mul-1N/A
+-commutativeN/A
distribute-neg-inN/A
sub-negN/A
lower--.f64N/A
distribute-neg-fracN/A
metadata-evalN/A
lower-/.f6498.6
Applied rewrites98.6%
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.8%
lift--.f64N/A
sub-negN/A
+-commutativeN/A
lift-/.f64N/A
distribute-neg-fracN/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 rewrites98.8%
Taylor expanded in x around 0
sub-negN/A
metadata-evalN/A
lower-fma.f64100.0
Applied rewrites100.0%
Final simplification99.4%
(FPCore (x) :precision binary64 (if (<= (- (/ x (+ 1.0 x)) (/ (+ 1.0 x) (- 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 / (1.0 + x)) - ((1.0 + x) / (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(1.0 + x)) - Float64(Float64(1.0 + x) / 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[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(N[(1.0 + x), $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}{1 + x} - \frac{1 + x}{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 8.1%
Taylor expanded in x around inf
lower-/.f6498.4
Applied rewrites98.4%
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.8%
lift--.f64N/A
sub-negN/A
+-commutativeN/A
lift-/.f64N/A
distribute-neg-fracN/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 rewrites98.8%
Taylor expanded in x around 0
sub-negN/A
metadata-evalN/A
lower-fma.f64100.0
Applied rewrites100.0%
Final simplification99.3%
(FPCore (x) :precision binary64 (if (<= (- (/ x (+ 1.0 x)) (/ (+ 1.0 x) (- x 1.0))) 0.5) (/ -3.0 x) (fma x (fma (fma x x 1.0) 3.0 x) 1.0)))
double code(double x) {
double tmp;
if (((x / (1.0 + x)) - ((1.0 + x) / (x - 1.0))) <= 0.5) {
tmp = -3.0 / x;
} else {
tmp = fma(x, fma(fma(x, x, 1.0), 3.0, x), 1.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(x / Float64(1.0 + x)) - Float64(Float64(1.0 + x) / Float64(x - 1.0))) <= 0.5) tmp = Float64(-3.0 / x); else tmp = fma(x, fma(fma(x, x, 1.0), 3.0, x), 1.0); end return tmp end
code[x_] := If[LessEqual[N[(N[(x / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(N[(1.0 + x), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5], N[(-3.0 / x), $MachinePrecision], N[(x * N[(N[(x * x + 1.0), $MachinePrecision] * 3.0 + x), $MachinePrecision] + 1.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{x}{1 + x} - \frac{1 + x}{x - 1} \leq 0.5:\\
\;\;\;\;\frac{-3}{x}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, \mathsf{fma}\left(\mathsf{fma}\left(x, x, 1\right), 3, x\right), 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.5Initial program 9.8%
Taylor expanded in x around inf
lower-/.f6497.3
Applied rewrites97.3%
if 0.5 < (-.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
lower-*.f64N/A
unpow2N/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6498.6
Applied rewrites98.6%
Applied rewrites98.6%
Applied rewrites98.6%
Final simplification98.0%
(FPCore (x) :precision binary64 (if (<= (- (/ x (+ 1.0 x)) (/ (+ 1.0 x) (- x 1.0))) 0.5) (/ -3.0 x) (* (fma x 3.0 1.0) (fma x x 1.0))))
double code(double x) {
double tmp;
if (((x / (1.0 + x)) - ((1.0 + x) / (x - 1.0))) <= 0.5) {
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(1.0 + x)) - Float64(Float64(1.0 + x) / Float64(x - 1.0))) <= 0.5) 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[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(N[(1.0 + x), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5], 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}{1 + x} - \frac{1 + x}{x - 1} \leq 0.5:\\
\;\;\;\;\frac{-3}{x}\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(x, 3, 1\right) \cdot \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.5Initial program 9.8%
Taylor expanded in x around inf
lower-/.f6497.3
Applied rewrites97.3%
if 0.5 < (-.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
lower-*.f64N/A
unpow2N/A
lower-fma.f64N/A
+-commutativeN/A
*-commutativeN/A
lower-fma.f6498.6
Applied rewrites98.6%
Final simplification98.0%
(FPCore (x) :precision binary64 (if (<= (- (/ x (+ 1.0 x)) (/ (+ 1.0 x) (- x 1.0))) 0.5) (/ -3.0 x) (fma (+ 3.0 x) x 1.0)))
double code(double x) {
double tmp;
if (((x / (1.0 + x)) - ((1.0 + x) / (x - 1.0))) <= 0.5) {
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(1.0 + x)) - Float64(Float64(1.0 + x) / Float64(x - 1.0))) <= 0.5) 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[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(N[(1.0 + x), $MachinePrecision] / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.5], 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}{1 + x} - \frac{1 + x}{x - 1} \leq 0.5:\\
\;\;\;\;\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.5Initial program 9.8%
Taylor expanded in x around inf
lower-/.f6497.3
Applied rewrites97.3%
if 0.5 < (-.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
+-commutativeN/A
lower-+.f6498.3
Applied rewrites98.3%
Final simplification97.8%
(FPCore (x) :precision binary64 (fma (+ 3.0 x) x 1.0))
double code(double x) {
return fma((3.0 + x), x, 1.0);
}
function code(x) return fma(Float64(3.0 + x), x, 1.0) end
code[x_] := N[(N[(3.0 + x), $MachinePrecision] * x + 1.0), $MachinePrecision]
\begin{array}{l}
\\
\mathsf{fma}\left(3 + x, x, 1\right)
\end{array}
Initial program 59.1%
Taylor expanded in x around 0
+-commutativeN/A
*-commutativeN/A
lower-fma.f64N/A
+-commutativeN/A
lower-+.f6454.7
Applied rewrites54.7%
Final simplification54.7%
(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 59.1%
Taylor expanded in x around 0
Applied rewrites54.3%
herbie shell --seed 2024270
(FPCore (x)
:name "Asymptote C"
:precision binary64
(- (/ x (+ x 1.0)) (/ (+ x 1.0) (- x 1.0))))