Average Error: 32.8 → 0
Time: 6.9s
Precision: 64
\[\frac{x}{x} - \frac{1}{x} \cdot \sqrt{x \cdot x}\]
\[\mathsf{fma}\left(1, -\frac{\left|x\right|}{x}, 1\right)\]
\frac{x}{x} - \frac{1}{x} \cdot \sqrt{x \cdot x}
\mathsf{fma}\left(1, -\frac{\left|x\right|}{x}, 1\right)
double f(double x) {
        double r85425 = x;
        double r85426 = r85425 / r85425;
        double r85427 = 1.0;
        double r85428 = r85427 / r85425;
        double r85429 = r85425 * r85425;
        double r85430 = sqrt(r85429);
        double r85431 = r85428 * r85430;
        double r85432 = r85426 - r85431;
        return r85432;
}

double f(double x) {
        double r85433 = 1.0;
        double r85434 = x;
        double r85435 = fabs(r85434);
        double r85436 = r85435 / r85434;
        double r85437 = -r85436;
        double r85438 = 1.0;
        double r85439 = fma(r85433, r85437, r85438);
        return r85439;
}

Error

Bits error versus x

Target

Original32.8
Target0
Herbie0
\[\begin{array}{l} \mathbf{if}\;x \lt 0.0:\\ \;\;\;\;2\\ \mathbf{else}:\\ \;\;\;\;0.0\\ \end{array}\]

Derivation

  1. Initial program 32.8

    \[\frac{x}{x} - \frac{1}{x} \cdot \sqrt{x \cdot x}\]
  2. Simplified4.5

    \[\leadsto \color{blue}{1 - \frac{1}{x} \cdot \left|x\right|}\]
  3. Using strategy rm
  4. Applied add-cube-cbrt4.5

    \[\leadsto \color{blue}{\left(\sqrt[3]{1} \cdot \sqrt[3]{1}\right) \cdot \sqrt[3]{1}} - \frac{1}{x} \cdot \left|x\right|\]
  5. Applied prod-diff30.5

    \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt[3]{1} \cdot \sqrt[3]{1}, \sqrt[3]{1}, -\left|x\right| \cdot \frac{1}{x}\right) + \mathsf{fma}\left(-\left|x\right|, \frac{1}{x}, \left|x\right| \cdot \frac{1}{x}\right)}\]
  6. Simplified30.5

    \[\leadsto \color{blue}{\mathsf{fma}\left(1, -\frac{\left|x\right|}{x}, 1\right)} + \mathsf{fma}\left(-\left|x\right|, \frac{1}{x}, \left|x\right| \cdot \frac{1}{x}\right)\]
  7. Simplified0

    \[\leadsto \mathsf{fma}\left(1, -\frac{\left|x\right|}{x}, 1\right) + \color{blue}{0}\]
  8. Final simplification0

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

Reproduce

herbie shell --seed 2019325 +o rules:numerics
(FPCore (x)
  :name "sqrt sqr"
  :precision binary64

  :herbie-target
  (if (< x 0.0) 2 0.0)

  (- (/ x x) (* (/ 1 x) (sqrt (* x x)))))