Average Error: 20.4 → 5.1
Time: 6.6s
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.3308054258701725 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.1512930280169555 \cdot 10^{-162}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{\left(x - y\right) \cdot \left(y + x\right)}}\\ \mathbf{elif}\;y \le 8.250332507489211 \cdot 10^{-160}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\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.3308054258701725 \cdot 10^{+154}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 8.250332507489211 \cdot 10^{-160}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r1504730 = x;
        double r1504731 = y;
        double r1504732 = r1504730 - r1504731;
        double r1504733 = r1504730 + r1504731;
        double r1504734 = r1504732 * r1504733;
        double r1504735 = r1504730 * r1504730;
        double r1504736 = r1504731 * r1504731;
        double r1504737 = r1504735 + r1504736;
        double r1504738 = r1504734 / r1504737;
        return r1504738;
}

double f(double x, double y) {
        double r1504739 = y;
        double r1504740 = -1.3308054258701725e+154;
        bool r1504741 = r1504739 <= r1504740;
        double r1504742 = -1.0;
        double r1504743 = -3.1512930280169555e-162;
        bool r1504744 = r1504739 <= r1504743;
        double r1504745 = 1.0;
        double r1504746 = x;
        double r1504747 = r1504746 * r1504746;
        double r1504748 = fma(r1504739, r1504739, r1504747);
        double r1504749 = r1504746 - r1504739;
        double r1504750 = r1504739 + r1504746;
        double r1504751 = r1504749 * r1504750;
        double r1504752 = r1504748 / r1504751;
        double r1504753 = r1504745 / r1504752;
        double r1504754 = 8.250332507489211e-160;
        bool r1504755 = r1504739 <= r1504754;
        double r1504756 = r1504751 / r1504748;
        double r1504757 = r1504755 ? r1504745 : r1504756;
        double r1504758 = r1504744 ? r1504753 : r1504757;
        double r1504759 = r1504741 ? r1504742 : r1504758;
        return r1504759;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.4
Target0.1
Herbie5.1
\[\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 4 regimes
  2. if y < -1.3308054258701725e+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(y, y, \left(x \cdot x\right)\right)}}\]
    3. Using strategy rm
    4. Applied clear-num63.6

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

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

    if -1.3308054258701725e+154 < y < -3.1512930280169555e-162

    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(y, y, \left(x \cdot x\right)\right)}}\]
    3. Using strategy rm
    4. Applied clear-num0.0

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

    if -3.1512930280169555e-162 < y < 8.250332507489211e-160

    1. Initial program 29.9

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

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

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

    if 8.250332507489211e-160 < y

    1. Initial program 0.1

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

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

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

Reproduce

herbie shell --seed 2019128 +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))))