| Alternative 1 | |
|---|---|
| Error | 0.4 |
| Cost | 26692 |
\[\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 10^{-15}:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\
\end{array}
\]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x) :precision binary64 (if (<= x 85000.0) (- (pow x -0.5) (pow (+ 1.0 x) -0.5)) (/ (/ 1.0 x) (+ (* (sqrt (/ 1.0 x)) 1.5) (* (sqrt x) 2.0)))))
double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
double tmp;
if (x <= 85000.0) {
tmp = pow(x, -0.5) - pow((1.0 + x), -0.5);
} else {
tmp = (1.0 / x) / ((sqrt((1.0 / x)) * 1.5) + (sqrt(x) * 2.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 <= 85000.0d0) then
tmp = (x ** (-0.5d0)) - ((1.0d0 + x) ** (-0.5d0))
else
tmp = (1.0d0 / x) / ((sqrt((1.0d0 / x)) * 1.5d0) + (sqrt(x) * 2.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 <= 85000.0) {
tmp = Math.pow(x, -0.5) - Math.pow((1.0 + x), -0.5);
} else {
tmp = (1.0 / x) / ((Math.sqrt((1.0 / x)) * 1.5) + (Math.sqrt(x) * 2.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 <= 85000.0: tmp = math.pow(x, -0.5) - math.pow((1.0 + x), -0.5) else: tmp = (1.0 / x) / ((math.sqrt((1.0 / x)) * 1.5) + (math.sqrt(x) * 2.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 <= 85000.0) tmp = Float64((x ^ -0.5) - (Float64(1.0 + x) ^ -0.5)); else tmp = Float64(Float64(1.0 / x) / Float64(Float64(sqrt(Float64(1.0 / x)) * 1.5) + Float64(sqrt(x) * 2.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 <= 85000.0) tmp = (x ^ -0.5) - ((1.0 + x) ^ -0.5); else tmp = (1.0 / x) / ((sqrt((1.0 / x)) * 1.5) + (sqrt(x) * 2.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, 85000.0], N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision], N[(N[(1.0 / x), $MachinePrecision] / N[(N[(N[Sqrt[N[(1.0 / x), $MachinePrecision]], $MachinePrecision] * 1.5), $MachinePrecision] + N[(N[Sqrt[x], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
\mathbf{if}\;x \leq 85000:\\
\;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x}}{\sqrt{\frac{1}{x}} \cdot 1.5 + \sqrt{x} \cdot 2}\\
\end{array}
Results
| Original | 19.3 |
|---|---|
| Target | 0.6 |
| Herbie | 0.2 |
if x < 85000Initial program 0.3
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 85000 < x Initial program 39.2
Applied egg-rr39.1
Simplified39.1
[Start]39.1 | \[ \left(\frac{1}{x} + \frac{-1}{1 + x}\right) \cdot \frac{1}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}
\] |
|---|---|
associate-*r/ [=>]39.1 | \[ \color{blue}{\frac{\left(\frac{1}{x} + \frac{-1}{1 + x}\right) \cdot 1}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}}
\] |
*-rgt-identity [=>]39.1 | \[ \frac{\color{blue}{\frac{1}{x} + \frac{-1}{1 + x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}
\] |
Applied egg-rr11.1
Simplified11.1
[Start]11.1 | \[ \frac{\left(1 + \left(x - x\right)\right) \cdot \frac{1}{x + x \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}
\] |
|---|---|
associate-*r/ [=>]11.1 | \[ \frac{\color{blue}{\frac{\left(1 + \left(x - x\right)\right) \cdot 1}{x + x \cdot x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}
\] |
*-rgt-identity [=>]11.1 | \[ \frac{\frac{\color{blue}{1 + \left(x - x\right)}}{x + x \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}
\] |
+-commutative [=>]11.1 | \[ \frac{\frac{\color{blue}{\left(x - x\right) + 1}}{x + x \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}
\] |
+-inverses [=>]11.1 | \[ \frac{\frac{\color{blue}{0} + 1}{x + x \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}
\] |
metadata-eval [=>]11.1 | \[ \frac{\frac{\color{blue}{1}}{x + x \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}
\] |
Applied egg-rr0.3
Simplified0.3
[Start]0.3 | \[ \frac{\frac{1}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}}{1 + x} \cdot \frac{1}{x}
\] |
|---|---|
associate-/l/ [=>]0.3 | \[ \color{blue}{\frac{1}{\left(1 + x\right) \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)}} \cdot \frac{1}{x}
\] |
associate-*l/ [=>]0.3 | \[ \color{blue}{\frac{1 \cdot \frac{1}{x}}{\left(1 + x\right) \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)}}
\] |
*-lft-identity [=>]0.3 | \[ \frac{\color{blue}{\frac{1}{x}}}{\left(1 + x\right) \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)}
\] |
Taylor expanded in x around inf 0.3
Simplified0.3
[Start]0.3 | \[ \frac{\frac{1}{x}}{-0.5 \cdot \sqrt{\frac{1}{x}} + \left(2 \cdot \sqrt{x} + 2 \cdot \sqrt{\frac{1}{x}}\right)}
\] |
|---|---|
+-commutative [=>]0.3 | \[ \frac{\frac{1}{x}}{-0.5 \cdot \sqrt{\frac{1}{x}} + \color{blue}{\left(2 \cdot \sqrt{\frac{1}{x}} + 2 \cdot \sqrt{x}\right)}}
\] |
associate-+r+ [=>]0.3 | \[ \frac{\frac{1}{x}}{\color{blue}{\left(-0.5 \cdot \sqrt{\frac{1}{x}} + 2 \cdot \sqrt{\frac{1}{x}}\right) + 2 \cdot \sqrt{x}}}
\] |
distribute-rgt-out [=>]0.3 | \[ \frac{\frac{1}{x}}{\color{blue}{\sqrt{\frac{1}{x}} \cdot \left(-0.5 + 2\right)} + 2 \cdot \sqrt{x}}
\] |
metadata-eval [=>]0.3 | \[ \frac{\frac{1}{x}}{\sqrt{\frac{1}{x}} \cdot \color{blue}{1.5} + 2 \cdot \sqrt{x}}
\] |
Final simplification0.2
| Alternative 1 | |
|---|---|
| Error | 0.4 |
| Cost | 26692 |
| Alternative 2 | |
|---|---|
| Error | 0.6 |
| Cost | 26240 |
| Alternative 3 | |
|---|---|
| Error | 0.4 |
| Cost | 13696 |
| Alternative 4 | |
|---|---|
| Error | 0.9 |
| Cost | 7044 |
| Alternative 5 | |
|---|---|
| Error | 2.0 |
| Cost | 6788 |
| Alternative 6 | |
|---|---|
| Error | 1.1 |
| Cost | 6788 |
| Alternative 7 | |
|---|---|
| Error | 31.2 |
| Cost | 6528 |
| Alternative 8 | |
|---|---|
| Error | 61.5 |
| Cost | 192 |
| Alternative 9 | |
|---|---|
| Error | 62.8 |
| Cost | 64 |
herbie shell --seed 2023027
(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)))))