Average Error: 19.5 → 4.9
Time: 10.0s
Precision: 64
\[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \le -1.3261488941008737 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -6.401413403488955 \cdot 10^{-157}:\\ \;\;\;\;\frac{1}{\frac{(x \cdot x + \left(y \cdot y\right))_*}{\left(x - y\right) \cdot \left(y + x\right)}}\\ \mathbf{elif}\;y \le 1.5746421432035006 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{(x \cdot x + \left(y \cdot y\right))_*}{\left(x - y\right) \cdot \left(y + x\right)}}\\ \end{array}\]
double f(double x, double y) {
        double r6591337 = x;
        double r6591338 = y;
        double r6591339 = r6591337 - r6591338;
        double r6591340 = r6591337 + r6591338;
        double r6591341 = r6591339 * r6591340;
        double r6591342 = r6591337 * r6591337;
        double r6591343 = r6591338 * r6591338;
        double r6591344 = r6591342 + r6591343;
        double r6591345 = r6591341 / r6591344;
        return r6591345;
}

double f(double x, double y) {
        double r6591346 = y;
        double r6591347 = -1.3261488941008737e+154;
        bool r6591348 = r6591346 <= r6591347;
        double r6591349 = -1.0;
        double r6591350 = -6.401413403488955e-157;
        bool r6591351 = r6591346 <= r6591350;
        double r6591352 = 1.0;
        double r6591353 = x;
        double r6591354 = r6591346 * r6591346;
        double r6591355 = fma(r6591353, r6591353, r6591354);
        double r6591356 = r6591353 - r6591346;
        double r6591357 = r6591346 + r6591353;
        double r6591358 = r6591356 * r6591357;
        double r6591359 = r6591355 / r6591358;
        double r6591360 = r6591352 / r6591359;
        double r6591361 = 1.5746421432035006e-162;
        bool r6591362 = r6591346 <= r6591361;
        double r6591363 = r6591362 ? r6591352 : r6591360;
        double r6591364 = r6591351 ? r6591360 : r6591363;
        double r6591365 = r6591348 ? r6591349 : r6591364;
        return r6591365;
}

\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
\mathbf{if}\;y \le -1.3261488941008737 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -6.401413403488955 \cdot 10^{-157}:\\
\;\;\;\;\frac{1}{\frac{(x \cdot x + \left(y \cdot y\right))_*}{\left(x - y\right) \cdot \left(y + x\right)}}\\

\mathbf{elif}\;y \le 1.5746421432035006 \cdot 10^{-162}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{(x \cdot x + \left(y \cdot y\right))_*}{\left(x - y\right) \cdot \left(y + x\right)}}\\

\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original19.5
Target0.1
Herbie4.9
\[\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.3261488941008737e+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)}{(x \cdot x + \left(y \cdot y\right))_*}}\]
    3. Taylor expanded around 0 0

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

    if -1.3261488941008737e+154 < y < -6.401413403488955e-157 or 1.5746421432035006e-162 < y

    1. Initial program 0.1

      \[\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)}{(x \cdot x + \left(y \cdot y\right))_*}}\]
    3. Using strategy rm
    4. Applied clear-num0.1

      \[\leadsto \color{blue}{\frac{1}{\frac{(x \cdot x + \left(y \cdot y\right))_*}{\left(x - y\right) \cdot \left(y + x\right)}}}\]

    if -6.401413403488955e-157 < y < 1.5746421432035006e-162

    1. Initial program 28.8

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

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}}\]
    3. Taylor expanded around inf 15.4

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3261488941008737 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -6.401413403488955 \cdot 10^{-157}:\\ \;\;\;\;\frac{1}{\frac{(x \cdot x + \left(y \cdot y\right))_*}{\left(x - y\right) \cdot \left(y + x\right)}}\\ \mathbf{elif}\;y \le 1.5746421432035006 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{(x \cdot x + \left(y \cdot y\right))_*}{\left(x - y\right) \cdot \left(y + x\right)}}\\ \end{array}\]

Reproduce

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