Average Error: 19.5 → 0.4
Time: 21.4s
Precision: binary64
Cost: 46408
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
\[\begin{array}{l} t_0 := \frac{1}{\sqrt{x}}\\ t_1 := \sqrt{x + 1}\\ t_2 := t_1 + \sqrt{x}\\ t_3 := \begin{array}{l} \mathbf{if}\;t_2 \ne 0:\\ \;\;\;\;\frac{1}{t_2}\\ \mathbf{else}:\\ \;\;\;\;t_1 - \sqrt{x}\\ \end{array}\\ \mathbf{if}\;t_0 \ne 0:\\ \;\;\;\;\frac{t_3}{\frac{t_1}{t_0}}\\ \mathbf{else}:\\ \;\;\;\;\frac{t_3}{\sqrt{x} \cdot t_1}\\ \end{array} \]
(FPCore (x) :precision binary64 (- (/ 1.0 (sqrt x)) (/ 1.0 (sqrt (+ x 1.0)))))
(FPCore (x)
 :precision binary64
 (let* ((t_0 (/ 1.0 (sqrt x)))
        (t_1 (sqrt (+ x 1.0)))
        (t_2 (+ t_1 (sqrt x)))
        (t_3 (if (!= t_2 0.0) (/ 1.0 t_2) (- t_1 (sqrt x)))))
   (if (!= t_0 0.0) (/ t_3 (/ t_1 t_0)) (/ t_3 (* (sqrt x) t_1)))))
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(x);
	double t_1 = sqrt((x + 1.0));
	double t_2 = t_1 + sqrt(x);
	double tmp;
	if (t_2 != 0.0) {
		tmp = 1.0 / t_2;
	} else {
		tmp = t_1 - sqrt(x);
	}
	double t_3 = tmp;
	double tmp_1;
	if (t_0 != 0.0) {
		tmp_1 = t_3 / (t_1 / t_0);
	} else {
		tmp_1 = t_3 / (sqrt(x) * t_1);
	}
	return tmp_1;
}
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) :: t_1
    real(8) :: t_2
    real(8) :: t_3
    real(8) :: tmp
    real(8) :: tmp_1
    t_0 = 1.0d0 / sqrt(x)
    t_1 = sqrt((x + 1.0d0))
    t_2 = t_1 + sqrt(x)
    if (t_2 /= 0.0d0) then
        tmp = 1.0d0 / t_2
    else
        tmp = t_1 - sqrt(x)
    end if
    t_3 = tmp
    if (t_0 /= 0.0d0) then
        tmp_1 = t_3 / (t_1 / t_0)
    else
        tmp_1 = t_3 / (sqrt(x) * t_1)
    end if
    code = tmp_1
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(x);
	double t_1 = Math.sqrt((x + 1.0));
	double t_2 = t_1 + Math.sqrt(x);
	double tmp;
	if (t_2 != 0.0) {
		tmp = 1.0 / t_2;
	} else {
		tmp = t_1 - Math.sqrt(x);
	}
	double t_3 = tmp;
	double tmp_1;
	if (t_0 != 0.0) {
		tmp_1 = t_3 / (t_1 / t_0);
	} else {
		tmp_1 = t_3 / (Math.sqrt(x) * t_1);
	}
	return tmp_1;
}
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(x)
	t_1 = math.sqrt((x + 1.0))
	t_2 = t_1 + math.sqrt(x)
	tmp = 0
	if t_2 != 0.0:
		tmp = 1.0 / t_2
	else:
		tmp = t_1 - math.sqrt(x)
	t_3 = tmp
	tmp_1 = 0
	if t_0 != 0.0:
		tmp_1 = t_3 / (t_1 / t_0)
	else:
		tmp_1 = t_3 / (math.sqrt(x) * t_1)
	return tmp_1
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(x))
	t_1 = sqrt(Float64(x + 1.0))
	t_2 = Float64(t_1 + sqrt(x))
	tmp = 0.0
	if (t_2 != 0.0)
		tmp = Float64(1.0 / t_2);
	else
		tmp = Float64(t_1 - sqrt(x));
	end
	t_3 = tmp
	tmp_1 = 0.0
	if (t_0 != 0.0)
		tmp_1 = Float64(t_3 / Float64(t_1 / t_0));
	else
		tmp_1 = Float64(t_3 / Float64(sqrt(x) * t_1));
	end
	return tmp_1
end
function tmp = code(x)
	tmp = (1.0 / sqrt(x)) - (1.0 / sqrt((x + 1.0)));
