Average Error: 24.6 → 1.0
Time: 8.2s
Precision: binary64
Cost: 20100
\[\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} \mathbf{if}\;x - \sqrt{x \cdot x - \varepsilon} \leq -5 \cdot 10^{-154}:\\ \;\;\;\;x - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\varepsilon}{x}}{2}\\ \end{array} \]
(FPCore (x eps) :precision binary64 (- x (sqrt (- (* x x) eps))))
(FPCore (x eps)
 :precision binary64
 (if (<= (- x (sqrt (- (* x x) eps))) -5e-154)
   (- x (sqrt (fma x x (- eps))))
   (/ (/ eps x) 2.0)))
double code(double x, double eps) {
	return x - sqrt(((x * x) - eps));
}
double code(double x, double eps) {
	double tmp;
	if ((x - sqrt(((x * x) - eps))) <= -5e-154) {
		tmp = x - sqrt(fma(x, x, -eps));
	} else {
		tmp = (eps / x) / 2.0;
	}
	return tmp;
}
function code(x, eps)
	return Float64(x - sqrt(Float64(Float64(x * x) - eps)))
end
function code(x, eps)
	tmp = 0.0
	if (Float64(x - sqrt(Float64(Float64(x * x) - eps))) <= -5e-154)
		tmp = Float64(x - sqrt(fma(x, x, Float64(-eps))));
	else
		tmp = Float64(Float64(eps / x) / 2.0);
	end
	return tmp
end
code[x_, eps_] := N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[x_, eps_] := If[LessEqual[N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], -5e-154], N[(x - N[Sqrt[N[(x * x + (-eps)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(eps / x), $MachinePrecision] / 2.0), $MachinePrecision]]
x - \sqrt{x \cdot x - \varepsilon}
\begin{array}{l}
\mathbf{if}\;x - \sqrt{x \cdot x - \varepsilon} \leq -5 \cdot 10^{-154}:\\
\;\;\;\;x - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\\

\mathbf{else}:\\
\;\;\;\;\frac{\frac{\varepsilon}{x}}{2}\\


\end{array}

Error

Target

Original24.6
Target0.3
Herbie1.0
\[\frac{\varepsilon}{x + \sqrt{x \cdot x - \varepsilon}} \]

Derivation

  1. Split input into 2 regimes
  2. if (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps))) < -5.0000000000000002e-154

    1. Initial program 0.8

      \[x - \sqrt{x \cdot x - \varepsilon} \]
    2. Simplified0.8

      \[\leadsto \color{blue}{x - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}} \]
      Proof

    if -5.0000000000000002e-154 < (-.f64 x (sqrt.f64 (-.f64 (*.f64 x x) eps)))

    1. Initial program 58.7

      \[x - \sqrt{x \cdot x - \varepsilon} \]
    2. Simplified58.7

      \[\leadsto \color{blue}{x - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}} \]
      Proof
    3. Taylor expanded in x around inf 59.6

      \[\leadsto x - \color{blue}{\left(-0.5 \cdot \frac{\varepsilon}{x} + x\right)} \]
    4. Applied egg-rr1.4

      \[\leadsto \color{blue}{\frac{\frac{\varepsilon}{x}}{2}} \]
  3. Recombined 2 regimes into one program.

Alternatives

Alternative 1
Error1.0
Cost13764
\[\begin{array}{l} t_0 := x - \sqrt{x \cdot x - \varepsilon}\\ \mathbf{if}\;t_0 \leq -5 \cdot 10^{-154}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\varepsilon}{x}}{2}\\ \end{array} \]
Alternative 2
Error8.7
Cost6788
\[\begin{array}{l} \mathbf{if}\;x \leq 8.4 \cdot 10^{-88}:\\ \;\;\;\;x - \sqrt{-\varepsilon}\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\varepsilon}{x}}{2}\\ \end{array} \]
Alternative 3
Error35.4
Cost320
\[\varepsilon \cdot \frac{0.5}{x} \]
Alternative 4
Error35.3
Cost320
\[\frac{\frac{\varepsilon}{x}}{2} \]
Alternative 5
Error61.3
Cost64
\[0 \]

Error

Reproduce

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