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;
}

Error

Bits error versus x

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original19.6
Target0.7
Herbie0.2
\[\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}}\]

Alternatives

Alternative 1
Error0.3
Cost20032
\[\frac{\frac{1}{x + \sqrt{x} \cdot \sqrt{1 + x}}}{\sqrt{1 + x}}\]
Alternative 2
Error0.7
Cost13632
\[\frac{1}{x \cdot \sqrt{1 + x} + \sqrt{x} \cdot \left(1 + x\right)}\]
Alternative 3
Error0.3
Cost13633
\[\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
Error0.6
Cost8065
\[\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
Error0.6
Cost7809
\[\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
Error0.6
Cost7681
\[\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
Error0.7
Cost7681
\[\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
Error0.7
Cost7553
\[\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
Error0.8
Cost7425
\[\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
Error1.1
Cost7425
\[\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
Error1.1
Cost7297
\[\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
Error1.2
Cost7169
\[\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
Error18.0
Cost7169
\[\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
Error20.8
Cost6977
\[\begin{array}{l} \mathbf{if}\;x \leq 0.9891998632382345:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array}\]
Alternative 15
Error50.0
Cost385
\[\begin{array}{l} \mathbf{if}\;x \leq 2.703773030902455 \cdot 10^{+102}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;0\\ \end{array}\]
Alternative 16
Error51.7
Cost64
\[0\]

Error

Derivation

  1. Split input into 2 regimes
  2. if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 1.45071069524e-8

    1. Initial program 39.6

      \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}\]
    2. Using strategy rm
    3. 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}}}\]
    4. Simplified39.5

      \[\leadsto \frac{\color{blue}{\sqrt{1 + x} - \sqrt{x}}}{\sqrt{x} \cdot \sqrt{x + 1}}\]
    5. Simplified39.5

      \[\leadsto \frac{\sqrt{1 + x} - \sqrt{x}}{\color{blue}{\sqrt{x} \cdot \sqrt{1 + x}}}\]
    6. Using strategy rm
    7. 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}}\]
    8. Simplified0.5

      \[\leadsto \frac{\frac{\color{blue}{1}}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x} \cdot \sqrt{1 + x}}\]
    9. Using strategy rm
    10. Applied associate-/r*_binary64_20680.5

      \[\leadsto \color{blue}{\frac{\frac{\frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x}}}{\sqrt{1 + x}}}\]
    11. Simplified0.4

      \[\leadsto \frac{\color{blue}{\frac{1}{x + \sqrt{x} \cdot \sqrt{x + 1}}}}{\sqrt{1 + x}}\]
    12. 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}}\]
    13. Simplified0.2

      \[\leadsto \frac{\frac{1}{x + \color{blue}{\left(\left(x + 0.5\right) - \frac{0.125}{x}\right)}}}{\sqrt{1 + x}}\]
    14. 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))))

    1. Initial program 0.3

      \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}\]
    2. Using strategy rm
    3. Applied pow1/2_binary64_22040.3

      \[\leadsto \frac{1}{\color{blue}{{x}^{0.5}}} - \frac{1}{\sqrt{x + 1}}\]
    4. Applied pow-flip_binary64_21980.1

      \[\leadsto \color{blue}{{x}^{\left(-0.5\right)}} - \frac{1}{\sqrt{x + 1}}\]
    5. Simplified0.1

      \[\leadsto {x}^{\color{blue}{-0.5}} - \frac{1}{\sqrt{x + 1}}\]
    6. Simplified0.1

      \[\leadsto \color{blue}{{x}^{-0.5} - \frac{1}{\sqrt{x + 1}}}\]
  3. Recombined 2 regimes into one program.
  4. 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)))))