Average Error: 20.6 → 5.3
Time: 2.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 -2.0372639617288886 \cdot 10^{151}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.3928899832237578 \cdot 10^{-156}:\\ \;\;\;\;\frac{x \cdot x - y \cdot y}{\sqrt{x \cdot x + y \cdot y} \cdot \left(x + y\right)} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\\ \mathbf{elif}\;y \le 9.3207497238124358 \cdot 10^{-170}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}\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 -2.0372639617288886 \cdot 10^{151}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -3.3928899832237578 \cdot 10^{-156}:\\
\;\;\;\;\frac{x \cdot x - y \cdot y}{\sqrt{x \cdot x + y \cdot y} \cdot \left(x + y\right)} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\\

\mathbf{elif}\;y \le 9.3207497238124358 \cdot 10^{-170}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r93038 = x;
        double r93039 = y;
        double r93040 = r93038 - r93039;
        double r93041 = r93038 + r93039;
        double r93042 = r93040 * r93041;
        double r93043 = r93038 * r93038;
        double r93044 = r93039 * r93039;
        double r93045 = r93043 + r93044;
        double r93046 = r93042 / r93045;
        return r93046;
}

double f(double x, double y) {
        double r93047 = y;
        double r93048 = -2.0372639617288886e+151;
        bool r93049 = r93047 <= r93048;
        double r93050 = -1.0;
        double r93051 = -3.392889983223758e-156;
        bool r93052 = r93047 <= r93051;
        double r93053 = x;
        double r93054 = r93053 * r93053;
        double r93055 = r93047 * r93047;
        double r93056 = r93054 - r93055;
        double r93057 = r93054 + r93055;
        double r93058 = sqrt(r93057);
        double r93059 = r93053 + r93047;
        double r93060 = r93058 * r93059;
        double r93061 = r93056 / r93060;
        double r93062 = r93059 / r93058;
        double r93063 = r93061 * r93062;
        double r93064 = 9.320749723812436e-170;
        bool r93065 = r93047 <= r93064;
        double r93066 = 1.0;
        double r93067 = r93053 - r93047;
        double r93068 = r93067 * r93059;
        double r93069 = r93068 / r93057;
        double r93070 = exp(r93069);
        double r93071 = log(r93070);
        double r93072 = r93065 ? r93066 : r93071;
        double r93073 = r93052 ? r93063 : r93072;
        double r93074 = r93049 ? r93050 : r93073;
        return r93074;
}

Error

Bits error versus x

Bits error versus y

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original20.6
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 4 regimes
  2. if y < -2.0372639617288886e+151

    1. Initial program 63.2

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

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

    if -2.0372639617288886e+151 < y < -3.392889983223758e-156

    1. Initial program 0.0

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

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}}\]
    4. Applied times-frac0.0

      \[\leadsto \color{blue}{\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}}\]
    5. Using strategy rm
    6. Applied flip--0.1

      \[\leadsto \frac{\color{blue}{\frac{x \cdot x - y \cdot y}{x + y}}}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\]
    7. Applied associate-/l/0.0

      \[\leadsto \color{blue}{\frac{x \cdot x - y \cdot y}{\sqrt{x \cdot x + y \cdot y} \cdot \left(x + y\right)}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\]

    if -3.392889983223758e-156 < y < 9.320749723812436e-170

    1. Initial program 30.3

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

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

    if 9.320749723812436e-170 < y

    1. Initial program 1.3

      \[\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.3

      \[\leadsto \color{blue}{\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}\right)}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification5.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -2.0372639617288886 \cdot 10^{151}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.3928899832237578 \cdot 10^{-156}:\\ \;\;\;\;\frac{x \cdot x - y \cdot y}{\sqrt{x \cdot x + y \cdot y} \cdot \left(x + y\right)} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\\ \mathbf{elif}\;y \le 9.3207497238124358 \cdot 10^{-170}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}\right)\\ \end{array}\]

Reproduce

herbie shell --seed 2020056 
(FPCore (x y)
  :name "Kahan p9 Example"
  :precision binary64
  :pre (and (< 0.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))))