Average Error: 19.8 → 6.0
Time: 1.9s
Precision: 64
\[0.0 \lt x \lt 1 \land y \lt 1\]
\[\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\]
\[\begin{array}{l} \mathbf{if}\;y \le -5.8743078437979291 \cdot 10^{153}:\\ \;\;\;\;\frac{x + y}{-\left(x + y\right)}\\ \mathbf{elif}\;y \le -3.9122793901032331 \cdot 10^{-168}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \le -9.7190819336199446 \cdot 10^{-219}:\\ \;\;\;\;\frac{x + y}{-\left(x + y\right)}\\ \mathbf{elif}\;y \le 3.72637595742545587 \cdot 10^{-192}:\\ \;\;\;\;\frac{x + y}{x + y}\\ \mathbf{elif}\;y \le 1.21187835279053997 \cdot 10^{-166}:\\ \;\;\;\;\frac{x + y}{-\left(x + y\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{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 \le -5.8743078437979291 \cdot 10^{153}:\\
\;\;\;\;\frac{x + y}{-\left(x + y\right)}\\

\mathbf{elif}\;y \le -3.9122793901032331 \cdot 10^{-168}:\\
\;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\

\mathbf{elif}\;y \le -9.7190819336199446 \cdot 10^{-219}:\\
\;\;\;\;\frac{x + y}{-\left(x + y\right)}\\

\mathbf{elif}\;y \le 3.72637595742545587 \cdot 10^{-192}:\\
\;\;\;\;\frac{x + y}{x + y}\\

\mathbf{elif}\;y \le 1.21187835279053997 \cdot 10^{-166}:\\
\;\;\;\;\frac{x + y}{-\left(x + y\right)}\\

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

\end{array}
double code(double x, double y) {
	return (((x - y) * (x + y)) / ((x * x) + (y * y)));
}
double code(double x, double y) {
	double VAR;
	if ((y <= -5.874307843797929e+153)) {
		VAR = ((x + y) / -(x + y));
	} else {
		double VAR_1;
		if ((y <= -3.912279390103233e-168)) {
			VAR_1 = (((x - y) * (x + y)) / ((x * x) + (y * y)));
		} else {
			double VAR_2;
			if ((y <= -9.719081933619945e-219)) {
				VAR_2 = ((x + y) / -(x + y));
			} else {
				double VAR_3;
				if ((y <= 3.726375957425456e-192)) {
					VAR_3 = ((x + y) / (x + y));
				} else {
					double VAR_4;
					if ((y <= 1.21187835279054e-166)) {
						VAR_4 = ((x + y) / -(x + y));
					} else {
						VAR_4 = (((x - y) * (x + y)) / ((x * x) + (y * y)));
					}
					VAR_3 = VAR_4;
				}
				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.8
Target0.1
Herbie6.0
\[\begin{array}{l} \mathbf{if}\;0.5 \lt \left|\frac{x}{y}\right| \lt 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 < -5.874307843797929e+153 or -3.912279390103233e-168 < y < -9.719081933619945e-219 or 3.726375957425456e-192 < y < 1.21187835279054e-166

    1. Initial program 52.8

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

      \[\leadsto \frac{\color{blue}{\left(x + y\right) \cdot \left(x - y\right)}}{x \cdot x + y \cdot y}\]
    4. Applied associate-/l*51.8

      \[\leadsto \color{blue}{\frac{x + y}{\frac{x \cdot x + y \cdot y}{x - y}}}\]
    5. Taylor expanded around 0 12.7

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

    if -5.874307843797929e+153 < y < -3.912279390103233e-168 or 1.21187835279054e-166 < y

    1. Initial program 0.6

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

    if -9.719081933619945e-219 < y < 3.726375957425456e-192

    1. Initial program 30.4

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

      \[\leadsto \frac{\color{blue}{\left(x + y\right) \cdot \left(x - y\right)}}{x \cdot x + y \cdot y}\]
    4. Applied associate-/l*31.1

      \[\leadsto \color{blue}{\frac{x + y}{\frac{x \cdot x + y \cdot y}{x - y}}}\]
    5. Taylor expanded around inf 11.4

      \[\leadsto \frac{x + y}{\color{blue}{x + y}}\]
  3. Recombined 3 regimes into one program.
  4. Final simplification6.0

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \le -5.8743078437979291 \cdot 10^{153}:\\ \;\;\;\;\frac{x + y}{-\left(x + y\right)}\\ \mathbf{elif}\;y \le -3.9122793901032331 \cdot 10^{-168}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{elif}\;y \le -9.7190819336199446 \cdot 10^{-219}:\\ \;\;\;\;\frac{x + y}{-\left(x + y\right)}\\ \mathbf{elif}\;y \le 3.72637595742545587 \cdot 10^{-192}:\\ \;\;\;\;\frac{x + y}{x + y}\\ \mathbf{elif}\;y \le 1.21187835279053997 \cdot 10^{-166}:\\ \;\;\;\;\frac{x + y}{-\left(x + y\right)}\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \end{array}\]

Reproduce

herbie shell --seed 2020071 +o rules:numerics
(FPCore (x y)
  :name "Kahan p9 Example"
  :precision binary64
  :pre (and (< 0.0 x 1) (< y 1))

  :herbie-target
  (if (< 0.5 (fabs (/ x y)) 2) (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))) (- 1 (/ 2 (+ 1 (* (/ x y) (/ x y))))))

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