Average Error: 20.5 → 5.3
Time: 2.2s
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.2350588066957499 \cdot 10^{153}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(-1\right)\right)\\ \mathbf{elif}\;y \le -7.33106158512129338 \cdot 10^{-158}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)\right)\\ \mathbf{elif}\;y \le 1.3645633303893502 \cdot 10^{-189}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)\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.2350588066957499 \cdot 10^{153}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(-1\right)\right)\\

\mathbf{elif}\;y \le -7.33106158512129338 \cdot 10^{-158}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)\right)\\

\mathbf{elif}\;y \le 1.3645633303893502 \cdot 10^{-189}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r68568 = x;
        double r68569 = y;
        double r68570 = r68568 - r68569;
        double r68571 = r68568 + r68569;
        double r68572 = r68570 * r68571;
        double r68573 = r68568 * r68568;
        double r68574 = r68569 * r68569;
        double r68575 = r68573 + r68574;
        double r68576 = r68572 / r68575;
        return r68576;
}

double f(double x, double y) {
        double r68577 = y;
        double r68578 = -3.23505880669575e+153;
        bool r68579 = r68577 <= r68578;
        double r68580 = -1.0;
        double r68581 = log1p(r68580);
        double r68582 = expm1(r68581);
        double r68583 = -7.331061585121293e-158;
        bool r68584 = r68577 <= r68583;
        double r68585 = x;
        double r68586 = r68585 - r68577;
        double r68587 = r68585 + r68577;
        double r68588 = r68586 * r68587;
        double r68589 = r68585 * r68585;
        double r68590 = r68577 * r68577;
        double r68591 = r68589 + r68590;
        double r68592 = r68588 / r68591;
        double r68593 = log1p(r68592);
        double r68594 = expm1(r68593);
        double r68595 = 1.3645633303893502e-189;
        bool r68596 = r68577 <= r68595;
        double r68597 = 1.0;
        double r68598 = r68596 ? r68597 : r68594;
        double r68599 = r68584 ? r68594 : r68598;
        double r68600 = r68579 ? r68582 : r68599;
        return r68600;
}

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.5
Target0.1
Herbie5.3
\[\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.23505880669575e+153

    1. Initial program 63.5

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied expm1-log1p-u63.5

      \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)\right)}\]
    4. Taylor expanded around 0 0

      \[\leadsto \mathsf{expm1}\left(\mathsf{log1p}\left(\color{blue}{-1}\right)\right)\]

    if -3.23505880669575e+153 < y < -7.331061585121293e-158 or 1.3645633303893502e-189 < y

    1. Initial program 1.7

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied expm1-log1p-u1.7

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

    if -7.331061585121293e-158 < y < 1.3645633303893502e-189

    1. Initial program 30.4

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -3.2350588066957499 \cdot 10^{153}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(-1\right)\right)\\ \mathbf{elif}\;y \le -7.33106158512129338 \cdot 10^{-158}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)\right)\\ \mathbf{elif}\;y \le 1.3645633303893502 \cdot 10^{-189}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right)\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2020083 +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))))