Average Error: 20.3 → 5.7
Time: 7.9s
Precision: 64
\[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 -1.3330097877511057 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.406936418392987 \cdot 10^{-189}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{elif}\;y \le -1.4153860890433093 \cdot 10^{-211}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 5.33062675147924 \cdot 10^{-161}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\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 -1.3330097877511057 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -2.406936418392987 \cdot 10^{-189}:\\
\;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\

\mathbf{elif}\;y \le -1.4153860890433093 \cdot 10^{-211}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le 5.33062675147924 \cdot 10^{-161}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r1380264 = x;
        double r1380265 = y;
        double r1380266 = r1380264 - r1380265;
        double r1380267 = r1380264 + r1380265;
        double r1380268 = r1380266 * r1380267;
        double r1380269 = r1380264 * r1380264;
        double r1380270 = r1380265 * r1380265;
        double r1380271 = r1380269 + r1380270;
        double r1380272 = r1380268 / r1380271;
        return r1380272;
}

double f(double x, double y) {
        double r1380273 = y;
        double r1380274 = -1.3330097877511057e+154;
        bool r1380275 = r1380273 <= r1380274;
        double r1380276 = -1.0;
        double r1380277 = -2.406936418392987e-189;
        bool r1380278 = r1380273 <= r1380277;
        double r1380279 = x;
        double r1380280 = r1380279 - r1380273;
        double r1380281 = r1380273 + r1380279;
        double r1380282 = r1380280 * r1380281;
        double r1380283 = r1380273 * r1380273;
        double r1380284 = fma(r1380279, r1380279, r1380283);
        double r1380285 = r1380282 / r1380284;
        double r1380286 = -1.4153860890433093e-211;
        bool r1380287 = r1380273 <= r1380286;
        double r1380288 = 5.33062675147924e-161;
        bool r1380289 = r1380273 <= r1380288;
        double r1380290 = 1.0;
        double r1380291 = r1380289 ? r1380290 : r1380285;
        double r1380292 = r1380287 ? r1380276 : r1380291;
        double r1380293 = r1380278 ? r1380285 : r1380292;
        double r1380294 = r1380275 ? r1380276 : r1380293;
        return r1380294;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.3
Target0.1
Herbie5.7
\[\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 < -1.3330097877511057e+154 or -2.406936418392987e-189 < y < -1.4153860890433093e-211

    1. Initial program 59.3

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

      \[\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 5.4

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

    if -1.3330097877511057e+154 < y < -2.406936418392987e-189 or 5.33062675147924e-161 < y

    1. Initial program 1.7

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

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\]

    if -1.4153860890433093e-211 < y < 5.33062675147924e-161

    1. Initial program 30.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified30.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 inf 14.2

      \[\leadsto \color{blue}{1}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification5.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3330097877511057 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.406936418392987 \cdot 10^{-189}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{elif}\;y \le -1.4153860890433093 \cdot 10^{-211}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 5.33062675147924 \cdot 10^{-161}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2019154 +o rules:numerics
(FPCore (x y)
  :name "Kahan p9 Example"
  :pre (and (< 0 x 1) (< y 1))

  :herbie-target
  (if (< 0.5 (fabs (/ x y)) 2) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1 (/ 2 (+ 1 (* (/ x y) (/ x y))))))

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