Average Error: 20.4 → 6.2
Time: 3.7s
Precision: 64
\[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 -2.0486376949351227 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.18765926777910252 \cdot 10^{-173}:\\ \;\;\;\;\frac{{y}^{2} - {x}^{2}}{-\left(x \cdot x + y \cdot y\right)}\\ \mathbf{elif}\;y \le -3.0453579094069954 \cdot 10^{-217}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -5.50439846459850614 \cdot 10^{-245}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le -7.20226558706363735 \cdot 10^{-257}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 3.0361243732160851 \cdot 10^{-168}:\\ \;\;\;\;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.4
Target0.0
Herbie6.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 < -2.0486376949351227e+153 or -1.1876592677791025e-173 < y < -3.0453579094069954e-217 or -5.504398464598506e-245 < y < -7.202265587063637e-257

    1. Initial program 55.0

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

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

    if -2.0486376949351227e+153 < y < -1.1876592677791025e-173 or 3.036124373216085e-168 < y

    1. Initial program 1.0

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

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

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

    if -3.0453579094069954e-217 < y < -5.504398464598506e-245 or -7.202265587063637e-257 < y < 3.036124373216085e-168

    1. Initial program 30.0

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -2.0486376949351227 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.18765926777910252 \cdot 10^{-173}:\\ \;\;\;\;\frac{{y}^{2} - {x}^{2}}{-\left(x \cdot x + y \cdot y\right)}\\ \mathbf{elif}\;y \le -3.0453579094069954 \cdot 10^{-217}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -5.50439846459850614 \cdot 10^{-245}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le -7.20226558706363735 \cdot 10^{-257}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 3.0361243732160851 \cdot 10^{-168}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{{y}^{2} - {x}^{2}}{-\left(x \cdot x + y \cdot y\right)}\\ \end{array}\]

Reproduce

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