Average Error: 20.4 → 5.5
Time: 3.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 -3.034508079287107 \cdot 10^{+152}:\\ \;\;\;\;-1 + 2 \cdot \frac{x \cdot x}{y \cdot y}\\ \mathbf{elif}\;y \leq -1.7556393841525514 \cdot 10^{-177}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \leq -3.6861139043320025 \cdot 10^{-201}:\\ \;\;\;\;-1 + 2 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right)\\ \mathbf{elif}\;y \leq 5.0918618588880355 \cdot 10^{-197}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 4.82726859401974 \cdot 10^{-168}:\\ \;\;\;\;-1 + 2 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(y + x\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 \leq -3.034508079287107 \cdot 10^{+152}:\\
\;\;\;\;-1 + 2 \cdot \frac{x \cdot x}{y \cdot y}\\

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

\mathbf{elif}\;y \leq -3.6861139043320025 \cdot 10^{-201}:\\
\;\;\;\;-1 + 2 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right)\\

\mathbf{elif}\;y \leq 5.0918618588880355 \cdot 10^{-197}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 4.82726859401974 \cdot 10^{-168}:\\
\;\;\;\;-1 + 2 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right)\\

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

\end{array}
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (if (<= y -3.034508079287107e+152)
   (+ -1.0 (* 2.0 (/ (* x x) (* y y))))
   (if (<= y -1.7556393841525514e-177)
     (/ (* (- x y) (+ y x)) (+ (* x x) (* y y)))
     (if (<= y -3.6861139043320025e-201)
       (+ -1.0 (* 2.0 (* (/ x y) (/ x y))))
       (if (<= y 5.0918618588880355e-197)
         1.0
         (if (<= y 4.82726859401974e-168)
           (+ -1.0 (* 2.0 (* (/ x y) (/ x y))))
           (/ 1.0 (/ (+ (* x x) (* y y)) (* (- x y) (+ y x))))))))))
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 <= -3.034508079287107e+152) {
		tmp = -1.0 + (2.0 * ((x * x) / (y * y)));
	} else if (y <= -1.7556393841525514e-177) {
		tmp = ((x - y) * (y + x)) / ((x * x) + (y * y));
	} else if (y <= -3.6861139043320025e-201) {
		tmp = -1.0 + (2.0 * ((x / y) * (x / y)));
	} else if (y <= 5.0918618588880355e-197) {
		tmp = 1.0;
	} else if (y <= 4.82726859401974e-168) {
		tmp = -1.0 + (2.0 * ((x / y) * (x / y)));
	} else {
		tmp = 1.0 / (((x * x) + (y * y)) / ((x - y) * (y + x)));
	}
	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.4
Target0.0
Herbie5.5
\[\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 5 regimes
  2. if y < -3.034508079287107e152

    1. Initial program 63.2

      \[\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}{2 \cdot \frac{{x}^{2}}{{y}^{2}} - 1}\]
    3. Simplified0

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

    if -3.034508079287107e152 < y < -1.75563938415255139e-177

    1. Initial program 1.3

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

    if -1.75563938415255139e-177 < y < -3.686113904332003e-201 or 5.0918618588880355e-197 < y < 4.8272685940197399e-168

    1. Initial program 31.2

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

      \[\leadsto \color{blue}{2 \cdot \frac{{x}^{2}}{{y}^{2}} - 1}\]
    3. Simplified64.0

      \[\leadsto \color{blue}{-1 + 2 \cdot \frac{x \cdot x}{y \cdot y}}\]
    4. Using strategy rm
    5. Applied times-frac_binary6436.4

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

    if -3.686113904332003e-201 < y < 5.0918618588880355e-197

    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 11.2

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

    if 4.8272685940197399e-168 < y

    1. Initial program 1.3

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -3.034508079287107 \cdot 10^{+152}:\\ \;\;\;\;-1 + 2 \cdot \frac{x \cdot x}{y \cdot y}\\ \mathbf{elif}\;y \leq -1.7556393841525514 \cdot 10^{-177}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \leq -3.6861139043320025 \cdot 10^{-201}:\\ \;\;\;\;-1 + 2 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right)\\ \mathbf{elif}\;y \leq 5.0918618588880355 \cdot 10^{-197}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 4.82726859401974 \cdot 10^{-168}:\\ \;\;\;\;-1 + 2 \cdot \left(\frac{x}{y} \cdot \frac{x}{y}\right)\\ \mathbf{else}:\\ \;\;\;\;\frac{1}{\frac{x \cdot x + y \cdot y}{\left(x - y\right) \cdot \left(y + x\right)}}\\ \end{array}\]

Reproduce

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