
(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 9 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 (* (/ (/ 1.0 (- 1.0 x)) (fma x x x)) -2.0))
double code(double x) {
return ((1.0 / (1.0 - x)) / fma(x, x, x)) * -2.0;
}
function code(x) return Float64(Float64(Float64(1.0 / Float64(1.0 - x)) / fma(x, x, x)) * -2.0) end
code[x_] := N[(N[(N[(1.0 / N[(1.0 - x), $MachinePrecision]), $MachinePrecision] / N[(x * x + x), $MachinePrecision]), $MachinePrecision] * -2.0), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{1 - x}}{\mathsf{fma}\left(x, x, x\right)} \cdot -2
\end{array}
Initial program 83.2%
frac-sub62.5%
frac-2neg62.5%
metadata-eval62.5%
frac-add63.3%
Applied egg-rr62.5%
*-commutative62.5%
neg-mul-162.5%
fma-neg63.3%
*-commutative63.3%
Simplified63.3%
Taylor expanded in x around 0 99.4%
clear-num99.4%
associate-/r/99.4%
*-commutative99.4%
associate-/r*99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x)
:precision binary64
(let* ((t_0 (/ 1.0 (+ x -1.0))) (t_1 (+ (- (/ 1.0 (+ 1.0 x)) (/ 2.0 x)) t_0)))
(if (<= t_1 -5000.0)
(+ t_0 (* (+ x 2.0) (/ -1.0 (* x (+ 1.0 x)))))
(if (<= t_1 0.0) (/ 2.0 (pow x 3.0)) t_1))))
double code(double x) {
double t_0 = 1.0 / (x + -1.0);
double t_1 = ((1.0 / (1.0 + x)) - (2.0 / x)) + t_0;
double tmp;
if (t_1 <= -5000.0) {
tmp = t_0 + ((x + 2.0) * (-1.0 / (x * (1.0 + x))));
} else if (t_1 <= 0.0) {
tmp = 2.0 / pow(x, 3.0);
} else {
tmp = t_1;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: t_0
real(8) :: t_1
real(8) :: tmp
t_0 = 1.0d0 / (x + (-1.0d0))
t_1 = ((1.0d0 / (1.0d0 + x)) - (2.0d0 / x)) + t_0
if (t_1 <= (-5000.0d0)) then
tmp = t_0 + ((x + 2.0d0) * ((-1.0d0) / (x * (1.0d0 + x))))
else if (t_1 <= 0.0d0) then
tmp = 2.0d0 / (x ** 3.0d0)
else
tmp = t_1
end if
code = tmp
end function
public static double code(double x) {
double t_0 = 1.0 / (x + -1.0);
double t_1 = ((1.0 / (1.0 + x)) - (2.0 / x)) + t_0;
double tmp;
if (t_1 <= -5000.0) {
tmp = t_0 + ((x + 2.0) * (-1.0 / (x * (1.0 + x))));
} else if (t_1 <= 0.0) {
tmp = 2.0 / Math.pow(x, 3.0);
} else {
tmp = t_1;
}
return tmp;
}
def code(x): t_0 = 1.0 / (x + -1.0) t_1 = ((1.0 / (1.0 + x)) - (2.0 / x)) + t_0 tmp = 0 if t_1 <= -5000.0: tmp = t_0 + ((x + 2.0) * (-1.0 / (x * (1.0 + x)))) elif t_1 <= 0.0: tmp = 2.0 / math.pow(x, 3.0) else: tmp = t_1 return tmp
function code(x) t_0 = Float64(1.0 / Float64(x + -1.0)) t_1 = Float64(Float64(Float64(1.0 / Float64(1.0 + x)) - Float64(2.0 / x)) + t_0) tmp = 0.0 if (t_1 <= -5000.0) tmp = Float64(t_0 + Float64(Float64(x + 2.0) * Float64(-1.0 / Float64(x * Float64(1.0 + x))))); elseif (t_1 <= 0.0) tmp = Float64(2.0 / (x ^ 3.0)); else tmp = t_1; end return tmp end
function tmp_2 = code(x) t_0 = 1.0 / (x + -1.0); t_1 = ((1.0 / (1.0 + x)) - (2.0 / x)) + t_0; tmp = 0.0; if (t_1 <= -5000.0) tmp = t_0 + ((x + 2.0) * (-1.0 / (x * (1.0 + x)))); elseif (t_1 <= 0.0) tmp = 2.0 / (x ^ 3.0); else tmp = t_1; end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(1.0 / N[(x + -1.0), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(2.0 / x), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]}, If[LessEqual[t$95$1, -5000.0], N[(t$95$0 + N[(N[(x + 2.0), $MachinePrecision] * N[(-1.0 / N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$1, 0.0], N[(2.0 / N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision], t$95$1]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{x + -1}\\
t_1 := \left(\frac{1}{1 + x} - \frac{2}{x}\right) + t_0\\
\mathbf{if}\;t_1 \leq -5000:\\
\;\;\;\;t_0 + \left(x + 2\right) \cdot \frac{-1}{x \cdot \left(1 + x\right)}\\
\mathbf{elif}\;t_1 \leq 0:\\
\;\;\;\;\frac{2}{{x}^{3}}\\
\mathbf{else}:\\
\;\;\;\;t_1\\
\end{array}
\end{array}
if (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < -5e3Initial program 100.0%
frac-sub99.9%
div-inv100.0%
/-rgt-identity100.0%
*-un-lft-identity100.0%
/-rgt-identity100.0%
+-commutative100.0%
*-commutative100.0%
+-commutative100.0%
Applied egg-rr100.0%
Taylor expanded in x around 0 100.0%
if -5e3 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) < 0.0Initial program 63.0%
Taylor expanded in x around inf 98.3%
if 0.0 < (+.f64 (-.f64 (/.f64 1 (+.f64 x 1)) (/.f64 2 x)) (/.f64 1 (-.f64 x 1))) Initial program 100.0%
Final simplification99.2%
(FPCore (x) :precision binary64 (* (/ 1.0 (- 1.0 x)) (/ -2.0 (fma x x x))))
double code(double x) {
return (1.0 / (1.0 - x)) * (-2.0 / fma(x, x, x));
}
function code(x) return Float64(Float64(1.0 / Float64(1.0 - x)) * Float64(-2.0 / fma(x, x, x))) end
code[x_] := N[(N[(1.0 / N[(1.0 - x), $MachinePrecision]), $MachinePrecision] * N[(-2.0 / N[(x * x + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{1 - x} \cdot \frac{-2}{\mathsf{fma}\left(x, x, x\right)}
\end{array}
Initial program 83.2%
frac-sub62.5%
frac-2neg62.5%
metadata-eval62.5%
frac-add63.3%
Applied egg-rr62.5%
*-commutative62.5%
neg-mul-162.5%
fma-neg63.3%
*-commutative63.3%
Simplified63.3%
Taylor expanded in x around 0 99.4%
associate-/r*99.9%
div-inv99.9%
Applied egg-rr99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (/ -2.0 (- x (pow x 3.0))))
double code(double x) {
return -2.0 / (x - pow(x, 3.0));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-2.0d0) / (x - (x ** 3.0d0))
end function
public static double code(double x) {
return -2.0 / (x - Math.pow(x, 3.0));
}
def code(x): return -2.0 / (x - math.pow(x, 3.0))
function code(x) return Float64(-2.0 / Float64(x - (x ^ 3.0))) end
function tmp = code(x) tmp = -2.0 / (x - (x ^ 3.0)); end
code[x_] := N[(-2.0 / N[(x - N[Power[x, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-2}{x - {x}^{3}}
\end{array}
Initial program 83.2%
frac-sub62.5%
frac-2neg62.5%
metadata-eval62.5%
frac-add63.3%
Applied egg-rr62.5%
*-commutative62.5%
neg-mul-162.5%
fma-neg63.3%
*-commutative63.3%
Simplified63.3%
Taylor expanded in x around 0 99.4%
Taylor expanded in x around 0 99.5%
+-commutative99.5%
mul-1-neg99.5%
unsub-neg99.5%
Simplified99.5%
Final simplification99.5%
(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 83.2%
Final simplification83.2%
(FPCore (x) :precision binary64 (if (or (<= x -0.38) (not (<= x 1.0))) (/ 1.0 (* x x)) (/ -2.0 x)))
double code(double x) {
double tmp;
if ((x <= -0.38) || !(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 <= (-0.38d0)) .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 <= -0.38) || !(x <= 1.0)) {
tmp = 1.0 / (x * x);
} else {
tmp = -2.0 / x;
}
return tmp;
}
def code(x): tmp = 0 if (x <= -0.38) 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 <= -0.38) || !(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 <= -0.38) || ~((x <= 1.0))) tmp = 1.0 / (x * x); else tmp = -2.0 / x; end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -0.38], 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 -0.38 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;\frac{1}{x \cdot x}\\
\mathbf{else}:\\
\;\;\;\;\frac{-2}{x}\\
\end{array}
\end{array}
if x < -0.38 or 1 < x Initial program 63.7%
Taylor expanded in x around inf 61.3%
Taylor expanded in x around inf 48.2%
unpow248.2%
Simplified48.2%
if -0.38 < x < 1Initial program 100.0%
Taylor expanded in x around 0 99.3%
Final simplification75.7%
(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%
Taylor expanded in x around 0 55.3%
Taylor expanded in x around 0 81.7%
Final simplification81.7%
(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%
Taylor expanded in x around 0 55.9%
Final simplification55.9%
(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%
Taylor expanded in x around 0 55.3%
Taylor expanded in x around inf 3.4%
Final simplification3.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 2023228
(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))))