Average Error: 20.8 → 5.6
Time: 2.4s
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.3549382125738884 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.0976423163705755 \cdot 10^{-160}:\\ \;\;\;\;\sqrt[3]{{\left(\left(y + x\right) \cdot \frac{x - y}{x \cdot x + y \cdot y}\right)}^{3}}\\ \mathbf{elif}\;y \leq -4.569358268130714 \cdot 10^{-209}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 7.111573363808515 \cdot 10^{-163}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{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.3549382125738884 \cdot 10^{+154}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq -1.0976423163705755 \cdot 10^{-160}:\\
\;\;\;\;\sqrt[3]{{\left(\left(y + x\right) \cdot \frac{x - y}{x \cdot x + y \cdot y}\right)}^{3}}\\

\mathbf{elif}\;y \leq -4.569358268130714 \cdot 10^{-209}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq 7.111573363808515 \cdot 10^{-163}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{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.3549382125738884e+154)
   -1.0
   (if (<= y -1.0976423163705755e-160)
     (cbrt (pow (* (+ y x) (/ (- x y) (+ (* x x) (* y y)))) 3.0))
     (if (<= y -4.569358268130714e-209)
       -1.0
       (if (<= y 7.111573363808515e-163)
         1.0
         (/ 1.0 (/ (+ (* x x) (* y y)) (- (* 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.3549382125738884e+154) {
		tmp = -1.0;
	} else if (y <= -1.0976423163705755e-160) {
		tmp = cbrt(pow(((y + x) * ((x - y) / ((x * x) + (y * y)))), 3.0));
	} else if (y <= -4.569358268130714e-209) {
		tmp = -1.0;
	} else if (y <= 7.111573363808515e-163) {
		tmp = 1.0;
	} else {
		tmp = 1.0 / (((x * x) + (y * y)) / ((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.0
Herbie5.6
\[\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.35493821257388839e154 or -1.09764231637057547e-160 < y < -4.5693582681307142e-209

    1. Initial program 56.8

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

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

    if -1.35493821257388839e154 < y < -1.09764231637057547e-160

    1. Initial program 0.1

      \[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
    2. Using strategy rm
    3. Applied add-cbrt-cube_binary64_11370.1

      \[\leadsto \color{blue}{\sqrt[3]{\left(\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \cdot \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\right) \cdot \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}}}\]
    4. Simplified0.1

      \[\leadsto \sqrt[3]{\color{blue}{{\left(\frac{x \cdot x - y \cdot y}{x \cdot x + y \cdot y}\right)}^{3}}}\]
    5. Using strategy rm
    6. Applied *-un-lft-identity_binary64_11010.1

      \[\leadsto \sqrt[3]{{\left(\frac{x \cdot x - y \cdot y}{\color{blue}{1 \cdot \left(x \cdot x + y \cdot y\right)}}\right)}^{3}}\]
    7. Applied difference-of-squares_binary64_10700.1

      \[\leadsto \sqrt[3]{{\left(\frac{\color{blue}{\left(x + y\right) \cdot \left(x - y\right)}}{1 \cdot \left(x \cdot x + y \cdot y\right)}\right)}^{3}}\]
    8. Applied times-frac_binary64_11070.2

      \[\leadsto \sqrt[3]{{\color{blue}{\left(\frac{x + y}{1} \cdot \frac{x - y}{x \cdot x + y \cdot y}\right)}}^{3}}\]
    9. Simplified0.2

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

    if -4.5693582681307142e-209 < y < 7.11157336380851536e-163

    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 13.8

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

    if 7.11157336380851536e-163 < y

    1. Initial program 0.2

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

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

      \[\leadsto \frac{1}{\color{blue}{\frac{x \cdot x + y \cdot y}{x \cdot x - y \cdot y}}}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification5.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.3549382125738884 \cdot 10^{+154}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.0976423163705755 \cdot 10^{-160}:\\ \;\;\;\;\sqrt[3]{{\left(\left(y + x\right) \cdot \frac{x - y}{x \cdot x + y \cdot y}\right)}^{3}}\\ \mathbf{elif}\;y \leq -4.569358268130714 \cdot 10^{-209}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq 7.111573363808515 \cdot 10^{-163}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{x \cdot x - y \cdot y}}\\ \end{array}\]

Reproduce

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