
(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 (+ x 1.0)) (+ x -1.0)) x))
double code(double x) {
return ((2.0 / (x + 1.0)) / (x + -1.0)) / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = ((2.0d0 / (x + 1.0d0)) / (x + (-1.0d0))) / x
end function
public static double code(double x) {
return ((2.0 / (x + 1.0)) / (x + -1.0)) / x;
}
def code(x): return ((2.0 / (x + 1.0)) / (x + -1.0)) / x
function code(x) return Float64(Float64(Float64(2.0 / Float64(x + 1.0)) / Float64(x + -1.0)) / x) end
function tmp = code(x) tmp = ((2.0 / (x + 1.0)) / (x + -1.0)) / x; end
code[x_] := N[(N[(N[(2.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] / N[(x + -1.0), $MachinePrecision]), $MachinePrecision] / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{\frac{2}{x + 1}}{x + -1}}{x}
\end{array}
Initial program 80.1%
associate-+l-80.1%
sub-neg80.1%
neg-mul-180.1%
metadata-eval80.1%
cancel-sign-sub-inv80.1%
+-commutative80.1%
*-lft-identity80.1%
sub-neg80.1%
metadata-eval80.1%
Simplified80.1%
frac-sub56.8%
frac-sub58.6%
*-un-lft-identity58.6%
distribute-rgt-in58.6%
neg-mul-158.6%
sub-neg58.6%
*-rgt-identity58.6%
distribute-rgt-in58.6%
metadata-eval58.6%
metadata-eval58.6%
fma-def58.6%
metadata-eval58.6%
distribute-rgt-in58.6%
neg-mul-158.6%
sub-neg58.6%
Applied egg-rr58.6%
+-commutative58.6%
remove-double-neg58.6%
metadata-eval58.6%
distribute-neg-in58.6%
neg-mul-158.6%
*-commutative58.6%
fma-udef58.6%
distribute-lft-neg-in58.6%
distribute-lft-neg-in58.6%
fma-udef58.6%
*-commutative58.6%
neg-mul-158.6%
distribute-neg-in58.6%
remove-double-neg58.6%
metadata-eval58.6%
+-commutative58.6%
Simplified58.6%
Taylor expanded in x around 0 99.5%
associate-/r*99.8%
div-inv99.8%
*-un-lft-identity99.8%
distribute-rgt-out--99.8%
sub-neg99.8%
metadata-eval99.8%
Applied egg-rr99.8%
un-div-inv99.8%
*-commutative99.8%
associate-/r*99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 0.85))) (/ (/ 2.0 (* x x)) (+ x -1.0)) (- (* x -2.0) (/ 2.0 x))))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 0.85)) {
tmp = (2.0 / (x * x)) / (x + -1.0);
} else {
tmp = (x * -2.0) - (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 <= 0.85d0))) then
tmp = (2.0d0 / (x * x)) / (x + (-1.0d0))
else
tmp = (x * (-2.0d0)) - (2.0d0 / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 0.85)) {
tmp = (2.0 / (x * x)) / (x + -1.0);
} else {
tmp = (x * -2.0) - (2.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if (x <= -1.0) or not (x <= 0.85): tmp = (2.0 / (x * x)) / (x + -1.0) else: tmp = (x * -2.0) - (2.0 / x) return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 0.85)) tmp = Float64(Float64(2.0 / Float64(x * x)) / Float64(x + -1.0)); else tmp = Float64(Float64(x * -2.0) - Float64(2.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -1.0) || ~((x <= 0.85))) tmp = (2.0 / (x * x)) / (x + -1.0); else tmp = (x * -2.0) - (2.0 / x); end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 0.85]], $MachinePrecision]], N[(N[(2.0 / N[(x * x), $MachinePrecision]), $MachinePrecision] / N[(x + -1.0), $MachinePrecision]), $MachinePrecision], N[(N[(x * -2.0), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 0.85\right):\\
\;\;\;\;\frac{\frac{2}{x \cdot x}}{x + -1}\\
\mathbf{else}:\\
\;\;\;\;x \cdot -2 - \frac{2}{x}\\
\end{array}
\end{array}
if x < -1 or 0.849999999999999978 < x Initial program 62.8%
associate-+l-62.8%
sub-neg62.8%
neg-mul-162.8%
metadata-eval62.8%
cancel-sign-sub-inv62.8%
+-commutative62.8%
*-lft-identity62.8%
sub-neg62.8%
metadata-eval62.8%
Simplified62.8%
frac-sub19.3%
frac-sub22.6%
*-un-lft-identity22.6%
distribute-rgt-in22.6%
neg-mul-122.6%
sub-neg22.6%
*-rgt-identity22.6%
distribute-rgt-in22.6%
metadata-eval22.6%
metadata-eval22.6%
fma-def22.6%
metadata-eval22.6%
distribute-rgt-in22.6%
neg-mul-122.6%
sub-neg22.6%
Applied egg-rr22.6%
+-commutative22.6%
remove-double-neg22.6%
metadata-eval22.6%
distribute-neg-in22.6%
neg-mul-122.6%
*-commutative22.6%
fma-udef22.6%
distribute-lft-neg-in22.6%
distribute-lft-neg-in22.6%
fma-udef22.6%
*-commutative22.6%
neg-mul-122.6%
distribute-neg-in22.6%
remove-double-neg22.6%
metadata-eval22.6%
+-commutative22.6%
Simplified22.6%
Taylor expanded in x around 0 99.1%
expm1-log1p-u99.1%
expm1-udef62.5%
associate-/r*62.5%
*-un-lft-identity62.5%
distribute-rgt-out--62.5%
sub-neg62.5%
metadata-eval62.5%
Applied egg-rr62.5%
expm1-def99.7%
expm1-log1p99.7%
associate-/r*99.8%
associate-/l/99.8%
*-commutative99.8%
distribute-lft1-in99.8%
fma-udef99.8%
Simplified99.8%
Taylor expanded in x around inf 97.9%
unpow297.9%
Simplified97.9%
if -1 < x < 0.849999999999999978Initial 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 100.0%
associate-*r/100.0%
metadata-eval100.0%
Simplified100.0%
Final simplification98.9%
(FPCore (x) :precision binary64 (/ 2.0 (* (+ x 1.0) (- (* x x) x))))
double code(double x) {
return 2.0 / ((x + 1.0) * ((x * x) - x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = 2.0d0 / ((x + 1.0d0) * ((x * x) - x))
end function
public static double code(double x) {
return 2.0 / ((x + 1.0) * ((x * x) - x));
}
def code(x): return 2.0 / ((x + 1.0) * ((x * x) - x))
function code(x) return Float64(2.0 / Float64(Float64(x + 1.0) * Float64(Float64(x * x) - x))) end
function tmp = code(x) tmp = 2.0 / ((x + 1.0) * ((x * x) - x)); end
code[x_] := N[(2.0 / N[(N[(x + 1.0), $MachinePrecision] * N[(N[(x * x), $MachinePrecision] - x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{2}{\left(x + 1\right) \cdot \left(x \cdot x - x\right)}
\end{array}
Initial program 80.1%
associate-+l-80.1%
sub-neg80.1%
neg-mul-180.1%
metadata-eval80.1%
cancel-sign-sub-inv80.1%
+-commutative80.1%
*-lft-identity80.1%
sub-neg80.1%
metadata-eval80.1%
Simplified80.1%
frac-sub56.8%
frac-sub58.6%
*-un-lft-identity58.6%
distribute-rgt-in58.6%
neg-mul-158.6%
sub-neg58.6%
*-rgt-identity58.6%
distribute-rgt-in58.6%
metadata-eval58.6%
metadata-eval58.6%
fma-def58.6%
metadata-eval58.6%
distribute-rgt-in58.6%
neg-mul-158.6%
sub-neg58.6%
Applied egg-rr58.6%
+-commutative58.6%
remove-double-neg58.6%
metadata-eval58.6%
distribute-neg-in58.6%
neg-mul-158.6%
*-commutative58.6%
fma-udef58.6%
distribute-lft-neg-in58.6%
distribute-lft-neg-in58.6%
fma-udef58.6%
*-commutative58.6%
neg-mul-158.6%
distribute-neg-in58.6%
remove-double-neg58.6%
metadata-eval58.6%
+-commutative58.6%
Simplified58.6%
Taylor expanded in x around 0 99.5%
Final simplification99.5%
(FPCore (x) :precision binary64 (/ (/ 2.0 x) (* (+ x 1.0) (+ x -1.0))))
double code(double x) {
return (2.0 / x) / ((x + 1.0) * (x + -1.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (2.0d0 / x) / ((x + 1.0d0) * (x + (-1.0d0)))
end function
public static double code(double x) {
return (2.0 / x) / ((x + 1.0) * (x + -1.0));
}
def code(x): return (2.0 / x) / ((x + 1.0) * (x + -1.0))
function code(x) return Float64(Float64(2.0 / x) / Float64(Float64(x + 1.0) * Float64(x + -1.0))) end
function tmp = code(x) tmp = (2.0 / x) / ((x + 1.0) * (x + -1.0)); end
code[x_] := N[(N[(2.0 / x), $MachinePrecision] / N[(N[(x + 1.0), $MachinePrecision] * N[(x + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{2}{x}}{\left(x + 1\right) \cdot \left(x + -1\right)}
\end{array}
Initial program 80.1%
associate-+l-80.1%
sub-neg80.1%
neg-mul-180.1%
metadata-eval80.1%
cancel-sign-sub-inv80.1%
+-commutative80.1%
*-lft-identity80.1%
sub-neg80.1%
metadata-eval80.1%
Simplified80.1%
frac-sub56.8%
frac-sub58.6%
*-un-lft-identity58.6%
distribute-rgt-in58.6%
neg-mul-158.6%
sub-neg58.6%
*-rgt-identity58.6%
distribute-rgt-in58.6%
metadata-eval58.6%
metadata-eval58.6%
fma-def58.6%
metadata-eval58.6%
distribute-rgt-in58.6%
neg-mul-158.6%
sub-neg58.6%
Applied egg-rr58.6%
+-commutative58.6%
remove-double-neg58.6%
metadata-eval58.6%
distribute-neg-in58.6%
neg-mul-158.6%
*-commutative58.6%
fma-udef58.6%
distribute-lft-neg-in58.6%
distribute-lft-neg-in58.6%
fma-udef58.6%
*-commutative58.6%
neg-mul-158.6%
distribute-neg-in58.6%
remove-double-neg58.6%
metadata-eval58.6%
+-commutative58.6%
Simplified58.6%
Taylor expanded in x around 0 99.5%
associate-/r*99.8%
div-inv99.8%
*-un-lft-identity99.8%
distribute-rgt-out--99.8%
sub-neg99.8%
metadata-eval99.8%
Applied egg-rr99.8%
*-commutative99.8%
associate-/r*99.8%
frac-times99.8%
associate-/r/99.8%
clear-num99.8%
Applied egg-rr99.8%
Final simplification99.8%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 0.52))) (/ -2.0 (* x x)) (/ -2.0 x)))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 0.52)) {
tmp = -2.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 <= 0.52d0))) then
tmp = (-2.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 <= 0.52)) {
tmp = -2.0 / (x * x);
} else {
tmp = -2.0 / x;
}
return tmp;
}
def code(x): tmp = 0 if (x <= -1.0) or not (x <= 0.52): tmp = -2.0 / (x * x) else: tmp = -2.0 / x return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 0.52)) tmp = Float64(-2.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 <= 0.52))) tmp = -2.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, 0.52]], $MachinePrecision]], N[(-2.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 0.52\right):\\
\;\;\;\;\frac{-2}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;\frac{-2}{x}\\
\end{array}
\end{array}
if x < -1 or 0.52000000000000002 < x Initial program 62.8%
associate-+l-62.8%
sub-neg62.8%
neg-mul-162.8%
metadata-eval62.8%
cancel-sign-sub-inv62.8%
+-commutative62.8%
*-lft-identity62.8%
sub-neg62.8%
metadata-eval62.8%
Simplified62.8%
sub-neg62.8%
sub-neg62.8%
frac-2neg62.8%
metadata-eval62.8%
distribute-neg-frac62.8%
metadata-eval62.8%
+-commutative62.8%
distribute-neg-in62.8%
metadata-eval62.8%
sub-neg62.8%
Applied egg-rr62.8%
+-commutative62.8%
rem-square-sqrt17.8%
fabs-sqr17.8%
rem-square-sqrt36.6%
fabs-neg36.6%
rem-square-sqrt3.0%
fabs-sqr3.0%
rem-square-sqrt6.1%
associate-+l+6.1%
+-commutative6.1%
Simplified6.1%
Taylor expanded in x around inf 6.2%
associate-*r/6.2%
metadata-eval6.2%
associate-*r/6.2%
metadata-eval6.2%
unpow26.2%
Simplified6.2%
Taylor expanded in x around 0 44.5%
unpow244.5%
Simplified44.5%
if -1 < x < 0.52000000000000002Initial 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 99.8%
Final simplification70.2%
(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 80.1%
associate-+l-80.1%
sub-neg80.1%
neg-mul-180.1%
metadata-eval80.1%
cancel-sign-sub-inv80.1%
+-commutative80.1%
*-lft-identity80.1%
sub-neg80.1%
metadata-eval80.1%
Simplified80.1%
Taylor expanded in x around 0 48.3%
Taylor expanded in x around 0 79.2%
Final simplification79.2%
(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 80.1%
associate-+l-80.1%
sub-neg80.1%
neg-mul-180.1%
metadata-eval80.1%
cancel-sign-sub-inv80.1%
+-commutative80.1%
*-lft-identity80.1%
sub-neg80.1%
metadata-eval80.1%
Simplified80.1%
Taylor expanded in x around 0 48.9%
Final simplification48.9%
(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 2023215
(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))))