Average Error: 20.2 → 5.1
Time: 13.3s
Precision: 64
\[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 -1.3740657909060598 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.2764103459019323 \cdot 10^{-152}:\\ \;\;\;\;\log_* (1 + (e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}} - 1)^*)\\ \mathbf{elif}\;y \le 7.647705034433286 \cdot 10^{-163}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log_* (1 + (e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(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 -1.3740657909060598 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -3.2764103459019323 \cdot 10^{-152}:\\
\;\;\;\;\log_* (1 + (e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}} - 1)^*)\\

\mathbf{elif}\;y \le 7.647705034433286 \cdot 10^{-163}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r3007611 = x;
        double r3007612 = y;
        double r3007613 = r3007611 - r3007612;
        double r3007614 = r3007611 + r3007612;
        double r3007615 = r3007613 * r3007614;
        double r3007616 = r3007611 * r3007611;
        double r3007617 = r3007612 * r3007612;
        double r3007618 = r3007616 + r3007617;
        double r3007619 = r3007615 / r3007618;
        return r3007619;
}

double f(double x, double y) {
        double r3007620 = y;
        double r3007621 = -1.3740657909060598e+154;
        bool r3007622 = r3007620 <= r3007621;
        double r3007623 = -1.0;
        double r3007624 = -3.2764103459019323e-152;
        bool r3007625 = r3007620 <= r3007624;
        double r3007626 = x;
        double r3007627 = r3007626 - r3007620;
        double r3007628 = r3007620 + r3007626;
        double r3007629 = r3007627 * r3007628;
        double r3007630 = r3007620 * r3007620;
        double r3007631 = fma(r3007626, r3007626, r3007630);
        double r3007632 = r3007629 / r3007631;
        double r3007633 = expm1(r3007632);
        double r3007634 = log1p(r3007633);
        double r3007635 = 7.647705034433286e-163;
        bool r3007636 = r3007620 <= r3007635;
        double r3007637 = 1.0;
        double r3007638 = r3007636 ? r3007637 : r3007634;
        double r3007639 = r3007625 ? r3007634 : r3007638;
        double r3007640 = r3007622 ? r3007623 : r3007639;
        return r3007640;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.2
Target0.1
Herbie5.1
\[\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 < -1.3740657909060598e+154

    1. Initial program 63.6

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified63.6

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}}\]
    3. Using strategy rm
    4. Applied log1p-expm1-u63.6

      \[\leadsto \color{blue}{\log_* (1 + (e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}} - 1)^*)}\]
    5. Taylor expanded around 0 0

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

    if -1.3740657909060598e+154 < y < -3.2764103459019323e-152 or 7.647705034433286e-163 < y

    1. Initial program 0.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified0.1

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

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

    if -3.2764103459019323e-152 < y < 7.647705034433286e-163

    1. Initial program 29.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified29.1

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}}\]
    3. Using strategy rm
    4. Applied log1p-expm1-u29.1

      \[\leadsto \color{blue}{\log_* (1 + (e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}} - 1)^*)}\]
    5. Taylor expanded around -inf 15.9

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3740657909060598 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.2764103459019323 \cdot 10^{-152}:\\ \;\;\;\;\log_* (1 + (e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}} - 1)^*)\\ \mathbf{elif}\;y \le 7.647705034433286 \cdot 10^{-163}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log_* (1 + (e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{(x \cdot x + \left(y \cdot y\right))_*}} - 1)^*)\\ \end{array}\]

Reproduce

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