Average Error: 20.0 → 5.2
Time: 8.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 r2672766 = x;
        double r2672767 = y;
        double r2672768 = r2672766 - r2672767;
        double r2672769 = r2672766 + r2672767;
        double r2672770 = r2672768 * r2672769;
        double r2672771 = r2672766 * r2672766;
        double r2672772 = r2672767 * r2672767;
        double r2672773 = r2672771 + r2672772;
        double r2672774 = r2672770 / r2672773;
        return r2672774;
}

double f(double x, double y) {
        double r2672775 = y;
        double r2672776 = -6.379960110729306e+150;
        bool r2672777 = r2672775 <= r2672776;
        double r2672778 = -1.0;
        double r2672779 = -1.5016765466948683e-155;
        bool r2672780 = r2672775 <= r2672779;
        double r2672781 = x;
        double r2672782 = r2672781 - r2672775;
        double r2672783 = r2672775 + r2672781;
        double r2672784 = r2672782 * r2672783;
        double r2672785 = r2672775 * r2672775;
        double r2672786 = fma(r2672781, r2672781, r2672785);
        double r2672787 = r2672784 / r2672786;
        double r2672788 = 1.6656408088991096e-162;
        bool r2672789 = r2672775 <= r2672788;
        double r2672790 = 1.0;
        double r2672791 = r2672789 ? r2672790 : r2672787;
        double r2672792 = r2672780 ? r2672787 : r2672791;
        double r2672793 = r2672777 ? r2672778 : r2672792;
        return r2672793;
}

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))))