Average Error: 20.3 → 5.3
Time: 2.1s
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.69107722936919096 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.2814315799774739 \cdot 10^{-150}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \mathbf{elif}\;y \le 1.04652046448043914 \cdot 10^{-180}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{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.69107722936919096 \cdot 10^{153}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \le 1.04652046448043914 \cdot 10^{-180}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r95065 = x;
        double r95066 = y;
        double r95067 = r95065 - r95066;
        double r95068 = r95065 + r95066;
        double r95069 = r95067 * r95068;
        double r95070 = r95065 * r95065;
        double r95071 = r95066 * r95066;
        double r95072 = r95070 + r95071;
        double r95073 = r95069 / r95072;
        return r95073;
}

double f(double x, double y) {
        double r95074 = y;
        double r95075 = -1.691077229369191e+153;
        bool r95076 = r95074 <= r95075;
        double r95077 = -1.0;
        double r95078 = -1.281431579977474e-150;
        bool r95079 = r95074 <= r95078;
        double r95080 = 1.0;
        double r95081 = x;
        double r95082 = r95081 * r95081;
        double r95083 = r95074 * r95074;
        double r95084 = r95082 + r95083;
        double r95085 = r95081 - r95074;
        double r95086 = r95081 + r95074;
        double r95087 = r95085 * r95086;
        double r95088 = r95084 / r95087;
        double r95089 = r95080 / r95088;
        double r95090 = 1.0465204644804391e-180;
        bool r95091 = r95074 <= r95090;
        double r95092 = r95087 / r95084;
        double r95093 = r95091 ? r95080 : r95092;
        double r95094 = r95079 ? r95089 : r95093;
        double r95095 = r95076 ? r95077 : r95094;
        return r95095;
}

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.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 < -1.691077229369191e+153

    1. Initial program 63.6

      \[\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 -1.691077229369191e+153 < y < -1.281431579977474e-150

    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.281431579977474e-150 < y < 1.0465204644804391e-180

    1. Initial program 28.9

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

      \[\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 inf 15.4

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

    if 1.0465204644804391e-180 < y

    1. Initial program 2.8

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.69107722936919096 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.2814315799774739 \cdot 10^{-150}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \mathbf{elif}\;y \le 1.04652046448043914 \cdot 10^{-180}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \end{array}\]

Reproduce

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