Average Error: 31.6 → 13.6
Time: 1.7s
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 \leq -3.915390878948144 \cdot 10^{+55}:\\ \;\;\;\;1\\ \mathbf{else}:\\ \;\;\;\;\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 -5.5870126386508446 \cdot 10^{-151}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;x \leq 4.6768304092466014 \cdot 10^{-97}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 1.8953957386349883 \cdot 10^{+27}:\\ \;\;\;\;t_0\\ \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 \leq -3.915390878948144 \cdot 10^{+55}:\\
\;\;\;\;1\\

\mathbf{else}:\\
\;\;\;\;\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 -5.5870126386508446 \cdot 10^{-151}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;x \leq 4.6768304092466014 \cdot 10^{-97}:\\
\;\;\;\;-1\\

\mathbf{elif}\;x \leq 1.8953957386349883 \cdot 10^{+27}:\\
\;\;\;\;t_0\\

\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 -3.915390878948144e+55)
   1.0
   (let* ((t_0 (/ (fma -4.0 (* y y) (* x x)) (fma x x (* y (* y 4.0))))))
     (if (<= x -5.5870126386508446e-151)
       t_0
       (if (<= x 4.6768304092466014e-97)
         -1.0
         (if (<= x 1.8953957386349883e+27) 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 tmp;
	if (x <= -3.915390878948144e+55) {
		tmp = 1.0;
	} else {
		double t_0 = fma(-4.0, (y * y), (x * x)) / fma(x, x, (y * (y * 4.0)));
		double tmp_1;
		if (x <= -5.5870126386508446e-151) {
			tmp_1 = t_0;
		} else if (x <= 4.6768304092466014e-97) {
			tmp_1 = -1.0;
		} else if (x <= 1.8953957386349883e+27) {
			tmp_1 = t_0;
		} else {
			tmp_1 = 1.0;
		}
		tmp = tmp_1;
	}
	return tmp;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.6
Target31.3
Herbie13.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 3 regimes
  2. if x < -3.9153908789481439e55 or 1.89539573863498833e27 < x

    1. Initial program 43.5

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

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

    if -3.9153908789481439e55 < x < -5.58701263865084461e-151 or 4.6768304092466014e-97 < x < 1.89539573863498833e27

    1. Initial program 15.7

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

    if -5.58701263865084461e-151 < x < 4.6768304092466014e-97

    1. Initial program 28.3

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \leq -3.915390878948144 \cdot 10^{+55}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \leq -5.5870126386508446 \cdot 10^{-151}:\\ \;\;\;\;\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 4.6768304092466014 \cdot 10^{-97}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \leq 1.8953957386349883 \cdot 10^{+27}:\\ \;\;\;\;\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 2022068 
(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))))