
(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 7 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 (/ (/ 1.0 x) (- -1.0 x)))
double code(double x) {
return (1.0 / x) / (-1.0 - x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / x) / ((-1.0d0) - x)
end function
public static double code(double x) {
return (1.0 / x) / (-1.0 - x);
}
def code(x): return (1.0 / x) / (-1.0 - x)
function code(x) return Float64(Float64(1.0 / x) / Float64(-1.0 - x)) end
function tmp = code(x) tmp = (1.0 / x) / (-1.0 - x); end
code[x_] := N[(N[(1.0 / x), $MachinePrecision] / N[(-1.0 - x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{\frac{1}{x}}{-1 - x}
\end{array}
Initial program 74.6%
sub-neg74.6%
+-commutative74.6%
distribute-neg-frac74.6%
metadata-eval74.6%
Applied egg-rr74.6%
metadata-eval74.6%
distribute-neg-frac74.6%
unsub-neg74.6%
*-inverses74.6%
associate-/r*50.6%
*-commutative50.6%
associate-/r*74.6%
div-sub74.6%
*-inverses74.6%
div-sub75.8%
associate-/r*75.8%
+-commutative75.8%
associate--r+98.5%
+-inverses98.5%
metadata-eval98.5%
distribute-lft-in98.5%
unpow298.5%
*-rgt-identity98.5%
Simplified98.5%
unpow298.5%
Applied egg-rr98.5%
frac-2neg98.5%
metadata-eval98.5%
inv-pow98.5%
neg-mul-198.5%
distribute-rgt1-in98.5%
add-sqr-sqrt51.0%
sqrt-prod55.3%
sqr-neg55.3%
sqrt-unprod13.2%
add-sqr-sqrt27.3%
distribute-rgt-neg-in27.3%
distribute-rgt1-in27.3%
distribute-neg-in27.3%
add-sqr-sqrt13.2%
sqrt-unprod12.2%
sqr-neg12.2%
sqrt-prod34.0%
add-sqr-sqrt70.4%
sub-neg70.4%
unpow-prod-down70.4%
metadata-eval70.4%
Applied egg-rr51.8%
neg-mul-151.8%
exp-to-pow48.6%
metadata-eval48.6%
distribute-rgt-neg-in48.6%
exp-neg48.0%
exp-to-pow51.1%
unpow251.1%
hypot-undefine51.1%
rem-square-sqrt51.1%
fma-undefine51.1%
hypot-undefine51.1%
rem-square-sqrt74.9%
fma-undefine74.9%
rem-square-sqrt98.5%
fma-undefine98.5%
*-rgt-identity98.5%
distribute-lft-in98.5%
associate-/r*99.9%
unpow-199.9%
Simplified99.9%
(FPCore (x) :precision binary64 (if (or (<= x -1.0) (not (<= x 1.0))) (/ (/ -1.0 x) x) (- (- 1.0 x) (/ 1.0 x))))
double code(double x) {
double tmp;
if ((x <= -1.0) || !(x <= 1.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) :: tmp
if ((x <= (-1.0d0)) .or. (.not. (x <= 1.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 tmp;
if ((x <= -1.0) || !(x <= 1.0)) {
tmp = (-1.0 / x) / x;
} else {
tmp = (1.0 - x) - (1.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 = (1.0 - x) - (1.0 / x) return tmp
function code(x) tmp = 0.0 if ((x <= -1.0) || !(x <= 1.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) tmp = 0.0; if ((x <= -1.0) || ~((x <= 1.0))) tmp = (-1.0 / x) / x; else tmp = (1.0 - x) - (1.0 / x); end tmp_2 = tmp; end
code[x_] := If[Or[LessEqual[x, -1.0], N[Not[LessEqual[x, 1.0]], $MachinePrecision]], 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}
\mathbf{if}\;x \leq -1 \lor \neg \left(x \leq 1\right):\\
\;\;\;\;\frac{\frac{-1}{x}}{x}\\
\mathbf{else}:\\
\;\;\;\;\left(1 - x\right) - \frac{1}{x}\\
\end{array}
\end{array}
if x < -1 or 1 < x Initial program 54.5%
sub-neg54.5%
+-commutative54.5%
distribute-neg-frac54.5%
metadata-eval54.5%
Applied egg-rr54.5%
metadata-eval54.5%
distribute-neg-frac54.5%
unsub-neg54.5%
*-inverses54.5%
associate-/r*11.5%
*-commutative11.5%
associate-/r*54.6%
div-sub54.6%
*-inverses54.6%
div-sub56.6%
associate-/r*56.6%
+-commutative56.6%
associate--r+97.3%
+-inverses97.3%
metadata-eval97.3%
distribute-lft-in97.3%
unpow297.3%
*-rgt-identity97.3%
Simplified97.3%
unpow297.3%
Applied egg-rr97.3%
frac-2neg97.3%
metadata-eval97.3%
inv-pow97.3%
neg-mul-197.3%
distribute-rgt1-in97.3%
add-sqr-sqrt54.5%
sqrt-prod78.0%
sqr-neg78.0%
sqrt-unprod23.3%
add-sqr-sqrt48.4%
distribute-rgt-neg-in48.4%
distribute-rgt1-in48.4%
distribute-neg-in48.4%
add-sqr-sqrt23.3%
sqrt-unprod1.9%
sqr-neg1.9%
sqrt-prod25.1%
add-sqr-sqrt48.4%
sub-neg48.4%
unpow-prod-down48.4%
metadata-eval48.4%
Applied egg-rr55.9%
neg-mul-155.9%
exp-to-pow53.2%
metadata-eval53.2%
distribute-rgt-neg-in53.2%
exp-neg52.0%
exp-to-pow54.6%
unpow254.6%
hypot-undefine54.6%
rem-square-sqrt54.6%
fma-undefine54.6%
hypot-undefine54.6%
rem-square-sqrt97.3%
fma-undefine97.3%
rem-square-sqrt97.3%
fma-undefine97.3%
*-rgt-identity97.3%
distribute-lft-in97.3%
associate-/r*99.9%
unpow-199.9%
Simplified99.9%
Taylor expanded in x around inf 96.0%
neg-mul-196.0%
Simplified96.0%
if -1 < x < 1Initial program 100.0%
Taylor expanded in x around 0 99.5%
neg-mul-199.5%
sub-neg99.5%
Simplified99.5%
Final simplification97.5%
(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 54.5%
sub-neg54.5%
+-commutative54.5%
distribute-neg-frac54.5%
metadata-eval54.5%
Applied egg-rr54.5%
metadata-eval54.5%
distribute-neg-frac54.5%
unsub-neg54.5%
*-inverses54.5%
associate-/r*11.5%
*-commutative11.5%
associate-/r*54.6%
div-sub54.6%
*-inverses54.6%
div-sub56.6%
associate-/r*56.6%
+-commutative56.6%
associate--r+97.3%
+-inverses97.3%
metadata-eval97.3%
distribute-lft-in97.3%
unpow297.3%
*-rgt-identity97.3%
Simplified97.3%
unpow297.3%
Applied egg-rr97.3%
frac-2neg97.3%
metadata-eval97.3%
inv-pow97.3%
neg-mul-197.3%
distribute-rgt1-in97.3%
add-sqr-sqrt54.5%
sqrt-prod78.0%
sqr-neg78.0%
sqrt-unprod23.3%
add-sqr-sqrt48.4%
distribute-rgt-neg-in48.4%
distribute-rgt1-in48.4%
distribute-neg-in48.4%
add-sqr-sqrt23.3%
sqrt-unprod1.9%
sqr-neg1.9%
sqrt-prod25.1%
add-sqr-sqrt48.4%
sub-neg48.4%
unpow-prod-down48.4%
metadata-eval48.4%
Applied egg-rr55.9%
neg-mul-155.9%
exp-to-pow53.2%
metadata-eval53.2%
distribute-rgt-neg-in53.2%
exp-neg52.0%
exp-to-pow54.6%
unpow254.6%
hypot-undefine54.6%
rem-square-sqrt54.6%
fma-undefine54.6%
hypot-undefine54.6%
rem-square-sqrt97.3%
fma-undefine97.3%
rem-square-sqrt97.3%
fma-undefine97.3%
*-rgt-identity97.3%
distribute-lft-in97.3%
associate-/r*99.9%
unpow-199.9%
Simplified99.9%
Taylor expanded in x around inf 96.0%
neg-mul-196.0%
Simplified96.0%
if -1 < x < 0.75Initial program 100.0%
Taylor expanded in x around 0 99.4%
Final simplification97.5%
(FPCore (x) :precision binary64 (if (<= x -1.0) 0.0 (if (<= x 4.5e+102) (/ -1.0 x) 0.0)))
double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = 0.0;
} else if (x <= 4.5e+102) {
tmp = -1.0 / x;
} else {
tmp = 0.0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (x <= (-1.0d0)) then
tmp = 0.0d0
else if (x <= 4.5d+102) then
tmp = (-1.0d0) / x
else
tmp = 0.0d0
end if
code = tmp
end function
public static double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = 0.0;
} else if (x <= 4.5e+102) {
tmp = -1.0 / x;
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= -1.0: tmp = 0.0 elif x <= 4.5e+102: tmp = -1.0 / x else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= -1.0) tmp = 0.0; elseif (x <= 4.5e+102) tmp = Float64(-1.0 / x); else tmp = 0.0; end return tmp end
function tmp_2 = code(x) tmp = 0.0; if (x <= -1.0) tmp = 0.0; elseif (x <= 4.5e+102) tmp = -1.0 / x; else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -1.0], 0.0, If[LessEqual[x, 4.5e+102], N[(-1.0 / x), $MachinePrecision], 0.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;0\\
\mathbf{elif}\;x \leq 4.5 \cdot 10^{+102}:\\
\;\;\;\;\frac{-1}{x}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < -1 or 4.50000000000000021e102 < x Initial program 64.7%
Taylor expanded in x around inf 61.7%
Taylor expanded in x around 0 61.7%
if -1 < x < 4.50000000000000021e102Initial program 82.3%
Taylor expanded in x around 0 78.9%
(FPCore (x) :precision binary64 (/ -1.0 (+ x (* x x))))
double code(double x) {
return -1.0 / (x + (x * x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-1.0d0) / (x + (x * x))
end function
public static double code(double x) {
return -1.0 / (x + (x * x));
}
def code(x): return -1.0 / (x + (x * x))
function code(x) return Float64(-1.0 / Float64(x + Float64(x * x))) end
function tmp = code(x) tmp = -1.0 / (x + (x * x)); end
code[x_] := N[(-1.0 / N[(x + N[(x * x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{x + x \cdot x}
\end{array}
Initial program 74.6%
sub-neg74.6%
+-commutative74.6%
distribute-neg-frac74.6%
metadata-eval74.6%
Applied egg-rr74.6%
metadata-eval74.6%
distribute-neg-frac74.6%
unsub-neg74.6%
*-inverses74.6%
associate-/r*50.6%
*-commutative50.6%
associate-/r*74.6%
div-sub74.6%
*-inverses74.6%
div-sub75.8%
associate-/r*75.8%
+-commutative75.8%
associate--r+98.5%
+-inverses98.5%
metadata-eval98.5%
distribute-lft-in98.5%
unpow298.5%
*-rgt-identity98.5%
Simplified98.5%
unpow298.5%
Applied egg-rr98.5%
(FPCore (x) :precision binary64 (/ -1.0 (* x (+ 1.0 x))))
double code(double x) {
return -1.0 / (x * (1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (-1.0d0) / (x * (1.0d0 + x))
end function
public static double code(double x) {
return -1.0 / (x * (1.0 + x));
}
def code(x): return -1.0 / (x * (1.0 + x))
function code(x) return Float64(-1.0 / Float64(x * Float64(1.0 + x))) end
function tmp = code(x) tmp = -1.0 / (x * (1.0 + x)); end
code[x_] := N[(-1.0 / N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{-1}{x \cdot \left(1 + x\right)}
\end{array}
Initial program 74.6%
sub-neg74.6%
+-commutative74.6%
distribute-neg-frac74.6%
metadata-eval74.6%
Applied egg-rr74.6%
metadata-eval74.6%
distribute-neg-frac74.6%
unsub-neg74.6%
*-inverses74.6%
associate-/r*50.6%
*-commutative50.6%
associate-/r*74.6%
div-sub74.6%
*-inverses74.6%
div-sub75.8%
associate-/r*75.8%
+-commutative75.8%
associate--r+98.5%
+-inverses98.5%
metadata-eval98.5%
distribute-lft-in98.5%
unpow298.5%
*-rgt-identity98.5%
Simplified98.5%
unpow298.5%
distribute-rgt1-in98.5%
Applied egg-rr98.5%
Final simplification98.5%
(FPCore (x) :precision binary64 0.0)
double code(double x) {
return 0.0;
}
real(8) function code(x)
real(8), intent (in) :: x
code = 0.0d0
end function
public static double code(double x) {
return 0.0;
}
def code(x): return 0.0
function code(x) return 0.0 end
function tmp = code(x) tmp = 0.0; end
code[x_] := 0.0
\begin{array}{l}
\\
0
\end{array}
Initial program 74.6%
Taylor expanded in x around inf 28.5%
Taylor expanded in x around 0 28.5%
herbie shell --seed 2024116
(FPCore (x)
:name "2frac (problem 3.3.1)"
:precision binary64
(- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))