Average Error: 20.5 → 5.2
Time: 2.6s
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 -6.0195069609054914 \cdot 10^{22}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -6.1493278535517628 \cdot 10^{-162}:\\ \;\;\;\;\frac{x - y}{\frac{{x}^{2} + {y}^{2}}{x + y}}\\ \mathbf{elif}\;y \le 3.48334728323537955 \cdot 10^{-161}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot 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 -6.0195069609054914 \cdot 10^{22}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -6.1493278535517628 \cdot 10^{-162}:\\
\;\;\;\;\frac{x - y}{\frac{{x}^{2} + {y}^{2}}{x + y}}\\

\mathbf{elif}\;y \le 3.48334728323537955 \cdot 10^{-161}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r91146 = x;
        double r91147 = y;
        double r91148 = r91146 - r91147;
        double r91149 = r91146 + r91147;
        double r91150 = r91148 * r91149;
        double r91151 = r91146 * r91146;
        double r91152 = r91147 * r91147;
        double r91153 = r91151 + r91152;
        double r91154 = r91150 / r91153;
        return r91154;
}

double f(double x, double y) {
        double r91155 = y;
        double r91156 = -6.0195069609054914e+22;
        bool r91157 = r91155 <= r91156;
        double r91158 = -1.0;
        double r91159 = -6.149327853551763e-162;
        bool r91160 = r91155 <= r91159;
        double r91161 = x;
        double r91162 = r91161 - r91155;
        double r91163 = 2.0;
        double r91164 = pow(r91161, r91163);
        double r91165 = pow(r91155, r91163);
        double r91166 = r91164 + r91165;
        double r91167 = r91161 + r91155;
        double r91168 = r91166 / r91167;
        double r91169 = r91162 / r91168;
        double r91170 = 3.4833472832353795e-161;
        bool r91171 = r91155 <= r91170;
        double r91172 = 1.0;
        double r91173 = r91161 * r91161;
        double r91174 = r91155 * r91155;
        double r91175 = r91173 + r91174;
        double r91176 = sqrt(r91175);
        double r91177 = r91162 / r91176;
        double r91178 = r91167 / r91176;
        double r91179 = r91177 * r91178;
        double r91180 = r91171 ? r91172 : r91179;
        double r91181 = r91160 ? r91169 : r91180;
        double r91182 = r91157 ? r91158 : r91181;
        return r91182;
}

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.5
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 < -6.0195069609054914e+22

    1. Initial program 34.4

      \[\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 -6.0195069609054914e+22 < y < -6.149327853551763e-162

    1. Initial program 0.1

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

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

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

    if -6.149327853551763e-162 < y < 3.4833472832353795e-161

    1. Initial program 30.9

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

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

    if 3.4833472832353795e-161 < y

    1. Initial program 0.1

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

      \[\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.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 4 regimes into one program.
  4. Final simplification5.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -6.0195069609054914 \cdot 10^{22}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -6.1493278535517628 \cdot 10^{-162}:\\ \;\;\;\;\frac{x - y}{\frac{{x}^{2} + {y}^{2}}{x + y}}\\ \mathbf{elif}\;y \le 3.48334728323537955 \cdot 10^{-161}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\\ \end{array}\]

Reproduce

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