Average Error: 20.1 → 5.1
Time: 12.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.788922078078905678245702835710036578543 \cdot 10^{-32}:\\ \;\;\;\;\frac{x - y}{y - x}\\ \mathbf{elif}\;y \le -1.382949905808310759111021249823258136601 \cdot 10^{-158} \lor \neg \left(y \le 7.961862811311691246218405838467989119993 \cdot 10^{-164}\right):\\ \;\;\;\;\frac{x \cdot \left(x + y\right)}{x \cdot x + y \cdot y} - \frac{y}{\frac{x \cdot x + y \cdot y}{x + y}}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{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.788922078078905678245702835710036578543 \cdot 10^{-32}:\\
\;\;\;\;\frac{x - y}{y - x}\\

\mathbf{elif}\;y \le -1.382949905808310759111021249823258136601 \cdot 10^{-158} \lor \neg \left(y \le 7.961862811311691246218405838467989119993 \cdot 10^{-164}\right):\\
\;\;\;\;\frac{x \cdot \left(x + y\right)}{x \cdot x + y \cdot y} - \frac{y}{\frac{x \cdot x + y \cdot y}{x + y}}\\

\mathbf{else}:\\
\;\;\;\;\frac{x - y}{x - y}\\

\end{array}
double f(double x, double y) {
        double r93970 = x;
        double r93971 = y;
        double r93972 = r93970 - r93971;
        double r93973 = r93970 + r93971;
        double r93974 = r93972 * r93973;
        double r93975 = r93970 * r93970;
        double r93976 = r93971 * r93971;
        double r93977 = r93975 + r93976;
        double r93978 = r93974 / r93977;
        return r93978;
}

double f(double x, double y) {
        double r93979 = y;
        double r93980 = -1.7889220780789057e-32;
        bool r93981 = r93979 <= r93980;
        double r93982 = x;
        double r93983 = r93982 - r93979;
        double r93984 = r93979 - r93982;
        double r93985 = r93983 / r93984;
        double r93986 = -1.3829499058083108e-158;
        bool r93987 = r93979 <= r93986;
        double r93988 = 7.961862811311691e-164;
        bool r93989 = r93979 <= r93988;
        double r93990 = !r93989;
        bool r93991 = r93987 || r93990;
        double r93992 = r93982 + r93979;
        double r93993 = r93982 * r93992;
        double r93994 = r93982 * r93982;
        double r93995 = r93979 * r93979;
        double r93996 = r93994 + r93995;
        double r93997 = r93993 / r93996;
        double r93998 = r93996 / r93992;
        double r93999 = r93979 / r93998;
        double r94000 = r93997 - r93999;
        double r94001 = r93983 / r93983;
        double r94002 = r93991 ? r94000 : r94001;
        double r94003 = r93981 ? r93985 : r94002;
        return r94003;
}

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.0
Herbie5.1
\[\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.7889220780789057e-32

    1. Initial program 28.7

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

      \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{y + x}}}\]
    3. Taylor expanded around 0 0.3

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

    if -1.7889220780789057e-32 < y < -1.3829499058083108e-158 or 7.961862811311691e-164 < y

    1. Initial program 0.2

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

      \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{y + x}}}\]
    3. Using strategy rm
    4. Applied div-sub0.7

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

      \[\leadsto \color{blue}{\frac{x \cdot \left(x + y\right)}{y \cdot y + x \cdot x}} - \frac{y}{\frac{x \cdot x + y \cdot y}{y + x}}\]
    6. Simplified0.6

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

    if -1.3829499058083108e-158 < y < 7.961862811311691e-164

    1. Initial program 29.2

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

      \[\leadsto \color{blue}{\frac{x - y}{\frac{x \cdot x + y \cdot y}{y + x}}}\]
    3. Taylor expanded around inf 15.0

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.788922078078905678245702835710036578543 \cdot 10^{-32}:\\ \;\;\;\;\frac{x - y}{y - x}\\ \mathbf{elif}\;y \le -1.382949905808310759111021249823258136601 \cdot 10^{-158} \lor \neg \left(y \le 7.961862811311691246218405838467989119993 \cdot 10^{-164}\right):\\ \;\;\;\;\frac{x \cdot \left(x + y\right)}{x \cdot x + y \cdot y} - \frac{y}{\frac{x \cdot x + y \cdot y}{x + y}}\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{x - y}\\ \end{array}\]

Reproduce

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