Average Error: 31.8 → 13.2
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} \mathbf{if}\;x \cdot x \leq 1.434275908034364 \cdot 10^{-214}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_0 := \mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)\\ t_1 := \mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)\\ \mathbf{if}\;x \cdot x \leq 3.549117142306552 \cdot 10^{+122}:\\ \;\;\;\;\frac{t_0}{t_1}\\ \mathbf{elif}\;x \cdot x \leq 4.0770094441479955 \cdot 10^{+154}:\\ \;\;\;\;\mathsf{fma}\left({\left(\frac{x}{y}\right)}^{4}, -0.125, \mathsf{fma}\left(0.5, \frac{x \cdot x}{y \cdot y}, -1\right)\right)\\ \mathbf{elif}\;x \cdot x \leq 4.926949301618463 \cdot 10^{+215}:\\ \;\;\;\;\frac{1}{\frac{t_1}{t_0}}\\ \mathbf{elif}\;x \cdot x \leq 2.6925800993611535 \cdot 10^{+241}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \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}
\mathbf{if}\;x \cdot x \leq 1.434275908034364 \cdot 10^{-214}:\\
\;\;\;\;-1\\

\mathbf{else}:\\
\;\;\;\;\begin{array}{l}
t_0 := \mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)\\
t_1 := \mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)\\
\mathbf{if}\;x \cdot x \leq 3.549117142306552 \cdot 10^{+122}:\\
\;\;\;\;\frac{t_0}{t_1}\\

\mathbf{elif}\;x \cdot x \leq 4.0770094441479955 \cdot 10^{+154}:\\
\;\;\;\;\mathsf{fma}\left({\left(\frac{x}{y}\right)}^{4}, -0.125, \mathsf{fma}\left(0.5, \frac{x \cdot x}{y \cdot y}, -1\right)\right)\\

\mathbf{elif}\;x \cdot x \leq 4.926949301618463 \cdot 10^{+215}:\\
\;\;\;\;\frac{1}{\frac{t_1}{t_0}}\\

\mathbf{elif}\;x \cdot x \leq 2.6925800993611535 \cdot 10^{+241}:\\
\;\;\;\;-1\\

\mathbf{else}:\\
\;\;\;\;1\\


\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
 (if (<= (* x x) 1.434275908034364e-214)
   -1.0
   (let* ((t_0 (fma -4.0 (* y y) (* x x))) (t_1 (fma x x (* y (* y 4.0)))))
     (if (<= (* x x) 3.549117142306552e+122)
       (/ t_0 t_1)
       (if (<= (* x x) 4.0770094441479955e+154)
         (fma (pow (/ x y) 4.0) -0.125 (fma 0.5 (/ (* x x) (* y y)) -1.0))
         (if (<= (* x x) 4.926949301618463e+215)
           (/ 1.0 (/ t_1 t_0))
           (if (<= (* x x) 2.6925800993611535e+241) -1.0 1.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 tmp;
	if ((x * x) <= 1.434275908034364e-214) {
		tmp = -1.0;
	} else {
		double t_0 = fma(-4.0, (y * y), (x * x));
		double t_1 = fma(x, x, (y * (y * 4.0)));
		double tmp_1;
		if ((x * x) <= 3.549117142306552e+122) {
			tmp_1 = t_0 / t_1;
		} else if ((x * x) <= 4.0770094441479955e+154) {
			tmp_1 = fma(pow((x / y), 4.0), -0.125, fma(0.5, ((x * x) / (y * y)), -1.0));
		} else if ((x * x) <= 4.926949301618463e+215) {
			tmp_1 = 1.0 / (t_1 / t_0);
		} else if ((x * x) <= 2.6925800993611535e+241) {
			tmp_1 = -1.0;
		} else {
			tmp_1 = 1.0;
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.8
Target31.5
Herbie13.2
\[\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 5 regimes
  2. if (*.f64 x x) < 1.43427590803436405e-214 or 4.9269493016184627e215 < (*.f64 x x) < 2.69258009936115346e241

    1. Initial program 27.2

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified27.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 12.5

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

    if 1.43427590803436405e-214 < (*.f64 x x) < 3.54911714230655185e122

    1. Initial program 16.0

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified16.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)}} \]

    if 3.54911714230655185e122 < (*.f64 x x) < 4.07700944414799548e154

    1. Initial program 16.4

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

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

      \[\leadsto \color{blue}{0.5 \cdot \frac{{x}^{2}}{{y}^{2}} - \left(1 + 0.125 \cdot \frac{{x}^{4}}{{y}^{4}}\right)} \]
    4. Simplified36.5

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

    if 4.07700944414799548e154 < (*.f64 x x) < 4.9269493016184627e215

    1. Initial program 13.2

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

      \[\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. Applied clear-num_binary6413.2

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

    if 2.69258009936115346e241 < (*.f64 x x)

    1. Initial program 55.3

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified55.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 0 9.6

      \[\leadsto \color{blue}{1} \]
  3. Recombined 5 regimes into one program.
  4. Final simplification13.2

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \cdot x \leq 1.434275908034364 \cdot 10^{-214}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \cdot x \leq 3.549117142306552 \cdot 10^{+122}:\\ \;\;\;\;\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}\;x \cdot x \leq 4.0770094441479955 \cdot 10^{+154}:\\ \;\;\;\;\mathsf{fma}\left({\left(\frac{x}{y}\right)}^{4}, -0.125, \mathsf{fma}\left(0.5, \frac{x \cdot x}{y \cdot y}, -1\right)\right)\\ \mathbf{elif}\;x \cdot x \leq 4.926949301618463 \cdot 10^{+215}:\\ \;\;\;\;\frac{1}{\frac{\mathsf{fma}\left(x, x, y \cdot \left(y \cdot 4\right)\right)}{\mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)}}\\ \mathbf{elif}\;x \cdot x \leq 2.6925800993611535 \cdot 10^{+241}:\\ \;\;\;\;-1\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array} \]

Reproduce

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