Average Error: 19.4 → 4.9
Time: 38.3s
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)}{(x \cdot x + \left(y \cdot y\right))_*}\\ \mathbf{elif}\;y \le 3.060862139420529 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}\\ \end{array}\]
double f(double x, double y) {
        double r6787207 = x;
        double r6787208 = y;
        double r6787209 = r6787207 - r6787208;
        double r6787210 = r6787207 + r6787208;
        double r6787211 = r6787209 * r6787210;
        double r6787212 = r6787207 * r6787207;
        double r6787213 = r6787208 * r6787208;
        double r6787214 = r6787212 + r6787213;
        double r6787215 = r6787211 / r6787214;
        return r6787215;
}

double f(double x, double y) {
        double r6787216 = y;
        double r6787217 = -1.366618915167733e+154;
        bool r6787218 = r6787216 <= r6787217;
        double r6787219 = -1.0;
        double r6787220 = -2.70713010924186e-162;
        bool r6787221 = r6787216 <= r6787220;
        double r6787222 = x;
        double r6787223 = r6787222 - r6787216;
        double r6787224 = r6787216 + r6787222;
        double r6787225 = r6787223 * r6787224;
        double r6787226 = r6787216 * r6787216;
        double r6787227 = fma(r6787222, r6787222, r6787226);
        double r6787228 = r6787225 / r6787227;
        double r6787229 = 3.060862139420529e-162;
        bool r6787230 = r6787216 <= r6787229;
        double r6787231 = 1.0;
        double r6787232 = r6787230 ? r6787231 : r6787228;
        double r6787233 = r6787221 ? r6787228 : r6787232;
        double r6787234 = r6787218 ? r6787219 : r6787233;
        return r6787234;
}

\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)}{(x \cdot x + \left(y \cdot y\right))_*}\\

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

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

\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. Simplified63.6

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}}\]
    3. 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}\]
    2. Simplified0.1

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

    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. Simplified29.0

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}}\]
    3. 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)}{(x \cdot x + \left(y \cdot y\right))_*}\\ \mathbf{elif}\;y \le 3.060862139420529 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}\\ \end{array}\]

Reproduce

herbie shell --seed 2019102 +o rules:numerics
(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))))