Average Error: 31.3 → 15.7
Time: 11.0s
Precision: 64
\[r1 \cdot r2 \ge 0.0\]
\[\sqrt{\left(r1 \cdot r1 + r2 \cdot r2\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)}\]
\[\begin{array}{l} \mathbf{if}\;r1 \le -4.903081935379276208383199239909765108787 \cdot 10^{114}:\\ \;\;\;\;1 \cdot \left(\cos \left(a1 - a2\right) \cdot r2\right) - r1\\ \mathbf{elif}\;r1 \le -3.000564200028333590835864285802753473041 \cdot 10^{-297}:\\ \;\;\;\;\sqrt{r1 \cdot r1 + \left(r2 \cdot r2 - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)\right)}\\ \mathbf{elif}\;r1 \le 7.704118185374544601168772085035844993565 \cdot 10^{-81}:\\ \;\;\;\;r2 - 1 \cdot r1\\ \mathbf{elif}\;r1 \le 1.882718002248940187669787563569777081448 \cdot 10^{101}:\\ \;\;\;\;\sqrt{r1 \cdot r1 + \left(r2 \cdot r2 - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;r1 - 1 \cdot \left(\cos \left(a1 - a2\right) \cdot r2\right)\\ \end{array}\]
\sqrt{\left(r1 \cdot r1 + r2 \cdot r2\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)}
\begin{array}{l}
\mathbf{if}\;r1 \le -4.903081935379276208383199239909765108787 \cdot 10^{114}:\\
\;\;\;\;1 \cdot \left(\cos \left(a1 - a2\right) \cdot r2\right) - r1\\

\mathbf{elif}\;r1 \le -3.000564200028333590835864285802753473041 \cdot 10^{-297}:\\
\;\;\;\;\sqrt{r1 \cdot r1 + \left(r2 \cdot r2 - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)\right)}\\

\mathbf{elif}\;r1 \le 7.704118185374544601168772085035844993565 \cdot 10^{-81}:\\
\;\;\;\;r2 - 1 \cdot r1\\

