Average Error: 19.4 → 5.4
Time: 17.6s
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 -3.2516136687883904 \cdot 10^{+109}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.3671339967957754 \cdot 10^{-146}:\\ \;\;\;\;\sqrt[3]{\frac{x - y}{\frac{y \cdot y + x \cdot x}{y + x}} \cdot \frac{\left(x - y\right) \cdot \left(x - y\right)}{\frac{y \cdot y + x \cdot x}{y + x} \cdot \frac{y \cdot y + x \cdot x}{y + x}}}\\ \mathbf{elif}\;y \le 5.2770893163471555 \cdot 10^{-177}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(y + x\right) \cdot \left(x - y\right)}{y \cdot y + x \cdot x}\\ \end{array}\]

Error

Bits error versus x

Bits error versus y

Target

Original19.4
Target0.0
Herbie5.4
\[\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 < -3.2516136687883904e+109

    1. Initial program 48.7

      \[\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 -3.2516136687883904e+109 < y < -1.3671339967957754e-146

    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 add-cbrt-cube34.3

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\sqrt[3]{\left(\left(x \cdot x + y \cdot y\right) \cdot \left(x \cdot x + y \cdot y\right)\right) \cdot \left(x \cdot x + y \cdot y\right)}}}\]
    4. Applied add-cbrt-cube34.5

      \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\sqrt[3]{\left(\left(x + y\right) \cdot \left(x + y\right)\right) \cdot \left(x + y\right)}}}{\sqrt[3]{\left(\left(x \cdot x + y \cdot y\right) \cdot \left(x \cdot x + y \cdot y\right)\right) \cdot \left(x \cdot x + y \cdot y\right)}}\]
    5. Applied add-cbrt-cube34.6

      \[\leadsto \frac{\color{blue}{\sqrt[3]{\left(\left(x - y\right) \cdot \left(x - y\right)\right) \cdot \left(x - y\right)}} \cdot \sqrt[3]{\left(\left(x + y\right) \cdot \left(x + y\right)\right) \cdot \left(x + y\right)}}{\sqrt[3]{\left(\left(x \cdot x + y \cdot y\right) \cdot \left(x \cdot x + y \cdot y\right)\right) \cdot \left(x \cdot x + y \cdot y\right)}}\]
    6. Applied cbrt-unprod34.7

      \[\leadsto \frac{\color{blue}{\sqrt[3]{\left(\left(\left(x - y\right) \cdot \left(x - y\right)\right) \cdot \left(x - y\right)\right) \cdot \left(\left(\left(x + y\right) \cdot \left(x + y\right)\right) \cdot \left(x + y\right)\right)}}}{\sqrt[3]{\left(\left(x \cdot x + y \cdot y\right) \cdot \left(x \cdot x + y \cdot y\right)\right) \cdot \left(x \cdot x + y \cdot y\right)}}\]
    7. Applied cbrt-undiv34.6

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

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

    if -1.3671339967957754e-146 < y < 5.2770893163471555e-177

    1. Initial program 27.1

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

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

    if 5.2770893163471555e-177 < y

    1. Initial program 2.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -3.2516136687883904 \cdot 10^{+109}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.3671339967957754 \cdot 10^{-146}:\\ \;\;\;\;\sqrt[3]{\frac{x - y}{\frac{y \cdot y + x \cdot x}{y + x}} \cdot \frac{\left(x - y\right) \cdot \left(x - y\right)}{\frac{y \cdot y + x \cdot x}{y + x} \cdot \frac{y \cdot y + x \cdot x}{y + x}}}\\ \mathbf{elif}\;y \le 5.2770893163471555 \cdot 10^{-177}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(y + x\right) \cdot \left(x - y\right)}{y \cdot y + x \cdot x}\\ \end{array}\]

Reproduce

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