Average Error: 19.6 → 0.2
Time: 2.0min
Precision: binary64
Cost: 26945
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}\]
↓
\[\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 1.4507106952353632 \cdot 10^{-08}:\\
\;\;\;\;\frac{\frac{1}{x + \left(\left(x + 0.5\right) - \frac{0.125}{x}\right)}}{\sqrt{1 + x}}\\
\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} - \frac{1}{\sqrt{1 + x}}\\
\end{array}\]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}↓
\begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 1.4507106952353632 \cdot 10^{-08}:\\
\;\;\;\;\frac{\frac{1}{x + \left(\left(x + 0.5\right) - \frac{0.125}{x}\right)}}{\sqrt{1 + x}}\\
\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} - \frac{1}{\sqrt{1 + x}}\\
\end{array}(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)))) 1.4507106952353632e-08)
(/ (/ 1.0 (+ x (- (+ x 0.5) (/ 0.125 x)))) (sqrt (+ 1.0 x)))
(- (pow x -0.5) (/ 1.0 (sqrt (+ 1.0 x))))))
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))) <= 1.4507106952353632e-08) {
tmp = (1.0 / (x + ((x + 0.5) - (0.125 / x)))) / sqrt(1.0 + x);
} else {
tmp = pow(x, -0.5) - (1.0 / sqrt(1.0 + x));
}
return tmp;
}
Try it out
Enter valid numbers for all inputs
Target
| Original | 19.6 |
|---|
| Target | 0.7 |
|---|
| Herbie | 0.2 |
|---|
\[\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}}\]
Alternatives
| Alternative 1 |
|---|
| Error | 0.3 |
|---|
| Cost | 20032 |
|---|
\[\frac{\frac{1}{x + \sqrt{x} \cdot \sqrt{1 + x}}}{\sqrt{1 + x}}\]
| Alternative 2 |
|---|
| Error | 0.7 |
|---|
| Cost | 13632 |
|---|
\[\frac{1}{x \cdot \sqrt{1 + x} + \sqrt{x} \cdot \left(1 + x\right)}\]
| Alternative 3 |
|---|
| Error | 0.3 |
|---|
| Cost | 13633 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 3608.7123357395576:\\
\;\;\;\;\frac{1}{\sqrt{x}} - {\left(1 + x\right)}^{-0.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x + \left(\left(x + 0.5\right) - \frac{0.125}{x}\right)}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 4 |
|---|
| Error | 0.6 |
|---|
| Cost | 8065 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 0.7497539621235764:\\
\;\;\;\;\frac{1}{\sqrt{x}} - \frac{1}{\left(1 + x \cdot 0.5\right) - 0.125 \cdot \left(x \cdot x\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x + \left(\left(\left(x + 0.5\right) - \frac{0.125}{x}\right) + \frac{0.0625}{x \cdot x}\right)}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 5 |
|---|
| Error | 0.6 |
|---|
| Cost | 7809 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 0.7932895805080596:\\
\;\;\;\;\frac{1}{\sqrt{x}} - \frac{1}{\left(1 + x \cdot 0.5\right) - 0.125 \cdot \left(x \cdot x\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x + \left(\left(x + 0.5\right) - \frac{0.125}{x}\right)}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 6 |
|---|
| Error | 0.6 |
|---|
| Cost | 7681 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 0.7932895805080596:\\
\;\;\;\;\frac{1}{\sqrt{x}} - \frac{1}{1 + x \cdot \left(0.5 + x \cdot -0.125\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x + \left(\left(x + 0.5\right) - \frac{0.125}{x}\right)}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 7 |
|---|
| Error | 0.7 |
|---|
| Cost | 7681 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 0.8585930080847847:\\
\;\;\;\;\frac{1}{\sqrt{x}} - \frac{1}{1 + x \cdot \left(0.5 + x \cdot -0.125\right)}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x + \left(x + 0.5\right)}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 8 |
|---|
| Error | 0.7 |
|---|
| Cost | 7553 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 0.5756114885856433:\\
\;\;\;\;\frac{1}{\sqrt{x}} - \left(1 + x \cdot \left(-0.5 + x \cdot 0.375\right)\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x + \left(x + 0.5\right)}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 9 |
|---|
| Error | 0.8 |
|---|
| Cost | 7425 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 0.6844505345468515:\\
\;\;\;\;\frac{1}{\sqrt{x}} - \frac{1}{1 + x \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x + \left(x + 0.5\right)}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 10 |
|---|
| Error | 1.1 |
|---|
| Cost | 7425 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 1.152542200014402:\\
\;\;\;\;\frac{1}{\sqrt{x}} - \frac{1}{1 + x \cdot 0.5}\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{0.5}{x}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 11 |
|---|
| Error | 1.1 |
|---|
| Cost | 7297 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 0.7062183437390931:\\
\;\;\;\;\frac{1}{\sqrt{x}} - \left(1 - x \cdot 0.5\right)\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{0.5}{x}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 12 |
|---|
| Error | 1.2 |
|---|
| Cost | 7169 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 0.4833862213122175:\\
\;\;\;\;{x}^{-0.5} + -1\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{0.5}{x}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 13 |
|---|
| Error | 18.0 |
|---|
| Cost | 7169 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 0.771521771315818:\\
\;\;\;\;{x}^{-0.5} + -1\\
\mathbf{else}:\\
\;\;\;\;\frac{\frac{1}{x}}{\sqrt{1 + x}}\\
\end{array}\]
| Alternative 14 |
|---|
| Error | 20.8 |
|---|
| Cost | 6977 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 0.9891998632382345:\\
\;\;\;\;{x}^{-0.5} + -1\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}\]
| Alternative 15 |
|---|
| Error | 50.0 |
|---|
| Cost | 385 |
|---|
\[\begin{array}{l}
\mathbf{if}\;x \leq 2.703773030902455 \cdot 10^{+102}:\\
\;\;\;\;1\\
\mathbf{else}:\\
\;\;\;\;0\\
\end{array}\]
| Alternative 16 |
|---|
| Error | 51.7 |
|---|
| Cost | 64 |
|---|
\[0\]
Error

