Average Error: 20.0 → 5.2
Time: 9.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 -6.379960110729305952978237513299639776117 \cdot 10^{150}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.501676546694868344414885960149825649924 \cdot 10^{-155}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{elif}\;y \le 1.665640808899109568529030107036989846971 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\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 -6.379960110729305952978237513299639776117 \cdot 10^{150}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 1.665640808899109568529030107036989846971 \cdot 10^{-162}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r2469358 = x;
        double r2469359 = y;
        double r2469360 = r2469358 - r2469359;
        double r2469361 = r2469358 + r2469359;
        double r2469362 = r2469360 * r2469361;
        double r2469363 = r2469358 * r2469358;
        double r2469364 = r2469359 * r2469359;
        double r2469365 = r2469363 + r2469364;
        double r2469366 = r2469362 / r2469365;
        return r2469366;
}

double f(double x, double y) {
        double r2469367 = y;
        double r2469368 = -6.379960110729306e+150;
        bool r2469369 = r2469367 <= r2469368;
        double r2469370 = -1.0;
        double r2469371 = -1.5016765466948683e-155;
        bool r2469372 = r2469367 <= r2469371;
        double r2469373 = x;
        double r2469374 = r2469373 - r2469367;
        double r2469375 = r2469367 + r2469373;
        double r2469376 = r2469374 * r2469375;
        double r2469377 = r2469367 * r2469367;
        double r2469378 = fma(r2469373, r2469373, r2469377);
        double r2469379 = r2469376 / r2469378;
        double r2469380 = 1.6656408088991096e-162;
        bool r2469381 = r2469367 <= r2469380;
        double r2469382 = 1.0;
        double r2469383 = r2469381 ? r2469382 : r2469379;
        double r2469384 = r2469372 ? r2469379 : r2469383;
        double r2469385 = r2469369 ? r2469370 : r2469384;
        return r2469385;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.0
Target0.0
Herbie5.2
\[\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 < -6.379960110729306e+150

    1. Initial program 62.9

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

      \[\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 -6.379960110729306e+150 < y < -1.5016765466948683e-155 or 1.6656408088991096e-162 < y

    1. Initial program 0.0

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

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

    if -1.5016765466948683e-155 < y < 1.6656408088991096e-162

    1. Initial program 29.7

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

      \[\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 16.4

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -6.379960110729305952978237513299639776117 \cdot 10^{150}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.501676546694868344414885960149825649924 \cdot 10^{-155}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \mathbf{elif}\;y \le 1.665640808899109568529030107036989846971 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\\ \end{array}\]

Reproduce

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