Average Error: 20.2 → 5.1
Time: 15.5s
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.3434000592863123 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -8.556032821094931 \cdot 10^{-156}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}}\right)\\ \mathbf{elif}\;y \le 2.1804237440776019 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\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.3434000592863123 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -8.556032821094931 \cdot 10^{-156}:\\
\;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}}\right)\\

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

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

\end{array}
double f(double x, double y) {
        double r2065801 = x;
        double r2065802 = y;
        double r2065803 = r2065801 - r2065802;
        double r2065804 = r2065801 + r2065802;
        double r2065805 = r2065803 * r2065804;
        double r2065806 = r2065801 * r2065801;
        double r2065807 = r2065802 * r2065802;
        double r2065808 = r2065806 + r2065807;
        double r2065809 = r2065805 / r2065808;
        return r2065809;
}

double f(double x, double y) {
        double r2065810 = y;
        double r2065811 = -1.3434000592863123e+154;
        bool r2065812 = r2065810 <= r2065811;
        double r2065813 = -1.0;
        double r2065814 = -8.556032821094931e-156;
        bool r2065815 = r2065810 <= r2065814;
        double r2065816 = x;
        double r2065817 = r2065816 - r2065810;
        double r2065818 = r2065810 + r2065816;
        double r2065819 = r2065817 * r2065818;
        double r2065820 = r2065810 * r2065810;
        double r2065821 = fma(r2065816, r2065816, r2065820);
        double r2065822 = r2065819 / r2065821;
        double r2065823 = exp(r2065822);
        double r2065824 = log(r2065823);
        double r2065825 = 2.1804237440776019e-162;
        bool r2065826 = r2065810 <= r2065825;
        double r2065827 = 1.0;
        double r2065828 = r2065826 ? r2065827 : r2065824;
        double r2065829 = r2065815 ? r2065824 : r2065828;
        double r2065830 = r2065812 ? r2065813 : r2065829;
        return r2065830;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.2
Target0.0
Herbie5.1
\[\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.3434000592863123e+154

    1. Initial program 63.6

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified63.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)}}\]
    3. Taylor expanded around 0 63.6

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

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}}\]
    5. Using strategy rm
    6. Applied add-log-exp63.6

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

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

    if -1.3434000592863123e+154 < y < -8.556032821094931e-156 or 2.1804237440776019e-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(y, y, \left(x \cdot x\right)\right)}}\]
    3. Taylor expanded around 0 0.0

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

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}}\]
    5. Using strategy rm
    6. Applied add-log-exp0.0

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

    if -8.556032821094931e-156 < y < 2.1804237440776019e-162

    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(y, y, \left(x \cdot x\right)\right)}}\]
    3. Taylor expanded around 0 29.2

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

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}}\]
    5. Using strategy rm
    6. Applied add-log-exp29.2

      \[\leadsto \color{blue}{\log \left(e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}}\right)}\]
    7. Taylor expanded around inf 15.7

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.3434000592863123 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -8.556032821094931 \cdot 10^{-156}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}}\right)\\ \mathbf{elif}\;y \le 2.1804237440776019 \cdot 10^{-162}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}}\right)\\ \end{array}\]

Reproduce

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