Derivation
- Split input into 2 regimes
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 1.45071069524e-8
Initial program 39.6
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}\]
- Using strategy
rm Applied frac-sub_binary64_213339.5
\[\leadsto \color{blue}{\frac{1 \cdot \sqrt{x + 1} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}}\]
Simplified39.5
\[\leadsto \frac{\color{blue}{\sqrt{1 + x} - \sqrt{x}}}{\sqrt{x} \cdot \sqrt{x + 1}}\]
Simplified39.5
\[\leadsto \frac{\sqrt{1 + x} - \sqrt{x}}{\color{blue}{\sqrt{x} \cdot \sqrt{1 + x}}}\]
- Using strategy
rm Applied flip--_binary64_209939.0
\[\leadsto \frac{\color{blue}{\frac{\sqrt{1 + x} \cdot \sqrt{1 + x} - \sqrt{x} \cdot \sqrt{x}}{\sqrt{1 + x} + \sqrt{x}}}}{\sqrt{x} \cdot \sqrt{1 + x}}\]
Simplified0.5
\[\leadsto \frac{\frac{\color{blue}{1}}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x} \cdot \sqrt{1 + x}}\]
- Using strategy
rm Applied associate-/r*_binary64_20680.5
\[\leadsto \color{blue}{\frac{\frac{\frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x}}}{\sqrt{1 + x}}}\]
Simplified0.4
\[\leadsto \frac{\color{blue}{\frac{1}{x + \sqrt{x} \cdot \sqrt{x + 1}}}}{\sqrt{1 + x}}\]
Taylor expanded around inf 0.2
\[\leadsto \frac{\frac{1}{x + \color{blue}{\left(\left(x + 0.5\right) - 0.125 \cdot \frac{1}{x}\right)}}}{\sqrt{1 + x}}\]
Simplified0.2
\[\leadsto \frac{\frac{1}{x + \color{blue}{\left(\left(x + 0.5\right) - \frac{0.125}{x}\right)}}}{\sqrt{1 + x}}\]
Simplified0.2
\[\leadsto \color{blue}{\frac{\frac{1}{x + \left(\left(x + 0.5\right) - \frac{0.125}{x}\right)}}{\sqrt{1 + x}}}\]
if 1.45071069524e-8 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1))))
Initial program 0.3
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}\]
- Using strategy
rm Applied pow1/2_binary64_22040.3
\[\leadsto \frac{1}{\color{blue}{{x}^{0.5}}} - \frac{1}{\sqrt{x + 1}}\]
Applied pow-flip_binary64_21980.1
\[\leadsto \color{blue}{{x}^{\left(-0.5\right)}} - \frac{1}{\sqrt{x + 1}}\]
Simplified0.1
\[\leadsto {x}^{\color{blue}{-0.5}} - \frac{1}{\sqrt{x + 1}}\]
Simplified0.1
\[\leadsto \color{blue}{{x}^{-0.5} - \frac{1}{\sqrt{x + 1}}}\]
- Recombined 2 regimes into one program.
Final simplification0.2
\[\leadsto \begin{array}{l}
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 1.4507106952353632 \cdot 10^{-08}:\\
\;\;\;\;\frac{\frac{1}{x + \left(\left(x + 0.5\right) - \frac{0.125}{x}\right)}}{\sqrt{1 + x}}\\
\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} - \frac{1}{\sqrt{1 + x}}\\
\end{array}\]
Reproduce
herbie shell --seed 2021014
(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)))))