\mathbf{elif}\;r1 \le 1.882718002248940187669787563569777081448 \cdot 10^{101}:\\
\;\;\;\;\sqrt{r1 \cdot r1 + \left(r2 \cdot r2 - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;r1 - 1 \cdot \left(\cos \left(a1 - a2\right) \cdot r2\right)\\

\end{array}
double f(double r1, double r2, double a1, double a2) {
        double r448143 = r1;
        double r448144 = r448143 * r448143;
        double r448145 = r2;
        double r448146 = r448145 * r448145;
        double r448147 = r448144 + r448146;
        double r448148 = 2.0;
        double r448149 = r448148 * r448143;
        double r448150 = r448149 * r448145;
        double r448151 = a1;
        double r448152 = a2;
        double r448153 = r448151 - r448152;
        double r448154 = cos(r448153);
        double r448155 = r448150 * r448154;
        double r448156 = r448147 - r448155;
        double r448157 = sqrt(r448156);
        return r448157;
}

double f(double r1, double r2, double a1, double a2) {
        double r448158 = r1;
        double r448159 = -4.903081935379276e+114;
        bool r448160 = r448158 <= r448159;
        double r448161 = 1.0;
        double r448162 = a1;
        double r448163 = a2;
        double r448164 = r448162 - r448163;
        double r448165 = cos(r448164);
        double r448166 = r2;
        double r448167 = r448165 * r448166;
        double r448168 = r448161 * r448167;
        double r448169 = r448168 - r448158;
        double r448170 = -3.0005642000283336e-297;
        bool r448171 = r448158 <= r448170;
        double r448172 = r448158 * r448158;
        double r448173 = r448166 * r448166;
        double r448174 = 2.0;
        double r448175 = r448174 * r448158;
        double r448176 = r448175 * r448166;
        double r448177 = r448176 * r448165;
        double r448178 = r448173 - r448177;
        double r448179 = r448172 + r448178;
        double r448180 = sqrt(r448179);
        double r448181 = 7.704118185374545e-81;
        bool r448182 = r448158 <= r448181;
        double r448183 = r448161 * r448158;
        double r448184 = r448166 - r448183;
        double r448185 = 1.8827180022489402e+101;
        bool r448186 = r448158 <= r448185;
        double r448187 = r448158 - r448168;
        double r448188 = r448186 ? r448180 : r448187;
        double r448189 = r448182 ? r448184 : r448188;
        double r448190 = r448171 ? r448180 : r448189;
        double r448191 = r448160 ? r448169 : r448190;
        return r448191;
}

Error

Bits error versus r1

Bits error versus r2

Bits error versus a1

Bits error versus a2

Try it out

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation

  1. Split input into 4 regimes
  2. if r1 < -4.903081935379276e+114

    1. Initial program 53.2

      \[\sqrt{\left(r1 \cdot r1 + r2 \cdot r2\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)}\]
    2. Taylor expanded around -inf 10.1

      \[\leadsto \color{blue}{1 \cdot \left(\cos \left(a1 - a2\right) \cdot r2\right) - r1}\]

    if -4.903081935379276e+114 < r1 < -3.0005642000283336e-297 or 7.704118185374545e-81 < r1 < 1.8827180022489402e+101

    1. Initial program 19.4

      \[\sqrt{\left(r1 \cdot r1 + r2 \cdot r2\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)}\]
    2. Using strategy rm
    3. Applied associate--l+19.4

      \[\leadsto \sqrt{\color{blue}{r1 \cdot r1 + \left(r2 \cdot r2 - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)\right)}}\]

    if -3.0005642000283336e-297 < r1 < 7.704118185374545e-81

    1. Initial program 25.1

      \[\sqrt{\left(r1 \cdot r1 + r2 \cdot r2\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)}\]
    2. Taylor expanded around 0 15.0

      \[\leadsto \color{blue}{r2 - 1 \cdot r1}\]

    if 1.8827180022489402e+101 < r1

    1. Initial program 52.1

      \[\sqrt{\left(r1 \cdot r1 + r2 \cdot r2\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)}\]
    2. Taylor expanded around inf 11.2

      \[\leadsto \color{blue}{r1 - 1 \cdot \left(\cos \left(a1 - a2\right) \cdot r2\right)}\]
  3. Recombined 4 regimes into one program.
  4. Final simplification15.7

    \[\leadsto \begin{array}{l} \mathbf{if}\;r1 \le -4.903081935379276208383199239909765108787 \cdot 10^{114}:\\ \;\;\;\;1 \cdot \left(\cos \left(a1 - a2\right) \cdot r2\right) - r1\\ \mathbf{elif}\;r1 \le -3.000564200028333590835864285802753473041 \cdot 10^{-297}:\\ \;\;\;\;\sqrt{r1 \cdot r1 + \left(r2 \cdot r2 - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)\right)}\\ \mathbf{elif}\;r1 \le 7.704118185374544601168772085035844993565 \cdot 10^{-81}:\\ \;\;\;\;r2 - 1 \cdot r1\\ \mathbf{elif}\;r1 \le 1.882718002248940187669787563569777081448 \cdot 10^{101}:\\ \;\;\;\;\sqrt{r1 \cdot r1 + \left(r2 \cdot r2 - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \cos \left(a1 - a2\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;r1 - 1 \cdot \left(\cos \left(a1 - a2\right) \cdot r2\right)\\ \end{array}\]

Reproduce

herbie shell --seed 1 
(FPCore (r1 r2 a1 a2)
  :name "sqrt(r1*r1 + r2*r2 - 2*r1*r2*cos(a1-a2))"
  :precision binary64
  :pre (>= (* r1 r2) 0.0)
  (sqrt (- (+ (* r1 r1) (* r2 r2)) (* (* (* 2 r1) r2) (cos (- a1 a2))))))