Average Error: 31.7 → 13.3
Time: 2.2s
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 := \mathsf{fma}\left(0.5, {\left(\frac{x}{y}\right)}^{2}, -1\right)\\ \mathbf{if}\;y \leq -5.217929847352783 \cdot 10^{+148}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_1 := \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)}\\ \mathbf{if}\;y \leq -2.542146711786574 \cdot 10^{-111}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 1.1008163448544512 \cdot 10^{-116}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 5.494591362659238 \cdot 10^{-74}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;y \leq 0.7272173049803466:\\ \;\;\;\;\mathsf{fma}\left(\frac{y \cdot y}{x \cdot x}, -8, 1\right)\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array}\\ \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 := \mathsf{fma}\left(0.5, {\left(\frac{x}{y}\right)}^{2}, -1\right)\\
\mathbf{if}\;y \leq -5.217929847352783 \cdot 10^{+148}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_1 := \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)}\\
\mathbf{if}\;y \leq -2.542146711786574 \cdot 10^{-111}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;y \leq 1.1008163448544512 \cdot 10^{-116}:\\
\;\;\;\;1\\

\mathbf{elif}\;y \leq 5.494591362659238 \cdot 10^{-74}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;y \leq 0.7272173049803466:\\
\;\;\;\;\mathsf{fma}\left(\frac{y \cdot y}{x \cdot x}, -8, 1\right)\\

\mathbf{else}:\\
\;\;\;\;t_0\\


\end{array}\\


\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 (fma 0.5 (pow (/ x y) 2.0) -1.0)))
   (if (<= y -5.217929847352783e+148)
     t_0
     (let* ((t_1 (/ (fma -4.0 (* y y) (* x x)) (fma x x (* y (* y 4.0))))))
       (if (<= y -2.542146711786574e-111)
         t_1
         (if (<= y 1.1008163448544512e-116)
           1.0
           (if (<= y 5.494591362659238e-74)
             t_1
             (if (<= y 0.7272173049803466)
               (fma (/ (* y y) (* x x)) -8.0 1.0)
               t_0))))))))
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 = fma(0.5, pow((x / y), 2.0), -1.0);
	double tmp;
	if (y <= -5.217929847352783e+148) {
		tmp = t_0;
	} else {
		double t_1 = fma(-4.0, (y * y), (x * x)) / fma(x, x, (y * (y * 4.0)));
		double tmp_1;
		if (y <= -2.542146711786574e-111) {
			tmp_1 = t_1;
		} else if (y <= 1.1008163448544512e-116) {
			tmp_1 = 1.0;
		} else if (y <= 5.494591362659238e-74) {
			tmp_1 = t_1;
		} else if (y <= 0.7272173049803466) {
			tmp_1 = fma(((y * y) / (x * x)), -8.0, 1.0);
		} else {
			tmp_1 = t_0;
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.7
Target31.4
Herbie13.3
\[\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 y < -5.2179298473527829e148 or 0.727217304980346646 < y

    1. Initial program 47.8

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

      \[\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 17.4

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

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

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

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

    if -5.2179298473527829e148 < y < -2.542146711786574e-111 or 1.10081634485445124e-116 < y < 5.494591362659238e-74

    1. Initial program 15.9

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

      \[\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)}} \]

    if -2.542146711786574e-111 < y < 1.10081634485445124e-116

    1. Initial program 27.9

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

      \[\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 10.0

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

    if 5.494591362659238e-74 < y < 0.727217304980346646

    1. Initial program 14.8

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

      \[\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 28.0

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\frac{y \cdot y}{x \cdot x}, -8, 1\right)} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification13.3

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \leq -5.217929847352783 \cdot 10^{+148}:\\ \;\;\;\;\mathsf{fma}\left(0.5, {\left(\frac{x}{y}\right)}^{2}, -1\right)\\ \mathbf{elif}\;y \leq -2.542146711786574 \cdot 10^{-111}:\\ \;\;\;\;\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)}\\ \mathbf{elif}\;y \leq 1.1008163448544512 \cdot 10^{-116}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \leq 5.494591362659238 \cdot 10^{-74}:\\ \;\;\;\;\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)}\\ \mathbf{elif}\;y \leq 0.7272173049803466:\\ \;\;\;\;\mathsf{fma}\left(\frac{y \cdot y}{x \cdot x}, -8, 1\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(0.5, {\left(\frac{x}{y}\right)}^{2}, -1\right)\\ \end{array} \]

Reproduce

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