Average Error: 20.1 → 4.9
Time: 17.0s
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 -5.797856225877881060769082412965027708037 \cdot 10^{150}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.22186147190904949709756605256725936551 \cdot 10^{-158}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot y + \left(x - y\right) \cdot x}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{elif}\;y \le 7.961862811311691246218405838467989119993 \cdot 10^{-164}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot y + \left(x - y\right) \cdot x}{\mathsf{fma}\left(x, x, y \cdot y\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 -5.797856225877881060769082412965027708037 \cdot 10^{150}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -1.22186147190904949709756605256725936551 \cdot 10^{-158}:\\
\;\;\;\;\frac{\left(x - y\right) \cdot y + \left(x - y\right) \cdot x}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\

\mathbf{elif}\;y \le 7.961862811311691246218405838467989119993 \cdot 10^{-164}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r2758931 = x;
        double r2758932 = y;
        double r2758933 = r2758931 - r2758932;
        double r2758934 = r2758931 + r2758932;
        double r2758935 = r2758933 * r2758934;
        double r2758936 = r2758931 * r2758931;
        double r2758937 = r2758932 * r2758932;
        double r2758938 = r2758936 + r2758937;
        double r2758939 = r2758935 / r2758938;
        return r2758939;
}

double f(double x, double y) {
        double r2758940 = y;
        double r2758941 = -5.797856225877881e+150;
        bool r2758942 = r2758940 <= r2758941;
        double r2758943 = -1.0;
        double r2758944 = -1.2218614719090495e-158;
        bool r2758945 = r2758940 <= r2758944;
        double r2758946 = x;
        double r2758947 = r2758946 - r2758940;
        double r2758948 = r2758947 * r2758940;
        double r2758949 = r2758947 * r2758946;
        double r2758950 = r2758948 + r2758949;
        double r2758951 = r2758940 * r2758940;
        double r2758952 = fma(r2758946, r2758946, r2758951);
        double r2758953 = r2758950 / r2758952;
        double r2758954 = 7.961862811311691e-164;
        bool r2758955 = r2758940 <= r2758954;
        double r2758956 = 1.0;
        double r2758957 = r2758955 ? r2758956 : r2758953;
        double r2758958 = r2758945 ? r2758953 : r2758957;
        double r2758959 = r2758942 ? r2758943 : r2758958;
        return r2758959;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.1
Target0.0
Herbie4.9
\[\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 < -5.797856225877881e+150

    1. Initial program 62.3

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

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

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

    if -5.797856225877881e+150 < y < -1.2218614719090495e-158 or 7.961862811311691e-164 < 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)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\]
    3. Using strategy rm
    4. Applied distribute-rgt-in0.1

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

    if -1.2218614719090495e-158 < y < 7.961862811311691e-164

    1. Initial program 29.2

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -5.797856225877881060769082412965027708037 \cdot 10^{150}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.22186147190904949709756605256725936551 \cdot 10^{-158}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot y + \left(x - y\right) \cdot x}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{elif}\;y \le 7.961862811311691246218405838467989119993 \cdot 10^{-164}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot y + \left(x - y\right) \cdot x}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \end{array}\]

Reproduce

herbie shell --seed 2019174 +o rules:numerics
(FPCore (x y)
  :name "Kahan p9 Example"
  :pre (and (< 0.0 x 1.0) (< y 1.0))

  :herbie-target
  (if (< 0.5 (fabs (/ x y)) 2.0) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1.0 (/ 2.0 (+ 1.0 (* (/ x y) (/ x y))))))

  (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))