Average Error: 20.4 → 6.3
Time: 11.1s
Precision: 64
Internal Precision: 128
\[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \le -1.8090807948555078 \cdot 10^{+19}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.0063877571766516 \cdot 10^{-174}:\\ \;\;\;\;\frac{y + x}{y \cdot y + x \cdot x} \cdot \left(x - y\right)\\ \mathbf{elif}\;y \le 6.579340210064974 \cdot 10^{-239}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{y + x}{y \cdot y + x \cdot x} \cdot \left(x - y\right)\\ \end{array}\]

Error

Bits error versus x

Bits error versus y

Target

Original20.4
Target0.1
Herbie6.3
\[\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.8090807948555078e+19

    1. Initial program 34.8

      \[\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.8090807948555078e+19 < y < -1.0063877571766516e-174 or 6.579340210064974e-239 < y

    1. Initial program 6.8

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

      \[\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-frac7.3

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

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

    if -1.0063877571766516e-174 < y < 6.579340210064974e-239

    1. Initial program 28.8

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

      \[\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.1

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.8090807948555078 \cdot 10^{+19}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.0063877571766516 \cdot 10^{-174}:\\ \;\;\;\;\frac{y + x}{y \cdot y + x \cdot x} \cdot \left(x - y\right)\\ \mathbf{elif}\;y \le 6.579340210064974 \cdot 10^{-239}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{y + x}{y \cdot y + x \cdot x} \cdot \left(x - y\right)\\ \end{array}\]

Reproduce

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