Average Error: 20.6 → 5.9
Time: 16.7s
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 -9.553125844209864019349205574997414631362 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.262962472101785089653746326381466961868 \cdot 10^{-159}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\right)\\ \mathbf{elif}\;y \le 4.982414937133197896822437436552107699435 \cdot 10^{-223}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 4.744541337219421296708284469703238777066 \cdot 10^{-178}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(y, y, 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 -9.553125844209864019349205574997414631362 \cdot 10^{153}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 4.982414937133197896822437436552107699435 \cdot 10^{-223}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \le 4.744541337219421296708284469703238777066 \cdot 10^{-178}:\\
\;\;\;\;-1\\

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

\end{array}
double f(double x, double y) {
        double r5043021 = x;
        double r5043022 = y;
        double r5043023 = r5043021 - r5043022;
        double r5043024 = r5043021 + r5043022;
        double r5043025 = r5043023 * r5043024;
        double r5043026 = r5043021 * r5043021;
        double r5043027 = r5043022 * r5043022;
        double r5043028 = r5043026 + r5043027;
        double r5043029 = r5043025 / r5043028;
        return r5043029;
}

double f(double x, double y) {
        double r5043030 = y;
        double r5043031 = -9.553125844209864e+153;
        bool r5043032 = r5043030 <= r5043031;
        double r5043033 = -1.0;
        double r5043034 = -2.262962472101785e-159;
        bool r5043035 = r5043030 <= r5043034;
        double r5043036 = x;
        double r5043037 = r5043030 + r5043036;
        double r5043038 = r5043036 - r5043030;
        double r5043039 = r5043037 * r5043038;
        double r5043040 = r5043036 * r5043036;
        double r5043041 = fma(r5043030, r5043030, r5043040);
        double r5043042 = r5043039 / r5043041;
        double r5043043 = exp(r5043042);
        double r5043044 = log(r5043043);
        double r5043045 = 4.982414937133198e-223;
        bool r5043046 = r5043030 <= r5043045;
        double r5043047 = 1.0;
        double r5043048 = 4.7445413372194213e-178;
        bool r5043049 = r5043030 <= r5043048;
        double r5043050 = r5043049 ? r5043033 : r5043044;
        double r5043051 = r5043046 ? r5043047 : r5043050;
        double r5043052 = r5043035 ? r5043044 : r5043051;
        double r5043053 = r5043032 ? r5043033 : r5043052;
        return r5043053;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.6
Target0.1
Herbie5.9
\[\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 < -9.553125844209864e+153 or 4.982414937133198e-223 < y < 4.7445413372194213e-178

    1. Initial program 57.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Taylor expanded around 0 8.7

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

    if -9.553125844209864e+153 < y < -2.262962472101785e-159 or 4.7445413372194213e-178 < y

    1. Initial program 1.0

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied add-log-exp1.0

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

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

    if -2.262962472101785e-159 < y < 4.982414937133198e-223

    1. Initial program 30.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Taylor expanded around inf 13.7

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -9.553125844209864019349205574997414631362 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -2.262962472101785089653746326381466961868 \cdot 10^{-159}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\right)\\ \mathbf{elif}\;y \le 4.982414937133197896822437436552107699435 \cdot 10^{-223}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 4.744541337219421296708284469703238777066 \cdot 10^{-178}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(y + x\right) \cdot \left(x - y\right)}{\mathsf{fma}\left(y, y, x \cdot x\right)}}\right)\\ \end{array}\]

Reproduce

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