Average Error: 20.3 → 4.8
Time: 5.5s
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.3574780207067694 \cdot 10^{+154}:\\ \;\;\;\;-1 + e^{\log 2 + \log \left(\frac{x}{y} \cdot \frac{x}{y}\right)}\\ \mathbf{elif}\;y \leq -1.5543412890393429 \cdot 10^{-162}:\\ \;\;\;\;\sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y} \cdot \left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y} \cdot \frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y}\right)}\\ \mathbf{elif}\;y \leq 6.230209348455822 \cdot 10^{-173}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{y \cdot y - x \cdot x}{-\left(x \cdot x + y \cdot y\right)}\\ \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.3574780207067694 \cdot 10^{+154}:\\
\;\;\;\;-1 + e^{\log 2 + \log \left(\frac{x}{y} \cdot \frac{x}{y}\right)}\\

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

\mathbf{elif}\;y \leq 6.230209348455822 \cdot 10^{-173}:\\
\;\;\;\;1\\

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

\end{array}
(FPCore (x y) :precision binary64 (/ (* (- x y) (+ x y)) (+ (* x x) (* y y))))
(FPCore (x y)
 :precision binary64
 (if (<= y -1.3574780207067694e+154)
   (+ -1.0 (exp (+ (log 2.0) (log (* (/ x y) (/ x y))))))
   (if (<= y -1.5543412890393429e-162)
     (cbrt
      (*
       (/ (* (- x y) (+ y x)) (+ (* x x) (* y y)))
       (*
        (/ (* (- x y) (+ y x)) (+ (* x x) (* y y)))
        (/ (* (- x y) (+ y x)) (+ (* x x) (* y y))))))
     (if (<= y 6.230209348455822e-173)
       1.0
       (/ (- (* y y) (* x 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.3574780207067694e+154) {
		tmp = -1.0 + exp(log(2.0) + log((x / y) * (x / y)));
	} else if (y <= -1.5543412890393429e-162) {
		tmp = cbrt((((x - y) * (y + x)) / ((x * x) + (y * y))) * ((((x - y) * (y + x)) / ((x * x) + (y * y))) * (((x - y) * (y + x)) / ((x * x) + (y * y)))));
	} else if (y <= 6.230209348455822e-173) {
		tmp = 1.0;
	} else {
		tmp = ((y * y) - (x * 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

Original20.3
Target0.1
Herbie4.8
\[\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.3574780207067694e154

    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}\]
    3. Simplified0

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

      \[\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. Simplified0

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

      \[\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 add-exp-log_binary64_28440

      \[\leadsto -1 + 2 \cdot \color{blue}{e^{\log \left(\left|\frac{x}{y}\right| \cdot \left|\frac{x}{y}\right|\right)}}\]
    10. Applied add-exp-log_binary64_28440

      \[\leadsto -1 + \color{blue}{e^{\log 2}} \cdot e^{\log \left(\left|\frac{x}{y}\right| \cdot \left|\frac{x}{y}\right|\right)}\]
    11. Applied prod-exp_binary64_28550

      \[\leadsto -1 + \color{blue}{e^{\log 2 + \log \left(\left|\frac{x}{y}\right| \cdot \left|\frac{x}{y}\right|\right)}}\]

    if -1.3574780207067694e154 < y < -1.5543412890393429e-162

    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 add-cbrt-cube_binary64_28420.1

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

    if -1.5543412890393429e-162 < y < 6.2302093484558222e-173

    1. Initial program 30.5

      \[\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_281730.5

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

      \[\leadsto \frac{\color{blue}{y \cdot y - x \cdot x}}{-\left(x \cdot x + y \cdot y\right)}\]
    5. Taylor expanded around 0 14.5

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

    if 6.2302093484558222e-173 < y

    1. Initial program 2.1

      \[\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_28172.1

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -1.3574780207067694 \cdot 10^{+154}:\\ \;\;\;\;-1 + e^{\log 2 + \log \left(\frac{x}{y} \cdot \frac{x}{y}\right)}\\ \mathbf{elif}\;y \leq -1.5543412890393429 \cdot 10^{-162}:\\ \;\;\;\;\sqrt[3]{\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y} \cdot \left(\frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y} \cdot \frac{\left(x - y\right) \cdot \left(y + x\right)}{x \cdot x + y \cdot y}\right)}\\ \mathbf{elif}\;y \leq 6.230209348455822 \cdot 10^{-173}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\frac{y \cdot y - x \cdot x}{-\left(x \cdot x + y \cdot y\right)}\\ \end{array}\]

Reproduce

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