Average Error: 20.1 → 4.9
Time: 15.3s
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.3341061642710286 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.1638085591132404 \cdot 10^{-160}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{elif}\;y \le 1.5708400865028228 \cdot 10^{-162}:\\ \;\;\;\;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.3341061642710286 \cdot 10^{+154}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 1.5708400865028228 \cdot 10^{-162}:\\
\;\;\;\;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 r3658667 = x;
        double r3658668 = y;
        double r3658669 = r3658667 - r3658668;
        double r3658670 = r3658667 + r3658668;
        double r3658671 = r3658669 * r3658670;
        double r3658672 = r3658667 * r3658667;
        double r3658673 = r3658668 * r3658668;
        double r3658674 = r3658672 + r3658673;
        double r3658675 = r3658671 / r3658674;
        return r3658675;
}

double f(double x, double y) {
        double r3658676 = y;
        double r3658677 = -1.3341061642710286e+154;
        bool r3658678 = r3658676 <= r3658677;
        double r3658679 = -1.0;
        double r3658680 = -1.1638085591132404e-160;
        bool r3658681 = r3658676 <= r3658680;
        double r3658682 = x;
        double r3658683 = r3658682 - r3658676;
        double r3658684 = r3658676 + r3658682;
        double r3658685 = r3658683 * r3658684;
        double r3658686 = r3658676 * r3658676;
        double r3658687 = fma(r3658682, r3658682, r3658686);
        double r3658688 = r3658685 / r3658687;
        double r3658689 = 1.5708400865028228e-162;
        bool r3658690 = r3658676 <= r3658689;
        double r3658691 = 1.0;
        double r3658692 = r3658690 ? r3658691 : r3658688;
        double r3658693 = r3658681 ? r3658688 : r3658692;
        double r3658694 = r3658678 ? r3658679 : r3658693;
        return r3658694;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.1
Target0.1
Herbie4.9
\[\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.3341061642710286e+154

    1. Initial program 63.6

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

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

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

    if -1.3341061642710286e+154 < y < -1.1638085591132404e-160 or 1.5708400865028228e-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)}}\]

    if -1.1638085591132404e-160 < y < 1.5708400865028228e-162

    1. Initial program 30.2

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3341061642710286 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.1638085591132404 \cdot 10^{-160}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{elif}\;y \le 1.5708400865028228 \cdot 10^{-162}:\\ \;\;\;\;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 2019164 +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))))