Average Error: 20.0 → 4.6
Time: 3.2s
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} t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{if}\;t_0 \leq 1:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\frac{1 - 4 \cdot {\left(\frac{x}{y}\right)}^{4}}{-1 - 2 \cdot {\left(\frac{x}{y}\right)}^{2}}\\ \end{array} \]
\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}
\begin{array}{l}
t_0 := \frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\
\mathbf{if}\;t_0 \leq 1:\\
\;\;\;\;t_0\\

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


\end{array}
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y)))))
   (if (<= t_0 1.0)
     t_0
     (/
      (- 1.0 (* 4.0 (pow (/ x y) 4.0)))
      (- -1.0 (* 2.0 (pow (/ x y) 2.0)))))))
double code(double x, double y) {
	return ((x - y) * (x + y)) / ((x * x) + (y * y));
}
double code(double x, double y) {
	double t_0 = ((x - y) * (x + y)) / ((x * x) + (y * y));
	double tmp;
	if (t_0 <= 1.0) {
		tmp = t_0;
	} else {
		tmp = (1.0 - (4.0 * pow((x / y), 4.0))) / (-1.0 - (2.0 * pow((x / y), 2.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.0
Target0.0
Herbie4.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 2 regimes
  2. if (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y))) < 1

    1. Initial program 0.0

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

    if 1 < (/.f64 (*.f64 (-.f64 x y) (+.f64 x y)) (+.f64 (*.f64 x x) (*.f64 y y)))

    1. Initial program 64.0

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

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

      \[\leadsto \color{blue}{-1 + 2 \cdot \frac{x \cdot x}{y \cdot y}} \]
    4. Using strategy rm
    5. Applied add-sqr-sqrt_binary6429.9

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

      \[\leadsto -1 + 2 \cdot \left(\color{blue}{\left|\frac{x}{y}\right|} \cdot \sqrt{\frac{x \cdot x}{y \cdot y}}\right) \]
    7. Simplified14.5

      \[\leadsto -1 + 2 \cdot \left(\left|\frac{x}{y}\right| \cdot \color{blue}{\left|\frac{x}{y}\right|}\right) \]
    8. Using strategy rm
    9. Applied flip-+_binary6414.6

      \[\leadsto \color{blue}{\frac{-1 \cdot -1 - \left(2 \cdot \left(\left|\frac{x}{y}\right| \cdot \left|\frac{x}{y}\right|\right)\right) \cdot \left(2 \cdot \left(\left|\frac{x}{y}\right| \cdot \left|\frac{x}{y}\right|\right)\right)}{-1 - 2 \cdot \left(\left|\frac{x}{y}\right| \cdot \left|\frac{x}{y}\right|\right)}} \]
    10. Simplified14.6

      \[\leadsto \frac{\color{blue}{1 - {\left(\frac{x}{y}\right)}^{4} \cdot 4}}{-1 - 2 \cdot \left(\left|\frac{x}{y}\right| \cdot \left|\frac{x}{y}\right|\right)} \]
    11. Simplified14.6

      \[\leadsto \frac{1 - {\left(\frac{x}{y}\right)}^{4} \cdot 4}{\color{blue}{-1 - 2 \cdot {\left(\frac{x}{y}\right)}^{2}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification4.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y} \leq 1:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(x + y\right)}{x \cdot x + y \cdot y}\\ \mathbf{else}:\\ \;\;\;\;\frac{1 - 4 \cdot {\left(\frac{x}{y}\right)}^{4}}{-1 - 2 \cdot {\left(\frac{x}{y}\right)}^{2}}\\ \end{array} \]

Reproduce

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