| Alternative 1 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 26948 |
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
:precision binary64
(let* ((t_0 (sqrt (+ 1.0 x))))
(if (<= (- (/ 1.0 (sqrt x)) (/ 1.0 t_0)) 5e-10)
(/ (/ 1.0 (+ (sqrt x) t_0)) (- (+ x 0.5) (/ 0.125 x)))
(- (pow x -0.5) (pow (+ 1.0 x) -0.5)))))double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
double t_0 = sqrt((1.0 + x));
double tmp;
if (((1.0 / sqrt(x)) - (1.0 / t_0)) <= 5e-10) {
tmp = (1.0 / (sqrt(x) + t_0)) / ((x + 0.5) - (0.125 / x));
} else {
tmp = pow(x, -0.5) - pow((1.0 + x), -0.5);
}
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) :: t_0
real(8) :: tmp
t_0 = sqrt((1.0d0 + x))
if (((1.0d0 / sqrt(x)) - (1.0d0 / t_0)) <= 5d-10) then
tmp = (1.0d0 / (sqrt(x) + t_0)) / ((x + 0.5d0) - (0.125d0 / x))
else
tmp = (x ** (-0.5d0)) - ((1.0d0 + x) ** (-0.5d0))
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 t_0 = Math.sqrt((1.0 + x));
double tmp;
if (((1.0 / Math.sqrt(x)) - (1.0 / t_0)) <= 5e-10) {
tmp = (1.0 / (Math.sqrt(x) + t_0)) / ((x + 0.5) - (0.125 / x));
} else {
tmp = Math.pow(x, -0.5) - Math.pow((1.0 + x), -0.5);
}
return tmp;
}
def code(x): return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
def code(x): t_0 = math.sqrt((1.0 + x)) tmp = 0 if ((1.0 / math.sqrt(x)) - (1.0 / t_0)) <= 5e-10: tmp = (1.0 / (math.sqrt(x) + t_0)) / ((x + 0.5) - (0.125 / x)) else: tmp = math.pow(x, -0.5) - math.pow((1.0 + x), -0.5) return tmp
function code(x) return Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(x + 1.0)))) end
function code(x) t_0 = sqrt(Float64(1.0 + x)) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / t_0)) <= 5e-10) tmp = Float64(Float64(1.0 / Float64(sqrt(x) + t_0)) / Float64(Float64(x + 0.5) - Float64(0.125 / x))); else tmp = Float64((x ^ -0.5) - (Float64(1.0 + x) ^ -0.5)); 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) t_0 = sqrt((1.0 + x)); tmp = 0.0; if (((1.0 / sqrt(x)) - (1.0 / t_0)) <= 5e-10) tmp = (1.0 / (sqrt(x) + t_0)) / ((x + 0.5) - (0.125 / x)); else tmp = (x ^ -0.5) - ((1.0 + x) ^ -0.5); 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_] := Block[{t$95$0 = N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / t$95$0), $MachinePrecision]), $MachinePrecision], 5e-10], N[(N[(1.0 / N[(N[Sqrt[x], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision] / N[(N[(x + 0.5), $MachinePrecision] - N[(0.125 / x), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Power[x, -0.5], $MachinePrecision] - N[Power[N[(1.0 + x), $MachinePrecision], -0.5], $MachinePrecision]), $MachinePrecision]]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
t_0 := \sqrt{1 + x}\\
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{t_0} \leq 5 \cdot 10^{-10}:\\
\;\;\;\;\frac{\frac{1}{\sqrt{x} + t_0}}{\left(x + 0.5\right) - \frac{0.125}{x}}\\
\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\
\end{array}
Results
| Original | 68.3% |
|---|---|
| Target | 99.0% |
| Herbie | 99.7% |
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 5.00000000000000031e-10Initial program 31.2%
Applied egg-rr31.3%
[Start]31.2 | \[ \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\] |
|---|---|
frac-sub [=>]31.3 | \[ \color{blue}{\frac{1 \cdot \sqrt{x + 1} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}}
\] |
*-un-lft-identity [<=]31.3 | \[ \frac{\color{blue}{\sqrt{x + 1}} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
+-commutative [=>]31.3 | \[ \frac{\sqrt{\color{blue}{1 + x}} - \sqrt{x} \cdot 1}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
*-rgt-identity [=>]31.3 | \[ \frac{\sqrt{1 + x} - \color{blue}{\sqrt{x}}}{\sqrt{x} \cdot \sqrt{x + 1}}
\] |
sqrt-unprod [=>]31.3 | \[ \frac{\sqrt{1 + x} - \sqrt{x}}{\color{blue}{\sqrt{x \cdot \left(x + 1\right)}}}
\] |
+-commutative [=>]31.3 | \[ \frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x \cdot \color{blue}{\left(1 + x\right)}}}
\] |
Applied egg-rr32.9%
[Start]31.3 | \[ \frac{\sqrt{1 + x} - \sqrt{x}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
|---|---|
flip-- [=>]32.0 | \[ \frac{\color{blue}{\frac{\sqrt{1 + x} \cdot \sqrt{1 + x} - \sqrt{x} \cdot \sqrt{x}}{\sqrt{1 + x} + \sqrt{x}}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
div-inv [=>]32.0 | \[ \frac{\color{blue}{\left(\sqrt{1 + x} \cdot \sqrt{1 + x} - \sqrt{x} \cdot \sqrt{x}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
add-sqr-sqrt [<=]32.5 | \[ \frac{\left(\color{blue}{\left(1 + x\right)} - \sqrt{x} \cdot \sqrt{x}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
+-commutative [=>]32.5 | \[ \frac{\left(\color{blue}{\left(x + 1\right)} - \sqrt{x} \cdot \sqrt{x}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
add-sqr-sqrt [<=]32.9 | \[ \frac{\left(\left(x + 1\right) - \color{blue}{x}\right) \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
associate--l+ [=>]32.9 | \[ \frac{\color{blue}{\left(x + \left(1 - x\right)\right)} \cdot \frac{1}{\sqrt{1 + x} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
+-commutative [=>]32.9 | \[ \frac{\left(x + \left(1 - x\right)\right) \cdot \frac{1}{\sqrt{\color{blue}{x + 1}} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
Simplified77.1%
[Start]32.9 | \[ \frac{\left(x + \left(1 - x\right)\right) \cdot \frac{1}{\sqrt{x + 1} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
|---|---|
associate-*r/ [=>]32.9 | \[ \frac{\color{blue}{\frac{\left(x + \left(1 - x\right)\right) \cdot 1}{\sqrt{x + 1} + \sqrt{x}}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
*-rgt-identity [=>]32.9 | \[ \frac{\frac{\color{blue}{x + \left(1 - x\right)}}{\sqrt{x + 1} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
+-commutative [=>]32.9 | \[ \frac{\frac{\color{blue}{\left(1 - x\right) + x}}{\sqrt{x + 1} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
associate-+l- [=>]77.1 | \[ \frac{\frac{\color{blue}{1 - \left(x - x\right)}}{\sqrt{x + 1} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
+-inverses [=>]77.1 | \[ \frac{\frac{1 - \color{blue}{0}}{\sqrt{x + 1} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
metadata-eval [=>]77.1 | \[ \frac{\frac{\color{blue}{1}}{\sqrt{x + 1} + \sqrt{x}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
+-commutative [=>]77.1 | \[ \frac{\frac{1}{\color{blue}{\sqrt{x} + \sqrt{x + 1}}}}{\sqrt{x \cdot \left(1 + x\right)}}
\] |
Taylor expanded in x around inf 99.7%
Simplified99.7%
[Start]99.7 | \[ \frac{\frac{1}{\sqrt{x} + \sqrt{x + 1}}}{\left(0.5 + x\right) - 0.125 \cdot \frac{1}{x}}
\] |
|---|---|
+-commutative [=>]99.7 | \[ \frac{\frac{1}{\sqrt{x} + \sqrt{x + 1}}}{\color{blue}{\left(x + 0.5\right)} - 0.125 \cdot \frac{1}{x}}
\] |
associate-*r/ [=>]99.7 | \[ \frac{\frac{1}{\sqrt{x} + \sqrt{x + 1}}}{\left(x + 0.5\right) - \color{blue}{\frac{0.125 \cdot 1}{x}}}
\] |
metadata-eval [=>]99.7 | \[ \frac{\frac{1}{\sqrt{x} + \sqrt{x + 1}}}{\left(x + 0.5\right) - \frac{\color{blue}{0.125}}{x}}
\] |
if 5.00000000000000031e-10 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) Initial program 99.5%
Applied egg-rr99.9%
[Start]99.5 | \[ \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\] |
|---|---|
*-un-lft-identity [=>]99.5 | \[ \color{blue}{1 \cdot \frac{1}{\sqrt{x}}} - \frac{1}{\sqrt{x + 1}}
\] |
clear-num [=>]99.5 | \[ 1 \cdot \frac{1}{\sqrt{x}} - \color{blue}{\frac{1}{\frac{\sqrt{x + 1}}{1}}}
\] |
associate-/r/ [=>]99.5 | \[ 1 \cdot \frac{1}{\sqrt{x}} - \color{blue}{\frac{1}{\sqrt{x + 1}} \cdot 1}
\] |
prod-diff [=>]99.5 | \[ \color{blue}{\mathsf{fma}\left(1, \frac{1}{\sqrt{x}}, -1 \cdot \frac{1}{\sqrt{x + 1}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)}
\] |
*-un-lft-identity [<=]99.5 | \[ \mathsf{fma}\left(1, \frac{1}{\sqrt{x}}, -\color{blue}{\frac{1}{\sqrt{x + 1}}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
fma-neg [<=]99.5 | \[ \color{blue}{\left(1 \cdot \frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}\right)} + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
*-un-lft-identity [<=]99.5 | \[ \left(\color{blue}{\frac{1}{\sqrt{x}}} - \frac{1}{\sqrt{x + 1}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
inv-pow [=>]99.5 | \[ \left(\color{blue}{{\left(\sqrt{x}\right)}^{-1}} - \frac{1}{\sqrt{x + 1}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
sqrt-pow2 [=>]99.9 | \[ \left(\color{blue}{{x}^{\left(\frac{-1}{2}\right)}} - \frac{1}{\sqrt{x + 1}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
metadata-eval [=>]99.9 | \[ \left({x}^{\color{blue}{-0.5}} - \frac{1}{\sqrt{x + 1}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
pow1/2 [=>]99.9 | \[ \left({x}^{-0.5} - \frac{1}{\color{blue}{{\left(x + 1\right)}^{0.5}}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
pow-flip [=>]99.9 | \[ \left({x}^{-0.5} - \color{blue}{{\left(x + 1\right)}^{\left(-0.5\right)}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
+-commutative [=>]99.9 | \[ \left({x}^{-0.5} - {\color{blue}{\left(1 + x\right)}}^{\left(-0.5\right)}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
metadata-eval [=>]99.9 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{\color{blue}{-0.5}}\right) + \mathsf{fma}\left(-1, \frac{1}{\sqrt{x + 1}}, 1 \cdot \frac{1}{\sqrt{x + 1}}\right)
\] |
Simplified99.9%
[Start]99.9 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \mathsf{fma}\left(-1, {\left(1 + x\right)}^{-0.5}, {\left(1 + x\right)}^{-0.5}\right)
\] |
|---|---|
fma-udef [=>]99.9 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \color{blue}{\left(-1 \cdot {\left(1 + x\right)}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)}
\] |
distribute-lft1-in [=>]99.9 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \color{blue}{\left(-1 + 1\right) \cdot {\left(1 + x\right)}^{-0.5}}
\] |
metadata-eval [=>]99.9 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \color{blue}{0} \cdot {\left(1 + x\right)}^{-0.5}
\] |
mul0-lft [=>]99.9 | \[ \left({x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\right) + \color{blue}{0}
\] |
+-rgt-identity [=>]99.9 | \[ \color{blue}{{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}}
\] |
Final simplification99.8%
| Alternative 1 | |
|---|---|
| Accuracy | 99.7% |
| Cost | 26948 |
| Alternative 2 | |
|---|---|
| Accuracy | 99.3% |
| Cost | 26820 |
| Alternative 3 | |
|---|---|
| Accuracy | 82.1% |
| Cost | 26692 |
| Alternative 4 | |
|---|---|
| Accuracy | 99.0% |
| Cost | 26240 |
| Alternative 5 | |
|---|---|
| Accuracy | 81.2% |
| Cost | 13316 |
| Alternative 6 | |
|---|---|
| Accuracy | 65.6% |
| Cost | 6848 |
| Alternative 7 | |
|---|---|
| Accuracy | 52.5% |
| Cost | 6788 |
| Alternative 8 | |
|---|---|
| Accuracy | 65.2% |
| Cost | 6784 |
| Alternative 9 | |
|---|---|
| Accuracy | 51.9% |
| Cost | 6720 |
| Alternative 10 | |
|---|---|
| Accuracy | 49.8% |
| Cost | 6528 |
| Alternative 11 | |
|---|---|
| Accuracy | 4.9% |
| Cost | 576 |
| Alternative 12 | |
|---|---|
| Accuracy | 3.9% |
| Cost | 192 |
herbie shell --seed 2023153
(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)))))