Average Error: 20.3 → 4.9
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 -5.916052332961234866514185830954886094509 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.752966852742461819588535195784437383997 \cdot 10^{-161}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \mathbf{elif}\;y \le 3.565272236996635744708165969889467996882 \cdot 10^{-170}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + 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 -5.916052332961234866514185830954886094509 \cdot 10^{153}:\\
\;\;\;\;-1\\

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

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

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

\end{array}
double f(double x, double y) {
        double r80261 = x;
        double r80262 = y;
        double r80263 = r80261 - r80262;
        double r80264 = r80261 + r80262;
        double r80265 = r80263 * r80264;
        double r80266 = r80261 * r80261;
        double r80267 = r80262 * r80262;
        double r80268 = r80266 + r80267;
        double r80269 = r80265 / r80268;
        return r80269;
}

double f(double x, double y) {
        double r80270 = y;
        double r80271 = -5.916052332961235e+153;
        bool r80272 = r80270 <= r80271;
        double r80273 = -1.0;
        double r80274 = -1.7529668527424618e-161;
        bool r80275 = r80270 <= r80274;
        double r80276 = 1.0;
        double r80277 = x;
        double r80278 = r80277 * r80277;
        double r80279 = r80270 * r80270;
        double r80280 = r80278 + r80279;
        double r80281 = r80277 - r80270;
        double r80282 = r80277 + r80270;
        double r80283 = r80281 * r80282;
        double r80284 = r80280 / r80283;
        double r80285 = r80276 / r80284;
        double r80286 = 3.565272236996636e-170;
        bool r80287 = r80270 <= r80286;
        double r80288 = r80287 ? r80276 : r80285;
        double r80289 = r80275 ? r80285 : r80288;
        double r80290 = r80272 ? r80273 : r80289;
        return r80290;
}

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
Herbie4.9
\[\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 < -5.916052332961235e+153

    1. Initial program 63.9

      \[\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 -5.916052332961235e+153 < y < -1.7529668527424618e-161 or 3.565272236996636e-170 < y

    1. Initial program 0.5

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

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

    if -1.7529668527424618e-161 < y < 3.565272236996636e-170

    1. Initial program 29.8

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

      \[\leadsto \color{blue}{1}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification4.9

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -5.916052332961234866514185830954886094509 \cdot 10^{153}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.752966852742461819588535195784437383997 \cdot 10^{-161}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \mathbf{elif}\;y \le 3.565272236996635744708165969889467996882 \cdot 10^{-170}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(x + y\right)}}\\ \end{array}\]

Reproduce

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