Average Error: 20.6 → 5.9
Time: 15.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 r3373204 = x;
        double r3373205 = y;
        double r3373206 = r3373204 - r3373205;
        double r3373207 = r3373204 + r3373205;
        double r3373208 = r3373206 * r3373207;
        double r3373209 = r3373204 * r3373204;
        double r3373210 = r3373205 * r3373205;
        double r3373211 = r3373209 + r3373210;
        double r3373212 = r3373208 / r3373211;
        return r3373212;
}

double f(double x, double y) {
        double r3373213 = y;
        double r3373214 = -9.553125844209864e+153;
        bool r3373215 = r3373213 <= r3373214;
        double r3373216 = -1.0;
        double r3373217 = -2.262962472101785e-159;
        bool r3373218 = r3373213 <= r3373217;
        double r3373219 = x;
        double r3373220 = r3373213 + r3373219;
        double r3373221 = r3373219 - r3373213;
        double r3373222 = r3373220 * r3373221;
        double r3373223 = r3373219 * r3373219;
        double r3373224 = fma(r3373213, r3373213, r3373223);
        double r3373225 = r3373222 / r3373224;
        double r3373226 = exp(r3373225);
        double r3373227 = log(r3373226);
        double r3373228 = 4.982414937133198e-223;
        bool r3373229 = r3373213 <= r3373228;
        double r3373230 = 1.0;
        double r3373231 = 4.7445413372194213e-178;
        bool r3373232 = r3373213 <= r3373231;
        double r3373233 = r3373232 ? r3373216 : r3373227;
        double r3373234 = r3373229 ? r3373230 : r3373233;
        double r3373235 = r3373218 ? r3373227 : r3373234;
        double r3373236 = r3373215 ? r3373216 : r3373235;
        return r3373236;
}

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