Average Error: 20.3 → 5.3
Time: 14.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.3379792666689724 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -6.704752375070274 \cdot 10^{-156}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\right)\\ \mathbf{elif}\;y \le 4.0567724911730275 \cdot 10^{-165}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(y, y, x \cdot x\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.3379792666689724 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -6.704752375070274 \cdot 10^{-156}:\\
\;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\right)\\

\mathbf{elif}\;y \le 4.0567724911730275 \cdot 10^{-165}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r1876831 = x;
        double r1876832 = y;
        double r1876833 = r1876831 - r1876832;
        double r1876834 = r1876831 + r1876832;
        double r1876835 = r1876833 * r1876834;
        double r1876836 = r1876831 * r1876831;
        double r1876837 = r1876832 * r1876832;
        double r1876838 = r1876836 + r1876837;
        double r1876839 = r1876835 / r1876838;
        return r1876839;
}

double f(double x, double y) {
        double r1876840 = y;
        double r1876841 = -1.3379792666689724e+154;
        bool r1876842 = r1876840 <= r1876841;
        double r1876843 = -1.0;
        double r1876844 = -6.704752375070274e-156;
        bool r1876845 = r1876840 <= r1876844;
        double r1876846 = x;
        double r1876847 = r1876840 + r1876846;
        double r1876848 = r1876846 - r1876840;
        double r1876849 = r1876847 * r1876848;
        double r1876850 = r1876846 * r1876846;
        double r1876851 = fma(r1876840, r1876840, r1876850);
        double r1876852 = r1876849 / r1876851;
        double r1876853 = exp(r1876852);
        double r1876854 = log(r1876853);
        double r1876855 = 4.0567724911730275e-165;
        bool r1876856 = r1876840 <= r1876855;
        double r1876857 = 1.0;
        double r1876858 = r1876856 ? r1876857 : r1876854;
        double r1876859 = r1876845 ? r1876854 : r1876858;
        double r1876860 = r1876842 ? r1876843 : r1876859;
        return r1876860;
}

Error

Bits error versus x

Bits error versus y

Target

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

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

    if -1.3379792666689724e+154 < y < -6.704752375070274e-156 or 4.0567724911730275e-165 < y

    1. Initial program 0.2

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

      \[\leadsto \color{blue}{\frac{\left(x + y\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\]
    3. Using strategy rm
    4. Applied add-log-exp0.2

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

    if -6.704752375070274e-156 < y < 4.0567724911730275e-165

    1. Initial program 30.5

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3379792666689724 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -6.704752375070274 \cdot 10^{-156}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\right)\\ \mathbf{elif}\;y \le 4.0567724911730275 \cdot 10^{-165}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\right)\\ \end{array}\]

Reproduce

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