Average Error: 20.2 → 5.2
Time: 26.7s
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.3343552386316892 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -5.56207691204812 \cdot 10^{-192}:\\ \;\;\;\;\sqrt[3]{\left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}\right) \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\\ \mathbf{elif}\;y \le 3.2126293943555167 \cdot 10^{-168}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt[3]{\left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}\right) \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\\ \end{array}\]

Error

Bits error versus x

Bits error versus y

Target

Original20.2
Target0.0
Herbie5.2
\[\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.3343552386316892e+154

    1. Initial program 63.6

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

      \[\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-frac62.0

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

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

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

    if -1.3343552386316892e+154 < y < -5.56207691204812e-192 or 3.2126293943555167e-168 < y

    1. Initial program 2.4

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

      \[\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 times-frac2.7

      \[\leadsto \color{blue}{\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}}\]
    5. Using strategy rm
    6. Applied add-cbrt-cube2.8

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

    if -5.56207691204812e-192 < y < 3.2126293943555167e-168

    1. Initial program 29.1

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

      \[\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 13.3

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3343552386316892 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -5.56207691204812 \cdot 10^{-192}:\\ \;\;\;\;\sqrt[3]{\left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}\right) \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\\ \mathbf{elif}\;y \le 3.2126293943555167 \cdot 10^{-168}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\sqrt[3]{\left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}\right) \cdot \frac{x - y}{\sqrt{x \cdot x + y \cdot y}}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\\ \end{array}\]

Reproduce

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