end
function tmp_3 = code(x)
	t_0 = 1.0 / sqrt(x);
	t_1 = sqrt((x + 1.0));
	t_2 = t_1 + sqrt(x);
	tmp = 0.0;
	if (t_2 ~= 0.0)
		tmp = 1.0 / t_2;
	else
		tmp = t_1 - sqrt(x);
	end
	t_3 = tmp;
	tmp_2 = 0.0;
	if (t_0 ~= 0.0)
		tmp_2 = t_3 / (t_1 / t_0);
	else
		tmp_2 = t_3 / (sqrt(x) * t_1);
	end
	tmp_3 = tmp_2;
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[x], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Sqrt[N[(x + 1.0), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 + N[Sqrt[x], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = If[Unequal[t$95$2, 0.0], N[(1.0 / t$95$2), $MachinePrecision], N[(t$95$1 - N[Sqrt[x], $MachinePrecision]), $MachinePrecision]]}, If[Unequal[t$95$0, 0.0], N[(t$95$3 / N[(t$95$1 / t$95$0), $MachinePrecision]), $MachinePrecision], N[(t$95$3 / N[(N[Sqrt[x], $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]]]]]]
\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}}
\begin{array}{l}
t_0 := \frac{1}{\sqrt{x}}\\
t_1 := \sqrt{x + 1}\\
t_2 := t_1 + \sqrt{x}\\
t_3 := \begin{array}{l}
\mathbf{if}\;t_2 \ne 0:\\
\;\;\;\;\frac{1}{t_2}\\

\mathbf{else}:\\
\;\;\;\;t_1 - \sqrt{x}\\


\end{array}\\
\mathbf{if}\;t_0 \ne 0:\\
\;\;\;\;\frac{t_3}{\frac{t_1}{t_0}}\\

\mathbf{else}:\\
\;\;\;\;\frac{t_3}{\sqrt{x} \cdot t_1}\\


\end{array}

Error

Target

Original19.5
Target0.6
Herbie0.4
\[\frac{1}{\left(x + 1\right) \cdot \sqrt{x} + x \cdot \sqrt{x + 1}} \]

Derivation

  1. Initial program 19.5

    \[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
  2. Applied egg-rr19.5

    \[\leadsto \color{blue}{\frac{\frac{\sqrt{x + 1} - \sqrt{x}}{\sqrt{x + 1}}}{\sqrt{x}}} \]
  3. Applied egg-rr19.3

    \[\leadsto \frac{\frac{\color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;\sqrt{x + 1} + \sqrt{x} \ne 0:\\ \;\;\;\;\frac{{\left(\sqrt{x + 1}\right)}^{2} - {\left(\sqrt{x}\right)}^{2}}{\sqrt{x + 1} + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{x + 1} - \sqrt{x}\\ } \end{array}}}{\sqrt{x + 1}}}{\sqrt{x}} \]
  4. Taylor expanded in x around 0 0.4

    \[\leadsto \frac{\frac{\begin{array}{l} \mathbf{if}\;\sqrt{x + 1} + \sqrt{x} \ne 0:\\ \;\;\;\;\frac{\color{blue}{1}}{\sqrt{x + 1} + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{x + 1} - \sqrt{x}\\ \end{array}}{\sqrt{x + 1}}}{\sqrt{x}} \]
  5. Applied egg-rr0.4

    \[\leadsto \color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;\frac{1}{\sqrt{x}} \ne 0:\\ \;\;\;\;\frac{\begin{array}{l} \mathbf{if}\;\sqrt{x + 1} + \sqrt{x} \ne 0:\\ \;\;\;\;\frac{1}{\sqrt{x + 1} + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{x + 1} - \sqrt{x}\\ \end{array}}{\frac{\sqrt{x + 1}}{\frac{1}{\sqrt{x}}}}\\ \mathbf{else}:\\ \;\;\;\;\frac{\begin{array}{l} \mathbf{if}\;\sqrt{x + 1} + \sqrt{x} \ne 0:\\ \;\;\;\;\frac{1}{\sqrt{x + 1} + \sqrt{x}}\\ \mathbf{else}:\\ \;\;\;\;\sqrt{x + 1} - \sqrt{x}\\ \end{array}}{\sqrt{x} \cdot \sqrt{x + 1}}\\ } \end{array}} \]

Alternatives

Alternative 1
Error0.4
Cost39620
\[\begin{array}{l} t_0 := \sqrt{x + 1}\\ t_1 := t_0 + \sqrt{x}\\ \frac{\frac{\begin{array}{l} \mathbf{if}\;t_1 \ne 0:\\ \;\;\;\;\frac{1}{t_1}\\ \mathbf{else}:\\ \;\;\;\;t_0 - \sqrt{x}\\ \end{array}}{\sqrt{x}}}{t_0} \end{array} \]
Alternative 2
Error19.5
Cost26304
\[\begin{array}{l} t_0 := \sqrt{x + 1}\\ \frac{\frac{t_0 - \sqrt{x}}{t_0}}{\sqrt{x}} \end{array} \]
Alternative 3
Error19.5
Cost13376
\[\frac{1}{\sqrt{x}} - \frac{1}{\sqrt{x + 1}} \]
Alternative 4
Error30.5
Cost7104
\[\frac{1}{\sqrt{x}} - \frac{1}{0.5 \cdot x + 1} \]
Alternative 5
Error31.2
Cost6976
\[\frac{1}{\sqrt{x}} - \left(-0.5 \cdot x + 1\right) \]
Alternative 6
Error31.6
Cost6852
\[\begin{array}{l} \mathbf{if}\;x \ne 0:\\ \;\;\;\;{x}^{-0.5}\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\sqrt{x}}\\ \end{array} - 1 \]
Alternative 7
Error31.7
Cost6720
\[\frac{1}{\sqrt{x}} - 1 \]
Alternative 8
Error61.5
Cost192
\[0.5 \cdot x \]

Error

Reproduce

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