Average Error: 20.3 → 5.3
Time: 14.0s
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.3298027661280014 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.7007429044399855 \cdot 10^{-160}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}\right)\right)\\ \mathbf{elif}\;y \le 8.21231542495005 \cdot 10^{-173}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}\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 -1.3298027661280014 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -2.7007429044399855 \cdot 10^{-160}:\\
\;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}\right)\right)\\

\mathbf{elif}\;y \le 8.21231542495005 \cdot 10^{-173}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r2517624 = x;
        double r2517625 = y;
        double r2517626 = r2517624 - r2517625;
        double r2517627 = r2517624 + r2517625;
        double r2517628 = r2517626 * r2517627;
        double r2517629 = r2517624 * r2517624;
        double r2517630 = r2517625 * r2517625;
        double r2517631 = r2517629 + r2517630;
        double r2517632 = r2517628 / r2517631;
        return r2517632;
}

double f(double x, double y) {
        double r2517633 = y;
        double r2517634 = -1.3298027661280014e+154;
        bool r2517635 = r2517633 <= r2517634;
        double r2517636 = -1.0;
        double r2517637 = -2.7007429044399855e-160;
        bool r2517638 = r2517633 <= r2517637;
        double r2517639 = x;
        double r2517640 = r2517639 - r2517633;
        double r2517641 = r2517633 + r2517639;
        double r2517642 = r2517640 * r2517641;
        double r2517643 = r2517639 * r2517639;
        double r2517644 = fma(r2517633, r2517633, r2517643);
        double r2517645 = r2517642 / r2517644;
        double r2517646 = log1p(r2517645);
        double r2517647 = expm1(r2517646);
        double r2517648 = 8.21231542495005e-173;
        bool r2517649 = r2517633 <= r2517648;
        double r2517650 = 1.0;
        double r2517651 = r2517649 ? r2517650 : r2517647;
        double r2517652 = r2517638 ? r2517647 : r2517651;
        double r2517653 = r2517635 ? r2517636 : r2517652;
        return r2517653;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.3
Target0.0
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 < -1.3298027661280014e+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)}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\]
    3. Taylor expanded around 0 0

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

    if -1.3298027661280014e+154 < y < -2.7007429044399855e-160 or 8.21231542495005e-173 < y

    1. Initial program 0.7

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

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

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

    if -2.7007429044399855e-160 < y < 8.21231542495005e-173

    1. Initial program 30.1

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

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\]
    3. Taylor expanded around inf 16.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3298027661280014 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.7007429044399855 \cdot 10^{-160}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}\right)\right)\\ \mathbf{elif}\;y \le 8.21231542495005 \cdot 10^{-173}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{expm1}\left(\mathsf{log1p}\left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}\right)\right)\\ \end{array}\]

Reproduce

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