Average Error: 20.4 → 5.8
Time: 10.1s
Precision: 64
\[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.359699432060768 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.2474742340765115 \cdot 10^{-174}:\\ \;\;\;\;\frac{x \cdot x}{x \cdot x + y \cdot y} - \frac{y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y}{\sqrt{x \cdot x + y \cdot y}}\\ \mathbf{elif}\;y \le -5.766846463446123 \cdot 10^{-210}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 1.1093912770840933 \cdot 10^{-203}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 1.1960469199561086 \cdot 10^{-189}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot x}{x \cdot x + y \cdot y} - \frac{y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{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.359699432060768 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -1.2474742340765115 \cdot 10^{-174}:\\
\;\;\;\;\frac{x \cdot x}{x \cdot x + y \cdot y} - \frac{y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y}{\sqrt{x \cdot x + y \cdot y}}\\

\mathbf{elif}\;y \le -5.766846463446123 \cdot 10^{-210}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le 1.1093912770840933 \cdot 10^{-203}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \le 1.1960469199561086 \cdot 10^{-189}:\\
\;\;\;\;-1\\

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

\end{array}
double f(double x, double y) {
        double r2667318 = x;
        double r2667319 = y;
        double r2667320 = r2667318 - r2667319;
        double r2667321 = r2667318 + r2667319;
        double r2667322 = r2667320 * r2667321;
        double r2667323 = r2667318 * r2667318;
        double r2667324 = r2667319 * r2667319;
        double r2667325 = r2667323 + r2667324;
        double r2667326 = r2667322 / r2667325;
        return r2667326;
}

double f(double x, double y) {
        double r2667327 = y;
        double r2667328 = -1.359699432060768e+154;
        bool r2667329 = r2667327 <= r2667328;
        double r2667330 = -1.0;
        double r2667331 = -1.2474742340765115e-174;
        bool r2667332 = r2667327 <= r2667331;
        double r2667333 = x;
        double r2667334 = r2667333 * r2667333;
        double r2667335 = r2667327 * r2667327;
        double r2667336 = r2667334 + r2667335;
        double r2667337 = r2667334 / r2667336;
        double r2667338 = sqrt(r2667336);
        double r2667339 = r2667327 / r2667338;
        double r2667340 = r2667339 * r2667339;
        double r2667341 = r2667337 - r2667340;
        double r2667342 = -5.766846463446123e-210;
        bool r2667343 = r2667327 <= r2667342;
        double r2667344 = 1.1093912770840933e-203;
        bool r2667345 = r2667327 <= r2667344;
        double r2667346 = 1.0;
        double r2667347 = 1.1960469199561086e-189;
        bool r2667348 = r2667327 <= r2667347;
        double r2667349 = r2667348 ? r2667330 : r2667341;
        double r2667350 = r2667345 ? r2667346 : r2667349;
        double r2667351 = r2667343 ? r2667330 : r2667350;
        double r2667352 = r2667332 ? r2667341 : r2667351;
        double r2667353 = r2667329 ? r2667330 : r2667352;
        return r2667353;
}

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.4
Target0.0
Herbie5.8
\[\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 < -1.359699432060768e+154 or -1.2474742340765115e-174 < y < -5.766846463446123e-210 or 1.1093912770840933e-203 < y < 1.1960469199561086e-189

    1. Initial program 55.7

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified55.7

      \[\leadsto \color{blue}{\frac{x \cdot x}{x \cdot x + y \cdot y} - \frac{y \cdot y}{x \cdot x + y \cdot y}}\]
    3. Taylor expanded around 0 9.6

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

    if -1.359699432060768e+154 < y < -1.2474742340765115e-174 or 1.1960469199561086e-189 < y

    1. Initial program 2.0

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified2.0

      \[\leadsto \color{blue}{\frac{x \cdot x}{x \cdot x + y \cdot y} - \frac{y \cdot y}{x \cdot x + y \cdot y}}\]
    3. Using strategy rm
    4. Applied add-sqr-sqrt2.0

      \[\leadsto \frac{x \cdot x}{x \cdot x + y \cdot y} - \frac{y \cdot y}{\color{blue}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}}\]
    5. Applied times-frac2.4

      \[\leadsto \frac{x \cdot x}{x \cdot x + y \cdot y} - \color{blue}{\frac{y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y}{\sqrt{x \cdot x + y \cdot y}}}\]

    if -5.766846463446123e-210 < y < 1.1093912770840933e-203

    1. Initial program 28.7

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Simplified28.7

      \[\leadsto \color{blue}{\frac{x \cdot x}{x \cdot x + y \cdot y} - \frac{y \cdot y}{x \cdot x + y \cdot y}}\]
    3. Taylor expanded around inf 10.2

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.359699432060768 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -1.2474742340765115 \cdot 10^{-174}:\\ \;\;\;\;\frac{x \cdot x}{x \cdot x + y \cdot y} - \frac{y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y}{\sqrt{x \cdot x + y \cdot y}}\\ \mathbf{elif}\;y \le -5.766846463446123 \cdot 10^{-210}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le 1.1093912770840933 \cdot 10^{-203}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \le 1.1960469199561086 \cdot 10^{-189}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\frac{x \cdot x}{x \cdot x + y \cdot y} - \frac{y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y}{\sqrt{x \cdot x + y \cdot y}}\\ \end{array}\]

Reproduce

herbie shell --seed 2019146 
(FPCore (x y)
  :name "Kahan p9 Example"
  :pre (and (< 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))))