Average Error: 19.5 → 4.9
Time: 11.5s
Precision: 64
\[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \le -1.3261488941008737 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -6.401413403488955 \cdot 10^{-157}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(y + x\right)}}\\ \mathbf{elif}\;y \le 1.5746421432035006 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(y + x\right)}}\\ \end{array}\]
double f(double x, double y) {
        double r8645981 = x;
        double r8645982 = y;
        double r8645983 = r8645981 - r8645982;
        double r8645984 = r8645981 + r8645982;
        double r8645985 = r8645983 * r8645984;
        double r8645986 = r8645981 * r8645981;
        double r8645987 = r8645982 * r8645982;
        double r8645988 = r8645986 + r8645987;
        double r8645989 = r8645985 / r8645988;
        return r8645989;
}

double f(double x, double y) {
        double r8645990 = y;
        double r8645991 = -1.3261488941008737e+154;
        bool r8645992 = r8645990 <= r8645991;
        double r8645993 = -1.0;
        double r8645994 = -6.401413403488955e-157;
        bool r8645995 = r8645990 <= r8645994;
        double r8645996 = 1.0;
        double r8645997 = x;
        double r8645998 = r8645997 * r8645997;
        double r8645999 = r8645990 * r8645990;
        double r8646000 = r8645998 + r8645999;
        double r8646001 = r8645997 - r8645990;
        double r8646002 = r8645990 + r8645997;
        double r8646003 = r8646001 * r8646002;
        double r8646004 = r8646000 / r8646003;
        double r8646005 = r8645996 / r8646004;
        double r8646006 = 1.5746421432035006e-162;
        bool r8646007 = r8645990 <= r8646006;
        double r8646008 = r8646007 ? r8645996 : r8646005;
        double r8646009 = r8645995 ? r8646005 : r8646008;
        double r8646010 = r8645992 ? r8645993 : r8646009;
        return r8646010;
}

\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
\mathbf{if}\;y \le -1.3261488941008737 \cdot 10^{+154}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 1.5746421432035006 \cdot 10^{-162}:\\
\;\;\;\;1\\

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

\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original19.5
Target0.1
Herbie4.9
\[\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.3261488941008737e+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.3261488941008737e+154 < y < -6.401413403488955e-157 or 1.5746421432035006e-162 < 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 clear-num0.1

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

    if -6.401413403488955e-157 < y < 1.5746421432035006e-162

    1. Initial program 28.8

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

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

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

Reproduce

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