Average Error: 20.2 → 5.2
Time: 9.1s
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.3483719131786158 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -6.3662280950353905 \cdot 10^{-155}:\\ \;\;\;\;\left(\sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\right) \cdot \sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\\ \mathbf{elif}\;y \le 3.1003292371794966 \cdot 10^{-168}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\left(\sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\right) \cdot \sqrt[3]{\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.3483719131786158 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -6.3662280950353905 \cdot 10^{-155}:\\
\;\;\;\;\left(\sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\right) \cdot \sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\\

\mathbf{elif}\;y \le 3.1003292371794966 \cdot 10^{-168}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\left(\sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\right) \cdot \sqrt[3]{\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 r1108103 = x;
        double r1108104 = y;
        double r1108105 = r1108103 - r1108104;
        double r1108106 = r1108103 + r1108104;
        double r1108107 = r1108105 * r1108106;
        double r1108108 = r1108103 * r1108103;
        double r1108109 = r1108104 * r1108104;
        double r1108110 = r1108108 + r1108109;
        double r1108111 = r1108107 / r1108110;
        return r1108111;
}

double f(double x, double y) {
        double r1108112 = y;
        double r1108113 = -1.3483719131786158e+154;
        bool r1108114 = r1108112 <= r1108113;
        double r1108115 = -1.0;
        double r1108116 = -6.3662280950353905e-155;
        bool r1108117 = r1108112 <= r1108116;
        double r1108118 = x;
        double r1108119 = r1108118 - r1108112;
        double r1108120 = r1108112 + r1108118;
        double r1108121 = r1108119 * r1108120;
        double r1108122 = r1108112 * r1108112;
        double r1108123 = fma(r1108118, r1108118, r1108122);
        double r1108124 = r1108121 / r1108123;
        double r1108125 = cbrt(r1108124);
        double r1108126 = r1108125 * r1108125;
        double r1108127 = r1108126 * r1108125;
        double r1108128 = 3.1003292371794966e-168;
        bool r1108129 = r1108112 <= r1108128;
        double r1108130 = 1.0;
        double r1108131 = r1108129 ? r1108130 : r1108127;
        double r1108132 = r1108117 ? r1108127 : r1108131;
        double r1108133 = r1108114 ? r1108115 : r1108132;
        return r1108133;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.2
Target0.1
Herbie5.2
\[\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.3483719131786158e+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.3483719131786158e+154 < y < -6.3662280950353905e-155 or 3.1003292371794966e-168 < y

    1. Initial program 0.3

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

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\]
    3. Using strategy rm
    4. Applied add-cube-cbrt0.3

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

    if -6.3662280950353905e-155 < y < 3.1003292371794966e-168

    1. Initial program 28.9

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3483719131786158 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -6.3662280950353905 \cdot 10^{-155}:\\ \;\;\;\;\left(\sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\right) \cdot \sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\\ \mathbf{elif}\;y \le 3.1003292371794966 \cdot 10^{-168}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\left(\sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}} \cdot \sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\right) \cdot \sqrt[3]{\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 2019153 +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))))