2isqrt (example 3.6)

?

Percentage Accurate: 69.5% → 99.8%
Time: 10.0s
Precision: binary64
Cost: 19776

?

\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\frac{{x}^{-0.5}}{\mathsf{hypot}\left(x, \sqrt{x}\right) + \left(x + 1\right)} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
 :precision binary64
 (/ (pow x -0.5) (+ (hypot x (sqrt x)) (+ x 1.0))))
double code(double x) {
	return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
	return pow(x, -0.5) / (hypot(x, sqrt(x)) + (x + 1.0));
}
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) {
	return Math.pow(x, -0.5) / (Math.hypot(x, Math.sqrt(x)) + (x + 1.0));
}
def code(x):
	return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
def code(x):
	return math.pow(x, -0.5) / (math.hypot(x, math.sqrt(x)) + (x + 1.0))
function code(x)
	return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0))))
end
function code(x)
	return Float64((x ^ -0.5) / Float64(hypot(x, sqrt(x)) + Float64(x + 1.0)))
end
function tmp = code(x)
	tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
end
function tmp = code(x)
	tmp = (x ^ -0.5) / (hypot(x, sqrt(x)) + (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]
code[x_] := N[(N[Power[x, -0.5], $MachinePrecision] / N[(N[Sqrt[x ^ 2 + N[Sqrt[x], $MachinePrecision] ^ 2], $MachinePrecision] + N[(x + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\frac{{x}^{-0.5}}{\mathsf{hypot}\left(x, \sqrt{x}\right) + \left(x + 1\right)}

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.

Herbie found 13 alternatives:

AlternativeAccuracySpeedup

Accuracy vs Speed

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.

Bogosity?

Bogosity

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original69.5%
Target99.0%
Herbie99.8%
\[\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}} \]

Derivation?

  1. Initial program 70.9%

    \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
  2. Applied egg-rr71.2%

    \[\leadsto \color{blue}{\left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \frac{{x}^{-0.5}}{\sqrt{1 + x}}} \]
    Step-by-step derivation

    [Start]70.9%

    \[ \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]

    frac-sub [=>]71.0%

    \[ \color{blue}{\frac{1 \cdot \sqrt{x + 1} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}} \]

    div-inv [=>]71.0%

    \[ \color{blue}{\left(1 \cdot \sqrt{x + 1} - \sqrt{x} \cdot 1\right) \cdot \frac{1}{\sqrt{x} \cdot \sqrt{x + 1}}} \]

    *-un-lft-identity [<=]71.0%

    \[ \left(\color{blue}{\sqrt{x + 1}} - \sqrt{x} \cdot 1\right) \cdot \frac{1}{\sqrt{x} \cdot \sqrt{x + 1}} \]

    +-commutative [=>]71.0%

    \[ \left(\sqrt{\color{blue}{1 + x}} - \sqrt{x} \cdot 1\right) \cdot \frac{1}{\sqrt{x} \cdot \sqrt{x + 1}} \]

    *-rgt-identity [=>]71.0%

    \[ \left(\sqrt{1 + x} - \color{blue}{\sqrt{x}}\right) \cdot \frac{1}{\sqrt{x} \cdot \sqrt{x + 1}} \]

    metadata-eval [<=]71.0%

    \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \frac{\color{blue}{1 \cdot 1}}{\sqrt{x} \cdot \sqrt{x + 1}} \]

    frac-times [<=]71.0%

    \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \color{blue}{\left(\frac{1}{\sqrt{x}} \cdot \frac{1}{\sqrt{x + 1}}\right)} \]

    un-div-inv [=>]71.0%

    \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \color{blue}{\frac{\frac{1}{\sqrt{x}}}{\sqrt{x + 1}}} \]

    pow1/2 [=>]71.0%

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

    pow-flip [=>]71.2%

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

    metadata-eval [=>]71.2%

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

    +-commutative [=>]71.2%

    \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \frac{{x}^{-0.5}}{\sqrt{\color{blue}{1 + x}}} \]
  3. Simplified71.2%

    \[\leadsto \color{blue}{\frac{\left(\sqrt{1 + x} - \sqrt{x}\right) \cdot {x}^{-0.5}}{\sqrt{1 + x}}} \]
    Step-by-step derivation

    [Start]71.2%

    \[ \left(\sqrt{1 + x} - \sqrt{x}\right) \cdot \frac{{x}^{-0.5}}{\sqrt{1 + x}} \]

    associate-*r/ [=>]71.2%

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

    \[\leadsto \frac{\color{blue}{\frac{\left(1 + x\right) - x}{\sqrt{1 + x} + \sqrt{x}}} \cdot {x}^{-0.5}}{\sqrt{1 + x}} \]
    Step-by-step derivation

    [Start]71.2%

    \[ \frac{\left(\sqrt{1 + x} - \sqrt{x}\right) \cdot {x}^{-0.5}}{\sqrt{1 + x}} \]

    flip-- [=>]71.3%

    \[ \frac{\color{blue}{\frac{\sqrt{1 + x} \cdot \sqrt{1 + x} - \sqrt{x} \cdot \sqrt{x}}{\sqrt{1 + x} + \sqrt{x}}} \cdot {x}^{-0.5}}{\sqrt{1 + x}} \]

    add-sqr-sqrt [<=]61.2%

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

    add-sqr-sqrt [<=]72.0%

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

    \[\leadsto \frac{\color{blue}{\frac{1}{\sqrt{1 + x} + \sqrt{x}}} \cdot {x}^{-0.5}}{\sqrt{1 + x}} \]
    Step-by-step derivation

    [Start]72.0%

    \[ \frac{\frac{\left(1 + x\right) - x}{\sqrt{1 + x} + \sqrt{x}} \cdot {x}^{-0.5}}{\sqrt{1 + x}} \]

    associate--l+ [=>]99.7%

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

    +-inverses [=>]99.7%

    \[ \frac{\frac{1 + \color{blue}{0}}{\sqrt{1 + x} + \sqrt{x}} \cdot {x}^{-0.5}}{\sqrt{1 + x}} \]

    metadata-eval [=>]99.7%

    \[ \frac{\frac{\color{blue}{1}}{\sqrt{1 + x} + \sqrt{x}} \cdot {x}^{-0.5}}{\sqrt{1 + x}} \]
  6. Applied egg-rr67.0%

    \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{\sqrt{1 + x} \cdot \left(\sqrt{1 + x} + \sqrt{x}\right)}\right)} - 1} \]
    Step-by-step derivation

    [Start]99.7%

    \[ \frac{\frac{1}{\sqrt{1 + x} + \sqrt{x}} \cdot {x}^{-0.5}}{\sqrt{1 + x}} \]

    expm1-log1p-u [=>]96.0%

    \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\frac{1}{\sqrt{1 + x} + \sqrt{x}} \cdot {x}^{-0.5}}{\sqrt{1 + x}}\right)\right)} \]

    expm1-udef [=>]66.9%

    \[ \color{blue}{e^{\mathsf{log1p}\left(\frac{\frac{1}{\sqrt{1 + x} + \sqrt{x}} \cdot {x}^{-0.5}}{\sqrt{1 + x}}\right)} - 1} \]

    associate-*l/ [=>]66.9%

    \[ e^{\mathsf{log1p}\left(\frac{\color{blue}{\frac{1 \cdot {x}^{-0.5}}{\sqrt{1 + x} + \sqrt{x}}}}{\sqrt{1 + x}}\right)} - 1 \]

    *-un-lft-identity [<=]66.9%

    \[ e^{\mathsf{log1p}\left(\frac{\frac{\color{blue}{{x}^{-0.5}}}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{1 + x}}\right)} - 1 \]

    associate-/l/ [=>]67.0%

    \[ e^{\mathsf{log1p}\left(\color{blue}{\frac{{x}^{-0.5}}{\sqrt{1 + x} \cdot \left(\sqrt{1 + x} + \sqrt{x}\right)}}\right)} - 1 \]
  7. Simplified99.8%

    \[\leadsto \color{blue}{\frac{{x}^{-0.5}}{\left(x + 1\right) + \sqrt{x} \cdot \sqrt{x + 1}}} \]
    Step-by-step derivation

    [Start]67.0%

    \[ e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{\sqrt{1 + x} \cdot \left(\sqrt{1 + x} + \sqrt{x}\right)}\right)} - 1 \]

    expm1-def [=>]96.0%

    \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{{x}^{-0.5}}{\sqrt{1 + x} \cdot \left(\sqrt{1 + x} + \sqrt{x}\right)}\right)\right)} \]

    expm1-log1p [=>]99.7%

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

    distribute-rgt-in [=>]99.7%

    \[ \frac{{x}^{-0.5}}{\color{blue}{\sqrt{1 + x} \cdot \sqrt{1 + x} + \sqrt{x} \cdot \sqrt{1 + x}}} \]

    rem-square-sqrt [=>]99.8%

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

    +-commutative [=>]99.8%

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

    +-commutative [=>]99.8%

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

    \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{x + \left(1 + \mathsf{hypot}\left(x, \sqrt{x}\right)\right)}\right)} - 1} \]
    Step-by-step derivation

    [Start]99.8%

    \[ \frac{{x}^{-0.5}}{\left(x + 1\right) + \sqrt{x} \cdot \sqrt{x + 1}} \]

    expm1-log1p-u [=>]96.1%

    \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{{x}^{-0.5}}{\left(x + 1\right) + \sqrt{x} \cdot \sqrt{x + 1}}\right)\right)} \]

    expm1-udef [=>]67.0%

    \[ \color{blue}{e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{\left(x + 1\right) + \sqrt{x} \cdot \sqrt{x + 1}}\right)} - 1} \]

    sqrt-prod [<=]67.0%

    \[ e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{\left(x + 1\right) + \color{blue}{\sqrt{x \cdot \left(x + 1\right)}}}\right)} - 1 \]

    associate-+l+ [=>]67.0%

    \[ e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{\color{blue}{x + \left(1 + \sqrt{x \cdot \left(x + 1\right)}\right)}}\right)} - 1 \]

    distribute-rgt-in [=>]67.0%

    \[ e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{x + \left(1 + \sqrt{\color{blue}{x \cdot x + 1 \cdot x}}\right)}\right)} - 1 \]

    *-un-lft-identity [<=]67.0%

    \[ e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{x + \left(1 + \sqrt{x \cdot x + \color{blue}{x}}\right)}\right)} - 1 \]

    add-sqr-sqrt [=>]67.0%

    \[ e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{x + \left(1 + \sqrt{x \cdot x + \color{blue}{\sqrt{x} \cdot \sqrt{x}}}\right)}\right)} - 1 \]

    hypot-def [=>]67.0%

    \[ e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{x + \left(1 + \color{blue}{\mathsf{hypot}\left(x, \sqrt{x}\right)}\right)}\right)} - 1 \]
  9. Simplified99.9%

    \[\leadsto \color{blue}{\frac{{x}^{-0.5}}{\mathsf{hypot}\left(x, \sqrt{x}\right) + \left(x + 1\right)}} \]
    Step-by-step derivation

    [Start]67.0%

    \[ e^{\mathsf{log1p}\left(\frac{{x}^{-0.5}}{x + \left(1 + \mathsf{hypot}\left(x, \sqrt{x}\right)\right)}\right)} - 1 \]

    expm1-def [=>]96.1%

    \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{{x}^{-0.5}}{x + \left(1 + \mathsf{hypot}\left(x, \sqrt{x}\right)\right)}\right)\right)} \]

    expm1-log1p [=>]99.9%

    \[ \color{blue}{\frac{{x}^{-0.5}}{x + \left(1 + \mathsf{hypot}\left(x, \sqrt{x}\right)\right)}} \]

    +-commutative [=>]99.9%

    \[ \frac{{x}^{-0.5}}{\color{blue}{\left(1 + \mathsf{hypot}\left(x, \sqrt{x}\right)\right) + x}} \]

    +-commutative [=>]99.9%

    \[ \frac{{x}^{-0.5}}{\color{blue}{\left(\mathsf{hypot}\left(x, \sqrt{x}\right) + 1\right)} + x} \]

    associate-+r+ [<=]99.9%

    \[ \frac{{x}^{-0.5}}{\color{blue}{\mathsf{hypot}\left(x, \sqrt{x}\right) + \left(1 + x\right)}} \]

    +-commutative [<=]99.9%

    \[ \frac{{x}^{-0.5}}{\mathsf{hypot}\left(x, \sqrt{x}\right) + \color{blue}{\left(x + 1\right)}} \]
  10. Final simplification99.9%

    \[\leadsto \frac{{x}^{-0.5}}{\mathsf{hypot}\left(x, \sqrt{x}\right) + \left(x + 1\right)} \]

