Average Error: 20.3 → 5.1
Time: 3.5s
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 -1.785532773814616831863731014933445423349 \cdot 10^{140}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.558923983411081976150022496277695268694 \cdot 10^{-162}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \mathbf{elif}\;y \le 3.047723682413627022763368644019132647617 \cdot 10^{-169}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\\ \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.785532773814616831863731014933445423349 \cdot 10^{140}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 3.047723682413627022763368644019132647617 \cdot 10^{-169}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\\

\end{array}
double f(double x, double y) {
        double r89809 = x;
        double r89810 = y;
        double r89811 = r89809 - r89810;
        double r89812 = r89809 + r89810;
        double r89813 = r89811 * r89812;
        double r89814 = r89809 * r89809;
        double r89815 = r89810 * r89810;
        double r89816 = r89814 + r89815;
        double r89817 = r89813 / r89816;
        return r89817;
}

double f(double x, double y) {
        double r89818 = y;
        double r89819 = -1.7855327738146168e+140;
        bool r89820 = r89818 <= r89819;
        double r89821 = -1.0;
        double r89822 = -1.558923983411082e-162;
        bool r89823 = r89818 <= r89822;
        double r89824 = 1.0;
        double r89825 = x;
        double r89826 = r89825 * r89825;
        double r89827 = r89818 * r89818;
        double r89828 = r89826 + r89827;
        double r89829 = r89825 - r89818;
        double r89830 = r89825 + r89818;
        double r89831 = r89829 * r89830;
        double r89832 = r89828 / r89831;
        double r89833 = r89824 / r89832;
        double r89834 = 3.047723682413627e-169;
        bool r89835 = r89818 <= r89834;
        double r89836 = sqrt(r89828);
        double r89837 = r89829 / r89836;
        double r89838 = r89830 / r89836;
        double r89839 = r89837 * r89838;
        double r89840 = r89835 ? r89824 : r89839;
        double r89841 = r89823 ? r89833 : r89840;
        double r89842 = r89820 ? r89821 : r89841;
        return r89842;
}

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.3
Target0.0
Herbie5.1
\[\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 < -1.7855327738146168e+140

    1. Initial program 58.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied clear-num58.1

      \[\leadsto \color{blue}{\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}}\]
    4. Taylor expanded around 0 0

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

    if -1.7855327738146168e+140 < y < -1.558923983411082e-162

    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 clear-num0.0

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

    if -1.558923983411082e-162 < y < 3.047723682413627e-169

    1. Initial program 30.6

      \[\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 3.047723682413627e-169 < y

    1. Initial program 1.1

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

      \[\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-frac1.6

      \[\leadsto \color{blue}{\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification5.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.785532773814616831863731014933445423349 \cdot 10^{140}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.558923983411081976150022496277695268694 \cdot 10^{-162}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \mathbf{elif}\;y \le 3.047723682413627022763368644019132647617 \cdot 10^{-169}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}\\ \end{array}\]

Reproduce

herbie shell --seed 2020001 
(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))))