Average Error: 20.3 → 5.3
Time: 10.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.3379792666689724 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -6.704752375070274 \cdot 10^{-156}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\right)\\ \mathbf{elif}\;y \le 4.0567724911730275 \cdot 10^{-165}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\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.3379792666689724 \cdot 10^{+154}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 4.0567724911730275 \cdot 10^{-165}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r1549051 = x;
        double r1549052 = y;
        double r1549053 = r1549051 - r1549052;
        double r1549054 = r1549051 + r1549052;
        double r1549055 = r1549053 * r1549054;
        double r1549056 = r1549051 * r1549051;
        double r1549057 = r1549052 * r1549052;
        double r1549058 = r1549056 + r1549057;
        double r1549059 = r1549055 / r1549058;
        return r1549059;
}

double f(double x, double y) {
        double r1549060 = y;
        double r1549061 = -1.3379792666689724e+154;
        bool r1549062 = r1549060 <= r1549061;
        double r1549063 = -1.0;
        double r1549064 = -6.704752375070274e-156;
        bool r1549065 = r1549060 <= r1549064;
        double r1549066 = x;
        double r1549067 = r1549066 - r1549060;
        double r1549068 = r1549060 + r1549066;
        double r1549069 = r1549067 * r1549068;
        double r1549070 = r1549060 * r1549060;
        double r1549071 = fma(r1549066, r1549066, r1549070);
        double r1549072 = r1549069 / r1549071;
        double r1549073 = exp(r1549072);
        double r1549074 = log(r1549073);
        double r1549075 = 4.0567724911730275e-165;
        bool r1549076 = r1549060 <= r1549075;
        double r1549077 = 1.0;
        double r1549078 = r1549076 ? r1549077 : r1549074;
        double r1549079 = r1549065 ? r1549074 : r1549078;
        double r1549080 = r1549062 ? r1549063 : r1549079;
        return r1549080;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.3
Target0.1
Herbie5.3
\[\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.3379792666689724e+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(x, x, y \cdot y\right)}}\]
    3. Using strategy rm
    4. Applied *-un-lft-identity63.6

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{1 \cdot \mathsf{fma}\left(x, x, y \cdot y\right)}}\]
    5. Applied associate-/r*63.6

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

      \[\leadsto \frac{\color{blue}{\left(x - y\right) \cdot \left(y + x\right)}}{\mathsf{fma}\left(x, x, y \cdot y\right)}\]
    7. Using strategy rm
    8. 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, y \cdot y\right)}}\right)}\]
    9. Taylor expanded around 0 0

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

    if -1.3379792666689724e+154 < y < -6.704752375070274e-156 or 4.0567724911730275e-165 < y

    1. Initial program 0.2

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

      \[\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 *-un-lft-identity0.2

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{1 \cdot \mathsf{fma}\left(x, x, y \cdot y\right)}}\]
    5. Applied associate-/r*0.2

      \[\leadsto \color{blue}{\frac{\frac{\left(x - y\right) \cdot \left(y + x\right)}{1}}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\]
    6. Simplified0.2

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

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

    if -6.704752375070274e-156 < y < 4.0567724911730275e-165

    1. Initial program 30.5

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

      \[\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 *-un-lft-identity30.5

      \[\leadsto \frac{\left(x - y\right) \cdot \left(y + x\right)}{\color{blue}{1 \cdot \mathsf{fma}\left(x, x, y \cdot y\right)}}\]
    5. Applied associate-/r*30.5

      \[\leadsto \color{blue}{\frac{\frac{\left(x - y\right) \cdot \left(y + x\right)}{1}}{\mathsf{fma}\left(x, x, y \cdot y\right)}}\]
    6. Simplified30.5

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

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

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

Reproduce

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