Average Error: 20.8 → 5.4
Time: 2.2s
Precision: binary64
\[0 < x \land x < 1 \land y < 1\]
\[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \leq -1.3567364441858618 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.5841081320227725 \cdot 10^{-162}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \leq -6.2204064707892276 \cdot 10^{-223}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq -6.9650057071736045 \cdot 10^{-239}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 7.652391033922207 \cdot 10^{-166}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\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 \leq -1.3567364441858618 \cdot 10^{+154}:\\
\;\;\;\;-1\\

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

\mathbf{elif}\;y \leq -6.2204064707892276 \cdot 10^{-223}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq -6.9650057071736045 \cdot 10^{-239}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq 7.652391033922207 \cdot 10^{-166}:\\
\;\;\;\;1\\

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

\end{array}
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (if (<= y -1.3567364441858618e+154)
   -1.0
   (if (<= y -1.5841081320227725e-162)
     (/ (* (- x y) (+ y x)) (+ (* x x) (* y y)))
     (if (<= y -6.2204064707892276e-223)
       1.0
       (if (<= y -6.9650057071736045e-239)
         -1.0
         (if (<= y 7.652391033922207e-166)
           1.0
           (*
            (/ (- x y) (sqrt (+ (* x x) (* y y))))
            (/ (+ y x) (sqrt (+ (* x x) (* y y)))))))))))
double code(double x, double y) {
	return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
double code(double x, double y) {
	double tmp;
	if (y <= -1.3567364441858618e+154) {
		tmp = -1.0;
	} else if (y <= -1.5841081320227725e-162) {
		tmp = ((x - y) * (y + x)) / ((x * x) + (y * y));
	} else if (y <= -6.2204064707892276e-223) {
		tmp = 1.0;
	} else if (y <= -6.9650057071736045e-239) {
		tmp = -1.0;
	} else if (y <= 7.652391033922207e-166) {
		tmp = 1.0;
	} else {
		tmp = ((x - y) / sqrt((x * x) + (y * y))) * ((y + x) / sqrt((x * x) + (y * y)));
	}
	return tmp;
}

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.8
Target0.1
Herbie5.4
\[\begin{array}{l} \mathbf{if}\;0.5 < \left|\frac{x}{y}\right| \land \left|\frac{x}{y}\right| < 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.3567364441858618e154 or -6.22040647078922761e-223 < y < -6.9650057071736045e-239

    1. Initial program 61.4

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

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

    if -1.3567364441858618e154 < y < -1.58410813202277254e-162

    1. Initial program 0.0

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]

    if -1.58410813202277254e-162 < y < -6.22040647078922761e-223 or -6.9650057071736045e-239 < y < 7.6523910339222066e-166

    1. Initial program 30.8

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

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

    if 7.6523910339222066e-166 < y

    1. Initial program 0.4

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

      \[\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-frac_binary64_11071.3

      \[\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.4

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.3567364441858618 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.5841081320227725 \cdot 10^{-162}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \leq -6.2204064707892276 \cdot 10^{-223}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq -6.9650057071736045 \cdot 10^{-239}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 7.652391033922207 \cdot 10^{-166}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\\ \end{array}\]

Reproduce

herbie shell --seed 2021022 
(FPCore (x y)
  :name "Kahan p9 Example"
  :precision binary64
  :pre (and (< 0.0 x 1.0) (< y 1.0))

  :herbie-target
  (if (< 0.5 (fabs (/ x y)) 2.0) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1.0 (/ 2.0 (+ 1.0 (* (/ x y) (/ x y))))))

  (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))