
(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
(let* ((t_0 (- (/ 1.0 (+ 1.0 x)) (/ 1.0 x))))
(if (<= t_0 -5e-22)
(/ (- x (+ 1.0 x)) (* x (+ 1.0 x)))
(if (<= t_0 0.0) (- (pow x -2.0)) t_0))))
double code(double x) {
double t_0 = (1.0 / (1.0 + x)) - (1.0 / x);
double tmp;
if (t_0 <= -5e-22) {
tmp = (x - (1.0 + x)) / (x * (1.0 + x));
} else if (t_0 <= 0.0) {
tmp = -pow(x, -2.0);
} else {
tmp = t_0;
}
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 <= (-5d-22)) then
tmp = (x - (1.0d0 + x)) / (x * (1.0d0 + x))
else if (t_0 <= 0.0d0) then
tmp = -(x ** (-2.0d0))
else
tmp = t_0
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 <= -5e-22) {
tmp = (x - (1.0 + x)) / (x * (1.0 + x));
} else if (t_0 <= 0.0) {
tmp = -Math.pow(x, -2.0);
} else {
tmp = t_0;
}
return tmp;
}
def code(x): t_0 = (1.0 / (1.0 + x)) - (1.0 / x) tmp = 0 if t_0 <= -5e-22: tmp = (x - (1.0 + x)) / (x * (1.0 + x)) elif t_0 <= 0.0: tmp = -math.pow(x, -2.0) else: tmp = t_0 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 <= -5e-22) tmp = Float64(Float64(x - Float64(1.0 + x)) / Float64(x * Float64(1.0 + x))); elseif (t_0 <= 0.0) tmp = Float64(-(x ^ -2.0)); else tmp = t_0; 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 <= -5e-22) tmp = (x - (1.0 + x)) / (x * (1.0 + x)); elseif (t_0 <= 0.0) tmp = -(x ^ -2.0); else tmp = t_0; 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, -5e-22], N[(N[(x - N[(1.0 + x), $MachinePrecision]), $MachinePrecision] / N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[t$95$0, 0.0], (-N[Power[x, -2.0], $MachinePrecision]), t$95$0]]]
\begin{array}{l}
\\
\begin{array}{l}
t_0 := \frac{1}{1 + x} - \frac{1}{x}\\
\mathbf{if}\;t\_0 \leq -5 \cdot 10^{-22}:\\
\;\;\;\;\frac{x - \left(1 + x\right)}{x \cdot \left(1 + x\right)}\\
\mathbf{elif}\;t\_0 \leq 0:\\
\;\;\;\;-{x}^{-2}\\
\mathbf{else}:\\
\;\;\;\;t\_0\\
\end{array}
\end{array}
if (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < -4.99999999999999954e-22Initial program 96.9%
clear-num96.9%
frac-sub99.9%
*-un-lft-identity99.9%
div-inv99.9%
metadata-eval99.9%
*-rgt-identity99.9%
*-rgt-identity99.9%
+-commutative99.9%
*-commutative99.9%
div-inv99.9%
metadata-eval99.9%
*-rgt-identity99.9%
+-commutative99.9%
Applied egg-rr99.9%
if -4.99999999999999954e-22 < (-.f64 (/.f64 #s(literal 1 binary64) (+.f64 x #s(literal 1 binary64))) (/.f64 #s(literal 1 binary64) x)) < 0.0Initial program 57.5%
Taylor expanded in x around inf 99.1%
div-inv99.1%
pow-flip100.0%
metadata-eval100.0%
Applied egg-rr100.0%
neg-mul-1100.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%
Final simplification100.0%
(FPCore (x) :precision binary64 (if (<= x -1.0) 0.0 (if (<= x 3.9e+61) (+ (- 1.0 x) (/ -1.0 x)) 0.0)))
double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = 0.0;
} else if (x <= 3.9e+61) {
tmp = (1.0 - x) + (-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 <= 3.9d+61) then
tmp = (1.0d0 - x) + ((-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 <= 3.9e+61) {
tmp = (1.0 - x) + (-1.0 / x);
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= -1.0: tmp = 0.0 elif x <= 3.9e+61: tmp = (1.0 - x) + (-1.0 / x) else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= -1.0) tmp = 0.0; elseif (x <= 3.9e+61) tmp = Float64(Float64(1.0 - x) + 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 <= 3.9e+61) tmp = (1.0 - x) + (-1.0 / x); else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -1.0], 0.0, If[LessEqual[x, 3.9e+61], N[(N[(1.0 - x), $MachinePrecision] + N[(-1.0 / x), $MachinePrecision]), $MachinePrecision], 0.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;0\\
\mathbf{elif}\;x \leq 3.9 \cdot 10^{+61}:\\
\;\;\;\;\left(1 - x\right) + \frac{-1}{x}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < -1 or 3.89999999999999987e61 < x Initial program 65.4%
*-un-lft-identity65.4%
inv-pow65.4%
add-sqr-sqrt22.7%
unpow-prod-down16.8%
prod-diff5.4%
unpow-prod-down5.3%
add-sqr-sqrt5.2%
inv-pow5.2%
fma-define5.2%
*-un-lft-identity5.2%
+-commutative5.2%
distribute-neg-frac5.2%
metadata-eval5.2%
unpow-prod-down5.3%
add-sqr-sqrt5.4%
inv-pow5.4%
Applied egg-rr5.4%
fma-undefine16.8%
+-commutative16.8%
associate-+r+16.8%
associate-+r+16.8%
*-rgt-identity16.8%
metadata-eval16.8%
associate-*l/16.8%
distribute-lft-out16.8%
metadata-eval16.8%
mul0-rgt16.8%
+-rgt-identity16.8%
Simplified14.5%
Taylor expanded in x around -inf 0.0%
unpow20.0%
rem-square-sqrt63.6%
metadata-eval63.6%
metadata-eval63.6%
metadata-eval63.6%
associate-*r/63.6%
mul0-lft63.6%
Simplified63.6%
if -1 < x < 3.89999999999999987e61Initial program 88.3%
Taylor expanded in x around 0 86.5%
neg-mul-186.5%
unsub-neg86.5%
Simplified86.5%
Final simplification76.3%
(FPCore (x) :precision binary64 (if (<= x -1.0) 0.0 (if (<= x 1.0) (- 1.0 (/ 1.0 x)) 0.0)))
double code(double x) {
double tmp;
if (x <= -1.0) {
tmp = 0.0;
} else if (x <= 1.0) {
tmp = 1.0 - (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 <= 1.0d0) then
tmp = 1.0d0 - (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 <= 1.0) {
tmp = 1.0 - (1.0 / x);
} else {
tmp = 0.0;
}
return tmp;
}
def code(x): tmp = 0 if x <= -1.0: tmp = 0.0 elif x <= 1.0: tmp = 1.0 - (1.0 / x) else: tmp = 0.0 return tmp
function code(x) tmp = 0.0 if (x <= -1.0) tmp = 0.0; elseif (x <= 1.0) tmp = Float64(1.0 - 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 <= 1.0) tmp = 1.0 - (1.0 / x); else tmp = 0.0; end tmp_2 = tmp; end
code[x_] := If[LessEqual[x, -1.0], 0.0, If[LessEqual[x, 1.0], N[(1.0 - N[(1.0 / x), $MachinePrecision]), $MachinePrecision], 0.0]]
\begin{array}{l}
\\
\begin{array}{l}
\mathbf{if}\;x \leq -1:\\
\;\;\;\;0\\
\mathbf{elif}\;x \leq 1:\\
\;\;\;\;1 - \frac{1}{x}\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}
\end{array}
if x < -1 or 1 < x Initial program 57.9%
*-un-lft-identity57.9%
inv-pow57.9%
add-sqr-sqrt21.4%
unpow-prod-down16.4%
prod-diff6.8%
unpow-prod-down6.8%
add-sqr-sqrt6.7%
inv-pow6.7%
fma-define6.7%
*-un-lft-identity6.7%
+-commutative6.7%
distribute-neg-frac6.7%
metadata-eval6.7%
unpow-prod-down6.8%
add-sqr-sqrt6.8%
inv-pow6.8%
Applied egg-rr6.8%
fma-undefine16.4%
+-commutative16.4%
associate-+r+16.4%
associate-+r+16.4%
*-rgt-identity16.4%
metadata-eval16.4%
associate-*l/16.4%
distribute-lft-out16.4%
metadata-eval16.4%
mul0-rgt16.4%
+-rgt-identity16.4%
Simplified14.4%
Taylor expanded in x around -inf 0.0%
unpow20.0%
rem-square-sqrt55.0%
metadata-eval55.0%
metadata-eval55.0%
metadata-eval55.0%
associate-*r/55.0%
mul0-lft55.0%
Simplified55.0%
if -1 < x < 1Initial program 100.0%
Taylor expanded in x around 0 98.2%
div-sub98.2%
*-inverses98.2%
Simplified98.2%
(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 69.9%
*-un-lft-identity69.9%
inv-pow69.9%
add-sqr-sqrt24.0%
unpow-prod-down17.6%
prod-diff5.4%
unpow-prod-down5.4%
add-sqr-sqrt5.4%
inv-pow5.4%
fma-define5.4%
*-un-lft-identity5.4%
+-commutative5.4%
distribute-neg-frac5.4%
metadata-eval5.4%
unpow-prod-down5.4%
add-sqr-sqrt5.4%
inv-pow5.4%
Applied egg-rr5.4%
fma-undefine17.6%
+-commutative17.6%
associate-+r+17.6%
associate-+r+17.6%
*-rgt-identity17.6%
metadata-eval17.6%
associate-*l/17.6%
distribute-lft-out17.6%
metadata-eval17.6%
mul0-rgt17.6%
+-rgt-identity17.6%
Simplified15.2%
Taylor expanded in x around -inf 0.0%
unpow20.0%
rem-square-sqrt68.0%
metadata-eval68.0%
metadata-eval68.0%
metadata-eval68.0%
associate-*r/68.0%
mul0-lft68.0%
Simplified68.0%
if -1 < x < 4.50000000000000021e102Initial program 83.9%
Taylor expanded in x around 0 80.6%
(FPCore (x) :precision binary64 (/ (- x (+ 1.0 x)) (* x (+ 1.0 x))))
double code(double x) {
return (x - (1.0 + x)) / (x * (1.0 + x));
}
real(8) function code(x)
real(8), intent (in) :: x
code = (x - (1.0d0 + x)) / (x * (1.0d0 + x))
end function
public static double code(double x) {
return (x - (1.0 + x)) / (x * (1.0 + x));
}
def code(x): return (x - (1.0 + x)) / (x * (1.0 + x))
function code(x) return Float64(Float64(x - Float64(1.0 + x)) / Float64(x * Float64(1.0 + x))) end
function tmp = code(x) tmp = (x - (1.0 + x)) / (x * (1.0 + x)); end
code[x_] := N[(N[(x - N[(1.0 + x), $MachinePrecision]), $MachinePrecision] / N[(x * N[(1.0 + x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{x - \left(1 + x\right)}{x \cdot \left(1 + x\right)}
\end{array}
Initial program 78.1%
clear-num78.1%
frac-sub78.9%
*-un-lft-identity78.9%
div-inv78.9%
metadata-eval78.9%
*-rgt-identity78.9%
*-rgt-identity78.9%
+-commutative78.9%
*-commutative78.9%
div-inv78.9%
metadata-eval78.9%
*-rgt-identity78.9%
+-commutative78.9%
Applied egg-rr78.9%
(FPCore (x) :precision binary64 (- (/ 1.0 (+ 1.0 x)) (/ 1.0 x)))
double code(double x) {
return (1.0 / (1.0 + x)) - (1.0 / x);
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / (1.0d0 + x)) - (1.0d0 / x)
end function
public static double code(double x) {
return (1.0 / (1.0 + x)) - (1.0 / x);
}
def code(x): return (1.0 / (1.0 + x)) - (1.0 / x)
function code(x) return Float64(Float64(1.0 / Float64(1.0 + x)) - Float64(1.0 / x)) end
function tmp = code(x) tmp = (1.0 / (1.0 + x)) - (1.0 / x); end
code[x_] := N[(N[(1.0 / N[(1.0 + x), $MachinePrecision]), $MachinePrecision] - N[(1.0 / x), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
\\
\frac{1}{1 + x} - \frac{1}{x}
\end{array}
Initial program 78.1%
Final simplification78.1%
(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 78.1%
*-un-lft-identity78.1%
inv-pow78.1%
add-sqr-sqrt34.7%
unpow-prod-down32.1%
prod-diff27.2%
unpow-prod-down27.2%
add-sqr-sqrt27.3%
inv-pow27.3%
fma-define27.3%
*-un-lft-identity27.3%
+-commutative27.3%
distribute-neg-frac27.3%
metadata-eval27.3%
unpow-prod-down27.2%
add-sqr-sqrt27.2%
inv-pow27.2%
Applied egg-rr27.2%
fma-undefine32.1%
+-commutative32.1%
associate-+r+32.1%
associate-+r+32.1%
*-rgt-identity32.1%
metadata-eval32.1%
associate-*l/32.1%
distribute-lft-out32.1%
metadata-eval32.1%
mul0-rgt32.1%
+-rgt-identity32.1%
Simplified31.1%
Taylor expanded in x around -inf 0.0%
unpow20.0%
rem-square-sqrt29.7%
metadata-eval29.7%
metadata-eval29.7%
metadata-eval29.7%
associate-*r/29.7%
mul0-lft29.7%
Simplified29.7%
herbie shell --seed 2024089
(FPCore (x)
:name "2frac (problem 3.3.1)"
:precision binary64
(- (/ 1.0 (+ x 1.0)) (/ 1.0 x)))