Average Error: 20.6 → 5.6
Time: 16.3s
Precision: 64
\[0.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 -2.676166605931690440041354829305747631762 \cdot 10^{-43}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.551916196244887166192370925126561411819 \cdot 10^{-157}:\\ \;\;\;\;\frac{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}\\ \mathbf{elif}\;y \le 3.974257954450127467240173976607583888966 \cdot 10^{-160}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}}{\sqrt{\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 -2.676166605931690440041354829305747631762 \cdot 10^{-43}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 3.974257954450127467240173976607583888966 \cdot 10^{-160}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r3880509 = x;
        double r3880510 = y;
        double r3880511 = r3880509 - r3880510;
        double r3880512 = r3880509 + r3880510;
        double r3880513 = r3880511 * r3880512;
        double r3880514 = r3880509 * r3880509;
        double r3880515 = r3880510 * r3880510;
        double r3880516 = r3880514 + r3880515;
        double r3880517 = r3880513 / r3880516;
        return r3880517;
}

double f(double x, double y) {
        double r3880518 = y;
        double r3880519 = -2.6761666059316904e-43;
        bool r3880520 = r3880518 <= r3880519;
        double r3880521 = -1.0;
        double r3880522 = -2.5519161962448872e-157;
        bool r3880523 = r3880518 <= r3880522;
        double r3880524 = x;
        double r3880525 = r3880524 - r3880518;
        double r3880526 = r3880518 + r3880524;
        double r3880527 = r3880525 * r3880526;
        double r3880528 = r3880518 * r3880518;
        double r3880529 = fma(r3880524, r3880524, r3880528);
        double r3880530 = sqrt(r3880529);
        double r3880531 = r3880527 / r3880530;
        double r3880532 = r3880531 / r3880530;
        double r3880533 = 3.9742579544501275e-160;
        bool r3880534 = r3880518 <= r3880533;
        double r3880535 = 1.0;
        double r3880536 = r3880534 ? r3880535 : r3880532;
        double r3880537 = r3880523 ? r3880532 : r3880536;
        double r3880538 = r3880520 ? r3880521 : r3880537;
        return r3880538;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.6
Target0.0
Herbie5.6
\[\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 < -2.6761666059316904e-43

    1. Initial program 29.0

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

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

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

    if -2.6761666059316904e-43 < y < -2.5519161962448872e-157 or 3.9742579544501275e-160 < y

    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(x, x, y \cdot y\right)}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt0.1

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}}\]
    5. Applied associate-/r*0.2

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

    if -2.5519161962448872e-157 < y < 3.9742579544501275e-160

    1. Initial program 29.3

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -2.676166605931690440041354829305747631762 \cdot 10^{-43}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.551916196244887166192370925126561411819 \cdot 10^{-157}:\\ \;\;\;\;\frac{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}\\ \mathbf{elif}\;y \le 3.974257954450127467240173976607583888966 \cdot 10^{-160}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}\\ \end{array}\]

Reproduce

herbie shell --seed 2019170 +o rules:numerics
(FPCore (x y)
  :name "Kahan p9 Example"
  :pre (and (< 0.0 x 1.0) (< y 1.0))

  :herbie-target
  (if (< 0.5 (fabs (/ x y)) 2.0) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1.0 (/ 2.0 (+ 1.0 (* (/ x y) (/ x y))))))

  (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))