Average Error: 20.1 → 5.2
Time: 1.8m
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 -1.3730354842790037 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -4.534922755068586 \cdot 10^{-162}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{y \cdot y + x \cdot x}\\ \mathbf{elif}\;y \le 3.053717103568057 \cdot 10^{-167}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{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 -1.3730354842790037 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -4.534922755068586 \cdot 10^{-162}:\\
\;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{y \cdot y + x \cdot x}\\

\mathbf{elif}\;y \le 3.053717103568057 \cdot 10^{-167}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r25378231 = x;
        double r25378232 = y;
        double r25378233 = r25378231 - r25378232;
        double r25378234 = r25378231 + r25378232;
        double r25378235 = r25378233 * r25378234;
        double r25378236 = r25378231 * r25378231;
        double r25378237 = r25378232 * r25378232;
        double r25378238 = r25378236 + r25378237;
        double r25378239 = r25378235 / r25378238;
        return r25378239;
}

double f(double x, double y) {
        double r25378240 = y;
        double r25378241 = -1.3730354842790037e+154;
        bool r25378242 = r25378240 <= r25378241;
        double r25378243 = -1.0;
        double r25378244 = -4.534922755068586e-162;
        bool r25378245 = r25378240 <= r25378244;
        double r25378246 = x;
        double r25378247 = r25378246 - r25378240;
        double r25378248 = r25378240 + r25378246;
        double r25378249 = r25378247 * r25378248;
        double r25378250 = r25378240 * r25378240;
        double r25378251 = r25378246 * r25378246;
        double r25378252 = r25378250 + r25378251;
        double r25378253 = r25378249 / r25378252;
        double r25378254 = 3.053717103568057e-167;
        bool r25378255 = r25378240 <= r25378254;
        double r25378256 = 1.0;
        double r25378257 = r25378255 ? r25378256 : r25378253;
        double r25378258 = r25378245 ? r25378253 : r25378257;
        double r25378259 = r25378242 ? r25378243 : r25378258;
        return r25378259;
}

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.1
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 3 regimes
  2. if y < -1.3730354842790037e+154

    1. Initial program 63.6

      \[\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.3730354842790037e+154 < y < -4.534922755068586e-162 or 3.053717103568057e-167 < y

    1. Initial program 0.5

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]

    if -4.534922755068586e-162 < y < 3.053717103568057e-167

    1. Initial program 29.6

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

      \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}}\]
    4. Using strategy rm
    5. Applied *-un-lft-identity29.9

      \[\leadsto \frac{x - y}{\frac{x \cdot x + y \cdot y}{\color{blue}{1 \cdot \left(x + y\right)}}}\]
    6. Applied add-sqr-sqrt29.9

      \[\leadsto \frac{x - y}{\frac{\color{blue}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}}{1 \cdot \left(x + y\right)}}\]
    7. Applied times-frac29.8

      \[\leadsto \frac{x - y}{\color{blue}{\frac{\sqrt{x \cdot x + y \cdot y}}{1} \cdot \frac{\sqrt{x \cdot x + y \cdot y}}{x + y}}}\]
    8. Applied *-un-lft-identity29.8

      \[\leadsto \frac{x - \color{blue}{1 \cdot y}}{\frac{\sqrt{x \cdot x + y \cdot y}}{1} \cdot \frac{\sqrt{x \cdot x + y \cdot y}}{x + y}}\]
    9. Applied *-un-lft-identity29.8

      \[\leadsto \frac{\color{blue}{1 \cdot x} - 1 \cdot y}{\frac{\sqrt{x \cdot x + y \cdot y}}{1} \cdot \frac{\sqrt{x \cdot x + y \cdot y}}{x + y}}\]
    10. Applied distribute-lft-out--29.8

      \[\leadsto \frac{\color{blue}{1 \cdot \left(x - y\right)}}{\frac{\sqrt{x \cdot x + y \cdot y}}{1} \cdot \frac{\sqrt{x \cdot x + y \cdot y}}{x + y}}\]
    11. Applied times-frac29.9

      \[\leadsto \color{blue}{\frac{1}{\frac{\sqrt{x \cdot x + y \cdot y}}{1}} \cdot \frac{x - y}{\frac{\sqrt{x \cdot x + y \cdot y}}{x + y}}}\]
    12. Simplified29.9

      \[\leadsto \color{blue}{\frac{1}{\sqrt{x \cdot x + y \cdot y}}} \cdot \frac{x - y}{\frac{\sqrt{x \cdot x + y \cdot y}}{x + y}}\]
    13. Taylor expanded around -inf 15.7

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3730354842790037 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -4.534922755068586 \cdot 10^{-162}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{y \cdot y + x \cdot x}\\ \mathbf{elif}\;y \le 3.053717103568057 \cdot 10^{-167}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{y \cdot y + x \cdot x}\\ \end{array}\]

Reproduce

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