Average Error: 31.5 → 18.3
Time: 17.3s
Precision: 64
\[\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 -0.01929507647729479871689939329826302127913:\\ \;\;\;\;r2 - 1 \cdot r1\\ \mathbf{elif}\;r1 \le -2.440081527836891534362372241579093937045 \cdot 10^{-135}:\\ \;\;\;\;\sqrt{\left(r1 \cdot r1 + r2 \cdot \left(r2 - \left(\cos a1 \cdot \cos a2\right) \cdot \left(2 \cdot r1\right)\right)\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \left(\sin a1 \cdot \sin a2\right)}\\ \mathbf{elif}\;r1 \le 1.578758057424873050820632410161984620039 \cdot 10^{-229}:\\ \;\;\;\;r2 - 1 \cdot r1\\ \mathbf{elif}\;r1 \le 3.114724519917716658260886683394897619023 \cdot 10^{-200}:\\ \;\;\;\;\sqrt{\left(r1 \cdot r1 + r2 \cdot \left(r2 - \left(\cos a1 \cdot \cos a2\right) \cdot \left(2 \cdot r1\right)\right)\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \left(\sin a1 \cdot \sin a2\right)}\\ \mathbf{elif}\;r1 \le 5.115803934409726180165957423667104320383 \cdot 10^{-132}:\\ \;\;\;\;r2 - 1 \cdot r1\\ \mathbf{elif}\;r1 \le 5.806824695993501907198552913769162842073 \cdot 10^{-4}:\\ \;\;\;\;\sqrt{\left(r1 \cdot r1 + r2 \cdot \left(r2 - \left(\cos a1 \cdot \cos a2\right) \cdot \left(2 \cdot r1\right)\right)\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \left(\sin a1 \cdot \sin a2\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 -0.01929507647729479871689939329826302127913:\\
\;\;\;\;r2 - 1 \cdot r1\\

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

\mathbf{elif}\;r1 \le 1.578758057424873050820632410161984620039 \cdot 10^{-229}:\\
\;\;\;\;r2 - 1 \cdot r1\\

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

\mathbf{elif}\;r1 \le 5.115803934409726180165957423667104320383 \cdot 10^{-132}:\\
\;\;\;\;r2 - 1 \cdot r1\\

\mathbf{elif}\;r1 \le 5.806824695993501907198552913769162842073 \cdot 10^{-4}:\\
\;\;\;\;\sqrt{\left(r1 \cdot r1 + r2 \cdot \left(r2 - \left(\cos a1 \cdot \cos a2\right) \cdot \left(2 \cdot r1\right)\right)\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \left(\sin a1 \cdot \sin a2\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 r415500 = r1;
        double r415501 = r415500 * r415500;
        double r415502 = r2;
        double r415503 = r415502 * r415502;
        double r415504 = r415501 + r415503;
        double r415505 = 2.0;
        double r415506 = r415505 * r415500;
        double r415507 = r415506 * r415502;
        double r415508 = a1;
        double r415509 = a2;
        double r415510 = r415508 - r415509;
        double r415511 = cos(r415510);
        double r415512 = r415507 * r415511;
        double r415513 = r415504 - r415512;
        double r415514 = sqrt(r415513);
        return r415514;
}

double f(double r1, double r2, double a1, double a2) {
        double r415515 = r1;
        double r415516 = -0.0192950764772948;
        bool r415517 = r415515 <= r415516;
        double r415518 = r2;
        double r415519 = 1.0;
        double r415520 = r415519 * r415515;
        double r415521 = r415518 - r415520;
        double r415522 = -2.4400815278368915e-135;
        bool r415523 = r415515 <= r415522;
        double r415524 = r415515 * r415515;
        double r415525 = a1;
        double r415526 = cos(r415525);
        double r415527 = a2;
        double r415528 = cos(r415527);
        double r415529 = r415526 * r415528;
        double r415530 = 2.0;
        double r415531 = r415530 * r415515;
        double r415532 = r415529 * r415531;
        double r415533 = r415518 - r415532;
        double r415534 = r415518 * r415533;
        double r415535 = r415524 + r415534;
        double r415536 = r415531 * r415518;
        double r415537 = sin(r415525);
        double r415538 = sin(r415527);
        double r415539 = r415537 * r415538;
        double r415540 = r415536 * r415539;
        double r415541 = r415535 - r415540;
        double r415542 = sqrt(r415541);
        double r415543 = 1.578758057424873e-229;
        bool r415544 = r415515 <= r415543;
        double r415545 = 3.1147245199177167e-200;
        bool r415546 = r415515 <= r415545;
        double r415547 = 5.115803934409726e-132;
        bool r415548 = r415515 <= r415547;
        double r415549 = 0.0005806824695993502;
        bool r415550 = r415515 <= r415549;
        double r415551 = r415525 - r415527;
        double r415552 = cos(r415551);
        double r415553 = r415552 * r415518;
        double r415554 = r415519 * r415553;
        double r415555 = r415515 - r415554;
        double r415556 = r415550 ? r415542 : r415555;
        double r415557 = r415548 ? r415521 : r415556;
        double r415558 = r415546 ? r415542 : r415557;
        double r415559 = r415544 ? r415521 : r415558;
        double r415560 = r415523 ? r415542 : r415559;
        double r415561 = r415517 ? r415521 : r415560;
        return r415561;
}

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 3 regimes
  2. if r1 < -0.0192950764772948 or -2.4400815278368915e-135 < r1 < 1.578758057424873e-229 or 3.1147245199177167e-200 < r1 < 5.115803934409726e-132

    1. Initial program 33.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. Taylor expanded around 0 20.6

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

    if -0.0192950764772948 < r1 < -2.4400815278368915e-135 or 1.578758057424873e-229 < r1 < 3.1147245199177167e-200 or 5.115803934409726e-132 < r1 < 0.0005806824695993502

    1. Initial program 17.7

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

      \[\leadsto \sqrt{\left(r1 \cdot r1 + r2 \cdot r2\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \color{blue}{\left(\cos a1 \cdot \cos a2 + \sin a1 \cdot \sin a2\right)}}\]
    4. Applied distribute-lft-in17.4

      \[\leadsto \sqrt{\left(r1 \cdot r1 + r2 \cdot r2\right) - \color{blue}{\left(\left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \left(\cos a1 \cdot \cos a2\right) + \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \left(\sin a1 \cdot \sin a2\right)\right)}}\]
    5. Applied associate--r+17.4

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

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

    if 0.0005806824695993502 < r1

    1. Initial program 39.9

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;r1 \le -0.01929507647729479871689939329826302127913:\\ \;\;\;\;r2 - 1 \cdot r1\\ \mathbf{elif}\;r1 \le -2.440081527836891534362372241579093937045 \cdot 10^{-135}:\\ \;\;\;\;\sqrt{\left(r1 \cdot r1 + r2 \cdot \left(r2 - \left(\cos a1 \cdot \cos a2\right) \cdot \left(2 \cdot r1\right)\right)\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \left(\sin a1 \cdot \sin a2\right)}\\ \mathbf{elif}\;r1 \le 1.578758057424873050820632410161984620039 \cdot 10^{-229}:\\ \;\;\;\;r2 - 1 \cdot r1\\ \mathbf{elif}\;r1 \le 3.114724519917716658260886683394897619023 \cdot 10^{-200}:\\ \;\;\;\;\sqrt{\left(r1 \cdot r1 + r2 \cdot \left(r2 - \left(\cos a1 \cdot \cos a2\right) \cdot \left(2 \cdot r1\right)\right)\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \left(\sin a1 \cdot \sin a2\right)}\\ \mathbf{elif}\;r1 \le 5.115803934409726180165957423667104320383 \cdot 10^{-132}:\\ \;\;\;\;r2 - 1 \cdot r1\\ \mathbf{elif}\;r1 \le 5.806824695993501907198552913769162842073 \cdot 10^{-4}:\\ \;\;\;\;\sqrt{\left(r1 \cdot r1 + r2 \cdot \left(r2 - \left(\cos a1 \cdot \cos a2\right) \cdot \left(2 \cdot r1\right)\right)\right) - \left(\left(2 \cdot r1\right) \cdot r2\right) \cdot \left(\sin a1 \cdot \sin a2\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
  (sqrt (- (+ (* r1 r1) (* r2 r2)) (* (* (* 2 r1) r2) (cos (- a1 a2))))))