Average Error: 20.5 → 5.0
Time: 25.6s
Precision: 64
\[0.0 \lt x \lt 1.0 \land y \lt 1.0\]
\[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \le -1.7747840756602994 \cdot 10^{+152}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.83405927104694 \cdot 10^{-156}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\right)\right)\\ \mathbf{elif}\;y \le 1.7320534110029677 \cdot 10^{-177}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, 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.7747840756602994 \cdot 10^{+152}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 1.7320534110029677 \cdot 10^{-177}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r3171118 = x;
        double r3171119 = y;
        double r3171120 = r3171118 - r3171119;
        double r3171121 = r3171118 + r3171119;
        double r3171122 = r3171120 * r3171121;
        double r3171123 = r3171118 * r3171118;
        double r3171124 = r3171119 * r3171119;
        double r3171125 = r3171123 + r3171124;
        double r3171126 = r3171122 / r3171125;
        return r3171126;
}

double f(double x, double y) {
        double r3171127 = y;
        double r3171128 = -1.7747840756602994e+152;
        bool r3171129 = r3171127 <= r3171128;
        double r3171130 = -1.0;
        double r3171131 = -3.83405927104694e-156;
        bool r3171132 = r3171127 <= r3171131;
        double r3171133 = x;
        double r3171134 = r3171133 - r3171127;
        double r3171135 = r3171127 + r3171133;
        double r3171136 = r3171134 * r3171135;
        double r3171137 = r3171127 * r3171127;
        double r3171138 = fma(r3171133, r3171133, r3171137);
        double r3171139 = r3171136 / r3171138;
        double r3171140 = expm1(r3171139);
        double r3171141 = log1p(r3171140);
        double r3171142 = 1.7320534110029677e-177;
        bool r3171143 = r3171127 <= r3171142;
        double r3171144 = 1.0;
        double r3171145 = r3171143 ? r3171144 : r3171141;
        double r3171146 = r3171132 ? r3171141 : r3171145;
        double r3171147 = r3171129 ? r3171130 : r3171146;
        return r3171147;
}

Error

Bits error versus x

Bits error versus y

Target

Original20.5
Target0.1
Herbie5.0
\[\begin{array}{l} \mathbf{if}\;0.5 \lt \left|\frac{x}{y}\right| \lt 2.0:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;1.0 - \frac{2.0}{1.0 + \frac{x}{y} \cdot \frac{x}{y}}\\ \end{array}\]

Derivation

  1. Split input into 3 regimes
  2. if y < -1.7747840756602994e+152

    1. Initial program 63.2

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

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

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

    if -1.7747840756602994e+152 < y < -3.83405927104694e-156 or 1.7320534110029677e-177 < y

    1. Initial program 0.9

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

      \[\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 log1p-expm1-u0.9

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

    if -3.83405927104694e-156 < y < 1.7320534110029677e-177

    1. Initial program 29.7

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.7747840756602994 \cdot 10^{+152}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.83405927104694 \cdot 10^{-156}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\right)\right)\\ \mathbf{elif}\;y \le 1.7320534110029677 \cdot 10^{-177}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\mathsf{log1p}\left(\mathsf{expm1}\left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{\mathsf{fma}\left(x, x, y \cdot y\right)}\right)\right)\\ \end{array}\]

Reproduce

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