Average Error: 20.1 → 5.3
Time: 3.1s
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.6640725549120233 \cdot 10^{-17}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.561338403315553 \cdot 10^{-162} \lor \neg \left(y \leq 1.5801171599198037 \cdot 10^{-162}\right):\\ \;\;\;\;\frac{y \cdot y - x \cdot x}{-\left(y \cdot y + x \cdot x\right)}\\ \mathbf{else}:\\ \;\;\;\;1\\ \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.6640725549120233 \cdot 10^{-17}:\\
\;\;\;\;-1\\

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

\mathbf{else}:\\
\;\;\;\;1\\

\end{array}
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (if (<= y -1.6640725549120233e-17)
   -1.0
   (if (or (<= y -1.561338403315553e-162) (not (<= y 1.5801171599198037e-162)))
     (/ (- (* y y) (* x x)) (- (+ (* y y) (* x x))))
     1.0)))
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.6640725549120233e-17) {
		tmp = -1.0;
	} else if ((y <= -1.561338403315553e-162) || !(y <= 1.5801171599198037e-162)) {
		tmp = ((y * y) - (x * x)) / -((y * y) + (x * x));
	} else {
		tmp = 1.0;
	}
	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.1
Target0.0
Herbie5.3
\[\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 3 regimes
  2. if y < -1.66407255491202333e-17

    1. Initial program 30.6

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

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

    if -1.66407255491202333e-17 < y < -1.561338403315553e-162 or 1.58011715991980372e-162 < y

    1. Initial program 0.0

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

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

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

    if -1.561338403315553e-162 < y < 1.58011715991980372e-162

    1. Initial program 30.0

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.6640725549120233 \cdot 10^{-17}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -1.561338403315553 \cdot 10^{-162} \lor \neg \left(y \leq 1.5801171599198037 \cdot 10^{-162}\right):\\ \;\;\;\;\frac{y \cdot y - x \cdot x}{-\left(y \cdot y + x \cdot x\right)}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array}\]

Reproduce

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