Average Error: 24.9 → 0.3
Time: 20.0s
Precision: binary64
Cost: 13700
\[\left(0 \leq x \land x \leq 1000000000\right) \land \left(-1 \leq \varepsilon \land \varepsilon \leq 1\right)\]
\[x - \sqrt{x \cdot x - \varepsilon} \]
\[\begin{array}{l} t_0 := \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\\ \mathbf{if}\;-2 \cdot x \ne 0:\\ \;\;\;\;\frac{-\varepsilon}{\left(-t_0\right) - x}\\ \mathbf{else}:\\ \;\;\;\;x - t_0\\ \end{array} \]
(FPCore (x eps) :precision binary64 (- x (sqrt (- (* x x) eps))))
(FPCore (x eps)
 :precision binary64
 (let* ((t_0 (sqrt (fma x x (- eps)))))
   (if (!= (* -2.0 x) 0.0) (/ (- eps) (- (- t_0) x)) (- x t_0))))
double code(double x, double eps) {
	return x - sqrt(((x * x) - eps));
}
double code(double x, double eps) {
	double t_0 = sqrt(fma(x, x, -eps));
	double tmp;
	if ((-2.0 * x) != 0.0) {
		tmp = -eps / (-t_0 - x);
	} else {
		tmp = x - t_0;
	}
	return tmp;
}
function code(x, eps)
	return Float64(x - sqrt(Float64(Float64(x * x) - eps)))
end
function code(x, eps)
	t_0 = sqrt(fma(x, x, Float64(-eps)))
	tmp = 0.0
	if (Float64(-2.0 * x) != 0.0)
		tmp = Float64(Float64(-eps) / Float64(Float64(-t_0) - x));
	else
		tmp = Float64(x - t_0);
	end
	return tmp
end
code[x_, eps_] := N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[x_, eps_] := Block[{t$95$0 = N[Sqrt[N[(x * x + (-eps)), $MachinePrecision]], $MachinePrecision]}, If[Unequal[N[(-2.0 * x), $MachinePrecision], 0.0], N[((-eps) / N[((-t$95$0) - x), $MachinePrecision]), $MachinePrecision], N[(x - t$95$0), $MachinePrecision]]]
x - \sqrt{x \cdot x - \varepsilon}
\begin{array}{l}
t_0 := \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\\
\mathbf{if}\;-2 \cdot x \ne 0:\\
\;\;\;\;\frac{-\varepsilon}{\left(-t_0\right) - x}\\

\mathbf{else}:\\
\;\;\;\;x - t_0\\


\end{array}

Error

Target

Original24.9
Target0.3
Herbie0.3
\[\frac{\varepsilon}{x + \sqrt{x \cdot x - \varepsilon}} \]

Derivation

  1. Initial program 24.9

    \[x - \sqrt{x \cdot x - \varepsilon} \]
  2. Applied egg-rr25.0

    \[\leadsto \color{blue}{\begin{array}{l} \color{blue}{\mathbf{if}\;\left(-\sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\right) - x \ne 0:\\ \;\;\;\;\frac{{\left(\sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\right)}^{2} - x \cdot x}{\left(-\sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\right) - x}\\ \mathbf{else}:\\ \;\;\;\;x - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\\ } \end{array}} \]
  3. Taylor expanded in x around inf 0.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(-\sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\right) - x \ne 0:\\ \;\;\;\;\frac{\color{blue}{-1 \cdot \varepsilon}}{\left(-\sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\right) - x}\\ \mathbf{else}:\\ \;\;\;\;x - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\\ \end{array} \]
  4. Simplified0.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;\left(-\sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\right) - x \ne 0:\\ \;\;\;\;\frac{\color{blue}{-\varepsilon}}{\left(-\sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\right) - x}\\ \mathbf{else}:\\ \;\;\;\;x - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\\ \end{array} \]
    Proof
  5. Taylor expanded in x around inf 0.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;\color{blue}{-2 \cdot x} \ne 0:\\ \;\;\;\;\frac{-\varepsilon}{\left(-\sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\right) - x}\\ \mathbf{else}:\\ \;\;\;\;x - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\\ \end{array} \]

Alternatives

Alternative 1
Error0.8
Cost13764
\[\begin{array}{l} t_0 := x - \sqrt{x \cdot x - \varepsilon}\\ \mathbf{if}\;t_0 \leq -1 \cdot 10^{-154}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;0.5 \cdot \left(\frac{\frac{0.25 \cdot \varepsilon}{x \cdot x} \cdot \varepsilon}{x} + \frac{\varepsilon}{x}\right)\\ \end{array} \]
Alternative 2
Error8.7
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 1.22 \cdot 10^{-88}:\\ \;\;\;\;x - \sqrt{-\varepsilon}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 \cdot \varepsilon}{x}\\ \end{array} \]
Alternative 3
Error8.7
Cost6724
\[\begin{array}{l} \mathbf{if}\;x \leq 1.25 \cdot 10^{-88}:\\ \;\;\;\;-\sqrt{-\varepsilon}\\ \mathbf{else}:\\ \;\;\;\;\frac{0.5 \cdot \varepsilon}{x}\\ \end{array} \]
Alternative 4
Error35.0
Cost320
\[\frac{0.5 \cdot \varepsilon}{x} \]
Alternative 5
Error60.6
Cost128
\[-x \]
Alternative 6
Error61.7
Cost64
\[x \]

Error

Reproduce

herbie shell --seed 2023010 
(FPCore (x eps)
  :name "ENA, Section 1.4, Exercise 4d"
  :precision binary64
  :pre (and (and (<= 0.0 x) (<= x 1000000000.0)) (and (<= -1.0 eps) (<= eps 1.0)))

  :herbie-target
  (/ eps (+ x (sqrt (- (* x x) eps))))

  (- x (sqrt (- (* x x) eps))))