Average Error: 31.5 → 14.3
Time: 4.9s
Precision: 64
\[\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 \le -3.877884302386560567265423807009686924875 \cdot 10^{-18}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \le 1.703195619996579311686110250358135262091 \cdot 10^{-158}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \le 4.17369206762082176000342914188807618096 \cdot 10^{81}:\\ \;\;\;\;\frac{x \cdot x}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)} - \frac{\left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\\ \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}
\mathbf{if}\;x \le -3.877884302386560567265423807009686924875 \cdot 10^{-18}:\\
\;\;\;\;1\\

\mathbf{elif}\;x \le 1.703195619996579311686110250358135262091 \cdot 10^{-158}:\\
\;\;\;\;-1\\

\mathbf{elif}\;x \le 4.17369206762082176000342914188807618096 \cdot 10^{81}:\\
\;\;\;\;\frac{x \cdot x}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)} - \frac{\left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\\

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

\end{array}
double f(double x, double y) {
        double r561884 = x;
        double r561885 = r561884 * r561884;
        double r561886 = y;
        double r561887 = 4.0;
        double r561888 = r561886 * r561887;
        double r561889 = r561888 * r561886;
        double r561890 = r561885 - r561889;
        double r561891 = r561885 + r561889;
        double r561892 = r561890 / r561891;
        return r561892;
}

double f(double x, double y) {
        double r561893 = x;
        double r561894 = -3.8778843023865606e-18;
        bool r561895 = r561893 <= r561894;
        double r561896 = 1.0;
        double r561897 = 1.7031956199965793e-158;
        bool r561898 = r561893 <= r561897;
        double r561899 = -1.0;
        double r561900 = 4.173692067620822e+81;
        bool r561901 = r561893 <= r561900;
        double r561902 = r561893 * r561893;
        double r561903 = y;
        double r561904 = 4.0;
        double r561905 = r561903 * r561904;
        double r561906 = r561905 * r561903;
        double r561907 = fma(r561893, r561893, r561906);
        double r561908 = r561902 / r561907;
        double r561909 = r561906 / r561907;
        double r561910 = r561908 - r561909;
        double r561911 = r561901 ? r561910 : r561896;
        double r561912 = r561898 ? r561899 : r561911;
        double r561913 = r561895 ? r561896 : r561912;
        return r561913;
}

Error

Bits error versus x

Bits error versus y

Target

Original31.5
Target31.2
Herbie14.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} \lt 0.9743233849626781184483093056769575923681:\\ \;\;\;\;\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.8778843023865606e-18 or 4.173692067620822e+81 < x

    1. Initial program 42.4

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

      \[\leadsto \color{blue}{\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}}\]
    3. Taylor expanded around inf 14.4

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

    if -3.8778843023865606e-18 < x < 1.7031956199965793e-158

    1. Initial program 25.8

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

      \[\leadsto \color{blue}{\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}}\]
    3. Taylor expanded around 0 12.2

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

    if 1.7031956199965793e-158 < x < 4.173692067620822e+81

    1. Initial program 17.7

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

      \[\leadsto \color{blue}{\frac{x \cdot x - \left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}}\]
    3. Using strategy rm
    4. Applied div-sub17.7

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;x \le -3.877884302386560567265423807009686924875 \cdot 10^{-18}:\\ \;\;\;\;1\\ \mathbf{elif}\;x \le 1.703195619996579311686110250358135262091 \cdot 10^{-158}:\\ \;\;\;\;-1\\ \mathbf{elif}\;x \le 4.17369206762082176000342914188807618096 \cdot 10^{81}:\\ \;\;\;\;\frac{x \cdot x}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)} - \frac{\left(y \cdot 4\right) \cdot y}{\mathsf{fma}\left(x, x, \left(y \cdot 4\right) \cdot y\right)}\\ \mathbf{else}:\\ \;\;\;\;1\\ \end{array}\]

Reproduce

herbie shell --seed 2019351 +o rules:numerics
(FPCore (x y)
  :name "Diagrams.TwoD.Arc:arcBetween from diagrams-lib-1.3.0.3"
  :precision binary64

  :herbie-target
  (if (< (/ (- (* x x) (* (* y 4) y)) (+ (* x x) (* (* y 4) y))) 0.9743233849626781) (- (/ (* x x) (+ (* x x) (* (* y y) 4))) (/ (* (* y y) 4) (+ (* x x) (* (* y y) 4)))) (- (pow (/ x (sqrt (+ (* x x) (* (* y y) 4)))) 2) (/ (* (* y y) 4) (+ (* x x) (* (* y y) 4)))))

  (/ (- (* x x) (* (* y 4) y)) (+ (* x x) (* (* y 4) y))))