Average Error: 20.3 → 5.4
Time: 1.1m
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.1564294007415584 \cdot 10^{+23}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -4.9781449920664 \cdot 10^{-177}:\\ \;\;\;\;\log \left(e^{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}{y + x}}}\right)\\ \mathbf{elif}\;y \le 1.0919689144745608 \cdot 10^{-199}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}{y + 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.1564294007415584 \cdot 10^{+23}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 1.0919689144745608 \cdot 10^{-199}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r19187147 = x;
        double r19187148 = y;
        double r19187149 = r19187147 - r19187148;
        double r19187150 = r19187147 + r19187148;
        double r19187151 = r19187149 * r19187150;
        double r19187152 = r19187147 * r19187147;
        double r19187153 = r19187148 * r19187148;
        double r19187154 = r19187152 + r19187153;
        double r19187155 = r19187151 / r19187154;
        return r19187155;
}

double f(double x, double y) {
        double r19187156 = y;
        double r19187157 = -1.1564294007415584e+23;
        bool r19187158 = r19187156 <= r19187157;
        double r19187159 = -1.0;
        double r19187160 = -4.9781449920664e-177;
        bool r19187161 = r19187156 <= r19187160;
        double r19187162 = x;
        double r19187163 = r19187162 - r19187156;
        double r19187164 = r19187156 * r19187156;
        double r19187165 = fma(r19187162, r19187162, r19187164);
        double r19187166 = r19187156 + r19187162;
        double r19187167 = r19187165 / r19187166;
        double r19187168 = r19187163 / r19187167;
        double r19187169 = exp(r19187168);
        double r19187170 = log(r19187169);
        double r19187171 = 1.0919689144745608e-199;
        bool r19187172 = r19187156 <= r19187171;
        double r19187173 = 1.0;
        double r19187174 = r19187172 ? r19187173 : r19187170;
        double r19187175 = r19187161 ? r19187170 : r19187174;
        double r19187176 = r19187158 ? r19187159 : r19187175;
        return r19187176;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.3
Target0.1
Herbie5.4
\[\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.1564294007415584e+23

    1. Initial program 35.3

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

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

      \[\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)}\]
    5. Using strategy rm
    6. Applied associate-/l*34.5

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

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

    if -1.1564294007415584e+23 < y < -4.9781449920664e-177 or 1.0919689144745608e-199 < y

    1. Initial program 4.2

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

      \[\leadsto \color{blue}{\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}}\]
    3. Using strategy rm
    4. Applied add-log-exp4.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)}\]
    5. Using strategy rm
    6. Applied associate-/l*4.7

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

    if -4.9781449920664e-177 < y < 1.0919689144745608e-199

    1. Initial program 30.0

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.1564294007415584 \cdot 10^{+23}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -4.9781449920664 \cdot 10^{-177}:\\ \;\;\;\;\log \left(e^{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}{y + x}}}\right)\\ \mathbf{elif}\;y \le 1.0919689144745608 \cdot 10^{-199}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{x - y}{\frac{\mathsf{fma}\left(x, x, \left(y \cdot y\right)\right)}{y + x}}}\right)\\ \end{array}\]

Reproduce

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