Average Error: 19.9 → 5.7
Time: 1.7s
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.2370463069395253 \cdot 10^{-39}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -3.460569152566315 \cdot 10^{-162}:\\ \;\;\;\;\frac{x - y}{\frac{x \cdot x + y \cdot y}{y + x}}\\ \mathbf{elif}\;y \leq 9.138373226188112 \cdot 10^{-202}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 1.0362297196073818 \cdot 10^{-162}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\frac{x \cdot x + y \cdot y}{y + x}}\\ \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.2370463069395253 \cdot 10^{-39}:\\
\;\;\;\;-1\\

\mathbf{elif}\;y \leq -3.460569152566315 \cdot 10^{-162}:\\
\;\;\;\;\frac{x - y}{\frac{x \cdot x + y \cdot y}{y + x}}\\

\mathbf{elif}\;y \leq 9.138373226188112 \cdot 10^{-202}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 1.0362297196073818 \cdot 10^{-162}:\\
\;\;\;\;-1\\

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

\end{array}
double code(double x, double y) {
	return (((double) (((double) (x - y)) * ((double) (x + y)))) / ((double) (((double) (x * x)) + ((double) (y * y)))));
}
double code(double x, double y) {
	double VAR;
	if ((y <= -1.2370463069395253e-39)) {
		VAR = -1.0;
	} else {
		double VAR_1;
		if ((y <= -3.460569152566315e-162)) {
			VAR_1 = (((double) (x - y)) / (((double) (((double) (x * x)) + ((double) (y * y)))) / ((double) (y + x))));
		} else {
			double VAR_2;
			if ((y <= 9.138373226188112e-202)) {
				VAR_2 = 1.0;
			} else {
				double VAR_3;
				if ((y <= 1.0362297196073818e-162)) {
					VAR_3 = -1.0;
				} else {
					VAR_3 = (((double) (x - y)) / (((double) (((double) (x * x)) + ((double) (y * y)))) / ((double) (y + x))));
				}
				VAR_2 = VAR_3;
			}
			VAR_1 = VAR_2;
		}
		VAR = VAR_1;
	}
	return VAR;
}

Error

Bits error versus x

Bits error versus y

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Target

Original19.9
Target0.0
Herbie5.7
\[\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.23704630693952534e-39 or 9.13837322618811221e-202 < y < 1.03622971960738184e-162

    1. Initial program 28.7

      \[\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.23704630693952534e-39 < y < -3.46056915256631509e-162 or 1.03622971960738184e-162 < y

    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 associate-/l*0.8

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

    if -3.46056915256631509e-162 < y < 9.13837322618811221e-202

    1. Initial program 28.9

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.2370463069395253 \cdot 10^{-39}:\\ \;\;\;\;-1\\ \mathbf{elif}\;y \leq -3.460569152566315 \cdot 10^{-162}:\\ \;\;\;\;\frac{x - y}{\frac{x \cdot x + y \cdot y}{y + x}}\\ \mathbf{elif}\;y \leq 9.138373226188112 \cdot 10^{-202}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 1.0362297196073818 \cdot 10^{-162}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\frac{x - y}{\frac{x \cdot x + y \cdot y}{y + x}}\\ \end{array}\]

Reproduce

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