Average Error: 19.8 → 5.2
Time: 5.6s
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.3405462746826308 \cdot 10^{+154}:\\ \;\;\;\;2 \cdot \frac{{x}^{2}}{{y}^{2}} - 1\\ \mathbf{elif}\;y \leq -1.4573801156715909 \cdot 10^{-154}:\\ \;\;\;\;\sqrt[3]{\left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\right) \cdot \left(\left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\right) \cdot \left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\right)\right)}\\ \mathbf{elif}\;y \leq 9.53965689937808 \cdot 10^{-163}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\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 \leq -1.3405462746826308 \cdot 10^{+154}:\\
\;\;\;\;2 \cdot \frac{{x}^{2}}{{y}^{2}} - 1\\

\mathbf{elif}\;y \leq -1.4573801156715909 \cdot 10^{-154}:\\
\;\;\;\;\sqrt[3]{\left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\right) \cdot \left(\left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\right) \cdot \left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\right)\right)}\\

\mathbf{elif}\;y \leq 9.53965689937808 \cdot 10^{-163}:\\
\;\;\;\;1\\

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

\end{array}
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (if (<= y -1.3405462746826308e+154)
   (- (* 2.0 (/ (pow x 2.0) (pow y 2.0))) 1.0)
   (if (<= y -1.4573801156715909e-154)
     (cbrt
      (*
       (*
        (/ (- x y) (sqrt (+ (* x x) (* y y))))
        (/ (+ y x) (sqrt (+ (* x x) (* y y)))))
       (*
        (*
         (/ (- x y) (sqrt (+ (* x x) (* y y))))
         (/ (+ y x) (sqrt (+ (* x x) (* y y)))))
        (*
         (/ (- x y) (sqrt (+ (* x x) (* y y))))
         (/ (+ y x) (sqrt (+ (* x x) (* y y))))))))
     (if (<= y 9.53965689937808e-163)
       1.0
       (/ (* (- x y) (+ y x)) (+ (* x x) (* y y)))))))
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.3405462746826308e+154) {
		tmp = (2.0 * (pow(x, 2.0) / pow(y, 2.0))) - 1.0;
	} else if (y <= -1.4573801156715909e-154) {
		tmp = cbrt((((x - y) / sqrt((x * x) + (y * y))) * ((y + x) / sqrt((x * x) + (y * y)))) * ((((x - y) / sqrt((x * x) + (y * y))) * ((y + x) / sqrt((x * x) + (y * y)))) * (((x - y) / sqrt((x * x) + (y * y))) * ((y + x) / sqrt((x * x) + (y * y))))));
	} else if (y <= 9.53965689937808e-163) {
		tmp = 1.0;
	} else {
		tmp = ((x - y) * (y + x)) / ((x * x) + (y * y));
	}
	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

Original19.8
Target0.0
Herbie5.2
\[\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 4 regimes
  2. if y < -1.34054627468263077e154

    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 0

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

    if -1.34054627468263077e154 < y < -1.4573801156715909e-154

    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 add-sqr-sqrt_binary64_28280.0

      \[\leadsto \frac{\left(x - y\right) \cdot \left(x + y\right)}{\color{blue}{\sqrt{x \cdot x + y \cdot y} \cdot \sqrt{x \cdot x + y \cdot y}}}\]
    4. Applied times-frac_binary64_28120.0

      \[\leadsto \color{blue}{\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{x + y}{\sqrt{x \cdot x + y \cdot y}}}\]
    5. Using strategy rm
    6. Applied add-cbrt-cube_binary64_28420.0

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

    if -1.4573801156715909e-154 < y < 9.5396568993780805e-163

    1. Initial program 29.7

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

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

    if 9.5396568993780805e-163 < y

    1. Initial program 0.1

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.3405462746826308 \cdot 10^{+154}:\\ \;\;\;\;2 \cdot \frac{{x}^{2}}{{y}^{2}} - 1\\ \mathbf{elif}\;y \leq -1.4573801156715909 \cdot 10^{-154}:\\ \;\;\;\;\sqrt[3]{\left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\right) \cdot \left(\left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\right) \cdot \left(\frac{x - y}{\sqrt{x \cdot x + y \cdot y}} \cdot \frac{y + x}{\sqrt{x \cdot x + y \cdot y}}\right)\right)}\\ \mathbf{elif}\;y \leq 9.53965689937808 \cdot 10^{-163}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y}\\ \end{array}\]

Reproduce

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