Average Error: 19.9 → 8.2
Time: 10.6s
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 -2.8213046869993915 \cdot 10^{-161}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 1.6059084603325851 \cdot 10^{-198}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\sqrt{y \cdot y + x \cdot x}} \cdot \frac{y + x}{\sqrt{y \cdot y + x \cdot x}}\\ \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.8213046869993915 \cdot 10^{-161}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le 1.6059084603325851 \cdot 10^{-198}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\frac{x - y}{\sqrt{y \cdot y + x \cdot x}} \cdot \frac{y + x}{\sqrt{y \cdot y + x \cdot x}}\\

\end{array}
double f(double x, double y) {
        double r2140966 = x;
        double r2140967 = y;
        double r2140968 = r2140966 - r2140967;
        double r2140969 = r2140966 + r2140967;
        double r2140970 = r2140968 * r2140969;
        double r2140971 = r2140966 * r2140966;
        double r2140972 = r2140967 * r2140967;
        double r2140973 = r2140971 + r2140972;
        double r2140974 = r2140970 / r2140973;
        return r2140974;
}

double f(double x, double y) {
        double r2140975 = y;
        double r2140976 = -2.8213046869993915e-161;
        bool r2140977 = r2140975 <= r2140976;
        double r2140978 = -1.0;
        double r2140979 = 1.6059084603325851e-198;
        bool r2140980 = r2140975 <= r2140979;
        double r2140981 = 1.0;
        double r2140982 = x;
        double r2140983 = r2140982 - r2140975;
        double r2140984 = r2140975 * r2140975;
        double r2140985 = r2140982 * r2140982;
        double r2140986 = r2140984 + r2140985;
        double r2140987 = sqrt(r2140986);
        double r2140988 = r2140983 / r2140987;
        double r2140989 = r2140975 + r2140982;
        double r2140990 = r2140989 / r2140987;
        double r2140991 = r2140988 * r2140990;
        double r2140992 = r2140980 ? r2140981 : r2140991;
        double r2140993 = r2140977 ? r2140978 : r2140992;
        return r2140993;
}

Error

Bits error versus x

Bits error versus y

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original19.9
Target0.1
Herbie8.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 < -2.8213046869993915e-161

    1. Initial program 20.6

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

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

    if -2.8213046869993915e-161 < y < 1.6059084603325851e-198

    1. Initial program 29.7

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

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

    if 1.6059084603325851e-198 < y

    1. Initial program 6.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-sqrt6.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-frac6.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -2.8213046869993915 \cdot 10^{-161}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 1.6059084603325851 \cdot 10^{-198}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\sqrt{y \cdot y + x \cdot x}} \cdot \frac{y + x}{\sqrt{y \cdot y + x \cdot x}}\\ \end{array}\]

Reproduce

herbie shell --seed 2019132 
(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))))