Average Error: 20.4 → 5.4
Time: 9.6s
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 -3.658988851323816306303491351742974655897 \cdot 10^{150}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.660304854296606245768089281383148543308 \cdot 10^{-162}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \mathbf{elif}\;y \le 1.955059603474676626176360228974583883699 \cdot 10^{-171}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \end{array}\]
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
\mathbf{if}\;y \le -3.658988851323816306303491351742974655897 \cdot 10^{150}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -1.660304854296606245768089281383148543308 \cdot 10^{-162}:\\
\;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\

\mathbf{elif}\;y \le 1.955059603474676626176360228974583883699 \cdot 10^{-171}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\

\end{array}
double f(double x, double y) {
        double r46303 = x;
        double r46304 = y;
        double r46305 = r46303 - r46304;
        double r46306 = r46303 + r46304;
        double r46307 = r46305 * r46306;
        double r46308 = r46303 * r46303;
        double r46309 = r46304 * r46304;
        double r46310 = r46308 + r46309;
        double r46311 = r46307 / r46310;
        return r46311;
}

double f(double x, double y) {
        double r46312 = y;
        double r46313 = -3.658988851323816e+150;
        bool r46314 = r46312 <= r46313;
        double r46315 = -1.0;
        double r46316 = -1.6603048542966062e-162;
        bool r46317 = r46312 <= r46316;
        double r46318 = 1.0;
        double r46319 = x;
        double r46320 = r46319 * r46319;
        double r46321 = r46312 * r46312;
        double r46322 = r46320 + r46321;
        double r46323 = r46319 - r46312;
        double r46324 = r46319 + r46312;
        double r46325 = r46323 * r46324;
        double r46326 = r46322 / r46325;
        double r46327 = r46318 / r46326;
        double r46328 = 1.9550596034746766e-171;
        bool r46329 = r46312 <= r46328;
        double r46330 = r46329 ? r46318 : r46327;
        double r46331 = r46317 ? r46327 : r46330;
        double r46332 = r46314 ? r46315 : r46331;
        return r46332;
}

Error

Bits error versus x

Bits error versus y

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original20.4
Target0.1
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 3 regimes
  2. if y < -3.658988851323816e+150

    1. Initial program 62.8

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

      \[\leadsto \color{blue}{\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}}\]
    4. Using strategy rm
    5. Applied *-un-lft-identity62.8

      \[\leadsto \frac{1}{\frac{\color{blue}{1 \cdot \left(x \cdot x + y \cdot y\right)}}{\left(x - y\right) \cdot \left(x + y\right)}}\]
    6. Applied times-frac60.8

      \[\leadsto \frac{1}{\color{blue}{\frac{1}{x - y} \cdot \frac{x \cdot x + y \cdot y}{x + y}}}\]
    7. Applied add-sqr-sqrt60.8

      \[\leadsto \frac{\color{blue}{\sqrt{1} \cdot \sqrt{1}}}{\frac{1}{x - y} \cdot \frac{x \cdot x + y \cdot y}{x + y}}\]
    8. Applied times-frac60.8

      \[\leadsto \color{blue}{\frac{\sqrt{1}}{\frac{1}{x - y}} \cdot \frac{\sqrt{1}}{\frac{x \cdot x + y \cdot y}{x + y}}}\]
    9. Simplified60.8

      \[\leadsto \color{blue}{\left(1 \cdot \left(x - y\right)\right)} \cdot \frac{\sqrt{1}}{\frac{x \cdot x + y \cdot y}{x + y}}\]
    10. Simplified60.8

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

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

    if -3.658988851323816e+150 < y < -1.6603048542966062e-162 or 1.9550596034746766e-171 < y

    1. Initial program 0.6

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

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

    if -1.6603048542966062e-162 < y < 1.9550596034746766e-171

    1. Initial program 30.5

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -3.658988851323816306303491351742974655897 \cdot 10^{150}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.660304854296606245768089281383148543308 \cdot 10^{-162}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \mathbf{elif}\;y \le 1.955059603474676626176360228974583883699 \cdot 10^{-171}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \end{array}\]

Reproduce

herbie shell --seed 2019298 
(FPCore (x y)
  :name "Kahan p9 Example"
  :precision binary64
  :pre (and (< 0.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))))