Average Error: 20.0 → 8.0
Time: 12.2s
Precision: 64
\[0.0 \lt x \lt 1 \land y \lt 1\]
\[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \le -2.92690375472246175352160983946616260701 \cdot 10^{-134}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 5.816163962969591972156246782697755183642 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \end{array}\]
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
\mathbf{if}\;y \le -2.92690375472246175352160983946616260701 \cdot 10^{-134}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le 5.816163962969591972156246782697755183642 \cdot 10^{-162}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\

\end{array}
double f(double x, double y) {
        double r4075125 = x;
        double r4075126 = y;
        double r4075127 = r4075125 - r4075126;
        double r4075128 = r4075125 + r4075126;
        double r4075129 = r4075127 * r4075128;
        double r4075130 = r4075125 * r4075125;
        double r4075131 = r4075126 * r4075126;
        double r4075132 = r4075130 + r4075131;
        double r4075133 = r4075129 / r4075132;
        return r4075133;
}

double f(double x, double y) {
        double r4075134 = y;
        double r4075135 = -2.9269037547224618e-134;
        bool r4075136 = r4075134 <= r4075135;
        double r4075137 = -1.0;
        double r4075138 = 5.816163962969592e-162;
        bool r4075139 = r4075134 <= r4075138;
        double r4075140 = 1.0;
        double r4075141 = x;
        double r4075142 = r4075134 + r4075141;
        double r4075143 = r4075141 - r4075134;
        double r4075144 = r4075142 * r4075143;
        double r4075145 = r4075134 * r4075134;
        double r4075146 = fma(r4075141, r4075141, r4075145);
        double r4075147 = r4075144 / r4075146;
        double r4075148 = r4075139 ? r4075140 : r4075147;
        double r4075149 = r4075136 ? r4075137 : r4075148;
        return r4075149;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.0
Target0.1
Herbie8.0
\[\begin{array}{l} \mathbf{if}\;0.5 \lt \left|\frac{x}{y}\right| \lt 2:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;1 - \frac{2}{1 + \frac{x}{y} \cdot \frac{x}{y}}\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if y < -2.9269037547224618e-134

    1. Initial program 22.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified22.1

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\]
    3. Taylor expanded around 0 4.2

      \[\leadsto \color{blue}{-1}\]

    if -2.9269037547224618e-134 < y < 5.816163962969592e-162

    1. Initial program 27.2

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified27.2

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\]
    3. Taylor expanded around inf 17.2

      \[\leadsto \color{blue}{1}\]

    if 5.816163962969592e-162 < y

    1. Initial program 0.0

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified0.0

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification8.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -2.92690375472246175352160983946616260701 \cdot 10^{-134}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 5.816163962969591972156246782697755183642 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2019192 +o rules:numerics
(FPCore (x y)
  :name "Kahan p9 Example"
  :pre (and (< 0.0 x 1.0) (< y 1.0))

  :herbie-target
  (if (< 0.5 (fabs (/ x y)) 2.0) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1.0 (/ 2.0 (+ 1.0 (* (/ x y) (/ x y))))))

  (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))