Average Error: 20.2 → 5.1
Time: 2.6m
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.3578365654257818 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.2831801671759717 \cdot 10^{-176}:\\ \;\;\;\;\frac{y + x}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}\\ \mathbf{elif}\;y \le -1.1047743871727693 \cdot 10^{-203}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 2.023453404126092 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(y + x\right) \cdot \left(x - y\right)}{x \cdot x + y \cdot y}\\ \end{array}\]

Error

Bits error versus x

Bits error versus y

Target

Original20.2
Target0.1
Herbie5.1
\[\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 4 regimes
  2. if y < -1.3578365654257818e+154 or -1.2831801671759717e-176 < y < -1.1047743871727693e-203

    1. Initial program 59.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt59.1

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}}\]
    4. Applied *-commutative59.1

      \[\leadsto \frac{\color{blue}{\left(x + y\right) \cdot \left(x - y\right)}}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}\]
    5. Applied times-frac57.9

      \[\leadsto \color{blue}{\frac{x + y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}}\]
    6. Taylor expanded around 0 5.4

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

    if -1.3578365654257818e+154 < y < -1.2831801671759717e-176

    1. Initial program 1.5

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied add-sqr-sqrt1.5

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}}\]
    4. Applied *-commutative1.5

      \[\leadsto \frac{\color{blue}{\left(x + y\right) \cdot \left(x - y\right)}}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}\]
    5. Applied times-frac1.8

      \[\leadsto \color{blue}{\frac{x + y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}}\]

    if -1.1047743871727693e-203 < y < 2.023453404126092e-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 add-sqr-sqrt29.0

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}}\]
    4. Applied *-commutative29.0

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

      \[\leadsto \color{blue}{\frac{x + y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}}\]
    6. Taylor expanded around -inf 12.8

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

    if 2.023453404126092e-162 < y

    1. Initial program 0.0

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification5.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3578365654257818 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.2831801671759717 \cdot 10^{-176}:\\ \;\;\;\;\frac{y + x}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}\\ \mathbf{elif}\;y \le -1.1047743871727693 \cdot 10^{-203}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 2.023453404126092 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(y + x\right) \cdot \left(x - y\right)}{x \cdot x + y \cdot y}\\ \end{array}\]

Reproduce

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