| Alternative 1 | |
|---|---|
| Accuracy | 99.8% |
| Cost | 13248 |
\[\frac{1}{\sqrt{x} + \sqrt{1 + x}}
\]
(FPCore (x) :precision binary64 (- (sqrt (+ x 1.0)) (sqrt x)))
(FPCore (x) :precision binary64 (let* ((t_0 (- (sqrt (+ 1.0 x)) (sqrt x)))) (if (<= t_0 5e-5) (* (pow x -0.5) 0.5) t_0)))
double code(double x) {
return sqrt((x + 1.0)) - sqrt(x);
}
double code(double x) {
double t_0 = sqrt((1.0 + x)) - sqrt(x);
double tmp;
if (t_0 <= 5e-5) {
tmp = pow(x, -0.5) * 0.5;
} else {
tmp = t_0;
}
return tmp;
}
real(8) function code(x)
real(8), intent (in) :: x
code = sqrt((x + 1.0d0)) - sqrt(x)
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)) - sqrt(x)
if (t_0 <= 5d-5) then
tmp = (x ** (-0.5d0)) * 0.5d0
else
tmp = t_0
end if
code = tmp
end function
public static double code(double x) {
return Math.sqrt((x + 1.0)) - Math.sqrt(x);
}
public static double code(double x) {
double t_0 = Math.sqrt((1.0 + x)) - Math.sqrt(x);
double tmp;
if (t_0 <= 5e-5) {
tmp = Math.pow(x, -0.5) * 0.5;
} else {
tmp = t_0;
}
return tmp;
}
def code(x): return math.sqrt((x + 1.0)) - math.sqrt(x)
def code(x): t_0 = math.sqrt((1.0 + x)) - math.sqrt(x) tmp = 0 if t_0 <= 5e-5: tmp = math.pow(x, -0.5) * 0.5 else: tmp = t_0 return tmp
function code(x) return Float64(sqrt(Float64(x + 1.0)) - sqrt(x)) end
function code(x) t_0 = Float64(sqrt(Float64(1.0 + x)) - sqrt(x)) tmp = 0.0 if (t_0 <= 5e-5) tmp = Float64((x ^ -0.5) * 0.5); else tmp = t_0; end return tmp end
function tmp = code(x) tmp = sqrt((x + 1.0)) - sqrt(x); end
function tmp_2 = code(x) t_0 = sqrt((1.0 + x)) - sqrt(x); tmp = 0.0; if (t_0 <= 5e-5) tmp = (x ^ -0.5) * 0.5; else tmp = t_0; end tmp_2 = tmp; end
code[x_] := N[(N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]
code[x_] := Block[{t$95$0 = N[(N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision] - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[t$95$0, 5e-5], N[(N[Power[x, -0.5], $MachinePrecision] * 0.5), $MachinePrecision], t$95$0]]
\sqrt{x + 1} - \sqrt{x}
\begin{array}{l}
t_0 := \sqrt{1 + x} - \sqrt{x}\\
\mathbf{if}\;t_0 \leq 5 \cdot 10^{-5}:\\
\;\;\;\;{x}^{-0.5} \cdot 0.5\\
\mathbf{else}:\\
\;\;\;\;t_0\\
\end{array}
Results
| Original | 53.9% |
|---|---|
| Target | 99.8% |
| Herbie | 99.5% |
if (-.f64 (sqrt.f64 (+.f64 x 1)) (sqrt.f64 x)) < 5.00000000000000024e-5Initial program 4.8%
Applied egg-rr3.4%
Taylor expanded in x around inf 99.4%
Simplified99.4%
[Start]99.4 | \[ 0.5 \cdot \sqrt{\frac{1}{x}}
\] |
|---|---|
*-commutative [=>]99.4 | \[ \color{blue}{\sqrt{\frac{1}{x}} \cdot 0.5}
\] |
Applied egg-rr7.9%
Simplified99.6%
[Start]7.9 | \[ \left(\left(1 + {x}^{-0.5}\right) - 1\right) \cdot 0.5
\] |
|---|---|
+-commutative [=>]7.9 | \[ \left(\color{blue}{\left({x}^{-0.5} + 1\right)} - 1\right) \cdot 0.5
\] |
associate--l+ [=>]99.6 | \[ \color{blue}{\left({x}^{-0.5} + \left(1 - 1\right)\right)} \cdot 0.5
\] |
metadata-eval [=>]99.6 | \[ \left({x}^{-0.5} + \color{blue}{0}\right) \cdot 0.5
\] |
+-rgt-identity [=>]99.6 | \[ \color{blue}{{x}^{-0.5}} \cdot 0.5
\] |
if 5.00000000000000024e-5 < (-.f64 (sqrt.f64 (+.f64 x 1)) (sqrt.f64 x)) Initial program 99.5%
Final simplification99.5%
| Alternative 1 | |
|---|---|
| Accuracy | 99.8% |
| Cost | 13248 |
| Alternative 2 | |
|---|---|
| Accuracy | 98.0% |
| Cost | 6980 |
| Alternative 3 | |
|---|---|
| Accuracy | 98.0% |
| Cost | 6980 |
| Alternative 4 | |
|---|---|
| Accuracy | 98.0% |
| Cost | 6852 |
| Alternative 5 | |
|---|---|
| Accuracy | 96.9% |
| Cost | 6788 |
| Alternative 6 | |
|---|---|
| Accuracy | 51.9% |
| Cost | 64 |
herbie shell --seed 2023122
(FPCore (x)
:name "2sqrt (example 3.1)"
:precision binary64
:herbie-target
(/ 1.0 (+ (sqrt (+ x 1.0)) (sqrt x)))
(- (sqrt (+ x 1.0)) (sqrt x)))