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

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

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

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

\end{array}
double f(double x, double y) {
        double r90644 = x;
        double r90645 = y;
        double r90646 = r90644 - r90645;
        double r90647 = r90644 + r90645;
        double r90648 = r90646 * r90647;
        double r90649 = r90644 * r90644;
        double r90650 = r90645 * r90645;
        double r90651 = r90649 + r90650;
        double r90652 = r90648 / r90651;
        return r90652;
}

double f(double x, double y) {
        double r90653 = y;
        double r90654 = -3.8474348794431934e+151;
        bool r90655 = r90653 <= r90654;
        double r90656 = -1.0;
        double r90657 = -1.6940632468089707e-162;
        bool r90658 = r90653 <= r90657;
        double r90659 = x;
        double r90660 = r90659 - r90653;
        double r90661 = r90659 + r90653;
        double r90662 = r90660 * r90661;
        double r90663 = r90659 * r90659;
        double r90664 = r90653 * r90653;
        double r90665 = r90663 + r90664;
        double r90666 = r90662 / r90665;
        double r90667 = log1p(r90666);
        double r90668 = exp(r90667);
        double r90669 = 1.0;
        double r90670 = r90668 - r90669;
        double r90671 = 6.45195923821976e-208;
        bool r90672 = r90653 <= r90671;
        double r90673 = 1.336866892806928e-166;
        bool r90674 = r90653 <= r90673;
        double r90675 = r90674 ? r90656 : r90670;
        double r90676 = r90672 ? r90669 : r90675;
        double r90677 = r90658 ? r90670 : r90676;
        double r90678 = r90655 ? r90656 : r90677;
        return r90678;
}

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 log1p-expm1-u0.3

      \[\leadsto \color{blue}{\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)\right)}\]
    4. Using strategy rm
    5. Applied expm1-log1p-u0.3

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)\right)\right)\right)}\]
    6. Simplified0.3

      \[\leadsto \mathsf{expm1}\left(\color{blue}{\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)}\right)\]
    7. Using strategy rm
    8. Applied expm1-udef0.3

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

    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. 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}:\\ \;\;\;\;e^{\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)} - 1\\ \mathbf{elif}\;y \le 6.451959238219760281097758056287369172736 \cdot 10^{-208}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 1.336866892806927934140174920733392856435 \cdot 10^{-166}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;e^{\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)} - 1\\ \end{array}\]

Reproduce

herbie shell --seed 2019353 +o rules:numerics
(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))))