2isqrt (example 3.6)

Percentage Accurate: 38.4% → 99.6%
Time: 13.7s
Alternatives: 4
Speedup: 2.0×

Specification

?
\[x > 1 \land x < 10^{+308}\]
\[\begin{array}{l} \\ \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
double code(double x) {
	return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))
end function
public static double code(double x) {
	return (1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)));
}
def code(x):
	return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
function code(x)
	return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0))))
end
function tmp = code(x)
	tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
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]
\begin{array}{l}

\\
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 4 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 38.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
double code(double x) {
	return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = (1.0d0 / sqrt(x)) - (1.0d0 / sqrt((x + 1.0d0)))
end function
public static double code(double x) {
	return (1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((x + 1.0)));
}
def code(x):
	return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
function code(x)
	return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0))))
end
function tmp = code(x)
	tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
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]
\begin{array}{l}

\\
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\end{array}

Alternative 1: 99.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{\frac{-1}{-1 - x}}{\sqrt{x} + x \cdot {\left(x + 1\right)}^{-0.5}} \end{array} \]
(FPCore (x)
 :precision binary64
 (/ (/ -1.0 (- -1.0 x)) (+ (sqrt x) (* x (pow (+ x 1.0) -0.5)))))
double code(double x) {
	return (-1.0 / (-1.0 - x)) / (sqrt(x) + (x * pow((x + 1.0), -0.5)));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = ((-1.0d0) / ((-1.0d0) - x)) / (sqrt(x) + (x * ((x + 1.0d0) ** (-0.5d0))))
end function
public static double code(double x) {
	return (-1.0 / (-1.0 - x)) / (Math.sqrt(x) + (x * Math.pow((x + 1.0), -0.5)));
}
def code(x):
	return (-1.0 / (-1.0 - x)) / (math.sqrt(x) + (x * math.pow((x + 1.0), -0.5)))
function code(x)
	return Float64(Float64(-1.0 / Float64(-1.0 - x)) / Float64(sqrt(x) + Float64(x * (Float64(x + 1.0) ^ -0.5))))
end
function tmp = code(x)
	tmp = (-1.0 / (-1.0 - x)) / (sqrt(x) + (x * ((x + 1.0) ^ -0.5)));
end
code[x_] := N[(N[(-1.0 / N[(-1.0 - x), $MachinePrecision]), $MachinePrecision] / N[(N[Sqrt[x], $MachinePrecision] + N[(x * N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{\frac{-1}{-1 - x}}{\sqrt{x} + x \cdot {\left(x + 1\right)}^{-0.5}}
\end{array}
Derivation
  1. Initial program 38.5%

    \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
  2. Add Preprocessing
  3. Step-by-step derivation
    1. flip--38.5%

      \[\leadsto \color{blue}{\frac{\frac{1}{\sqrt{x}} \cdot \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \cdot \frac{1}{\sqrt{x + 1}}}{\frac{1}{\sqrt{x}} + \frac{1}{\sqrt{x + 1}}}} \]
    2. div-inv38.5%

      \[\leadsto \color{blue}{\left(\frac{1}{\sqrt{x}} \cdot \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \cdot \frac{1}{\sqrt{x + 1}}\right) \cdot \frac{1}{\frac{1}{\sqrt{x}} + \frac{1}{\sqrt{x + 1}}}} \]
    3. frac-times22.5%

      \[\leadsto \left(\color{blue}{\frac{1 \cdot 1}{\sqrt{x} \cdot \sqrt{x}}} - \frac{1}{\sqrt{x + 1}} \cdot \frac{1}{\sqrt{x + 1}}\right) \cdot \frac{1}{\frac{1}{\sqrt{x}} + \frac{1}{\sqrt{x + 1}}} \]
    4. metadata-eval22.5%

      \[\leadsto \left(\frac{\color{blue}{1}}{\sqrt{x} \cdot \sqrt{x}} - \frac{1}{\sqrt{x + 1}} \cdot \frac{1}{\sqrt{x + 1}}\right) \cdot \frac{1}{\frac{1}{\sqrt{x}} + \frac{1}{\sqrt{x + 1}}} \]
    5. add-sqr-sqrt21.7%

      \[\leadsto \left(\frac{1}{\color{blue}{x}} - \frac{1}{\sqrt{x + 1}} \cdot \frac{1}{\sqrt{x + 1}}\right) \cdot \frac{1}{\frac{1}{\sqrt{x}} + \frac{1}{\sqrt{x + 1}}} \]
    6. frac-times26.9%

      \[\leadsto \left(\frac{1}{x} - \color{blue}{\frac{1 \cdot 1}{\sqrt{x + 1} \cdot \sqrt{x + 1}}}\right) \cdot \frac{1}{\frac{1}{\sqrt{x}} + \frac{1}{\sqrt{x + 1}}} \]
    7. metadata-eval26.9%

      \[\leadsto \left(\frac{1}{x} - \frac{\color{blue}{1}}{\sqrt{x + 1} \cdot \sqrt{x + 1}}\right) \cdot \frac{1}{\frac{1}{\sqrt{x}} + \frac{1}{\sqrt{x + 1}}} \]
    8. add-sqr-sqrt38.7%

      \[\leadsto \left(\frac{1}{x} - \frac{1}{\color{blue}{x + 1}}\right) \cdot \frac{1}{\frac{1}{\sqrt{x}} + \frac{1}{\sqrt{x + 1}}} \]
    9. +-commutative38.7%

      \[\leadsto \left(\frac{1}{x} - \frac{1}{\color{blue}{1 + x}}\right) \cdot \frac{1}{\frac{1}{\sqrt{x}} + \frac{1}{\sqrt{x + 1}}} \]
    10. inv-pow38.7%

      \[\leadsto \left(\frac{1}{x} - \frac{1}{1 + x}\right) \cdot \frac{1}{\color{blue}{{\left(\sqrt{x}\right)}^{-1}} + \frac{1}{\sqrt{x + 1}}} \]
    11. sqrt-pow238.7%

      \[\leadsto \left(\frac{1}{x} - \frac{1}{1 + x}\right) \cdot \frac{1}{\color{blue}{{x}^{\left(\frac{-1}{2}\right)}} + \frac{1}{\sqrt{x + 1}}} \]
    12. metadata-eval38.7%

      \[\leadsto \left(\frac{1}{x} - \frac{1}{1 + x}\right) \cdot \frac{1}{{x}^{\color{blue}{-0.5}} + \frac{1}{\sqrt{x + 1}}} \]
    13. inv-pow38.7%

      \[\leadsto \left(\frac{1}{x} - \frac{1}{1 + x}\right) \cdot \frac{1}{{x}^{-0.5} + \color{blue}{{\left(\sqrt{x + 1}\right)}^{-1}}} \]
    14. sqrt-pow238.7%

      \[\leadsto \left(\frac{1}{x} - \frac{1}{1 + x}\right) \cdot \frac{1}{{x}^{-0.5} + \color{blue}{{\left(x + 1\right)}^{\left(\frac{-1}{2}\right)}}} \]
    15. +-commutative38.7%

      \[\leadsto \left(\frac{1}{x} - \frac{1}{1 + x}\right) \cdot \frac{1}{{x}^{-0.5} + {\color{blue}{\left(1 + x\right)}}^{\left(\frac{-1}{2}\right)}} \]
    16. metadata-eval38.7%

      \[\leadsto \left(\frac{1}{x} - \frac{1}{1 + x}\right) \cdot \frac{1}{{x}^{-0.5} + {\left(1 + x\right)}^{\color{blue}{-0.5}}} \]
  4. Applied egg-rr38.7%

    \[\leadsto \color{blue}{\left(\frac{1}{x} - \frac{1}{1 + x}\right) \cdot \frac{1}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}} \]
  5. Step-by-step derivation
    1. associate-*r/38.7%

      \[\leadsto \color{blue}{\frac{\left(\frac{1}{x} - \frac{1}{1 + x}\right) \cdot 1}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}} \]
    2. *-rgt-identity38.7%

      \[\leadsto \frac{\color{blue}{\frac{1}{x} - \frac{1}{1 + x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
  6. Simplified38.7%

    \[\leadsto \color{blue}{\frac{\frac{1}{x} - \frac{1}{1 + x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}} \]
  7. Step-by-step derivation
    1. frac-sub40.7%

      \[\leadsto \frac{\color{blue}{\frac{1 \cdot \left(1 + x\right) - x \cdot 1}{x \cdot \left(1 + x\right)}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    2. div-inv40.7%

      \[\leadsto \frac{\color{blue}{\left(1 \cdot \left(1 + x\right) - x \cdot 1\right) \cdot \frac{1}{x \cdot \left(1 + x\right)}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    3. *-un-lft-identity40.7%

      \[\leadsto \frac{\left(\color{blue}{\left(1 + x\right)} - x \cdot 1\right) \cdot \frac{1}{x \cdot \left(1 + x\right)}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    4. *-rgt-identity40.7%

      \[\leadsto \frac{\left(\left(1 + x\right) - \color{blue}{x}\right) \cdot \frac{1}{x \cdot \left(1 + x\right)}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    5. metadata-eval40.7%

      \[\leadsto \frac{\left(\left(1 + x\right) - x\right) \cdot \frac{\color{blue}{1 \cdot 1}}{x \cdot \left(1 + x\right)}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    6. frac-times40.7%

      \[\leadsto \frac{\left(\left(1 + x\right) - x\right) \cdot \color{blue}{\left(\frac{1}{x} \cdot \frac{1}{1 + x}\right)}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    7. un-div-inv40.7%

      \[\leadsto \frac{\left(\left(1 + x\right) - x\right) \cdot \color{blue}{\frac{\frac{1}{x}}{1 + x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
  8. Applied egg-rr40.7%

    \[\leadsto \frac{\color{blue}{\left(\left(1 + x\right) - x\right) \cdot \frac{\frac{1}{x}}{1 + x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
  9. Step-by-step derivation
    1. associate-*r/40.7%

      \[\leadsto \frac{\color{blue}{\frac{\left(\left(1 + x\right) - x\right) \cdot \frac{1}{x}}{1 + x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    2. associate--l+82.6%

      \[\leadsto \frac{\frac{\color{blue}{\left(1 + \left(x - x\right)\right)} \cdot \frac{1}{x}}{1 + x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    3. +-inverses82.6%

      \[\leadsto \frac{\frac{\left(1 + \color{blue}{0}\right) \cdot \frac{1}{x}}{1 + x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    4. metadata-eval82.6%

      \[\leadsto \frac{\frac{\color{blue}{1} \cdot \frac{1}{x}}{1 + x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    5. *-lft-identity82.6%

      \[\leadsto \frac{\frac{\color{blue}{\frac{1}{x}}}{1 + x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    6. associate-/l/80.8%

      \[\leadsto \frac{\color{blue}{\frac{1}{\left(1 + x\right) \cdot x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    7. metadata-eval80.8%

      \[\leadsto \frac{\frac{\color{blue}{1 + 0}}{\left(1 + x\right) \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    8. +-inverses80.8%

      \[\leadsto \frac{\frac{1 + \color{blue}{\left(x - x\right)}}{\left(1 + x\right) \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    9. associate--l+40.7%

      \[\leadsto \frac{\frac{\color{blue}{\left(1 + x\right) - x}}{\left(1 + x\right) \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    10. *-rgt-identity40.7%

      \[\leadsto \frac{\frac{\color{blue}{\left(\left(1 + x\right) - x\right) \cdot 1}}{\left(1 + x\right) \cdot x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    11. times-frac40.7%

      \[\leadsto \frac{\color{blue}{\frac{\left(1 + x\right) - x}{1 + x} \cdot \frac{1}{x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    12. associate--l+82.4%

      \[\leadsto \frac{\frac{\color{blue}{1 + \left(x - x\right)}}{1 + x} \cdot \frac{1}{x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    13. +-inverses82.4%

      \[\leadsto \frac{\frac{1 + \color{blue}{0}}{1 + x} \cdot \frac{1}{x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    14. metadata-eval82.4%

      \[\leadsto \frac{\frac{\color{blue}{1}}{1 + x} \cdot \frac{1}{x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    15. remove-double-neg82.4%

      \[\leadsto \frac{\color{blue}{\left(-\left(-\frac{1}{1 + x}\right)\right)} \cdot \frac{1}{x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    16. distribute-lft-neg-in82.4%

      \[\leadsto \frac{\color{blue}{-\left(-\frac{1}{1 + x}\right) \cdot \frac{1}{x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    17. *-commutative82.4%

      \[\leadsto \frac{-\color{blue}{\frac{1}{x} \cdot \left(-\frac{1}{1 + x}\right)}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    18. associate-*l/82.6%

      \[\leadsto \frac{-\color{blue}{\frac{1 \cdot \left(-\frac{1}{1 + x}\right)}{x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    19. *-lft-identity82.6%

      \[\leadsto \frac{-\frac{\color{blue}{-\frac{1}{1 + x}}}{x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    20. distribute-neg-frac82.6%

      \[\leadsto \frac{\color{blue}{\frac{-\left(-\frac{1}{1 + x}\right)}{x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
  10. Simplified82.6%

    \[\leadsto \frac{\color{blue}{\frac{\frac{-1}{-1 - x}}{x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
  11. Step-by-step derivation
    1. *-un-lft-identity82.6%

      \[\leadsto \color{blue}{1 \cdot \frac{\frac{\frac{-1}{-1 - x}}{x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}} \]
    2. associate-/l/80.8%

      \[\leadsto 1 \cdot \frac{\color{blue}{\frac{-1}{x \cdot \left(-1 - x\right)}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}} \]
    3. associate-/l/80.8%

      \[\leadsto 1 \cdot \color{blue}{\frac{-1}{\left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right) \cdot \left(x \cdot \left(-1 - x\right)\right)}} \]
    4. +-commutative80.8%

      \[\leadsto 1 \cdot \frac{-1}{\left({x}^{-0.5} + {\color{blue}{\left(x + 1\right)}}^{-0.5}\right) \cdot \left(x \cdot \left(-1 - x\right)\right)} \]
  12. Applied egg-rr80.8%

    \[\leadsto \color{blue}{1 \cdot \frac{-1}{\left({x}^{-0.5} + {\left(x + 1\right)}^{-0.5}\right) \cdot \left(x \cdot \left(-1 - x\right)\right)}} \]
  13. Step-by-step derivation
    1. *-lft-identity80.8%

      \[\leadsto \color{blue}{\frac{-1}{\left({x}^{-0.5} + {\left(x + 1\right)}^{-0.5}\right) \cdot \left(x \cdot \left(-1 - x\right)\right)}} \]
    2. associate-/l/80.8%

      \[\leadsto \color{blue}{\frac{\frac{-1}{x \cdot \left(-1 - x\right)}}{{x}^{-0.5} + {\left(x + 1\right)}^{-0.5}}} \]
    3. *-commutative80.8%

      \[\leadsto \frac{\frac{-1}{\color{blue}{\left(-1 - x\right) \cdot x}}}{{x}^{-0.5} + {\left(x + 1\right)}^{-0.5}} \]
    4. associate-/r*82.6%

      \[\leadsto \frac{\color{blue}{\frac{\frac{-1}{-1 - x}}{x}}}{{x}^{-0.5} + {\left(x + 1\right)}^{-0.5}} \]
    5. associate-/r*99.6%

      \[\leadsto \color{blue}{\frac{\frac{-1}{-1 - x}}{x \cdot \left({x}^{-0.5} + {\left(x + 1\right)}^{-0.5}\right)}} \]
    6. distribute-rgt-in99.6%

      \[\leadsto \frac{\frac{-1}{-1 - x}}{\color{blue}{{x}^{-0.5} \cdot x + {\left(x + 1\right)}^{-0.5} \cdot x}} \]
    7. pow-plus99.6%

      \[\leadsto \frac{\frac{-1}{-1 - x}}{\color{blue}{{x}^{\left(-0.5 + 1\right)}} + {\left(x + 1\right)}^{-0.5} \cdot x} \]
    8. metadata-eval99.6%

      \[\leadsto \frac{\frac{-1}{-1 - x}}{{x}^{\color{blue}{0.5}} + {\left(x + 1\right)}^{-0.5} \cdot x} \]
    9. unpow1/299.6%

      \[\leadsto \frac{\frac{-1}{-1 - x}}{\color{blue}{\sqrt{x}} + {\left(x + 1\right)}^{-0.5} \cdot x} \]
    10. *-commutative99.6%

      \[\leadsto \frac{\frac{-1}{-1 - x}}{\sqrt{x} + \color{blue}{x \cdot {\left(x + 1\right)}^{-0.5}}} \]
    11. +-commutative99.6%

      \[\leadsto \frac{\frac{-1}{-1 - x}}{\sqrt{x} + x \cdot {\color{blue}{\left(1 + x\right)}}^{-0.5}} \]
  14. Simplified99.6%

    \[\leadsto \color{blue}{\frac{\frac{-1}{-1 - x}}{\sqrt{x} + x \cdot {\left(1 + x\right)}^{-0.5}}} \]
  15. Final simplification99.6%

    \[\leadsto \frac{\frac{-1}{-1 - x}}{\sqrt{x} + x \cdot {\left(x + 1\right)}^{-0.5}} \]
  16. Add Preprocessing

Alternative 2: 99.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;x \leq 120000000:\\ \;\;\;\;{x}^{-0.5} - {\left(x + 1\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot {x}^{-1.5}\\ \end{array} \end{array} \]
(FPCore (x)
 :precision binary64
 (if (<= x 120000000.0)
   (- (pow x -0.5) (pow (+ x 1.0) -0.5))
   (* 0.5 (pow x -1.5))))
double code(double x) {
	double tmp;
	if (x <= 120000000.0) {
		tmp = pow(x, -0.5) - pow((x + 1.0), -0.5);
	} else {
		tmp = 0.5 * pow(x, -1.5);
	}
	return tmp;
}
real(8) function code(x)
    real(8), intent (in) :: x
    real(8) :: tmp
    if (x <= 120000000.0d0) then
        tmp = (x ** (-0.5d0)) - ((x + 1.0d0) ** (-0.5d0))
    else
        tmp = 0.5d0 * (x ** (-1.5d0))
    end if
    code = tmp
end function
public static double code(double x) {
	double tmp;
	if (x <= 120000000.0) {
		tmp = Math.pow(x, -0.5) - Math.pow((x + 1.0), -0.5);
	} else {
		tmp = 0.5 * Math.pow(x, -1.5);
	}
	return tmp;
}
def code(x):
	tmp = 0
	if x <= 120000000.0:
		tmp = math.pow(x, -0.5) - math.pow((x + 1.0), -0.5)
	else:
		tmp = 0.5 * math.pow(x, -1.5)
	return tmp
function code(x)
	tmp = 0.0
	if (x <= 120000000.0)
		tmp = Float64((x ^ -0.5) - (Float64(x + 1.0) ^ -0.5));
	else
		tmp = Float64(0.5 * (x ^ -1.5));
	end
	return tmp
end
function tmp_2 = code(x)
	tmp = 0.0;
	if (x <= 120000000.0)
		tmp = (x ^ -0.5) - ((x + 1.0) ^ -0.5);
	else
		tmp = 0.5 * (x ^ -1.5);
	end
	tmp_2 = tmp;
end
code[x_] := If[LessEqual[x, 120000000.0], N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(x + 1.0), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;x \leq 120000000:\\
\;\;\;\;{x}^{-0.5} - {\left(x + 1\right)}^{-0.5}\\

\mathbf{else}:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if x < 1.2e8

    1. Initial program 79.6%

      \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. sub-neg79.6%

        \[\leadsto \color{blue}{\frac{1}{\sqrt{x}} + \left(-\frac{1}{\sqrt{x + 1}}\right)} \]
      2. inv-pow79.6%

        \[\leadsto \color{blue}{{\left(\sqrt{x}\right)}^{-1}} + \left(-\frac{1}{\sqrt{x + 1}}\right) \]
      3. sqrt-pow279.6%

        \[\leadsto \color{blue}{{x}^{\left(\frac{-1}{2}\right)}} + \left(-\frac{1}{\sqrt{x + 1}}\right) \]
      4. metadata-eval79.6%

        \[\leadsto {x}^{\color{blue}{-0.5}} + \left(-\frac{1}{\sqrt{x + 1}}\right) \]
      5. distribute-neg-frac79.6%

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

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

        \[\leadsto {x}^{-0.5} + \frac{-1}{\sqrt{\color{blue}{1 + x}}} \]
    4. Applied egg-rr79.6%

      \[\leadsto \color{blue}{{x}^{-0.5} + \frac{-1}{\sqrt{1 + x}}} \]
    5. Step-by-step derivation
      1. *-rgt-identity79.6%

        \[\leadsto {x}^{-0.5} + \color{blue}{\frac{-1}{\sqrt{1 + x}} \cdot 1} \]
      2. cancel-sign-sub79.6%

        \[\leadsto \color{blue}{{x}^{-0.5} - \left(-\frac{-1}{\sqrt{1 + x}}\right) \cdot 1} \]
      3. distribute-lft-neg-in79.6%

        \[\leadsto {x}^{-0.5} - \color{blue}{\left(-\frac{-1}{\sqrt{1 + x}} \cdot 1\right)} \]
      4. *-rgt-identity79.6%

        \[\leadsto {x}^{-0.5} - \left(-\color{blue}{\frac{-1}{\sqrt{1 + x}}}\right) \]
      5. distribute-neg-frac79.6%

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

        \[\leadsto {x}^{-0.5} - \frac{\color{blue}{1}}{\sqrt{1 + x}} \]
      7. unpow1/279.6%

        \[\leadsto {x}^{-0.5} - \frac{1}{\color{blue}{{\left(1 + x\right)}^{0.5}}} \]
      8. exp-to-pow77.4%

        \[\leadsto {x}^{-0.5} - \frac{1}{\color{blue}{e^{\log \left(1 + x\right) \cdot 0.5}}} \]
      9. log1p-undefine77.4%

        \[\leadsto {x}^{-0.5} - \frac{1}{e^{\color{blue}{\mathsf{log1p}\left(x\right)} \cdot 0.5}} \]
      10. *-commutative77.4%

        \[\leadsto {x}^{-0.5} - \frac{1}{e^{\color{blue}{0.5 \cdot \mathsf{log1p}\left(x\right)}}} \]
      11. exp-neg77.8%

        \[\leadsto {x}^{-0.5} - \color{blue}{e^{-0.5 \cdot \mathsf{log1p}\left(x\right)}} \]
      12. *-commutative77.8%

        \[\leadsto {x}^{-0.5} - e^{-\color{blue}{\mathsf{log1p}\left(x\right) \cdot 0.5}} \]
      13. distribute-rgt-neg-in77.8%

        \[\leadsto {x}^{-0.5} - e^{\color{blue}{\mathsf{log1p}\left(x\right) \cdot \left(-0.5\right)}} \]
      14. log1p-undefine77.8%

        \[\leadsto {x}^{-0.5} - e^{\color{blue}{\log \left(1 + x\right)} \cdot \left(-0.5\right)} \]
      15. metadata-eval77.8%

        \[\leadsto {x}^{-0.5} - e^{\log \left(1 + x\right) \cdot \color{blue}{-0.5}} \]
      16. exp-to-pow79.9%

        \[\leadsto {x}^{-0.5} - \color{blue}{{\left(1 + x\right)}^{-0.5}} \]
    6. Simplified79.9%

      \[\leadsto \color{blue}{{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}} \]

    if 1.2e8 < x

    1. Initial program 36.5%

      \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
    2. Add Preprocessing
    3. Taylor expanded in x around inf 79.4%

      \[\leadsto \color{blue}{\frac{-0.5 \cdot \sqrt{\frac{1}{x}} - -0.5 \cdot \sqrt{x}}{{x}^{2}}} \]
    4. Step-by-step derivation
      1. div-sub79.4%

        \[\leadsto \color{blue}{\frac{-0.5 \cdot \sqrt{\frac{1}{x}}}{{x}^{2}} - \frac{-0.5 \cdot \sqrt{x}}{{x}^{2}}} \]
      2. *-commutative79.4%

        \[\leadsto \frac{\color{blue}{\sqrt{\frac{1}{x}} \cdot -0.5}}{{x}^{2}} - \frac{-0.5 \cdot \sqrt{x}}{{x}^{2}} \]
      3. associate-/l*79.4%

        \[\leadsto \color{blue}{\sqrt{\frac{1}{x}} \cdot \frac{-0.5}{{x}^{2}}} - \frac{-0.5 \cdot \sqrt{x}}{{x}^{2}} \]
      4. *-commutative79.4%

        \[\leadsto \sqrt{\frac{1}{x}} \cdot \frac{-0.5}{{x}^{2}} - \frac{\color{blue}{\sqrt{x} \cdot -0.5}}{{x}^{2}} \]
      5. associate-/l*79.4%

        \[\leadsto \sqrt{\frac{1}{x}} \cdot \frac{-0.5}{{x}^{2}} - \color{blue}{\sqrt{x} \cdot \frac{-0.5}{{x}^{2}}} \]
      6. distribute-rgt-out--79.4%

        \[\leadsto \color{blue}{\frac{-0.5}{{x}^{2}} \cdot \left(\sqrt{\frac{1}{x}} - \sqrt{x}\right)} \]
    5. Simplified79.4%

      \[\leadsto \color{blue}{\frac{-0.5}{{x}^{2}} \cdot \left(\sqrt{\frac{1}{x}} - \sqrt{x}\right)} \]
    6. Taylor expanded in x around inf 99.2%

      \[\leadsto \color{blue}{\frac{-0.5 \cdot \sqrt{\frac{1}{{x}^{3}}} + 0.5 \cdot \sqrt{\frac{1}{x}}}{x}} \]
    7. Taylor expanded in x around inf 66.0%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{\frac{1}{{x}^{3}}}} \]
    8. Step-by-step derivation
      1. exp-to-pow63.5%

        \[\leadsto 0.5 \cdot \sqrt{\frac{1}{\color{blue}{e^{\log x \cdot 3}}}} \]
      2. *-commutative63.5%

        \[\leadsto 0.5 \cdot \sqrt{\frac{1}{e^{\color{blue}{3 \cdot \log x}}}} \]
      3. exp-neg64.1%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{e^{-3 \cdot \log x}}} \]
      4. distribute-lft-neg-in64.1%

        \[\leadsto 0.5 \cdot \sqrt{e^{\color{blue}{\left(-3\right) \cdot \log x}}} \]
      5. metadata-eval64.1%

        \[\leadsto 0.5 \cdot \sqrt{e^{\color{blue}{-3} \cdot \log x}} \]
      6. *-commutative64.1%

        \[\leadsto 0.5 \cdot \sqrt{e^{\color{blue}{\log x \cdot -3}}} \]
      7. exp-to-pow66.7%

        \[\leadsto 0.5 \cdot \sqrt{\color{blue}{{x}^{-3}}} \]
    9. Simplified66.7%

      \[\leadsto \color{blue}{0.5 \cdot \sqrt{{x}^{-3}}} \]
    10. Step-by-step derivation
      1. sqrt-pow199.4%

        \[\leadsto 0.5 \cdot \color{blue}{{x}^{\left(\frac{-3}{2}\right)}} \]
      2. metadata-eval99.4%

        \[\leadsto 0.5 \cdot {x}^{\color{blue}{-1.5}} \]
      3. *-un-lft-identity99.4%

        \[\leadsto 0.5 \cdot \color{blue}{\left(1 \cdot {x}^{-1.5}\right)} \]
      4. *-commutative99.4%

        \[\leadsto 0.5 \cdot \color{blue}{\left({x}^{-1.5} \cdot 1\right)} \]
    11. Applied egg-rr99.4%

      \[\leadsto 0.5 \cdot \color{blue}{\left({x}^{-1.5} \cdot 1\right)} \]
    12. Step-by-step derivation
      1. *-rgt-identity99.4%

        \[\leadsto 0.5 \cdot \color{blue}{{x}^{-1.5}} \]
    13. Simplified99.4%

      \[\leadsto 0.5 \cdot \color{blue}{{x}^{-1.5}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification98.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq 120000000:\\ \;\;\;\;{x}^{-0.5} - {\left(x + 1\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot {x}^{-1.5}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 97.8% accurate, 2.0× speedup?

\[\begin{array}{l} \\ 0.5 \cdot {x}^{-1.5} \end{array} \]
(FPCore (x) :precision binary64 (* 0.5 (pow x -1.5)))
double code(double x) {
	return 0.5 * pow(x, -1.5);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 0.5d0 * (x ** (-1.5d0))
end function
public static double code(double x) {
	return 0.5 * Math.pow(x, -1.5);
}
def code(x):
	return 0.5 * math.pow(x, -1.5)
function code(x)
	return Float64(0.5 * (x ^ -1.5))
end
function tmp = code(x)
	tmp = 0.5 * (x ^ -1.5);
end
code[x_] := N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
0.5 \cdot {x}^{-1.5}
\end{array}
Derivation
  1. Initial program 38.5%

    \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around inf 77.7%

    \[\leadsto \color{blue}{\frac{-0.5 \cdot \sqrt{\frac{1}{x}} - -0.5 \cdot \sqrt{x}}{{x}^{2}}} \]
  4. Step-by-step derivation
    1. div-sub77.7%

      \[\leadsto \color{blue}{\frac{-0.5 \cdot \sqrt{\frac{1}{x}}}{{x}^{2}} - \frac{-0.5 \cdot \sqrt{x}}{{x}^{2}}} \]
    2. *-commutative77.7%

      \[\leadsto \frac{\color{blue}{\sqrt{\frac{1}{x}} \cdot -0.5}}{{x}^{2}} - \frac{-0.5 \cdot \sqrt{x}}{{x}^{2}} \]
    3. associate-/l*77.7%

      \[\leadsto \color{blue}{\sqrt{\frac{1}{x}} \cdot \frac{-0.5}{{x}^{2}}} - \frac{-0.5 \cdot \sqrt{x}}{{x}^{2}} \]
    4. *-commutative77.7%

      \[\leadsto \sqrt{\frac{1}{x}} \cdot \frac{-0.5}{{x}^{2}} - \frac{\color{blue}{\sqrt{x} \cdot -0.5}}{{x}^{2}} \]
    5. associate-/l*77.6%

      \[\leadsto \sqrt{\frac{1}{x}} \cdot \frac{-0.5}{{x}^{2}} - \color{blue}{\sqrt{x} \cdot \frac{-0.5}{{x}^{2}}} \]
    6. distribute-rgt-out--77.6%

      \[\leadsto \color{blue}{\frac{-0.5}{{x}^{2}} \cdot \left(\sqrt{\frac{1}{x}} - \sqrt{x}\right)} \]
  5. Simplified77.6%

    \[\leadsto \color{blue}{\frac{-0.5}{{x}^{2}} \cdot \left(\sqrt{\frac{1}{x}} - \sqrt{x}\right)} \]
  6. Taylor expanded in x around inf 96.6%

    \[\leadsto \color{blue}{\frac{-0.5 \cdot \sqrt{\frac{1}{{x}^{3}}} + 0.5 \cdot \sqrt{\frac{1}{x}}}{x}} \]
  7. Taylor expanded in x around inf 64.8%

    \[\leadsto \color{blue}{0.5 \cdot \sqrt{\frac{1}{{x}^{3}}}} \]
  8. Step-by-step derivation
    1. exp-to-pow62.4%

      \[\leadsto 0.5 \cdot \sqrt{\frac{1}{\color{blue}{e^{\log x \cdot 3}}}} \]
    2. *-commutative62.4%

      \[\leadsto 0.5 \cdot \sqrt{\frac{1}{e^{\color{blue}{3 \cdot \log x}}}} \]
    3. exp-neg63.0%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{e^{-3 \cdot \log x}}} \]
    4. distribute-lft-neg-in63.0%

      \[\leadsto 0.5 \cdot \sqrt{e^{\color{blue}{\left(-3\right) \cdot \log x}}} \]
    5. metadata-eval63.0%

      \[\leadsto 0.5 \cdot \sqrt{e^{\color{blue}{-3} \cdot \log x}} \]
    6. *-commutative63.0%

      \[\leadsto 0.5 \cdot \sqrt{e^{\color{blue}{\log x \cdot -3}}} \]
    7. exp-to-pow65.4%

      \[\leadsto 0.5 \cdot \sqrt{\color{blue}{{x}^{-3}}} \]
  9. Simplified65.4%

    \[\leadsto \color{blue}{0.5 \cdot \sqrt{{x}^{-3}}} \]
  10. Step-by-step derivation
    1. sqrt-pow196.6%

      \[\leadsto 0.5 \cdot \color{blue}{{x}^{\left(\frac{-3}{2}\right)}} \]
    2. metadata-eval96.6%

      \[\leadsto 0.5 \cdot {x}^{\color{blue}{-1.5}} \]
    3. *-un-lft-identity96.6%

      \[\leadsto 0.5 \cdot \color{blue}{\left(1 \cdot {x}^{-1.5}\right)} \]
    4. *-commutative96.6%

      \[\leadsto 0.5 \cdot \color{blue}{\left({x}^{-1.5} \cdot 1\right)} \]
  11. Applied egg-rr96.6%

    \[\leadsto 0.5 \cdot \color{blue}{\left({x}^{-1.5} \cdot 1\right)} \]
  12. Step-by-step derivation
    1. *-rgt-identity96.6%

      \[\leadsto 0.5 \cdot \color{blue}{{x}^{-1.5}} \]
  13. Simplified96.6%

    \[\leadsto 0.5 \cdot \color{blue}{{x}^{-1.5}} \]
  14. Final simplification96.6%

    \[\leadsto 0.5 \cdot {x}^{-1.5} \]
  15. Add Preprocessing

Alternative 4: 5.6% accurate, 2.0× speedup?

\[\begin{array}{l} \\ {x}^{-0.5} \end{array} \]
(FPCore (x) :precision binary64 (pow x -0.5))
double code(double x) {
	return pow(x, -0.5);
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = x ** (-0.5d0)
end function
public static double code(double x) {
	return Math.pow(x, -0.5);
}
def code(x):
	return math.pow(x, -0.5)
function code(x)
	return x ^ -0.5
end
function tmp = code(x)
	tmp = x ^ -0.5;
end
code[x_] := N[Power[x, -0.5], $MachinePrecision]
\begin{array}{l}

\\
{x}^{-0.5}
\end{array}
Derivation
  1. Initial program 38.5%

    \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
  2. Add Preprocessing
  3. Taylor expanded in x around 0 5.8%

    \[\leadsto \color{blue}{\sqrt{\frac{1}{x}}} \]
  4. Step-by-step derivation
    1. inv-pow5.8%

      \[\leadsto \sqrt{\color{blue}{{x}^{-1}}} \]
    2. sqrt-pow15.8%

      \[\leadsto \color{blue}{{x}^{\left(\frac{-1}{2}\right)}} \]
    3. metadata-eval5.8%

      \[\leadsto {x}^{\color{blue}{-0.5}} \]
    4. *-un-lft-identity5.8%

      \[\leadsto \color{blue}{1 \cdot {x}^{-0.5}} \]
  5. Applied egg-rr5.8%

    \[\leadsto \color{blue}{1 \cdot {x}^{-0.5}} \]
  6. Step-by-step derivation
    1. *-lft-identity5.8%

      \[\leadsto \color{blue}{{x}^{-0.5}} \]
  7. Simplified5.8%

    \[\leadsto \color{blue}{{x}^{-0.5}} \]
  8. Final simplification5.8%

    \[\leadsto {x}^{-0.5} \]
  9. Add Preprocessing

Developer target: 98.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}} \end{array} \]
(FPCore (x)
 :precision binary64
 (/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0))))))
double code(double x) {
	return 1.0 / (((x + 1.0) * sqrt(x)) + (x * sqrt((x + 1.0))));
}
real(8) function code(x)
    real(8), intent (in) :: x
    code = 1.0d0 / (((x + 1.0d0) * sqrt(x)) + (x * sqrt((x + 1.0d0))))
end function
public static double code(double x) {
	return 1.0 / (((x + 1.0) * Math.sqrt(x)) + (x * Math.sqrt((x + 1.0))));
}
def code(x):
	return 1.0 / (((x + 1.0) * math.sqrt(x)) + (x * math.sqrt((x + 1.0))))
function code(x)
	return Float64(1.0 / Float64(Float64(Float64(x + 1.0) * sqrt(x)) + Float64(x * sqrt(Float64(x + 1.0)))))
end
function tmp = code(x)
	tmp = 1.0 / (((x + 1.0) * sqrt(x)) + (x * sqrt((x + 1.0))));
end
code[x_] := N[(1.0 / N[(N[(N[(x + 1.0), $MachinePrecision] * N[Sqrt[x], $MachinePrecision]), $MachinePrecision] + N[(x * N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}}
\end{array}

Reproduce

?
herbie shell --seed 2024079 
(FPCore (x)
  :name "2isqrt (example 3.6)"
  :precision binary64
  :pre (and (> x 1.0) (< x 1e+308))

  :alt
  (/ 1.0 (+ (* (+ x 1.0) (sqrt x)) (* x (sqrt (+ x 1.0)))))

  (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))