Average Error: 20.3 → 5.3
Time: 15.3s
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.3573856910724084 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.3408391082234118 \cdot 10^{-160}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}\\ \mathbf{elif}\;y \le 3.238102132712735 \cdot 10^{-164}:\\ \;\;\;\;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

Original20.3
Target0.1
Herbie5.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.3573856910724084e+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. Using strategy rm
    4. Applied *-un-lft-identity63.6

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{1 \cdot (x \cdot x + \left(y \cdot y\right))_*}}\]
    5. Applied times-frac62.0

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

      \[\leadsto \color{blue}{\left(x - y\right)} \cdot \frac{y + x}{(x \cdot x + \left(y \cdot y\right))_*}\]
    7. Taylor expanded around 0 0

      \[\leadsto \color{blue}{-1}\]

    if -1.3573856910724084e+154 < y < -1.3408391082234118e-160 or 3.238102132712735e-164 < 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))_*}}\]
    3. Using strategy rm
    4. Applied *-un-lft-identity0.1

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{1 \cdot (x \cdot x + \left(y \cdot y\right))_*}}\]
    5. Applied times-frac0.7

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

      \[\leadsto \color{blue}{\left(x - y\right)} \cdot \frac{y + x}{(x \cdot x + \left(y \cdot y\right))_*}\]
    7. Using strategy rm
    8. Applied associate-*r/0.1

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

    if -1.3408391082234118e-160 < y < 3.238102132712735e-164

    1. Initial program 29.5

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified29.5

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}}\]
    3. Using strategy rm
    4. Applied *-un-lft-identity29.5

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{1 \cdot (x \cdot x + \left(y \cdot y\right))_*}}\]
    5. Applied times-frac29.9

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

      \[\leadsto \color{blue}{\left(x - y\right)} \cdot \frac{y + x}{(x \cdot x + \left(y \cdot y\right))_*}\]
    7. Using strategy rm
    8. Applied associate-*r/29.5

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}}\]
    9. Taylor expanded around inf 16.5

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3573856910724084 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.3408391082234118 \cdot 10^{-160}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}\\ \mathbf{elif}\;y \le 3.238102132712735 \cdot 10^{-164}:\\ \;\;\;\;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 2019068 +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))))