Average Error: 32.4 → 0
Time: 6.3s
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 r239048 = x;
        double r239049 = r239048 / r239048;
        double r239050 = 1.0;
        double r239051 = r239050 / r239048;
        double r239052 = r239048 * r239048;
        double r239053 = sqrt(r239052);
        double r239054 = r239051 * r239053;
        double r239055 = r239049 - r239054;
        return r239055;
}

double f(double x) {
        double r239056 = 1.0;
        double r239057 = x;
        double r239058 = fabs(r239057);
        double r239059 = r239058 / r239057;
        double r239060 = -r239059;
        double r239061 = 1.0;
        double r239062 = fma(r239056, r239060, r239061);
        return r239062;
}

Error

Bits error versus x

Target

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

Derivation

  1. Initial program 32.4

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

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

    \[\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.8

    \[\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.8

    \[\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 2019323 +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)))))