Average Error: 19.6 → 5.5
Time: 2.6s
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 -3.847434879443193393633321330852372755378 \cdot 10^{151}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.694063246808970668811003130641552295003 \cdot 10^{-162}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}\right)\\ \mathbf{elif}\;y \le 6.451959238219760281097758056287369172736 \cdot 10^{-208}:\\ \;\;\;\;\log \left(e^{1}\right)\\ \mathbf{elif}\;y \le 1.336866892806927934140174920733392856435 \cdot 10^{-166}:\\ \;\;\;\;-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 -3.847434879443193393633321330852372755378 \cdot 10^{151}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -1.694063246808970668811003130641552295003 \cdot 10^{-162}:\\
\;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}\right)\\

\mathbf{elif}\;y \le 6.451959238219760281097758056287369172736 \cdot 10^{-208}:\\
\;\;\;\;\log \left(e^{1}\right)\\

\mathbf{elif}\;y \le 1.336866892806927934140174920733392856435 \cdot 10^{-166}:\\
\;\;\;\;-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 r113916 = x;
        double r113917 = y;
        double r113918 = r113916 - r113917;
        double r113919 = r113916 + r113917;
        double r113920 = r113918 * r113919;
        double r113921 = r113916 * r113916;
        double r113922 = r113917 * r113917;
        double r113923 = r113921 + r113922;
        double r113924 = r113920 / r113923;
        return r113924;
}

double f(double x, double y) {
        double r113925 = y;
        double r113926 = -3.8474348794431934e+151;
        bool r113927 = r113925 <= r113926;
        double r113928 = -1.0;
        double r113929 = -1.6940632468089707e-162;
        bool r113930 = r113925 <= r113929;
        double r113931 = x;
        double r113932 = r113931 - r113925;
        double r113933 = r113931 + r113925;
        double r113934 = r113932 * r113933;
        double r113935 = r113931 * r113931;
        double r113936 = r113925 * r113925;
        double r113937 = r113935 + r113936;
        double r113938 = r113934 / r113937;
        double r113939 = exp(r113938);
        double r113940 = log(r113939);
        double r113941 = 6.45195923821976e-208;
        bool r113942 = r113925 <= r113941;
        double r113943 = 1.0;
        double r113944 = exp(r113943);
        double r113945 = log(r113944);
        double r113946 = 1.336866892806928e-166;
        bool r113947 = r113925 <= r113946;
        double r113948 = r113947 ? r113928 : r113940;
        double r113949 = r113942 ? r113945 : r113948;
        double r113950 = r113930 ? r113940 : r113949;
        double r113951 = r113927 ? r113928 : r113950;
        return r113951;
}

Error

Bits error versus x

Bits error versus y

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original19.6
Target0.0
Herbie5.5
\[\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 < -3.8474348794431934e+151 or 6.45195923821976e-208 < y < 1.336866892806928e-166

    1. Initial program 55.7

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

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

    if -3.8474348794431934e+151 < y < -1.6940632468089707e-162 or 1.336866892806928e-166 < y

    1. Initial program 0.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-exp0.3

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

    if -1.6940632468089707e-162 < y < 6.45195923821976e-208

    1. Initial program 28.7

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied add-log-exp28.7

      \[\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 13.4

      \[\leadsto \log \left(e^{\color{blue}{1}}\right)\]
  3. Recombined 3 regimes into one program.
  4. Final simplification5.5

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -3.847434879443193393633321330852372755378 \cdot 10^{151}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.694063246808970668811003130641552295003 \cdot 10^{-162}:\\ \;\;\;\;\log \left(e^{\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}\right)\\ \mathbf{elif}\;y \le 6.451959238219760281097758056287369172736 \cdot 10^{-208}:\\ \;\;\;\;\log \left(e^{1}\right)\\ \mathbf{elif}\;y \le 1.336866892806927934140174920733392856435 \cdot 10^{-166}:\\ \;\;\;\;-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 2019353 
(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))))