| Alternative 1 | |
|---|---|
| Error | 0.3 |
| Cost | 32704 |
\[\frac{-1}{\mathsf{hypot}\left(x, \sqrt{x}\right)} \cdot \frac{-1}{\sqrt{x} + \mathsf{hypot}\left(1, \sqrt{x}\right)}
\]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x) :precision binary64 (if (<= x 170000.0) (- (pow x -0.5) (pow (+ x 1.0) -0.5)) (/ (/ 1.0 (+ x 0.5)) (+ (sqrt x) (sqrt (+ x 1.0))))))
double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
double tmp;
if (x <= 170000.0) {
tmp = pow(x, -0.5) - pow((x + 1.0), -0.5);
} else {
tmp = (1.0 / (x + 0.5)) / (sqrt(x) + sqrt((x + 1.0)));
}
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 (x <= 170000.0d0) then
tmp = (x ** (-0.5d0)) - ((x + 1.0d0) ** (-0.5d0))
else
tmp = (1.0d0 / (x + 0.5d0)) / (sqrt(x) + sqrt((x + 1.0d0)))
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 (x <= 170000.0) {
tmp = Math.pow(x, -0.5) - Math.pow((x + 1.0), -0.5);
} else {
tmp = (1.0 / (x + 0.5)) / (Math.sqrt(x) + Math.sqrt((x + 1.0)));
}
return tmp;
}
def code(x): return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
def code(x): tmp = 0 if x <= 170000.0: tmp = math.pow(x, -0.5) - math.pow((x + 1.0), -0.5) else: tmp = (1.0 / (x + 0.5)) / (math.sqrt(x) + math.sqrt((x + 1.0))) 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 (x <= 170000.0) tmp = Float64((x ^ -0.5) - (Float64(x + 1.0) ^ -0.5)); else tmp = Float64(Float64(1.0 / Float64(x + 0.5)) / Float64(sqrt(x) + sqrt(Float64(x + 1.0)))); 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 (x <= 170000.0) tmp = (x ^ -0.5) - ((x + 1.0) ^ -0.5); else tmp = (1.0 / (x + 0.5)) / (sqrt(x) + sqrt((x + 1.0))); 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[x, 170000.0], N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / N[(x + 0.5), $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
\mathbf{if}\;x \leq 170000:\\
\;\;\;\;{x}^{-0.5} - {\left(x + 1\right)}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x + 0.5}}{\sqrt{x} + \sqrt{x + 1}}\\
\end{array}
Results
| Original | 19.4 |
|---|---|
| Target | 0.7 |
| Herbie | 0.2 |
if x < 1.7e5Initial program 0.4
Applied egg-rr0.1
Simplified0.1
[Start]0.1 | \[ {x}^{-0.5} + \left(-{\left(1 + x\right)}^{-0.5}\right)
\] |
|---|---|
sub-neg [<=]0.1 | \[ \color{blue}{{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}}
\] |
if 1.7e5 < x Initial program 39.0
Applied egg-rr11.0
Simplified11.0
[Start]11.0 | \[ \frac{1 + \left(x - x\right)}{\sqrt{x + x \cdot x} \cdot \left(\sqrt{x} + \sqrt{1 + x}\right)}
\] |
|---|---|
associate-/r* [=>]11.0 | \[ \color{blue}{\frac{\frac{1 + \left(x - x\right)}{\sqrt{x + x \cdot x}}}{\sqrt{x} + \sqrt{1 + x}}}
\] |
+-commutative [=>]11.0 | \[ \frac{\frac{\color{blue}{\left(x - x\right) + 1}}{\sqrt{x + x \cdot x}}}{\sqrt{x} + \sqrt{1 + x}}
\] |
+-inverses [=>]11.0 | \[ \frac{\frac{\color{blue}{0} + 1}{\sqrt{x + x \cdot x}}}{\sqrt{x} + \sqrt{1 + x}}
\] |
metadata-eval [=>]11.0 | \[ \frac{\frac{\color{blue}{1}}{\sqrt{x + x \cdot x}}}{\sqrt{x} + \sqrt{1 + x}}
\] |
+-commutative [=>]11.0 | \[ \frac{\frac{1}{\sqrt{x + x \cdot x}}}{\color{blue}{\sqrt{1 + x} + \sqrt{x}}}
\] |
Taylor expanded in x around inf 0.3
Simplified0.3
[Start]0.3 | \[ \frac{\frac{1}{0.5 + x}}{\sqrt{1 + x} + \sqrt{x}}
\] |
|---|---|
+-commutative [=>]0.3 | \[ \frac{\frac{1}{\color{blue}{x + 0.5}}}{\sqrt{1 + x} + \sqrt{x}}
\] |
Final simplification0.2
| Alternative 1 | |
|---|---|
| Error | 0.3 |
| Cost | 32704 |
| Alternative 2 | |
|---|---|
| Error | 0.4 |
| Cost | 26820 |
| Alternative 3 | |
|---|---|
| Error | 0.4 |
| Cost | 13760 |
| Alternative 4 | |
|---|---|
| Error | 10.6 |
| Cost | 13380 |
| Alternative 5 | |
|---|---|
| Error | 11.1 |
| Cost | 13316 |
| Alternative 6 | |
|---|---|
| Error | 21.0 |
| Cost | 6848 |
| Alternative 7 | |
|---|---|
| Error | 29.9 |
| Cost | 6788 |
| Alternative 8 | |
|---|---|
| Error | 20.2 |
| Cost | 6788 |
| Alternative 9 | |
|---|---|
| Error | 21.3 |
| Cost | 6784 |
| Alternative 10 | |
|---|---|
| Error | 31.5 |
| Cost | 6528 |
| Alternative 11 | |
|---|---|
| Error | 59.3 |
| Cost | 320 |
| Alternative 12 | |
|---|---|
| Error | 59.3 |
| Cost | 192 |
| Alternative 13 | |
|---|---|
| Error | 62.8 |
| Cost | 64 |
herbie shell --seed 2023016
(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)))))