Average Error: 19.7 → 5.0
Time: 6.8s
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.3386474296726014 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -5.572350587881258 \cdot 10^{-188}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{elif}\;y \le -3.6548784939791494 \cdot 10^{-199}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 2.0258790624487682 \cdot 10^{-176}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{\left(x - y\right) \cdot \left(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.3386474296726014 \cdot 10^{+154}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le -3.6548784939791494 \cdot 10^{-199}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le 2.0258790624487682 \cdot 10^{-176}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r1664213 = x;
        double r1664214 = y;
        double r1664215 = r1664213 - r1664214;
        double r1664216 = r1664213 + r1664214;
        double r1664217 = r1664215 * r1664216;
        double r1664218 = r1664213 * r1664213;
        double r1664219 = r1664214 * r1664214;
        double r1664220 = r1664218 + r1664219;
        double r1664221 = r1664217 / r1664220;
        return r1664221;
}

double f(double x, double y) {
        double r1664222 = y;
        double r1664223 = -1.3386474296726014e+154;
        bool r1664224 = r1664222 <= r1664223;
        double r1664225 = -1.0;
        double r1664226 = -5.572350587881258e-188;
        bool r1664227 = r1664222 <= r1664226;
        double r1664228 = x;
        double r1664229 = r1664228 - r1664222;
        double r1664230 = r1664222 + r1664228;
        double r1664231 = r1664229 * r1664230;
        double r1664232 = r1664222 * r1664222;
        double r1664233 = fma(r1664228, r1664228, r1664232);
        double r1664234 = r1664231 / r1664233;
        double r1664235 = -3.6548784939791494e-199;
        bool r1664236 = r1664222 <= r1664235;
        double r1664237 = 2.0258790624487682e-176;
        bool r1664238 = r1664222 <= r1664237;
        double r1664239 = 1.0;
        double r1664240 = r1664233 / r1664231;
        double r1664241 = r1664239 / r1664240;
        double r1664242 = r1664238 ? r1664239 : r1664241;
        double r1664243 = r1664236 ? r1664225 : r1664242;
        double r1664244 = r1664227 ? r1664234 : r1664243;
        double r1664245 = r1664224 ? r1664225 : r1664244;
        return r1664245;
}

Error

Bits error versus x

Bits error versus y

Target

Original19.7
Target0.1
Herbie5.0
\[\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.3386474296726014e+154 or -5.572350587881258e-188 < y < -3.6548784939791494e-199

    1. Initial program 61.1

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

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

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

    if -1.3386474296726014e+154 < y < -5.572350587881258e-188

    1. Initial program 2.2

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

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

    if -3.6548784939791494e-199 < y < 2.0258790624487682e-176

    1. Initial program 28.4

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

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

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

    if 2.0258790624487682e-176 < y

    1. Initial program 1.9

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

      \[\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 clear-num1.9

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3386474296726014 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -5.572350587881258 \cdot 10^{-188}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{elif}\;y \le -3.6548784939791494 \cdot 10^{-199}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 2.0258790624487682 \cdot 10^{-176}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(x, x, y \cdot y\right)}{\left(x - y\right) \cdot \left(y + x\right)}}\\ \end{array}\]

Reproduce

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