Average Error: 32.2 → 12.6
Time: 2.4s
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)\\ \mathbf{if}\;t_0 \leq 5.532585927385684 \cdot 10^{-221}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\begin{array}{l} t_1 := \frac{\mathsf{fma}\left(-4, y \cdot y, x \cdot x\right)}{\mathsf{fma}\left(x, x, t_0\right)}\\ \mathbf{if}\;t_0 \leq 2.6024495877732724 \cdot 10^{-139}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;t_0 \leq 5.15477157741059 \cdot 10^{-70}:\\ \;\;\;\;1 - 8 \cdot \frac{{y}^{2}}{{x}^{2}}\\ \mathbf{elif}\;t_0 \leq 6.315446797825809 \cdot 10^{+249}:\\ \;\;\;\;t_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}
t_0 := y \cdot \left(y \cdot 4\right)\\
\mathbf{if}\;t_0 \leq 5.532585927385684 \cdot 10^{-221}:\\
\;\;\;\;1\\

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

\mathbf{elif}\;t_0 \leq 5.15477157741059 \cdot 10^{-70}:\\
\;\;\;\;1 - 8 \cdot \frac{{y}^{2}}{{x}^{2}}\\

\mathbf{elif}\;t_0 \leq 6.315446797825809 \cdot 10^{+249}:\\
\;\;\;\;t_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
 (let* ((t_0 (* y (* y 4.0))))
   (if (<= t_0 5.532585927385684e-221)
     1.0
     (let* ((t_1 (/ (fma -4.0 (* y y) (* x x)) (fma x x t_0))))
       (if (<= t_0 2.6024495877732724e-139)
         t_1
         (if (<= t_0 5.15477157741059e-70)
           (- 1.0 (* 8.0 (/ (pow y 2.0) (pow x 2.0))))
           (if (<= t_0 6.315446797825809e+249) t_1 -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 = y * (y * 4.0);
	double tmp;
	if (t_0 <= 5.532585927385684e-221) {
		tmp = 1.0;
	} else {
		double t_1 = fma(-4.0, (y * y), (x * x)) / fma(x, x, t_0);
		double tmp_1;
		if (t_0 <= 2.6024495877732724e-139) {
			tmp_1 = t_1;
		} else if (t_0 <= 5.15477157741059e-70) {
			tmp_1 = 1.0 - (8.0 * (pow(y, 2.0) / pow(x, 2.0)));
		} else if (t_0 <= 6.315446797825809e+249) {
			tmp_1 = t_1;
		} else {
			tmp_1 = -1.0;
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original32.2
Target31.9
Herbie12.6
\[\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 (*.f64 (*.f64 y 4) y) < 5.53258592738568434e-221

    1. Initial program 28.1

      \[\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{x \cdot x + \left(y \cdot 4\right) \cdot y} \]
    2. Simplified28.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. Taylor expanded in y around 0 9.9

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

    if 5.53258592738568434e-221 < (*.f64 (*.f64 y 4) y) < 2.60244958777327243e-139 or 5.15477157741059e-70 < (*.f64 (*.f64 y 4) y) < 6.3154467978258089e249

    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} \]
    2. Simplified16.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)}} \]

    if 2.60244958777327243e-139 < (*.f64 (*.f64 y 4) y) < 5.15477157741059e-70

    1. Initial program 17.1

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

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

    if 6.3154467978258089e249 < (*.f64 (*.f64 y 4) y)

    1. Initial program 56.1

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

      \[\leadsto \color{blue}{-1} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification12.6

    \[\leadsto \begin{array}{l} \mathbf{if}\;y \cdot \left(y \cdot 4\right) \leq 5.532585927385684 \cdot 10^{-221}:\\ \;\;\;\;1\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 2.6024495877732724 \cdot 10^{-139}:\\ \;\;\;\;\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 \cdot \left(y \cdot 4\right) \leq 5.15477157741059 \cdot 10^{-70}:\\ \;\;\;\;1 - 8 \cdot \frac{{y}^{2}}{{x}^{2}}\\ \mathbf{elif}\;y \cdot \left(y \cdot 4\right) \leq 6.315446797825809 \cdot 10^{+249}:\\ \;\;\;\;\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 2022097 
(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))))