Average Error: 20.5 → 5.3
Time: 19.0s
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.3561015051531283 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.0295842870860447 \cdot 10^{-160}:\\ \;\;\;\;\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 4.593586257584354 \cdot 10^{-186}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt[3]{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}{y + x}} \cdot \left(\frac{x - y}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}{y + x}} \cdot \frac{x - y}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}{y + x}}\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.3561015051531283 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -2.0295842870860447 \cdot 10^{-160}:\\
\;\;\;\;\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 4.593586257584354 \cdot 10^{-186}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r1927013 = x;
        double r1927014 = y;
        double r1927015 = r1927013 - r1927014;
        double r1927016 = r1927013 + r1927014;
        double r1927017 = r1927015 * r1927016;
        double r1927018 = r1927013 * r1927013;
        double r1927019 = r1927014 * r1927014;
        double r1927020 = r1927018 + r1927019;
        double r1927021 = r1927017 / r1927020;
        return r1927021;
}

double f(double x, double y) {
        double r1927022 = y;
        double r1927023 = -1.3561015051531283e+154;
        bool r1927024 = r1927022 <= r1927023;
        double r1927025 = -1.0;
        double r1927026 = -2.0295842870860447e-160;
        bool r1927027 = r1927022 <= r1927026;
        double r1927028 = 1.0;
        double r1927029 = x;
        double r1927030 = r1927029 * r1927029;
        double r1927031 = fma(r1927022, r1927022, r1927030);
        double r1927032 = r1927029 - r1927022;
        double r1927033 = r1927022 + r1927029;
        double r1927034 = r1927032 * r1927033;
        double r1927035 = r1927031 / r1927034;
        double r1927036 = r1927028 / r1927035;
        double r1927037 = 4.593586257584354e-186;
        bool r1927038 = r1927022 <= r1927037;
        double r1927039 = r1927022 * r1927022;
        double r1927040 = fma(r1927029, r1927029, r1927039);
        double r1927041 = r1927040 / r1927033;
        double r1927042 = r1927032 / r1927041;
        double r1927043 = r1927042 * r1927042;
        double r1927044 = r1927042 * r1927043;
        double r1927045 = cbrt(r1927044);
        double r1927046 = r1927038 ? r1927028 : r1927045;
        double r1927047 = r1927027 ? r1927036 : r1927046;
        double r1927048 = r1927024 ? r1927025 : r1927047;
        return r1927048;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.5
Target0.1
Herbie5.3
\[\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.3561015051531283e+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. Taylor expanded around 0 0

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

    if -1.3561015051531283e+154 < y < -2.0295842870860447e-160

    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 -2.0295842870860447e-160 < y < 4.593586257584354e-186

    1. Initial program 30.5

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

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

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

    if 4.593586257584354e-186 < y

    1. Initial program 4.3

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

      \[\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 associate-/l*4.9

      \[\leadsto \color{blue}{\frac{x - y}{\frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{y + x}}}\]
    5. Using strategy rm
    6. Applied add-cbrt-cube18.5

      \[\leadsto \frac{x - y}{\color{blue}{\sqrt[3]{\left(\frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{y + x} \cdot \frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{y + x}\right) \cdot \frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{y + x}}}}\]
    7. Applied add-cbrt-cube17.9

      \[\leadsto \frac{\color{blue}{\sqrt[3]{\left(\left(x - y\right) \cdot \left(x - y\right)\right) \cdot \left(x - y\right)}}}{\sqrt[3]{\left(\frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{y + x} \cdot \frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{y + x}\right) \cdot \frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{y + x}}}\]
    8. Applied cbrt-undiv17.8

      \[\leadsto \color{blue}{\sqrt[3]{\frac{\left(\left(x - y\right) \cdot \left(x - y\right)\right) \cdot \left(x - y\right)}{\left(\frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{y + x} \cdot \frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{y + x}\right) \cdot \frac{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}{y + x}}}}\]
    9. Simplified4.8

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3561015051531283 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.0295842870860447 \cdot 10^{-160}:\\ \;\;\;\;\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 4.593586257584354 \cdot 10^{-186}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt[3]{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}{y + x}} \cdot \left(\frac{x - y}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}{y + x}} \cdot \frac{x - y}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}{y + x}}\right)}\\ \end{array}\]

Reproduce

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