Average Error: 31.9 → 13.1
Time: 1.8s
Precision: binary64
\[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
\[\begin{array}{l} t_0 := y \cdot \left(y \cdot 4\right)\\ t_1 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\ t_2 := \mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\ \mathbf{if}\;x \leq -2.1078549869940875 \cdot 10^{+83}:\\ \;\;\;\;t_2\\ \mathbf{elif}\;x \leq -8.642290443819559 \cdot 10^{-126}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 9.883008504928267 \cdot 10^{-118}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 2.259789577185776 \cdot 10^{-48}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;x \leq 8.07270989499859 \cdot 10^{-14}:\\ \;\;\;\;-1 + 0.5 \cdot \frac{{x}^{2}}{{y}^{2}}\\ \mathbf{elif}\;x \leq 1.11024680895731 \cdot 10^{+69}:\\ \;\;\;\;t_1\\ \mathbf{else}:\\ \;\;\;\;t_2\\ \end{array} \]
\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y}
\begin{array}{l}
t_0 := y \cdot \left(y \cdot 4\right)\\
t_1 := \frac{x \cdot x - t_0}{x \cdot x + t_0}\\
t_2 := \mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\
\mathbf{if}\;x \leq -2.1078549869940875 \cdot 10^{+83}:\\
\;\;\;\;t_2\\

\mathbf{elif}\;x \leq -8.642290443819559 \cdot 10^{-126}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \leq 9.883008504928267 \cdot 10^{-118}:\\
\;\;\;\;-1\\

\mathbf{elif}\;x \leq 2.259789577185776 \cdot 10^{-48}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;x \leq 8.07270989499859 \cdot 10^{-14}:\\
\;\;\;\;-1 + 0.5 \cdot \frac{{x}^{2}}{{y}^{2}}\\

\mathbf{elif}\;x \leq 1.11024680895731 \cdot 10^{+69}:\\
\;\;\;\;t_1\\

\mathbf{else}:\\
\;\;\;\;t_2\\


\end{array}
(FPCore (x y)
 :precision binary64
 (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))))
(FPCore (x y)
 :precision binary64
 (let* ((t_0 (* y (* y 4.0)))
        (t_1 (/ (- (* x x) t_0) (+ (* x x) t_0)))
        (t_2 (fma (pow (/ y x) 2.0) -8.0 1.0)))
   (if (<= x -2.1078549869940875e+83)
     t_2
     (if (<= x -8.642290443819559e-126)
       t_1
       (if (<= x 9.883008504928267e-118)
         -1.0
         (if (<= x 2.259789577185776e-48)
           t_1
           (if (<= x 8.07270989499859e-14)
             (+ -1.0 (* 0.5 (/ (pow x 2.0) (pow y 2.0))))
             (if (<= x 1.11024680895731e+69) t_1 t_2))))))))
double code(double x, double y) {
	return ((x * x) - ((y * 4.0) * y)) / ((x * x) + ((y * 4.0) * y));
}
double code(double x, double y) {
	double t_0 = y * (y * 4.0);
	double t_1 = ((x * x) - t_0) / ((x * x) + t_0);
	double t_2 = fma(pow((y / x), 2.0), -8.0, 1.0);
	double tmp;
	if (x <= -2.1078549869940875e+83) {
		tmp = t_2;
	} else if (x <= -8.642290443819559e-126) {
		tmp = t_1;
	} else if (x <= 9.883008504928267e-118) {
		tmp = -1.0;
	} else if (x <= 2.259789577185776e-48) {
		tmp = t_1;
	} else if (x <= 8.07270989499859e-14) {
		tmp = -1.0 + (0.5 * (pow(x, 2.0) / pow(y, 2.0)));
	} else if (x <= 1.11024680895731e+69) {
		tmp = t_1;
	} else {
		tmp = t_2;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.9
Target31.6
Herbie13.1
\[\begin{array}{l} \mathbf{if}\;\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} < 0.9743233849626781:\\ \;\;\;\;\frac{x \cdot x}{x \cdot x + \left(y \cdot y\right) \cdot 4} - \frac{\left(y \cdot y\right) \cdot 4}{x \cdot x + \left(y \cdot y\right) \cdot 4}\\ \mathbf{else}:\\ \;\;\;\;{\left(\frac{x}{\sqrt{x \cdot x + \left(y \cdot y\right) \cdot 4}}\right)}^{2} - \frac{\left(y \cdot y\right) \cdot 4}{x \cdot x + \left(y \cdot y\right) \cdot 4}\\ \end{array} \]

Derivation

  1. Split input into 4 regimes
  2. if x < -2.1078549869940875e83 or 1.11024680895730993e69 < x

    1. Initial program 48.5

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified48.5

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}} \]
    3. Taylor expanded in y around 0 18.2

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y \cdot y}{x \cdot x}, -8, 1\right)} \]
    5. Taylor expanded in y around 0 18.2

      \[\leadsto \mathsf{fma}\left(\color{blue}{\frac{{y}^{2}}{{x}^{2}}}, -8, 1\right) \]
    6. Simplified12.0

      \[\leadsto \mathsf{fma}\left(\color{blue}{{\left(\frac{y}{x}\right)}^{2}}, -8, 1\right) \]

    if -2.1078549869940875e83 < x < -8.64229044381955858e-126 or 9.8830085049282675e-118 < x < 2.25978957718577587e-48 or 8.07270989499859019e-14 < x < 1.11024680895730993e69

    1. Initial program 16.2

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

    if -8.64229044381955858e-126 < x < 9.8830085049282675e-118

    1. Initial program 28.0

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified28.3

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}} \]
    3. Taylor expanded in y around inf 10.0

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

    if 2.25978957718577587e-48 < x < 8.07270989499859019e-14

    1. Initial program 15.3

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified15.3

      \[\leadsto \color{blue}{\frac{\mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)}{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}} \]
    3. Taylor expanded in y around inf 28.6

      \[\leadsto \color{blue}{0.5 \cdot \frac{{x}^{2}}{{y}^{2}} - 1} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification13.1

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -2.1078549869940875 \cdot 10^{+83}:\\ \;\;\;\;\mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\ \mathbf{elif}\;x \leq -8.642290443819559 \cdot 10^{-126}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{elif}\;x \leq 9.883008504928267 \cdot 10^{-118}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 2.259789577185776 \cdot 10^{-48}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{elif}\;x \leq 8.07270989499859 \cdot 10^{-14}:\\ \;\;\;\;-1 + 0.5 \cdot \frac{{x}^{2}}{{y}^{2}}\\ \mathbf{elif}\;x \leq 1.11024680895731 \cdot 10^{+69}:\\ \;\;\;\;\frac{x \cdot x - y \cdot \left(y \cdot 4\right)}{x \cdot x + y \cdot \left(y \cdot 4\right)}\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left({\left(\frac{y}{x}\right)}^{2}, -8, 1\right)\\ \end{array} \]

Reproduce

herbie shell --seed 2022160 
(FPCore (x y)
  :name "Diagrams.TwoD.Arc:arcBetween from diagrams-lib-1.3.0.3"
  :precision binary64

  :herbie-target
  (if (< (/ (- (* x x) (* (* y 4.0) y)) (+ (* x x) (* (* y 4.0) y))) 0.9743233849626781) (- (/ (* x x) (+ (* x x) (* (* y y) 4.0))) (/ (* (* y y) 4.0) (+ (* x x) (* (* y y) 4.0)))) (- (pow (/ x (sqrt (+ (* x x) (* (* y y) 4.0)))) 2.0) (/ (* (* y y) 4.0) (+ (* x x) (* (* y y) 4.0)))))

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