Average Error: 19.5 → 4.8
Time: 10.2s
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.369677616151796 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.3534166982027225 \cdot 10^{-177}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}\\ \mathbf{elif}\;y \le -1.8586020041360913 \cdot 10^{-183}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 8.42369621266438 \cdot 10^{-175}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, \left(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.369677616151796 \cdot 10^{+154}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le -1.8586020041360913 \cdot 10^{-183}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le 8.42369621266438 \cdot 10^{-175}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r2898294 = x;
        double r2898295 = y;
        double r2898296 = r2898294 - r2898295;
        double r2898297 = r2898294 + r2898295;
        double r2898298 = r2898296 * r2898297;
        double r2898299 = r2898294 * r2898294;
        double r2898300 = r2898295 * r2898295;
        double r2898301 = r2898299 + r2898300;
        double r2898302 = r2898298 / r2898301;
        return r2898302;
}

double f(double x, double y) {
        double r2898303 = y;
        double r2898304 = -1.369677616151796e+154;
        bool r2898305 = r2898303 <= r2898304;
        double r2898306 = -1.0;
        double r2898307 = -3.3534166982027225e-177;
        bool r2898308 = r2898303 <= r2898307;
        double r2898309 = x;
        double r2898310 = r2898309 - r2898303;
        double r2898311 = r2898303 + r2898309;
        double r2898312 = r2898310 * r2898311;
        double r2898313 = r2898309 * r2898309;
        double r2898314 = fma(r2898303, r2898303, r2898313);
        double r2898315 = r2898312 / r2898314;
        double r2898316 = -1.8586020041360913e-183;
        bool r2898317 = r2898303 <= r2898316;
        double r2898318 = 8.42369621266438e-175;
        bool r2898319 = r2898303 <= r2898318;
        double r2898320 = 1.0;
        double r2898321 = r2898319 ? r2898320 : r2898315;
        double r2898322 = r2898317 ? r2898306 : r2898321;
        double r2898323 = r2898308 ? r2898315 : r2898322;
        double r2898324 = r2898305 ? r2898306 : r2898323;
        return r2898324;
}

Error

Bits error versus x

Bits error versus y

Target

Original19.5
Target0.0
Herbie4.8
\[\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.369677616151796e+154 or -3.3534166982027225e-177 < y < -1.8586020041360913e-183

    1. Initial program 61.9

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

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

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

    if -1.369677616151796e+154 < y < -3.3534166982027225e-177 or 8.42369621266438e-175 < y

    1. Initial program 1.6

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

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

    if -1.8586020041360913e-183 < y < 8.42369621266438e-175

    1. Initial program 28.9

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.369677616151796 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.3534166982027225 \cdot 10^{-177}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}\\ \mathbf{elif}\;y \le -1.8586020041360913 \cdot 10^{-183}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 8.42369621266438 \cdot 10^{-175}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, \left(x \cdot x\right)\right)}\\ \end{array}\]

Reproduce

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