Average Error: 19.6 → 5.5
Time: 2.4s
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.847434879443193393633321330852372755378 \cdot 10^{151}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.694063246808970668811003130641552295003 \cdot 10^{-162}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}\right)\\ \mathbf{elif}\;y \le 6.451959238219760281097758056287369172736 \cdot 10^{-208}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 1.336866892806927934140174920733392856435 \cdot 10^{-166}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot 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.847434879443193393633321330852372755378 \cdot 10^{151}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 6.451959238219760281097758056287369172736 \cdot 10^{-208}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \le 1.336866892806927934140174920733392856435 \cdot 10^{-166}:\\
\;\;\;\;-1\\

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

\end{array}
double f(double x, double y) {
        double r103457 = x;
        double r103458 = y;
        double r103459 = r103457 - r103458;
        double r103460 = r103457 + r103458;
        double r103461 = r103459 * r103460;
        double r103462 = r103457 * r103457;
        double r103463 = r103458 * r103458;
        double r103464 = r103462 + r103463;
        double r103465 = r103461 / r103464;
        return r103465;
}

double f(double x, double y) {
        double r103466 = y;
        double r103467 = -3.8474348794431934e+151;
        bool r103468 = r103466 <= r103467;
        double r103469 = -1.0;
        double r103470 = -1.6940632468089707e-162;
        bool r103471 = r103466 <= r103470;
        double r103472 = x;
        double r103473 = r103472 - r103466;
        double r103474 = r103472 + r103466;
        double r103475 = r103473 * r103474;
        double r103476 = r103472 * r103472;
        double r103477 = r103466 * r103466;
        double r103478 = r103476 + r103477;
        double r103479 = r103475 / r103478;
        double r103480 = exp(r103479);
        double r103481 = log(r103480);
        double r103482 = 6.45195923821976e-208;
        bool r103483 = r103466 <= r103482;
        double r103484 = 1.0;
        double r103485 = 1.336866892806928e-166;
        bool r103486 = r103466 <= r103485;
        double r103487 = r103486 ? r103469 : r103481;
        double r103488 = r103483 ? r103484 : r103487;
        double r103489 = r103471 ? r103481 : r103488;
        double r103490 = r103468 ? r103469 : r103489;
        return r103490;
}

Error

Bits error versus x

Bits error versus y

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original19.6
Target0.0
Herbie5.5
\[\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.8474348794431934e+151 or 6.45195923821976e-208 < y < 1.336866892806928e-166

    1. Initial program 55.7

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

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

    if -3.8474348794431934e+151 < y < -1.6940632468089707e-162 or 1.336866892806928e-166 < y

    1. Initial program 0.3

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

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

    if -1.6940632468089707e-162 < y < 6.45195923821976e-208

    1. Initial program 28.7

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

      \[\leadsto \frac{\left(x - y\right) \cdot \color{blue}{\frac{x \cdot x - y \cdot y}{x - y}}}{x \cdot x + y \cdot y}\]
    4. Applied associate-*r/41.6

      \[\leadsto \frac{\color{blue}{\frac{\left(x - y\right) \cdot \left(x \cdot x - y \cdot y\right)}{x - y}}}{x \cdot x + y \cdot y}\]
    5. Applied associate-/l/41.4

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -3.847434879443193393633321330852372755378 \cdot 10^{151}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.694063246808970668811003130641552295003 \cdot 10^{-162}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}\right)\\ \mathbf{elif}\;y \le 6.451959238219760281097758056287369172736 \cdot 10^{-208}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 1.336866892806927934140174920733392856435 \cdot 10^{-166}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}\right)\\ \end{array}\]

Reproduce

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