| Alternative 1 | |
|---|---|
| Error | 0.4 |
| Cost | 13760 |
\[\frac{\frac{1}{x \cdot \left({x}^{-0.5} + {\left(1 + x\right)}^{-0.5}\right)}}{1 + x}
\]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
:precision binary64
(let* ((t_0 (/ 1.0 (sqrt (+ 1.0 x)))))
(if (<= (- (/ 1.0 (sqrt x)) t_0) 2e-19)
(* 0.5 (pow x -1.5))
(- (pow x -0.5) t_0))))double code(double x) {
return (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
}
double code(double x) {
double t_0 = 1.0 / sqrt((1.0 + x));
double tmp;
if (((1.0 / sqrt(x)) - t_0) <= 2e-19) {
tmp = 0.5 * pow(x, -1.5);
} else {
tmp = pow(x, -0.5) - t_0;
}
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 = 1.0d0 / sqrt((1.0d0 + x))
if (((1.0d0 / sqrt(x)) - t_0) <= 2d-19) then
tmp = 0.5d0 * (x ** (-1.5d0))
else
tmp = (x ** (-0.5d0)) - t_0
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 = 1.0 / Math.sqrt((1.0 + x));
double tmp;
if (((1.0 / Math.sqrt(x)) - t_0) <= 2e-19) {
tmp = 0.5 * Math.pow(x, -1.5);
} else {
tmp = Math.pow(x, -0.5) - t_0;
}
return tmp;
}
def code(x): return (1.0 / math.sqrt(x)) - (1.0 / math.sqrt((x + 1.0)))
def code(x): t_0 = 1.0 / math.sqrt((1.0 + x)) tmp = 0 if ((1.0 / math.sqrt(x)) - t_0) <= 2e-19: tmp = 0.5 * math.pow(x, -1.5) else: tmp = math.pow(x, -0.5) - t_0 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 = Float64(1.0 / sqrt(Float64(1.0 + x))) tmp = 0.0 if (Float64(Float64(1.0 / sqrt(x)) - t_0) <= 2e-19) tmp = Float64(0.5 * (x ^ -1.5)); else tmp = Float64((x ^ -0.5) - t_0); 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 = 1.0 / sqrt((1.0 + x)); tmp = 0.0; if (((1.0 / sqrt(x)) - t_0) <= 2e-19) tmp = 0.5 * (x ^ -1.5); else tmp = (x ^ -0.5) - t_0; 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[(1.0 / N[Sqrt[N[(1.0 + x), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(N[(1.0 / N[Sqrt[x], $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision], 2e-19], N[(0.5 * N[Power[x, -1.5], $MachinePrecision]), $MachinePrecision], N[(N[Power[x, -0.5], $MachinePrecision] - t$95$0), $MachinePrecision]]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
t_0 := \frac{1}{\sqrt{1 + x}}\\
\mathbf{if}\;\frac{1}{\sqrt{x}} - t_0 \leq 2 \cdot 10^{-19}:\\
\;\;\;\;0.5 \cdot {x}^{-1.5}\\
\mathbf{else}:\\
\;\;\;\;{x}^{-0.5} - t_0\\
\end{array}
Results
| Original | 19.6 |
|---|---|
| Target | 0.7 |
| Herbie | 0.5 |
if (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) < 2e-19Initial program 39.6
Applied egg-rr39.6
Simplified39.6
[Start]39.6 | \[ \left(\frac{1}{x} + \frac{-1}{1 + x}\right) \cdot \frac{1}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}
\] |
|---|---|
associate-*r/ [=>]39.6 | \[ \color{blue}{\frac{\left(\frac{1}{x} + \frac{-1}{1 + x}\right) \cdot 1}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}}
\] |
*-rgt-identity [=>]39.6 | \[ \frac{\color{blue}{\frac{1}{x} + \frac{-1}{1 + x}}}{{x}^{-0.5} + {\left(1 + x\right)}^{-0.5}}
\] |
Taylor expanded in x around inf 21.6
Applied egg-rr39.8
Simplified0.1
[Start]39.8 | \[ 0.5 \cdot \left(e^{\mathsf{log1p}\left({x}^{-1.5}\right)} - 1\right)
\] |
|---|---|
expm1-def [=>]0.1 | \[ 0.5 \cdot \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left({x}^{-1.5}\right)\right)}
\] |
expm1-log1p [=>]0.1 | \[ 0.5 \cdot \color{blue}{{x}^{-1.5}}
\] |
if 2e-19 < (-.f64 (/.f64 1 (sqrt.f64 x)) (/.f64 1 (sqrt.f64 (+.f64 x 1)))) Initial program 1.0
Applied egg-rr5.8
Simplified0.8
[Start]5.8 | \[ \left(e^{\mathsf{log1p}\left({x}^{-0.5}\right)} - 1\right) - \frac{1}{\sqrt{x + 1}}
\] |
|---|---|
expm1-def [=>]5.4 | \[ \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left({x}^{-0.5}\right)\right)} - \frac{1}{\sqrt{x + 1}}
\] |
expm1-log1p [=>]0.8 | \[ \color{blue}{{x}^{-0.5}} - \frac{1}{\sqrt{x + 1}}
\] |
Final simplification0.5
| Alternative 1 | |
|---|---|
| Error | 0.4 |
| Cost | 13760 |
| Alternative 2 | |
|---|---|
| Error | 0.3 |
| Cost | 13380 |
| Alternative 3 | |
|---|---|
| Error | 1.0 |
| Cost | 7172 |
| Alternative 4 | |
|---|---|
| Error | 1.0 |
| Cost | 7044 |
| Alternative 5 | |
|---|---|
| Error | 2.0 |
| Cost | 6788 |
| Alternative 6 | |
|---|---|
| Error | 1.1 |
| Cost | 6788 |
| Alternative 7 | |
|---|---|
| Error | 31.6 |
| Cost | 6528 |
herbie shell --seed 2023088
(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)))))