
(FPCore (x) :precision binary64 (- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))
double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x + 1.0d0)) - (1.0d0 / x)
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
def code(x): return (1.0 / (x + 1.0)) - (1.0 / x)
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / x)) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) - (1.0 / x); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} - \frac{1}{x}
\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)))
double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (x + 1.0d0)) - (1.0d0 / x)
end function
public static double code(double x) {
return (1.0 / (x + 1.0)) - (1.0 / x);
}
def code(x): return (1.0 / (x + 1.0)) - (1.0 / x)
function code(x) return Float64(Float64(1.0 / Float64(x + 1.0)) - Float64(1.0 / x)) end
function tmp = code(x) tmp = (1.0 / (x + 1.0)) - (1.0 / x); end
code[x_] := N[(N[(1.0 / N[(x + 1.0), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{x + 1} - \frac{1}{x}
\end{array}
(FPCore (x)
:precision binary64
(let* ((t_0 (+ (/ 1.0 (+ 1.0 x)) (/ -1.0 x))))
(if (<= t_0 -20000000000.0)
(+ 1.0 (/ -1.0 x))
(if (<= t_0 0.0) (- (pow x -2.0)) (+ (- 1.0 x) (/ -1.0 x))))))
double code(double x) {
double t_0 = (1.0 / (1.0 + x)) + (-1.0 / x);
double tmp;
if (t_0 <= -20000000000.0) {
tmp = 1.0 + (-1.0 / x);
} else if (t_0 <= 0.0) {
tmp = -pow(x, -2.0);
} else {
tmp = (1.0 - x) + (-1.0 / x);
}
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)) + ((-1.0d0) / x)
if (t_0 <= (-20000000000.0d0)) then
tmp = 1.0d0 + ((-1.0d0) / x)
else if (t_0 <= 0.0d0) then
tmp = -(x ** (-2.0d0))
else
tmp = (1.0d0 - x) + ((-1.0d0) / x)
end if
code = tmp
end function
public static double code(double x) {
double t_0 = (1.0 / (1.0 + x)) + (-1.0 / x);
double tmp;
if (t_0 <= -20000000000.0) {
tmp = 1.0 + (-1.0 / x);
} else if (t_0 <= 0.0) {
tmp = -Math.pow(x, -2.0);
} else {
tmp = (1.0 - x) + (-1.0 / x);
}
return tmp;
}
def code(x): t_0 = (1.0 / (1.0 + x)) + (-1.0 / x) tmp = 0 if t_0 <= -20000000000.0: tmp = 1.0 + (-1.0 / x) elif t_0 <= 0.0: tmp = -math.pow(x, -2.0) else: tmp = (1.0 - x) + (-1.0 / x) return tmp
function code(x) t_0 = Float64(Float64(1.0 / Float64(1.0 + x)) + Float64(-1.0 / x)) tmp = 0.0 if (t_0 <= -20000000000.0) tmp = Float64(1.0 + Float64(-1.0 / x)); elseif (t_0 <= 0.0) tmp = Float64(-(x ^ -2.0)); else tmp = Float64(Float64(1.0 - x) + Float64(-1.0 / x)); end return tmp end
function tmp_2 = code(x) t_0 = (1.0 / (1.0 + x)) + (-1.0 / x); tmp = 0.0; if (t_0 <= -20000000000.0) tmp = 1.0 + (-1.0 / x); elseif (t_0 <= 0.0) tmp = -(x ^ -2.0); else tmp = (1.0 - x) + (-1.0 / x); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -20000000000.0], N[(1.0 + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 0.0], (-N[Power[x, -2.0], $MachinePrecision]), N[(N[(1.0 - x), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{1 + x} + \frac{-1}{x}\\
\mathbf{if}\;t\_0 \leq -20000000000:\\
\;\;\;\;1 + \frac{-1}{x}\\
\mathbf{elif}\;t\_0 \leq 0:\\
\;\;\;\;-{x}^{-2}\\
\mathbf{else}:\\
\;\;\;\;\left(1 - x\right) + \frac{-1}{x}\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < -2e10Initial program 100.0%
Taylor expanded in x around 0 100.0%
if -2e10 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < 0.0Initial program 55.6%
Taylor expanded in x around inf 99.8%
unpow299.8%
associate-/r*99.8%
*-lft-identity99.8%
associate-*l/99.6%
metadata-eval99.6%
distribute-neg-frac99.6%
distribute-rgt-neg-out99.6%
unpow-199.6%
unpow-199.6%
pow-sqr100.0%
metadata-eval100.0%
Simplified100.0%
if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) Initial program 100.0%
Taylor expanded in x around 0 100.0%
neg-mul-1100.0%
sub-neg100.0%
Simplified100.0%
Final simplification100.0%
(FPCore (x)
:precision binary64
(let* ((t_0 (+ (/ 1.0 (+ 1.0 x)) (/ -1.0 x))))
(if (<= t_0 -20000000000.0)
(+ 1.0 (/ -1.0 x))
(if (<= t_0 0.0) (/ (/ -1.0 x) x) (+ (- 1.0 x) (/ -1.0 x))))))
double code(double x) {
double t_0 = (1.0 / (1.0 + x)) + (-1.0 / x);
double tmp;
if (t_0 <= -20000000000.0) {
tmp = 1.0 + (-1.0 / x);
} else if (t_0 <= 0.0) {
tmp = (-1.0 / x) / x;
} else {
tmp = (1.0 - x) + (-1.0 / x);
}
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)) + ((-1.0d0) / x)
if (t_0 <= (-20000000000.0d0)) then
tmp = 1.0d0 + ((-1.0d0) / x)
else if (t_0 <= 0.0d0) then
tmp = ((-1.0d0) / x) / x
else
tmp = (1.0d0 - x) + ((-1.0d0) / x)
end if
code = tmp
end function
public static double code(double x) {
double t_0 = (1.0 / (1.0 + x)) + (-1.0 / x);
double tmp;
if (t_0 <= -20000000000.0) {
tmp = 1.0 + (-1.0 / x);
} else if (t_0 <= 0.0) {
tmp = (-1.0 / x) / x;
} else {
tmp = (1.0 - x) + (-1.0 / x);
}
return tmp;
}
def code(x): t_0 = (1.0 / (1.0 + x)) + (-1.0 / x) tmp = 0 if t_0 <= -20000000000.0: tmp = 1.0 + (-1.0 / x) elif t_0 <= 0.0: tmp = (-1.0 / x) / x else: tmp = (1.0 - x) + (-1.0 / x) return tmp
function code(x) t_0 = Float64(Float64(1.0 / Float64(1.0 + x)) + Float64(-1.0 / x)) tmp = 0.0 if (t_0 <= -20000000000.0) tmp = Float64(1.0 + Float64(-1.0 / x)); elseif (t_0 <= 0.0) tmp = Float64(Float64(-1.0 / x) / x); else tmp = Float64(Float64(1.0 - x) + Float64(-1.0 / x)); end return tmp end
function tmp_2 = code(x) t_0 = (1.0 / (1.0 + x)) + (-1.0 / x); tmp = 0.0; if (t_0 <= -20000000000.0) tmp = 1.0 + (-1.0 / x); elseif (t_0 <= 0.0) tmp = (-1.0 / x) / x; else tmp = (1.0 - x) + (-1.0 / x); end tmp_2 = tmp; end
code[x_] := Block[{t$95$0 = N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, -20000000000.0], N[(1.0 + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 0.0], N[(N[(-1.0 / x), $MachinePrecision] / x), $MachinePrecision], N[(N[(1.0 - x), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{1 + x} + \frac{-1}{x}\\
\mathbf{if}\;t\_0 \leq -20000000000:\\
\;\;\;\;1 + \frac{-1}{x}\\
\mathbf{elif}\;t\_0 \leq 0:\\
\;\;\;\;\frac{\frac{-1}{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;\left(1 - x\right) + \frac{-1}{x}\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < -2e10Initial program 100.0%
Taylor expanded in x around 0 100.0%
if -2e10 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < 0.0Initial program 55.6%
frac-sub55.6%
*-rgt-identity55.6%
metadata-eval55.6%
div-inv55.6%
associate-/r*55.6%
*-un-lft-identity55.6%
*-rgt-identity55.6%
+-commutative55.6%
div-inv55.6%
metadata-eval55.6%
*-rgt-identity55.6%
+-commutative55.6%
Applied egg-rr55.6%
Taylor expanded in x around inf 99.8%
if 0.0 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) Initial program 100.0%
Taylor expanded in x around 0 100.0%
neg-mul-1100.0%
sub-neg100.0%
Simplified100.0%
Final simplification99.9%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 0.75))) (/ (/ -1.0 x) x) (+ 1.0 (/ -1.0 x))))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 0.75)) {
tmp = (-1.0 / x) / x;
} else {
tmp = 1.0 + (-1.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.75d0))) then
tmp = ((-1.0d0) / x) / x
else
tmp = 1.0d0 + ((-1.0d0) / x)
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 0.75)) {
tmp = (-1.0 / x) / x;
} else {
tmp = 1.0 + (-1.0 / x);
}
return tmp;
}
def code(x): tmp = 0 if (x <= -1.0) or not (x <= 0.75): tmp = (-1.0 / x) / x else: tmp = 1.0 + (-1.0 / x) return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 0.75)) tmp = Float64(Float64(-1.0 / x) / x); else tmp = Float64(1.0 + Float64(-1.0 / x)); end return tmp end
function tmp_2 = code(x) tmp = 0.0; if ((x <= -1.0) || ~((x <= 0.75))) tmp = (-1.0 / x) / x; else tmp = 1.0 + (-1.0 / x); end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 0.75]], $MachinePrecision]], N[(N[(-1.0 / x), $MachinePrecision] / x), $MachinePrecision], N[(1.0 + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 0.75\right):\\
\;\;\;\;\frac{\frac{-1}{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;1 + \frac{-1}{x}\\
\end{array}
\end{array}
if x < -1 or 0.75 < x Initial program 55.6%
frac-sub55.6%
*-rgt-identity55.6%
metadata-eval55.6%
div-inv55.6%
associate-/r*55.6%
*-un-lft-identity55.6%
*-rgt-identity55.6%
+-commutative55.6%
div-inv55.6%
metadata-eval55.6%
*-rgt-identity55.6%
+-commutative55.6%
Applied egg-rr55.6%
Taylor expanded in x around inf 99.8%
if -1 < x < 0.75Initial program 100.0%
Taylor expanded in x around 0 99.9%
Final simplification99.9%
(FPCore (x) :precision binary64 (/ -1.0 x))
double code(double x) {
return -1.0 / x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-1.0d0) / x
end function
public static double code(double x) {
return -1.0 / x;
}
def code(x): return -1.0 / x
function code(x) return Float64(-1.0 / x) end
function tmp = code(x) tmp = -1.0 / x; end
code[x_] := N[(-1.0 / x), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{x}
\end{array}
Initial program 77.5%
Taylor expanded in x around 0 51.5%
(FPCore (x) :precision binary64 (- x))
double code(double x) {
return -x;
}
real(8) function code(x)
real(8), intent (in) :: x
code = -x
end function
public static double code(double x) {
return -x;
}
def code(x): return -x
function code(x) return Float64(-x) end
function tmp = code(x) tmp = -x; end
code[x_] := (-x)
\begin{array}{l}
\\
-x
\end{array}
Initial program 77.5%
Taylor expanded in x around 0 50.4%
neg-mul-150.4%
sub-neg50.4%
Simplified50.4%
Taylor expanded in x around inf 3.0%
neg-mul-13.0%
Simplified3.0%
herbie shell --seed 2024130
(FPCore (x)
:name "2frac (problem 3.3.1)"
:precision binary64
(- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))