Alternatives

Alternative 1
Accuracy99.8%
Cost19776
\[\frac{{x}^{-0.5}}{\mathsf{hypot}\left(x, \sqrt{x}\right) + \left(x + 1\right)} \]
Alternative 2
Accuracy99.8%
Cost26756
\[\begin{array}{l} t_0 := \frac{-1}{\sqrt{x + 1}}\\ \mathbf{if}\;\frac{1}{\sqrt{x}} + t_0 \leq 10^{-7}:\\ \;\;\;\;\frac{{x}^{-0.5}}{\left(x + 1\right) + \left(\frac{0.0625}{x \cdot x} + \left(x + \left(0.5 + \frac{-0.125}{x}\right)\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;{x}^{-0.5} + t_0\\ \end{array} \]
Alternative 3
Accuracy99.8%
Cost13380
\[\begin{array}{l} \mathbf{if}\;x \leq 820:\\ \;\;\;\;{x}^{-0.5} - {\left(x + 1\right)}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5}}{\left(x + 1\right) + \left(\frac{0.0625}{x \cdot x} + \left(x + \left(0.5 + \frac{-0.125}{x}\right)\right)\right)}\\ \end{array} \]
Alternative 4
Accuracy99.2%
Cost7812
\[\begin{array}{l} \mathbf{if}\;x \leq 0.44:\\ \;\;\;\;\left({x}^{-0.5} + x \cdot 0.5\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5}}{\left(x + 1\right) + \left(\frac{0.0625}{x \cdot x} + \left(x + \left(0.5 + \frac{-0.125}{x}\right)\right)\right)}\\ \end{array} \]
Alternative 5
Accuracy99.2%
Cost7300
\[\begin{array}{l} \mathbf{if}\;x \leq 0.43:\\ \;\;\;\;\left({x}^{-0.5} + x \cdot 0.5\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5}}{1.5 + \left(x \cdot 2 - \frac{0.125}{x}\right)}\\ \end{array} \]
Alternative 6
Accuracy98.5%
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 0.72:\\ \;\;\;\;\left({x}^{-0.5} + x \cdot 0.5\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5}{x}}{\sqrt{x + 1}}\\ \end{array} \]
Alternative 7
Accuracy99.0%
Cost7044
\[\begin{array}{l} \mathbf{if}\;x \leq 0.41:\\ \;\;\;\;\left({x}^{-0.5} + x \cdot 0.5\right) + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5}}{1.5 + x \cdot 2}\\ \end{array} \]
Alternative 8
Accuracy98.2%
Cost6980
\[\begin{array}{l} \mathbf{if}\;x \leq 0.48:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5}{x}}{\sqrt{x + 1}}\\ \end{array} \]
Alternative 9
Accuracy98.2%
Cost6916
\[\begin{array}{l} \mathbf{if}\;x \leq 0.68:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{{x}^{-0.5}}{x \cdot 2}\\ \end{array} \]
Alternative 10
Accuracy68.6%
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 0.48:\\ \;\;\;\;{x}^{-0.5} + -1\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5}{x}}{1 + x \cdot 0.5}\\ \end{array} \]
Alternative 11
Accuracy67.2%
Cost6660
\[\begin{array}{l} \mathbf{if}\;x \leq 0.205:\\ \;\;\;\;{x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{0.5}{x}}{1 + x \cdot 0.5}\\ \end{array} \]
Alternative 12
Accuracy22.3%
Cost576
\[\frac{\frac{0.5}{x}}{1 + x \cdot 0.5} \]
Alternative 13
Accuracy7.4%
Cost192
\[\frac{0.5}{x} \]

Reproduce?

herbie shell --seed 2023165 
(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)))))