Average Error: 20.2 → 5.3
Time: 2.3s
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.510213869296349197348532579075514817675 \cdot 10^{118}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.678792469308792923731110608656718832118 \cdot 10^{-161}:\\ \;\;\;\;\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}\\ \mathbf{elif}\;y \le 7.907709664956101501536326464483134704674 \cdot 10^{-167}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + 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.510213869296349197348532579075514817675 \cdot 10^{118}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \le -3.678792469308792923731110608656718832118 \cdot 10^{-161}:\\
\;\;\;\;\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}\\

\mathbf{elif}\;y \le 7.907709664956101501536326464483134704674 \cdot 10^{-167}:\\
\;\;\;\;1\\

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

\end{array}
double f(double x, double y) {
        double r67328 = x;
        double r67329 = y;
        double r67330 = r67328 - r67329;
        double r67331 = r67328 + r67329;
        double r67332 = r67330 * r67331;
        double r67333 = r67328 * r67328;
        double r67334 = r67329 * r67329;
        double r67335 = r67333 + r67334;
        double r67336 = r67332 / r67335;
        return r67336;
}

double f(double x, double y) {
        double r67337 = y;
        double r67338 = -1.5102138692963492e+118;
        bool r67339 = r67337 <= r67338;
        double r67340 = -1.0;
        double r67341 = -3.678792469308793e-161;
        bool r67342 = r67337 <= r67341;
        double r67343 = x;
        double r67344 = r67343 - r67337;
        double r67345 = r67343 * r67343;
        double r67346 = r67337 * r67337;
        double r67347 = r67345 + r67346;
        double r67348 = r67343 + r67337;
        double r67349 = r67347 / r67348;
        double r67350 = r67344 / r67349;
        double r67351 = 7.907709664956102e-167;
        bool r67352 = r67337 <= r67351;
        double r67353 = 1.0;
        double r67354 = r67352 ? r67353 : r67350;
        double r67355 = r67342 ? r67350 : r67354;
        double r67356 = r67339 ? r67340 : r67355;
        return r67356;
}

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.2
Target0.0
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 3 regimes
  2. if y < -1.5102138692963492e+118

    1. Initial program 50.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 -1.5102138692963492e+118 < y < -3.678792469308793e-161 or 7.907709664956102e-167 < 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 associate-/l*0.9

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

    if -3.678792469308793e-161 < y < 7.907709664956102e-167

    1. Initial program 30.2

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -1.510213869296349197348532579075514817675 \cdot 10^{118}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \le -3.678792469308792923731110608656718832118 \cdot 10^{-161}:\\ \;\;\;\;\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}\\ \mathbf{elif}\;y \le 7.907709664956101501536326464483134704674 \cdot 10^{-167}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\frac{x \cdot x + y \cdot y}{x + y}}\\ \end{array}\]

Reproduce

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