Average Error: 20.7 → 5.2
Time: 2.7s
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 -1.3322860122897779 \cdot 10^{154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -4.021556983409163 \cdot 10^{-161}:\\ \;\;\;\;\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\\ \mathbf{elif}\;y \le 6.56455594317470941 \cdot 10^{-161}:\\ \;\;\;\;\frac{x - y}{x - y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\frac{{x}^{2} + {y}^{2}}{x + y}}\\ \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.3322860122897779 \cdot 10^{154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -4.021556983409163 \cdot 10^{-161}:\\
\;\;\;\;\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\\

\mathbf{elif}\;y \le 6.56455594317470941 \cdot 10^{-161}:\\
\;\;\;\;\frac{x - y}{x - y}\\

\mathbf{else}:\\
\;\;\;\;\frac{x - y}{\frac{{x}^{2} + {y}^{2}}{x + y}}\\

\end{array}
double f(double x, double y) {
        double r81687 = x;
        double r81688 = y;
        double r81689 = r81687 - r81688;
        double r81690 = r81687 + r81688;
        double r81691 = r81689 * r81690;
        double r81692 = r81687 * r81687;
        double r81693 = r81688 * r81688;
        double r81694 = r81692 + r81693;
        double r81695 = r81691 / r81694;
        return r81695;
}

double f(double x, double y) {
        double r81696 = y;
        double r81697 = -1.332286012289778e+154;
        bool r81698 = r81696 <= r81697;
        double r81699 = -1.0;
        double r81700 = -4.021556983409163e-161;
        bool r81701 = r81696 <= r81700;
        double r81702 = x;
        double r81703 = r81702 - r81696;
        double r81704 = r81702 * r81702;
        double r81705 = r81696 * r81696;
        double r81706 = r81704 + r81705;
        double r81707 = sqrt(r81706);
        double r81708 = r81703 / r81707;
        double r81709 = r81702 + r81696;
        double r81710 = r81709 / r81707;
        double r81711 = r81708 * r81710;
        double r81712 = 6.5645559431747094e-161;
        bool r81713 = r81696 <= r81712;
        double r81714 = r81703 / r81703;
        double r81715 = 2.0;
        double r81716 = pow(r81702, r81715);
        double r81717 = pow(r81696, r81715);
        double r81718 = r81716 + r81717;
        double r81719 = r81718 / r81709;
        double r81720 = r81703 / r81719;
        double r81721 = r81713 ? r81714 : r81720;
        double r81722 = r81701 ? r81711 : r81721;
        double r81723 = r81698 ? r81699 : r81722;
        return r81723;
}

Error

Bits error versus x

Bits error versus y

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original20.7
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 4 regimes
  2. if y < -1.332286012289778e+154

    1. Initial program 64.0

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

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

    if -1.332286012289778e+154 < y < -4.021556983409163e-161

    1. Initial program 0.0

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt0.0

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}}\]
    4. Applied times-frac0.2

      \[\leadsto \color{blue}{\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}}\]

    if -4.021556983409163e-161 < y < 6.5645559431747094e-161

    1. Initial program 30.7

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied associate-/l*31.5

      \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}}\]
    4. Simplified31.5

      \[\leadsto \frac{x - y}{\color{blue}{\frac{{x}^{2} + {y}^{2}}{x + y}}}\]
    5. Taylor expanded around inf 16.3

      \[\leadsto \frac{x - y}{\color{blue}{x - y}}\]

    if 6.5645559431747094e-161 < y

    1. Initial program 0.0

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied associate-/l*0.4

      \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}}\]
    4. Simplified0.4

      \[\leadsto \frac{x - y}{\color{blue}{\frac{{x}^{2} + {y}^{2}}{x + y}}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification5.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3322860122897779 \cdot 10^{154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -4.021556983409163 \cdot 10^{-161}:\\ \;\;\;\;\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\\ \mathbf{elif}\;y \le 6.56455594317470941 \cdot 10^{-161}:\\ \;\;\;\;\frac{x - y}{x - y}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\frac{{x}^{2} + {y}^{2}}{x + y}}\\ \end{array}\]

Reproduce

herbie shell --seed 2020024 
(FPCore (x y)
  :name "Kahan p9 Example"
  :precision binary64
  :pre (and (< 0.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))))