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}:\\ \;\;\;\;\log \left(e^{1}\right)\\ \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}:\\
\;\;\;\;\log \left(e^{1}\right)\\

\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 r74778 = x;
        double r74779 = y;
        double r74780 = r74778 - r74779;
        double r74781 = r74778 + r74779;
        double r74782 = r74780 * r74781;
        double r74783 = r74778 * r74778;
        double r74784 = r74779 * r74779;
        double r74785 = r74783 + r74784;
        double r74786 = r74782 / r74785;
        return r74786;
}

double f(double x, double y) {
        double r74787 = y;
        double r74788 = -2.0372639617288886e+151;
        bool r74789 = r74787 <= r74788;
        double r74790 = -1.0;
        double r74791 = -3.392889983223758e-156;
        bool r74792 = r74787 <= r74791;
        double r74793 = x;
        double r74794 = r74793 * r74793;
        double r74795 = r74787 * r74787;
        double r74796 = r74794 - r74795;
        double r74797 = r74794 + r74795;
        double r74798 = sqrt(r74797);
        double r74799 = r74793 + r74787;
        double r74800 = r74798 * r74799;
        double r74801 = r74796 / r74800;
        double r74802 = r74799 / r74798;
        double r74803 = r74801 * r74802;
        double r74804 = 9.320749723812436e-170;
        bool r74805 = r74787 <= r74804;
        double r74806 = 1.0;
        double r74807 = exp(r74806);
        double r74808 = log(r74807);
        double r74809 = r74793 - r74787;
        double r74810 = r74809 * r74799;
        double r74811 = r74810 / r74797;
        double r74812 = exp(r74811);
        double r74813 = log(r74812);
        double r74814 = r74805 ? r74808 : r74813;
        double r74815 = r74792 ? r74803 : r74814;
        double r74816 = r74789 ? r74790 : r74815;
        return r74816;
}

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. Using strategy rm
    3. Applied add-log-exp30.3

      \[\leadsto \color{blue}{\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}\right)}\]
    4. Taylor expanded around inf 15.9

      \[\leadsto \log \left(e^{\color{blue}{1}}\right)\]

    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}:\\ \;\;\;\;\log \left(e^{1}\right)\\ \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))))