
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 (- x 1.0))))
double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x + 1.0d0)) - (1.0d0 / (x - 1.0d0))
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
def code(x): return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0))
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / Float64(x - 1.0))) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) - (1.0 / (x - 1.0)); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} - \frac{1}{x - 1}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 5 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 (- x 1.0))))
double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x + 1.0d0)) - (1.0d0 / (x - 1.0d0))
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0));
}
def code(x): return (1.0 / (x + 1.0)) - (1.0 / (x - 1.0))
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / Float64(x - 1.0))) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) - (1.0 / (x - 1.0)); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} - \frac{1}{x - 1}
\end{array}
(FPCore (x)
:precision binary64
(if (<= (+ (/ 1.0 (+ 1.0 x)) (/ -1.0 (+ x -1.0))) 0.0)
(*
(/ (+ 2.0 (/ 2.0 (* x x))) (* x x))
(+ -1.0 (/ -1.0 (* x (* x (* x x))))))
(fma 2.0 (* x x) 2.0)))
double code(double x) {
double tmp;
if (((1.0 / (1.0 + x)) + (-1.0 / (x + -1.0))) <= 0.0) {
tmp = ((2.0 + (2.0 / (x * x))) / (x * x)) * (-1.0 + (-1.0 / (x * (x * (x * x)))));
} else {
tmp = fma(2.0, (x * x), 2.0);
}
return tmp;
}
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / Float64(1.0 + x)) + Float64(-1.0 / Float64(x + -1.0))) <= 0.0) tmp = Float64(Float64(Float64(2.0 + Float64(2.0 / Float64(x * x))) / Float64(x * x)) * Float64(-1.0 + Float64(-1.0 / Float64(x * Float64(x * Float64(x * x)))))); else tmp = fma(2.0, Float64(x * x), 2.0); end return tmp end
code[x_] := If[LessEqual[N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.0], N[(N[(N[(2.0 + N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision] * N[(-1.0 + N[(-1.0 / N[(x * N[(x * N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(2.0 * N[(x * x), $MachinePrecision] + 2.0), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;\frac{1}{1 + x} + \frac{-1}{x + -1} \leq 0:\\
\;\;\;\;\frac{2 + \frac{2}{x \cdot x}}{x \cdot x} \cdot \left(-1 + \frac{-1}{x \cdot \left(x \cdot \left(x \cdot x\right)\right)}\right)\\
\mathbf{else}:\\
\;\;\;\;\mathsf{fma}\left(2, x \cdot x, 2\right)\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) (-.f64 x #s(literal 1 binary64)))) < 0.0Initial program 58.6%
Taylor expanded in x around inf
div-subN/A
sub-negN/A
mul-1-negN/A
+-commutativeN/A
associate-*r/N/A
associate-/r*N/A
times-fracN/A
distribute-rgt-outN/A
lower-*.f64N/A
Simplified98.9%
if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #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
unpow2N/A
lower-*.f64100.0
Simplified100.0%
Final simplification99.4%
(FPCore (x) :precision binary64 (if (<= x 2700.0) (+ (/ 1.0 (+ 1.0 x)) (/ -1.0 (+ x -1.0))) (/ (- -2.0 (/ 2.0 (* x x))) (* x x))))
double code(double x) {
double tmp;
if (x <= 2700.0) {
tmp = (1.0 / (1.0 + x)) + (-1.0 / (x + -1.0));
} else {
tmp = (-2.0 - (2.0 / (x * x))) / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 2700.0d0) then
tmp = (1.0d0 / (1.0d0 + x)) + ((-1.0d0) / (x + (-1.0d0)))
else
tmp = ((-2.0d0) - (2.0d0 / (x * x))) / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 2700.0) {
tmp = (1.0 / (1.0 + x)) + (-1.0 / (x + -1.0));
} else {
tmp = (-2.0 - (2.0 / (x * x))) / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 2700.0: tmp = (1.0 / (1.0 + x)) + (-1.0 / (x + -1.0)) else: tmp = (-2.0 - (2.0 / (x * x))) / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 2700.0) tmp = Float64(Float64(1.0 / Float64(1.0 + x)) + Float64(-1.0 / Float64(x + -1.0))); else tmp = Float64(Float64(-2.0 - Float64(2.0 / Float64(x * x))) / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 2700.0) tmp = (1.0 / (1.0 + x)) + (-1.0 / (x + -1.0)); else tmp = (-2.0 - (2.0 / (x * x))) / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 2700.0], N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(-2.0 - N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 2700:\\
\;\;\;\;\frac{1}{1 + x} + \frac{-1}{x + -1}\\
\mathbf{else}:\\
\;\;\;\;\frac{-2 - \frac{2}{x \cdot x}}{x \cdot x}\\
\end{array}
\end{array}
if x < 2700Initial program 86.1%
if 2700 < x Initial program 50.9%
Taylor expanded in x around inf
associate-*r/N/A
lower-/.f64N/A
neg-mul-1N/A
distribute-neg-inN/A
metadata-evalN/A
unsub-negN/A
lower--.f64N/A
associate-*r/N/A
metadata-evalN/A
lower-/.f64N/A
unpow2N/A
lower-*.f64N/A
unpow2N/A
lower-*.f6499.7
Simplified99.7%
Final simplification89.3%
(FPCore (x) :precision binary64 (if (<= x 390000.0) (+ (/ 1.0 (+ 1.0 x)) (/ -1.0 (+ x -1.0))) (/ -2.0 (* x x))))
double code(double x) {
double tmp;
if (x <= 390000.0) {
tmp = (1.0 / (1.0 + x)) + (-1.0 / (x + -1.0));
} else {
tmp = -2.0 / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= 390000.0d0) then
tmp = (1.0d0 / (1.0d0 + x)) + ((-1.0d0) / (x + (-1.0d0)))
else
tmp = (-2.0d0) / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= 390000.0) {
tmp = (1.0 / (1.0 + x)) + (-1.0 / (x + -1.0));
} else {
tmp = -2.0 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= 390000.0: tmp = (1.0 / (1.0 + x)) + (-1.0 / (x + -1.0)) else: tmp = -2.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= 390000.0) tmp = Float64(Float64(1.0 / Float64(1.0 + x)) + Float64(-1.0 / Float64(x + -1.0))); else tmp = Float64(-2.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= 390000.0) tmp = (1.0 / (1.0 + x)) + (-1.0 / (x + -1.0)); else tmp = -2.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, 390000.0], N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(-2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 390000:\\
\;\;\;\;\frac{1}{1 + x} + \frac{-1}{x + -1}\\
\mathbf{else}:\\
\;\;\;\;\frac{-2}{x \cdot x}\\
\end{array}
\end{array}
if x < 3.9e5Initial program 86.1%
if 3.9e5 < x Initial program 50.4%
Taylor expanded in x around inf
lower-/.f64N/A
unpow2N/A
lower-*.f6499.4
Simplified99.4%
Final simplification89.2%
(FPCore (x) :precision binary64 (if (<= x 1.0) (fma 2.0 (* x x) 2.0) (/ -2.0 (* x x))))
double code(double x) {
double tmp;
if (x <= 1.0) {
tmp = fma(2.0, (x * x), 2.0);
} else {
tmp = -2.0 / (x * x);
}
return tmp;
}
function code(x) tmp = 0.0 if (x <= 1.0) tmp = fma(2.0, Float64(x * x), 2.0); else tmp = Float64(-2.0 / Float64(x * x)); end return tmp end
code[x_] := If[LessEqual[x, 1.0], N[(2.0 * N[(x * x), $MachinePrecision] + 2.0), $MachinePrecision], N[(-2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq 1:\\
\;\;\;\;\mathsf{fma}\left(2, x \cdot x, 2\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{-2}{x \cdot x}\\
\end{array}
\end{array}
if x < 1Initial program 86.0%
Taylor expanded in x around 0
+-commutativeN/A
lower-fma.f64N/A
unpow2N/A
lower-*.f6461.7
Simplified61.7%
if 1 < x Initial program 52.3%
Taylor expanded in x around inf
lower-/.f64N/A
unpow2N/A
lower-*.f6497.1
Simplified97.1%
(FPCore (x) :precision binary64 2.0)
double code(double x) {
return 2.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0
end function
public static double code(double x) {
return 2.0;
}
def code(x): return 2.0
function code(x) return 2.0 end
function tmp = code(x) tmp = 2.0; end
code[x_] := 2.0
\begin{array}{l}
\\
2
\end{array}
Initial program 77.7%
Taylor expanded in x around 0
Simplified47.5%
herbie shell --seed 2024215
(FPCore (x)
:name "Asymptote A"
:precision binary64
(- (/ 1.0 (+ x 1.0)) (/ 1.0 (- x 1.0))))