Average Error: 24.3 → 0.3
Time: 2.7s
Precision: binary64
\[\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} \]
\[\frac{-\varepsilon}{\left(-x\right) - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}} \]
(FPCore (x eps) :precision binary64 (- x (sqrt (- (* x x) eps))))
(FPCore (x eps)
 :precision binary64
 (/ (- eps) (- (- x) (sqrt (fma x x (- eps))))))
double code(double x, double eps) {
	return x - sqrt(((x * x) - eps));
}
double code(double x, double eps) {
	return -eps / (-x - sqrt(fma(x, x, -eps)));
}
function code(x, eps)
	return Float64(x - sqrt(Float64(Float64(x * x) - eps)))
end
function code(x, eps)
	return Float64(Float64(-eps) / Float64(Float64(-x) - sqrt(fma(x, x, Float64(-eps)))))
end
code[x_, eps_] := N[(x - N[Sqrt[N[(N[(x * x), $MachinePrecision] - eps), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
code[x_, eps_] := N[((-eps) / N[((-x) - N[Sqrt[N[(x * x + (-eps)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
x - \sqrt{x \cdot x - \varepsilon}
\frac{-\varepsilon}{\left(-x\right) - \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}}

Error

Bits error versus x

Bits error versus eps

Target

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

Derivation

  1. Initial program 24.3

    \[x - \sqrt{x \cdot x - \varepsilon} \]
  2. Applied egg-rr0.3

    \[\leadsto \color{blue}{\frac{\left(x \cdot x - x \cdot x\right) + \varepsilon}{x + \sqrt{x \cdot x - \varepsilon}}} \]
  3. Applied egg-rr0.3

    \[\leadsto \color{blue}{-\frac{\varepsilon}{-\left(x + \sqrt{\mathsf{fma}\left(x, x, -\varepsilon\right)}\right)}} \]
  4. Final simplification0.3

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

Reproduce

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