Average Error: 19.7 → 5.0
Time: 13.2s
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 -1.576060656443760050067630439871422555514 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -7.512106064144896311879552836863591950545 \cdot 10^{-163}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}\\ \mathbf{elif}\;y \le -9.739334495050348959786376614389218616652 \cdot 10^{-187}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 6.255179780863846976704429693615073578173 \cdot 10^{-169}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, x \cdot x\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.576060656443760050067630439871422555514 \cdot 10^{153}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le -9.739334495050348959786376614389218616652 \cdot 10^{-187}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le 6.255179780863846976704429693615073578173 \cdot 10^{-169}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r3877746 = x;
        double r3877747 = y;
        double r3877748 = r3877746 - r3877747;
        double r3877749 = r3877746 + r3877747;
        double r3877750 = r3877748 * r3877749;
        double r3877751 = r3877746 * r3877746;
        double r3877752 = r3877747 * r3877747;
        double r3877753 = r3877751 + r3877752;
        double r3877754 = r3877750 / r3877753;
        return r3877754;
}

double f(double x, double y) {
        double r3877755 = y;
        double r3877756 = -1.57606065644376e+153;
        bool r3877757 = r3877755 <= r3877756;
        double r3877758 = -1.0;
        double r3877759 = -7.512106064144896e-163;
        bool r3877760 = r3877755 <= r3877759;
        double r3877761 = x;
        double r3877762 = r3877761 - r3877755;
        double r3877763 = r3877755 + r3877761;
        double r3877764 = r3877762 * r3877763;
        double r3877765 = r3877755 * r3877755;
        double r3877766 = fma(r3877761, r3877761, r3877765);
        double r3877767 = sqrt(r3877766);
        double r3877768 = r3877767 * r3877767;
        double r3877769 = r3877764 / r3877768;
        double r3877770 = -9.739334495050349e-187;
        bool r3877771 = r3877755 <= r3877770;
        double r3877772 = 6.255179780863847e-169;
        bool r3877773 = r3877755 <= r3877772;
        double r3877774 = 1.0;
        double r3877775 = r3877761 * r3877761;
        double r3877776 = fma(r3877755, r3877755, r3877775);
        double r3877777 = r3877764 / r3877776;
        double r3877778 = r3877773 ? r3877774 : r3877777;
        double r3877779 = r3877771 ? r3877758 : r3877778;
        double r3877780 = r3877760 ? r3877769 : r3877779;
        double r3877781 = r3877757 ? r3877758 : r3877780;
        return r3877781;
}

Error

Bits error versus x

Bits error versus y

Target

Original19.7
Target0.1
Herbie5.0
\[\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 4 regimes
  2. if y < -1.57606065644376e+153 or -7.512106064144896e-163 < y < -9.739334495050349e-187

    1. Initial program 59.2

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified59.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 0 4.6

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

    if -1.57606065644376e+153 < y < -7.512106064144896e-163

    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 add-sqr-sqrt0.1

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

    if -9.739334495050349e-187 < y < 6.255179780863847e-169

    1. Initial program 28.6

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

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

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

    if 6.255179780863847e-169 < y

    1. Initial program 1.5

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

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

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{{y}^{2} + {x}^{2}}}\]
    4. Simplified1.5

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification5.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.576060656443760050067630439871422555514 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -7.512106064144896311879552836863591950545 \cdot 10^{-163}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)} \cdot \sqrt{\mathsf{fma}\left(x, x, y \cdot y\right)}}\\ \mathbf{elif}\;y \le -9.739334495050348959786376614389218616652 \cdot 10^{-187}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 6.255179780863846976704429693615073578173 \cdot 10^{-169}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}\\ \end{array}\]

Reproduce

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