
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / (x + 1.0d0)) - (2.0d0 / x)) + (1.0d0 / (x - 1.0d0))
end function
public static double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
def code(x): return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0))
function code(x) return Float64(Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x - 1.0))) end
function tmp = code(x) tmp = ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0)); end
code[x_] := N[(N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\end{array}
Sampling outcomes in binary64 precision:
Herbie found 6 alternatives:
| Alternative | Accuracy | Speedup |
|---|
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))
double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / (x + 1.0d0)) - (2.0d0 / x)) + (1.0d0 / (x - 1.0d0))
end function
public static double code(double x) {
return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0));
}
def code(x): return ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0))
function code(x) return Float64(Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x - 1.0))) end
function tmp = code(x) tmp = ((1.0 / (x + 1.0)) - (2.0 / x)) + (1.0 / (x - 1.0)); end
code[x_] := N[(N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{1}{x + 1} - \frac{2}{x}\right) + \frac{1}{x - 1}
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (+ (- (/ 1.0 (+ 1.0 x)) (/ 2.0 x)) (/ 1.0 (+ x -1.0)))))
(if (<= t_0 -0.002)
(fma (/ 1.0 (- 1.0 (* x x))) (- 1.0 x) (- (/ -1.0 (- 1.0 x)) (/ 2.0 x)))
(if (<= t_0 2e-24) (/ 2.0 (pow x 3.0)) t_0))))
double code(double x) {
double t_0 = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0));
double tmp;
if (t_0 <= -0.002) {
tmp = fma((1.0 / (1.0 - (x * x))), (1.0 - x), ((-1.0 / (1.0 - x)) - (2.0 / x)));
} else if (t_0 <= 2e-24) {
tmp = 2.0 / pow(x, 3.0);
} else {
tmp = t_0;
}
return tmp;
}
function code(x) t_0 = Float64(Float64(Float64(1.0 / Float64(1.0 + x)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x + -1.0))) tmp = 0.0 if (t_0 <= -0.002) tmp = fma(Float64(1.0 / Float64(1.0 - Float64(x * x))), Float64(1.0 - x), Float64(Float64(-1.0 / Float64(1.0 - x)) - Float64(2.0 / x))); elseif (t_0 <= 2e-24) tmp = Float64(2.0 / (x ^ 3.0)); else tmp = t_0; end return tmp end
code[x_] := Block[{t$95$0 = N[(N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -0.002], N[(N[(1.0 / N[(1.0 - N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(1.0 - x), $MachinePrecision] + N[(N[(-1.0 / N[(1.0 - x), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 2e-24], N[(2.0 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x + -1}\\
\mathbf{if}\;t_0 \leq -0.002:\\
\;\;\;\;\mathsf{fma}\left(\frac{1}{1 - x \cdot x}, 1 - x, \frac{-1}{1 - x} - \frac{2}{x}\right)\\
\mathbf{elif}\;t_0 \leq 2 \cdot 10^{-24}:\\
\;\;\;\;\frac{2}{{x}^{3}}\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
\end{array}
if (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < -2e-3Initial program 99.9%
associate-+l-99.9%
sub-neg99.9%
neg-mul-199.9%
metadata-eval99.9%
cancel-sign-sub-inv99.9%
+-commutative99.9%
*-lft-identity99.9%
sub-neg99.9%
metadata-eval99.9%
Simplified99.9%
flip-+99.9%
sub-neg99.9%
metadata-eval99.9%
distribute-neg-in99.9%
+-commutative99.9%
associate-/r/99.9%
fma-neg100.0%
metadata-eval100.0%
+-commutative100.0%
distribute-neg-in100.0%
metadata-eval100.0%
sub-neg100.0%
sub-neg100.0%
frac-2neg100.0%
metadata-eval100.0%
distribute-neg-frac100.0%
metadata-eval100.0%
+-commutative100.0%
Applied egg-rr100.0%
distribute-neg-in100.0%
distribute-neg-frac100.0%
metadata-eval100.0%
Simplified100.0%
if -2e-3 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 1.99999999999999985e-24Initial program 72.8%
associate-+l-72.8%
sub-neg72.8%
neg-mul-172.8%
metadata-eval72.8%
cancel-sign-sub-inv72.8%
+-commutative72.8%
*-lft-identity72.8%
sub-neg72.8%
metadata-eval72.8%
Simplified72.8%
Taylor expanded in x around inf 98.9%
if 1.99999999999999985e-24 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) Initial program 100.0%
Final simplification99.4%
(FPCore (x) :precision binary64 (let* ((t_0 (+ (- (/ 1.0 (+ 1.0 x)) (/ 2.0 x)) (/ 1.0 (+ x -1.0))))) (if (or (<= t_0 -0.002) (not (<= t_0 2e-24))) t_0 (/ 2.0 (pow x 3.0)))))
double code(double x) {
double t_0 = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0));
double tmp;
if ((t_0 <= -0.002) || !(t_0 <= 2e-24)) {
tmp = t_0;
} else {
tmp = 2.0 / pow(x, 3.0);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: tmp
t_0 = ((1.0d0 / (1.0d0 + x)) - (2.0d0 / x)) + (1.0d0 / (x + (-1.0d0)))
if ((t_0 <= (-0.002d0)) .or. (.not. (t_0 <= 2d-24))) then
tmp = t_0
else
tmp = 2.0d0 / (x ** 3.0d0)
end if
code = tmp
end function
public static double code(double x) {
double t_0 = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0));
double tmp;
if ((t_0 <= -0.002) || !(t_0 <= 2e-24)) {
tmp = t_0;
} else {
tmp = 2.0 / Math.pow(x, 3.0);
}
return tmp;
}
def code(x): t_0 = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0)) tmp = 0 if (t_0 <= -0.002) or not (t_0 <= 2e-24): tmp = t_0 else: tmp = 2.0 / math.pow(x, 3.0) return tmp
function code(x) t_0 = Float64(Float64(Float64(1.0 / Float64(1.0 + x)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x + -1.0))) tmp = 0.0 if ((t_0 <= -0.002) || !(t_0 <= 2e-24)) tmp = t_0; else tmp = Float64(2.0 / (x ^ 3.0)); end return tmp end
function tmp_2 = code(x) t_0 = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0)); tmp = 0.0; if ((t_0 <= -0.002) || ~((t_0 <= 2e-24))) tmp = t_0; else tmp = 2.0 / (x ^ 3.0); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[t$95$0, -0.002], N[Not[LessEqual[t$95$0, 2e-24]], $MachinePrecision]], t$95$0, N[(2.0 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x + -1}\\
\mathbf{if}\;t_0 \leq -0.002 \lor \neg \left(t_0 \leq 2 \cdot 10^{-24}\right):\\
\;\;\;\;t_0\\
\mathbf{else}:\\
\;\;\;\;\frac{2}{{x}^{3}}\\
\end{array}
\end{array}
if (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < -2e-3 or 1.99999999999999985e-24 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) Initial program 99.9%
if -2e-3 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 1.99999999999999985e-24Initial program 72.8%
associate-+l-72.8%
sub-neg72.8%
neg-mul-172.8%
metadata-eval72.8%
cancel-sign-sub-inv72.8%
+-commutative72.8%
*-lft-identity72.8%
sub-neg72.8%
metadata-eval72.8%
Simplified72.8%
Taylor expanded in x around inf 98.9%
Final simplification99.4%
(FPCore (x) :precision binary64 (+ (- (/ 1.0 (+ 1.0 x)) (/ 2.0 x)) (/ 1.0 (+ x -1.0))))
double code(double x) {
return ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((1.0d0 / (1.0d0 + x)) - (2.0d0 / x)) + (1.0d0 / (x + (-1.0d0)))
end function
public static double code(double x) {
return ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0));
}
def code(x): return ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0))
function code(x) return Float64(Float64(Float64(1.0 / Float64(1.0 + x)) - Float64(2.0 / x)) + Float64(1.0 / Float64(x + -1.0))) end
function tmp = code(x) tmp = ((1.0 / (1.0 + x)) - (2.0 / x)) + (1.0 / (x + -1.0)); end
code[x_] := N[(N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\left(\frac{1}{1 + x} - \frac{2}{x}\right) + \frac{1}{x + -1}
\end{array}
Initial program 85.9%
Final simplification85.9%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 1.0))) (/ -0.3333333333333333 (* x x)) (/ -2.0 x)))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = -0.3333333333333333 / (x * x);
} else {
tmp = -2.0 / x;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if ((x <= (-1.0d0)) .or. (.not. (x <= 1.0d0))) then
tmp = (-0.3333333333333333d0) / (x * x)
else
tmp = (-2.0d0) / x
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = -0.3333333333333333 / (x * x);
} else {
tmp = -2.0 / x;
}
return tmp;
}
def code(x): tmp = 0 if (x <= -1.0) or not (x <= 1.0): tmp = -0.3333333333333333 / (x * x) else: tmp = -2.0 / x return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 1.0)) tmp = Float64(-0.3333333333333333 / Float64(x * x)); else tmp = Float64(-2.0 / x); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -1.0) || ~((x <= 1.0))) tmp = -0.3333333333333333 / (x * x); else tmp = -2.0 / x; end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], N[(-0.3333333333333333 / N[(x * x), $MachinePrecision]), $MachinePrecision], N[(-2.0 / x), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;\frac{-0.3333333333333333}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;\frac{-2}{x}\\
\end{array}
\end{array}
if x < -1 or 1 < x Initial program 73.0%
associate-+l-73.0%
sub-neg73.0%
neg-mul-173.0%
metadata-eval73.0%
cancel-sign-sub-inv73.0%
+-commutative73.0%
*-lft-identity73.0%
sub-neg73.0%
metadata-eval73.0%
Simplified73.0%
sub-neg73.0%
flip-+13.8%
Applied egg-rr12.7%
associate-*r/12.8%
*-rgt-identity12.8%
sub-neg12.8%
distribute-neg-frac12.8%
metadata-eval12.8%
Simplified12.8%
Taylor expanded in x around inf 15.7%
Taylor expanded in x around inf 58.8%
unpow258.8%
Simplified58.8%
if -1 < x < 1Initial program 100.0%
associate-+l-100.0%
sub-neg100.0%
neg-mul-1100.0%
metadata-eval100.0%
cancel-sign-sub-inv100.0%
+-commutative100.0%
*-lft-identity100.0%
sub-neg100.0%
metadata-eval100.0%
Simplified100.0%
Taylor expanded in x around 0 97.7%
Final simplification77.3%
(FPCore (x) :precision binary64 (- 1.0 (- (/ 2.0 x) -1.0)))
double code(double x) {
return 1.0 - ((2.0 / x) - -1.0);
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 - ((2.0d0 / x) - (-1.0d0))
end function
public static double code(double x) {
return 1.0 - ((2.0 / x) - -1.0);
}
def code(x): return 1.0 - ((2.0 / x) - -1.0)
function code(x) return Float64(1.0 - Float64(Float64(2.0 / x) - -1.0)) end
function tmp = code(x) tmp = 1.0 - ((2.0 / x) - -1.0); end
code[x_] := N[(1.0 - N[(N[(2.0 / x), $MachinePrecision] - -1.0), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 - \left(\frac{2}{x} - -1\right)
\end{array}
Initial program 85.9%
associate-+l-85.9%
sub-neg85.9%
neg-mul-185.9%
metadata-eval85.9%
cancel-sign-sub-inv85.9%
+-commutative85.9%
*-lft-identity85.9%
sub-neg85.9%
metadata-eval85.9%
Simplified85.9%
Taylor expanded in x around 0 48.3%
Taylor expanded in x around 0 84.3%
Final simplification84.3%
(FPCore (x) :precision binary64 (/ -2.0 x))
double code(double x) {
return -2.0 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-2.0d0) / x
end function
public static double code(double x) {
return -2.0 / x;
}
def code(x): return -2.0 / x
function code(x) return Float64(-2.0 / x) end
function tmp = code(x) tmp = -2.0 / x; end
code[x_] := N[(-2.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2}{x}
\end{array}
Initial program 85.9%
associate-+l-85.9%
sub-neg85.9%
neg-mul-185.9%
metadata-eval85.9%
cancel-sign-sub-inv85.9%
+-commutative85.9%
*-lft-identity85.9%
sub-neg85.9%
metadata-eval85.9%
Simplified85.9%
Taylor expanded in x around 0 49.4%
Final simplification49.4%
(FPCore (x) :precision binary64 (/ 2.0 (* x (- (* x x) 1.0))))
double code(double x) {
return 2.0 / (x * ((x * x) - 1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 / (x * ((x * x) - 1.0d0))
end function
public static double code(double x) {
return 2.0 / (x * ((x * x) - 1.0));
}
def code(x): return 2.0 / (x * ((x * x) - 1.0))
function code(x) return Float64(2.0 / Float64(x * Float64(Float64(x * x) - 1.0))) end
function tmp = code(x) tmp = 2.0 / (x * ((x * x) - 1.0)); end
code[x_] := N[(2.0 / N[(x * N[(N[(x * x), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{x \cdot \left(x \cdot x - 1\right)}
\end{array}
herbie shell --seed 2023238
(FPCore (x)
:name "3frac (problem 3.3.3)"
:precision binary64
:herbie-target
(/ 2.0 (* x (- (* x x) 1.0)))
(+ (- (/ 1.0 (+ x 1.0)) (/ 2.0 x)) (/ 1.0 (- x 1.0))))