Average Error: 19.9 → 5.1
Time: 12.7s
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.3546652535010843 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.1881673277355116 \cdot 10^{-166}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}\\ \mathbf{elif}\;y \le -1.1550750865120013 \cdot 10^{-166}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 2.887433405492649 \cdot 10^{-163}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\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.3546652535010843 \cdot 10^{+154}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le -1.1550750865120013 \cdot 10^{-166}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le 2.887433405492649 \cdot 10^{-163}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r2190833 = x;
        double r2190834 = y;
        double r2190835 = r2190833 - r2190834;
        double r2190836 = r2190833 + r2190834;
        double r2190837 = r2190835 * r2190836;
        double r2190838 = r2190833 * r2190833;
        double r2190839 = r2190834 * r2190834;
        double r2190840 = r2190838 + r2190839;
        double r2190841 = r2190837 / r2190840;
        return r2190841;
}

double f(double x, double y) {
        double r2190842 = y;
        double r2190843 = -1.3546652535010843e+154;
        bool r2190844 = r2190842 <= r2190843;
        double r2190845 = -1.0;
        double r2190846 = -1.1881673277355116e-166;
        bool r2190847 = r2190842 <= r2190846;
        double r2190848 = x;
        double r2190849 = r2190848 - r2190842;
        double r2190850 = r2190842 + r2190848;
        double r2190851 = r2190849 * r2190850;
        double r2190852 = r2190842 * r2190842;
        double r2190853 = fma(r2190848, r2190848, r2190852);
        double r2190854 = r2190851 / r2190853;
        double r2190855 = -1.1550750865120013e-166;
        bool r2190856 = r2190842 <= r2190855;
        double r2190857 = 2.887433405492649e-163;
        bool r2190858 = r2190842 <= r2190857;
        double r2190859 = 1.0;
        double r2190860 = r2190858 ? r2190859 : r2190854;
        double r2190861 = r2190856 ? r2190845 : r2190860;
        double r2190862 = r2190847 ? r2190854 : r2190861;
        double r2190863 = r2190844 ? r2190845 : r2190862;
        return r2190863;
}

Error

Bits error versus x

Bits error versus y

Target

Original19.9
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 3 regimes
  2. if y < -1.3546652535010843e+154 or -1.1881673277355116e-166 < y < -1.1550750865120013e-166

    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. Taylor expanded around 0 0

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

    if -1.3546652535010843e+154 < y < -1.1881673277355116e-166 or 2.887433405492649e-163 < y

    1. Initial program 0.4

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

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

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{{y}^{2} + {x}^{2}}}\]
    4. Simplified0.4

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

    if -1.1550750865120013e-166 < y < 2.887433405492649e-163

    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(y, y, \left(x \cdot x\right)\right)}}\]
    3. Taylor expanded around inf 30.2

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{{y}^{2} + {x}^{2}}}\]
    4. Simplified30.2

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3546652535010843 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.1881673277355116 \cdot 10^{-166}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}\\ \mathbf{elif}\;y \le -1.1550750865120013 \cdot 10^{-166}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 2.887433405492649 \cdot 10^{-163}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}\\ \end{array}\]

Reproduce

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