Average Error: 20.5 → 4.8
Time: 4.1s
Precision: binary64
\[0.0 \lt x \lt 1 \land y \lt 1\]
\[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \le -7.6184336366029451 \cdot 10^{149}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.54877378415330119 \cdot 10^{-161}:\\ \;\;\;\;\frac{{y}^{2} - {x}^{2}}{-\left(x \cdot x + y \cdot y\right)}\\ \mathbf{elif}\;y \le 1.3657440974188289 \cdot 10^{-159}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{{y}^{2} - {x}^{2}}{-\left(x \cdot x + y \cdot y\right)}\\ \end{array}\]

Error

Bits error versus x

Bits error versus y

Target

Original20.5
Target0.1
Herbie4.8
\[\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 < -7.6184336366029451e149

    1. Initial program 62.3

      \[\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 -7.6184336366029451e149 < y < -2.54877378415330119e-161 or 1.3657440974188289e-159 < y

    1. Initial program 0.0

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied frac-2neg0.0

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

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

    if -2.54877378415330119e-161 < y < 1.3657440974188289e-159

    1. Initial program 29.5

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Taylor expanded around inf 15.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -7.6184336366029451 \cdot 10^{149}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.54877378415330119 \cdot 10^{-161}:\\ \;\;\;\;\frac{{y}^{2} - {x}^{2}}{-\left(x \cdot x + y \cdot y\right)}\\ \mathbf{elif}\;y \le 1.3657440974188289 \cdot 10^{-159}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{{y}^{2} - {x}^{2}}{-\left(x \cdot x + y \cdot y\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2020147 
(FPCore (x y)
  :name "Kahan p9 Example"
  :precision binary64
  :pre (and (< 0.0 x 1.0) (< y 1.0))

  :herbie-target
  (if (< 0.5 (fabs (/ x y)) 2.0) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1.0 (/ 2.0 (+ 1.0 (* (/ x y) (/ x y))))))

  (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))