
(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 7 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 (/ 2.0 (* (- 1.0 x) (* x (- -1.0 x)))))
double code(double x) {
return 2.0 / ((1.0 - x) * (x * (-1.0 - x)));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 / ((1.0d0 - x) * (x * ((-1.0d0) - x)))
end function
public static double code(double x) {
return 2.0 / ((1.0 - x) * (x * (-1.0 - x)));
}
def code(x): return 2.0 / ((1.0 - x) * (x * (-1.0 - x)))
function code(x) return Float64(2.0 / Float64(Float64(1.0 - x) * Float64(x * Float64(-1.0 - x)))) end
function tmp = code(x) tmp = 2.0 / ((1.0 - x) * (x * (-1.0 - x))); end
code[x_] := N[(2.0 / N[(N[(1.0 - x), $MachinePrecision] * N[(x * N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{\left(1 - x\right) \cdot \left(x \cdot \left(-1 - x\right)\right)}
\end{array}
Initial program 83.2%
Simplified83.2%
clear-num83.2%
frac-2neg83.2%
neg-mul-183.2%
*-commutative83.2%
metadata-eval83.2%
metadata-eval83.2%
clear-num83.2%
frac-add60.3%
+-commutative60.3%
*-commutative60.3%
fma-def60.3%
metadata-eval60.3%
*-lft-identity60.3%
*-commutative60.3%
neg-mul-160.3%
*-commutative60.3%
+-commutative60.3%
distribute-lft-in60.3%
*-commutative60.3%
neg-mul-160.3%
*-commutative60.3%
neg-mul-160.3%
remove-double-neg60.3%
*-rgt-identity60.3%
*-commutative60.3%
Applied egg-rr60.3%
fma-udef60.3%
unsub-neg60.3%
+-commutative60.3%
distribute-rgt-in60.3%
metadata-eval60.3%
Simplified60.3%
frac-add61.4%
frac-2neg61.4%
*-lft-identity61.4%
fma-def60.4%
*-commutative60.4%
+-commutative60.4%
fma-def60.4%
+-commutative60.4%
associate-*r*60.4%
*-commutative60.4%
+-commutative60.4%
distribute-rgt-out60.4%
*-commutative60.4%
fma-def60.4%
*-lft-identity60.4%
Applied egg-rr60.4%
Simplified61.1%
remove-double-neg61.1%
sub-neg61.1%
distribute-neg-in61.1%
sub-neg61.1%
+-commutative61.1%
*-rgt-identity61.1%
sqr-neg61.1%
distribute-lft-in61.4%
sub-neg61.4%
distribute-lft-neg-in61.4%
remove-double-neg61.4%
remove-double-neg61.4%
+-commutative61.4%
*-commutative61.4%
Applied egg-rr61.4%
Taylor expanded in x around 0 99.4%
Final simplification99.4%
(FPCore (x) :precision binary64 (if (<= x -1.0) (/ -1.0 (* x x)) (if (<= x 1.0) (- (- x) (/ 2.0 x)) (/ 1.0 (* x x)))))
double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = -1.0 / (x * x);
} else if (x <= 1.0) {
tmp = -x - (2.0 / x);
} else {
tmp = 1.0 / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-1.0d0)) then
tmp = (-1.0d0) / (x * x)
else if (x <= 1.0d0) then
tmp = -x - (2.0d0 / x)
else
tmp = 1.0d0 / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = -1.0 / (x * x);
} else if (x <= 1.0) {
tmp = -x - (2.0 / x);
} else {
tmp = 1.0 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= -1.0: tmp = -1.0 / (x * x) elif x <= 1.0: tmp = -x - (2.0 / x) else: tmp = 1.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= -1.0) tmp = Float64(-1.0 / Float64(x * x)); elseif (x <= 1.0) tmp = Float64(Float64(-x) - Float64(2.0 / x)); else tmp = Float64(1.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -1.0) tmp = -1.0 / (x * x); elseif (x <= 1.0) tmp = -x - (2.0 / x); else tmp = 1.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -1.0], N[(-1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1.0], N[((-x) - N[(2.0 / x), $MachinePrecision]), $MachinePrecision], N[(1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;\frac{-1}{x \cdot x}\\
\mathbf{elif}\;x \leq 1:\\
\;\;\;\;\left(-x\right) - \frac{2}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot x}\\
\end{array}
\end{array}
if x < -1Initial program 68.7%
Simplified68.7%
Taylor expanded in x around inf 68.5%
Taylor expanded in x around inf 53.2%
unpow253.2%
Simplified53.2%
if -1 < x < 1Initial program 100.0%
Simplified100.0%
Taylor expanded in x around 0 99.0%
distribute-neg-in99.0%
metadata-eval99.0%
+-commutative99.0%
distribute-lft-neg-in99.0%
metadata-eval99.0%
associate-*r/99.0%
metadata-eval99.0%
Simplified99.0%
Taylor expanded in x around 0 99.0%
neg-mul-199.0%
associate-*r/99.0%
metadata-eval99.0%
Simplified99.0%
if 1 < x Initial program 64.2%
Simplified64.2%
flip-+24.2%
associate-/r/17.0%
metadata-eval17.0%
Applied egg-rr17.0%
Taylor expanded in x around inf 63.9%
Taylor expanded in x around inf 47.4%
unpow247.4%
Simplified47.4%
Final simplification74.7%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 1.0))) (/ -1.0 (* x x)) (/ -2.0 x)))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = -1.0 / (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 = (-1.0d0) / (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 = -1.0 / (x * x);
} else {
tmp = -2.0 / x;
}
return tmp;
}
def code(x): tmp = 0 if (x <= -1.0) or not (x <= 1.0): tmp = -1.0 / (x * x) else: tmp = -2.0 / x return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 1.0)) tmp = Float64(-1.0 / 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 = -1.0 / (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[(-1.0 / 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{-1}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;\frac{-2}{x}\\
\end{array}
\end{array}
if x < -1 or 1 < x Initial program 66.2%
Simplified66.2%
Taylor expanded in x around inf 65.5%
Taylor expanded in x around inf 49.0%
unpow249.0%
Simplified49.0%
if -1 < x < 1Initial program 100.0%
Simplified100.0%
Taylor expanded in x around 0 99.0%
Final simplification74.2%
(FPCore (x) :precision binary64 (if (<= x -1.0) (/ -1.0 (* x x)) (if (<= x 1.0) (/ -2.0 x) (/ 1.0 (* x x)))))
double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = -1.0 / (x * x);
} else if (x <= 1.0) {
tmp = -2.0 / x;
} else {
tmp = 1.0 / (x * x);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-1.0d0)) then
tmp = (-1.0d0) / (x * x)
else if (x <= 1.0d0) then
tmp = (-2.0d0) / x
else
tmp = 1.0d0 / (x * x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = -1.0 / (x * x);
} else if (x <= 1.0) {
tmp = -2.0 / x;
} else {
tmp = 1.0 / (x * x);
}
return tmp;
}
def code(x): tmp = 0 if x <= -1.0: tmp = -1.0 / (x * x) elif x <= 1.0: tmp = -2.0 / x else: tmp = 1.0 / (x * x) return tmp
function code(x) tmp = 0.0 if (x <= -1.0) tmp = Float64(-1.0 / Float64(x * x)); elseif (x <= 1.0) tmp = Float64(-2.0 / x); else tmp = Float64(1.0 / Float64(x * x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -1.0) tmp = -1.0 / (x * x); elseif (x <= 1.0) tmp = -2.0 / x; else tmp = 1.0 / (x * x); end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -1.0], N[(-1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision], If[LessEqual[x, 1.0], N[(-2.0 / x), $MachinePrecision], N[(1.0 / N[(x * x), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;\frac{-1}{x \cdot x}\\
\mathbf{elif}\;x \leq 1:\\
\;\;\;\;\frac{-2}{x}\\
\mathbf{else}:\\
\;\;\;\;\frac{1}{x \cdot x}\\
\end{array}
\end{array}
if x < -1Initial program 68.7%
Simplified68.7%
Taylor expanded in x around inf 68.5%
Taylor expanded in x around inf 53.2%
unpow253.2%
Simplified53.2%
if -1 < x < 1Initial program 100.0%
Simplified100.0%
Taylor expanded in x around 0 99.0%
if 1 < x Initial program 64.2%
Simplified64.2%
flip-+24.2%
associate-/r/17.0%
metadata-eval17.0%
Applied egg-rr17.0%
Taylor expanded in x around inf 63.9%
Taylor expanded in x around inf 47.4%
unpow247.4%
Simplified47.4%
Final simplification74.6%
(FPCore (x) :precision binary64 (+ 1.0 (+ -1.0 (/ -2.0 x))))
double code(double x) {
return 1.0 + (-1.0 + (-2.0 / x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 1.0d0 + ((-1.0d0) + ((-2.0d0) / x))
end function
public static double code(double x) {
return 1.0 + (-1.0 + (-2.0 / x));
}
def code(x): return 1.0 + (-1.0 + (-2.0 / x))
function code(x) return Float64(1.0 + Float64(-1.0 + Float64(-2.0 / x))) end
function tmp = code(x) tmp = 1.0 + (-1.0 + (-2.0 / x)); end
code[x_] := N[(1.0 + N[(-1.0 + N[(-2.0 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
1 + \left(-1 + \frac{-2}{x}\right)
\end{array}
Initial program 83.2%
Simplified83.2%
Taylor expanded in x around 0 51.5%
distribute-neg-in51.5%
metadata-eval51.5%
+-commutative51.5%
distribute-lft-neg-in51.5%
metadata-eval51.5%
associate-*r/51.5%
metadata-eval51.5%
Simplified51.5%
Taylor expanded in x around 0 82.3%
Final simplification82.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 83.2%
Simplified83.2%
Taylor expanded in x around 0 52.4%
Final simplification52.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 83.2%
Simplified83.2%
Taylor expanded in x around 0 51.5%
distribute-neg-in51.5%
metadata-eval51.5%
+-commutative51.5%
distribute-lft-neg-in51.5%
metadata-eval51.5%
associate-*r/51.5%
metadata-eval51.5%
Simplified51.5%
Taylor expanded in x around inf 3.2%
Final simplification3.2%
(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 2023297
(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))))