| Alternative 1 | |
|---|---|
| Error | 0.41% |
| Cost | 26240 |
\[\frac{\frac{1}{\mathsf{hypot}\left(x, \sqrt{x}\right)}}{\sqrt{x} + \sqrt{1 + x}}
\]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x) :precision binary64 (if (<= (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ 1.0 x)))) 5e-22) (* 0.5 (pow x -1.5)) (- (pow x -0.5) (pow (+ 1.0 x) -0.5))))
double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
double tmp;
if (((1.0 / sqrt(x)) - (1.0 / sqrt((1.0 + x)))) <= 5e-22) {
tmp = 0.5 * pow(x, -1.5);
} else {
tmp = pow(x, -0.5) - pow((1.0 + x), -0.5);
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
code = (1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))
end function
real(8) function code(x)
real(8), intent (in) :: x
real(8) :: tmp
if (((1.0d0 / sqrt(x)) - (1.0d0 / sqrt((1.0d0 + x)))) <= 5d-22) then
tmp = 0.5d0 * (x ** (-1.5d0))
else
tmp = (x ** (-0.5d0)) - ((1.0d0 + x) ** (-0.5d0))
end if
code = tmp
end function
public static double code(double x) {
return (1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)));
}
public static double code(double x) {
double tmp;
if (((1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((1.0 + x)))) <= 5e-22) {
tmp = 0.5 * Math.pow(x, -1.5);
} else {
tmp = Math.pow(x, -0.5) - Math.pow((1.0 + x), -0.5);
}
return tmp;
}
def code(x): return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
def code(x): tmp = 0 if ((1.0 / math.sqrt(x)) - (1.0 / math.sqrt((1.0 + x)))) <= 5e-22: tmp = 0.5 * math.pow(x, -1.5) else: tmp = math.pow(x, -0.5) - math.pow((1.0 + x), -0.5) return tmp
function code(x) return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) end
function code(x) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(1.0 + x)))) <= 5e-22) tmp = Float64(0.5 * (x ^ -1.5)); else tmp = Float64((x ^ -0.5) - (Float64(1.0 + x) ^ -0.5)); end return tmp end
function tmp = code(x) tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0))); end
function tmp_2 = code(x) tmp = 0.0; if (((1.0 / sqrt(x)) - (1.0 / sqrt((1.0 + x)))) <= 5e-22) tmp = 0.5 * (x ^ -1.5); else tmp = (x ^ -0.5) - ((1.0 + x) ^ -0.5); end tmp_2 = tmp; end
code[x_] := N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
code[x_] := If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 5e-22], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision], N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 5 \cdot 10^{-22}:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\
\end{array}
Results
| Original | 30.73% |
|---|---|
| Target | 0.93% |
| Herbie | 0.87% |
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 4.99999999999999954e-22Initial program 62.62
Applied egg-rr94.06
Simplified94.08
[Start]94.06 | \[ \frac{\frac{\frac{1}{x}}{x} - \frac{\frac{1}{1 + x}}{1 + x}}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(\frac{1}{1 + x} + \frac{1}{x}\right)}
\] |
|---|---|
sub-neg [=>]94.06 | \[ \frac{\color{blue}{\frac{\frac{1}{x}}{x} + \left(-\frac{\frac{1}{1 + x}}{1 + x}\right)}}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(\frac{1}{1 + x} + \frac{1}{x}\right)}
\] |
associate-/l/ [=>]94.08 | \[ \frac{\frac{\frac{1}{x}}{x} + \left(-\color{blue}{\frac{1}{\left(1 + x\right) \cdot \left(1 + x\right)}}\right)}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(\frac{1}{1 + x} + \frac{1}{x}\right)}
\] |
distribute-neg-frac [=>]94.08 | \[ \frac{\frac{\frac{1}{x}}{x} + \color{blue}{\frac{-1}{\left(1 + x\right) \cdot \left(1 + x\right)}}}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(\frac{1}{1 + x} + \frac{1}{x}\right)}
\] |
metadata-eval [=>]94.08 | \[ \frac{\frac{\frac{1}{x}}{x} + \frac{\color{blue}{-1}}{\left(1 + x\right) \cdot \left(1 + x\right)}}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(\frac{1}{1 + x} + \frac{1}{x}\right)}
\] |
+-commutative [=>]94.08 | \[ \frac{\frac{\frac{1}{x}}{x} + \frac{-1}{\left(1 + x\right) \cdot \left(1 + x\right)}}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \color{blue}{\left(\frac{1}{x} + \frac{1}{1 + x}\right)}}
\] |
Taylor expanded in x around inf 34.71
Applied egg-rr62.74
Simplified0.04
[Start]62.74 | \[ 0.5 \cdot \left(\left(1 + {x}^{-1.5}\right) - 1\right)
\] |
|---|---|
+-commutative [=>]62.74 | \[ 0.5 \cdot \left(\color{blue}{\left({x}^{-1.5} + 1\right)} - 1\right)
\] |
associate--l+ [=>]0.04 | \[ 0.5 \cdot \color{blue}{\left({x}^{-1.5} + \left(1 - 1\right)\right)}
\] |
metadata-eval [=>]0.04 | \[ 0.5 \cdot \left({x}^{-1.5} + \color{blue}{0}\right)
\] |
+-rgt-identity [=>]0.04 | \[ 0.5 \cdot \color{blue}{{x}^{-1.5}}
\] |
if 4.99999999999999954e-22 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) Initial program 2.01
Applied egg-rr1.61
Simplified1.61
[Start]1.61 | \[ {x}^{-0.5} + \left(-{\left(1 + x\right)}^{-0.5}\right)
\] |
|---|---|
sub-neg [<=]1.61 | \[ \color{blue}{{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}}
\] |
Final simplification0.87
| Alternative 1 | |
|---|---|
| Error | 0.41% |
| Cost | 26240 |
| Alternative 2 | |
|---|---|
| Error | 1.32% |
| Cost | 7044 |
| Alternative 3 | |
|---|---|
| Error | 3.19% |
| Cost | 6788 |
| Alternative 4 | |
|---|---|
| Error | 1.63% |
| Cost | 6788 |
| Alternative 5 | |
|---|---|
| Error | 49.06% |
| Cost | 6528 |
| Alternative 6 | |
|---|---|
| Error | 92.65% |
| Cost | 192 |
| Alternative 7 | |
|---|---|
| Error | 98.07% |
| Cost | 64 |
herbie shell --seed 2023102
(FPCore (x)
:name "2isqrt (example 3.6)"
:precision binary64
:herbie-target
(/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))
(- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))