| Alternative 1 | |
|---|---|
| Error | 0.4 |
| Cost | 13760 |
\[\frac{\frac{\frac{1}{x}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}}{1 + x}
\]
(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)))) 4e-8) (* (/ (pow x -0.5) x) (+ 0.5 (+ (/ 0.3125 (* x x)) (/ -0.375 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 tmp;
if (((1.0 / sqrt(x)) - (1.0 / sqrt((1.0 + x)))) <= 4e-8) {
tmp = (pow(x, -0.5) / x) * (0.5 + ((0.3125 / (x * x)) + (-0.375 / 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) :: tmp
if (((1.0d0 / sqrt(x)) - (1.0d0 / sqrt((1.0d0 + x)))) <= 4d-8) then
tmp = ((x ** (-0.5d0)) / x) * (0.5d0 + ((0.3125d0 / (x * x)) + ((-0.375d0) / 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 tmp;
if (((1.0 / Math.sqrt(x)) - (1.0 / Math.sqrt((1.0 + x)))) <= 4e-8) {
tmp = (Math.pow(x, -0.5) / x) * (0.5 + ((0.3125 / (x * x)) + (-0.375 / 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): tmp = 0 if ((1.0 / math.sqrt(x)) - (1.0 / math.sqrt((1.0 + x)))) <= 4e-8: tmp = (math.pow(x, -0.5) / x) * (0.5 + ((0.3125 / (x * x)) + (-0.375 / 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) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) - Float64(1.0 / sqrt(Float64(1.0 + x)))) <= 4e-8) tmp = Float64(Float64((x ^ -0.5) / x) * Float64(0.5 + Float64(Float64(0.3125 / Float64(x * x)) + Float64(-0.375 / 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) tmp = 0.0; if (((1.0 / sqrt(x)) - (1.0 / sqrt((1.0 + x)))) <= 4e-8) tmp = ((x ^ -0.5) / x) * (0.5 + ((0.3125 / (x * x)) + (-0.375 / 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_] := If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - N[(1.0 / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 4e-8], N[(N[(N[Power[x, -0.5], $MachinePrecision] / x), $MachinePrecision] * N[(0.5 + N[(N[(0.3125 / N[(x * x), $MachinePrecision]), $MachinePrecision] + N[(-0.375 / x), $MachinePrecision]), $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}
\mathbf{if}\;\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{1 + x}} \leq 4 \cdot 10^{-8}:\\
\;\;\;\;\frac{{x}^{-0.5}}{x} \cdot \left(0.5 + \left(\frac{0.3125}{x \cdot x} + \frac{-0.375}{x}\right)\right)\\
\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} - {\left(1 + x\right)}^{-0.5}\\
\end{array}
Results
| Original | 19.6 |
|---|---|
| Target | 0.6 |
| Herbie | 0.2 |
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 4.0000000000000001e-8Initial program 39.6
Applied egg-rr39.5
Applied egg-rr39.6
Taylor expanded in x around -inf 64.0
Simplified0.2
if 4.0000000000000001e-8 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) Initial program 0.4
Applied egg-rr0.6
Applied egg-rr0.1
Final simplification0.2
| Alternative 1 | |
|---|---|
| Error | 0.4 |
| Cost | 13760 |
| Alternative 2 | |
|---|---|
| Error | 0.6 |
| Cost | 7556 |
| Alternative 3 | |
|---|---|
| Error | 0.7 |
| Cost | 7172 |
| Alternative 4 | |
|---|---|
| Error | 1.0 |
| Cost | 7044 |
| Alternative 5 | |
|---|---|
| Error | 2.2 |
| Cost | 6788 |
| Alternative 6 | |
|---|---|
| Error | 1.2 |
| Cost | 6788 |
| Alternative 7 | |
|---|---|
| Error | 31.7 |
| Cost | 6528 |
| Alternative 8 | |
|---|---|
| Error | 59.2 |
| Cost | 192 |
| Alternative 9 | |
|---|---|
| Error | 62.8 |
| Cost | 64 |

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