Average Error: 19.4 → 4.9
Time: 3.4m
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.366618915167733 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.70713010924186 \cdot 10^{-162}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{y \cdot y + x \cdot x}\\ \mathbf{elif}\;y \le 3.060862139420529 \cdot 10^{-162}:\\ \;\;\;\;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 r31478121 = x;
        double r31478122 = y;
        double r31478123 = r31478121 - r31478122;
        double r31478124 = r31478121 + r31478122;
        double r31478125 = r31478123 * r31478124;
        double r31478126 = r31478121 * r31478121;
        double r31478127 = r31478122 * r31478122;
        double r31478128 = r31478126 + r31478127;
        double r31478129 = r31478125 / r31478128;
        return r31478129;
}

double f(double x, double y) {
        double r31478130 = y;
        double r31478131 = -1.366618915167733e+154;
        bool r31478132 = r31478130 <= r31478131;
        double r31478133 = -1.0;
        double r31478134 = -2.70713010924186e-162;
        bool r31478135 = r31478130 <= r31478134;
        double r31478136 = x;
        double r31478137 = r31478136 - r31478130;
        double r31478138 = r31478130 + r31478136;
        double r31478139 = r31478137 * r31478138;
        double r31478140 = r31478130 * r31478130;
        double r31478141 = r31478136 * r31478136;
        double r31478142 = r31478140 + r31478141;
        double r31478143 = r31478139 / r31478142;
        double r31478144 = 3.060862139420529e-162;
        bool r31478145 = r31478130 <= r31478144;
        double r31478146 = 1.0;
        double r31478147 = r31478145 ? r31478146 : r31478143;
        double r31478148 = r31478135 ? r31478143 : r31478147;
        double r31478149 = r31478132 ? r31478133 : r31478148;
        return r31478149;
}

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

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

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

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

\end{array}

Error

Bits error versus x

Bits error versus y

Target

Original19.4
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.366618915167733e+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.366618915167733e+154 < y < -2.70713010924186e-162 or 3.060862139420529e-162 < y

    1. Initial program 0.1

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

    if -2.70713010924186e-162 < y < 3.060862139420529e-162

    1. Initial program 29.0

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied *-un-lft-identity29.0

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

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

      \[\leadsto \color{blue}{\left(x - y\right)} \cdot \frac{x + y}{x \cdot x + y \cdot y}\]
    6. Taylor expanded around -inf 15.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.366618915167733 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.70713010924186 \cdot 10^{-162}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{y \cdot y + x \cdot x}\\ \mathbf{elif}\;y \le 3.060862139420529 \cdot 10^{-162}:\\ \;\;\;\;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 2019102 
(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))))