Average Error: 20.5 → 6.1
Time: 1.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 -1.64081725270371729 \cdot 10^{149}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.75047117514697987 \cdot 10^{-136}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \le 1.7784148690303961 \cdot 10^{-177}:\\ \;\;\;\;\frac{x - y}{x - y}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{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 -1.64081725270371729 \cdot 10^{149}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 1.7784148690303961 \cdot 10^{-177}:\\
\;\;\;\;\frac{x - y}{x - y}\\

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

\end{array}
double f(double x, double y) {
        double r71159 = x;
        double r71160 = y;
        double r71161 = r71159 - r71160;
        double r71162 = r71159 + r71160;
        double r71163 = r71161 * r71162;
        double r71164 = r71159 * r71159;
        double r71165 = r71160 * r71160;
        double r71166 = r71164 + r71165;
        double r71167 = r71163 / r71166;
        return r71167;
}

double f(double x, double y) {
        double r71168 = y;
        double r71169 = -1.6408172527037173e+149;
        bool r71170 = r71168 <= r71169;
        double r71171 = -1.0;
        double r71172 = -1.75047117514698e-136;
        bool r71173 = r71168 <= r71172;
        double r71174 = x;
        double r71175 = r71174 - r71168;
        double r71176 = r71174 + r71168;
        double r71177 = r71175 * r71176;
        double r71178 = r71174 * r71174;
        double r71179 = r71168 * r71168;
        double r71180 = r71178 + r71179;
        double r71181 = r71177 / r71180;
        double r71182 = 1.7784148690303961e-177;
        bool r71183 = r71168 <= r71182;
        double r71184 = r71175 / r71175;
        double r71185 = r71183 ? r71184 : r71181;
        double r71186 = r71173 ? r71181 : r71185;
        double r71187 = r71170 ? r71171 : r71186;
        return r71187;
}

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
Herbie6.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.6408172527037173e+149

    1. Initial program 62.2

      \[\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.6408172527037173e+149 < y < -1.75047117514698e-136 or 1.7784148690303961e-177 < y

    1. Initial program 1.1

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

    if -1.75047117514698e-136 < y < 1.7784148690303961e-177

    1. Initial program 28.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*28.9

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.64081725270371729 \cdot 10^{149}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.75047117514697987 \cdot 10^{-136}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \le 1.7784148690303961 \cdot 10^{-177}:\\ \;\;\;\;\frac{x - y}{x - y}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \end{array}\]

Reproduce

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