Average Error: 31.5 → 12.1
Time: 2.0s
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 := \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}\;x \leq -2.413208438391072 \cdot 10^{+115}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -3.960288759049152 \cdot 10^{-146}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 3.688311616163236 \cdot 10^{-129}:\\ \;\;\;\;\mathsf{fma}\left(0.5, {\left(\frac{x}{y}\right)}^{2}, -1\right)\\ \mathbf{elif}\;x \leq 3.0538265363810125 \cdot 10^{+127}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;1\\ \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 := \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}\;x \leq -2.413208438391072 \cdot 10^{+115}:\\
\;\;\;\;1\\

\mathbf{elif}\;x \leq -3.960288759049152 \cdot 10^{-146}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq 3.688311616163236 \cdot 10^{-129}:\\
\;\;\;\;\mathsf{fma}\left(0.5, {\left(\frac{x}{y}\right)}^{2}, -1\right)\\

\mathbf{elif}\;x \leq 3.0538265363810125 \cdot 10^{+127}:\\
\;\;\;\;t_0\\

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


\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 -4.0 (* y y) (* x x)) (fma x x (* y (* y 4.0))))))
   (if (<= x -2.413208438391072e+115)
     1.0
     (if (<= x -3.960288759049152e-146)
       t_0
       (if (<= x 3.688311616163236e-129)
         (fma 0.5 (pow (/ x y) 2.0) -1.0)
         (if (<= x 3.0538265363810125e+127) t_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 t_0 = fma(-4.0, (y * y), (x * x)) / fma(x, x, (y * (y * 4.0)));
	double tmp;
	if (x <= -2.413208438391072e+115) {
		tmp = 1.0;
	} else if (x <= -3.960288759049152e-146) {
		tmp = t_0;
	} else if (x <= 3.688311616163236e-129) {
		tmp = fma(0.5, pow((x / y), 2.0), -1.0);
	} else if (x <= 3.0538265363810125e+127) {
		tmp = t_0;
	} else {
		tmp = 1.0;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.5
Target31.2
Herbie12.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 3 regimes
  2. if x < -2.413208438391072e115 or 3.05382653638101246e127 < x

    1. Initial program 55.7

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

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

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

    if -2.413208438391072e115 < x < -3.96028875904915183e-146 or 3.6883116161632359e-129 < x < 3.05382653638101246e127

    1. Initial program 15.8

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

    if -3.96028875904915183e-146 < x < 3.6883116161632359e-129

    1. Initial program 28.9

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(0.5, \frac{x \cdot x}{y \cdot y}, -1\right)} \]
    5. Applied egg-rr8.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -2.413208438391072 \cdot 10^{+115}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -3.960288759049152 \cdot 10^{-146}:\\ \;\;\;\;\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 \leq 3.688311616163236 \cdot 10^{-129}:\\ \;\;\;\;\mathsf{fma}\left(0.5, {\left(\frac{x}{y}\right)}^{2}, -1\right)\\ \mathbf{elif}\;x \leq 3.0538265363810125 \cdot 10^{+127}:\\ \;\;\;\;\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{else}:\\ \;\;\;\;1\\ \end{array} \]

Reproduce

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