\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\left(\frac{\pi}{2} - \left(\frac{\pi}{2} - \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \mathsf{expm1}\left(\mathsf{log1p}\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right)\right)\right) \cdot Rdouble code(double R, double lambda1, double lambda2, double phi1, double phi2) {
return (acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R);
}
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
return (((((double) M_PI) / 2.0) - ((((double) M_PI) / 2.0) - acos(((sin(phi1) * sin(phi2)) + (((cos(phi1) * cos(phi2)) * (cos(lambda1) * cos(lambda2))) + expm1(log1p(((cos(phi1) * cos(phi2)) * (sin(lambda1) * sin(lambda2)))))))))) * R);
}



Bits error versus R



Bits error versus lambda1



Bits error versus lambda2



Bits error versus phi1



Bits error versus phi2
Results
Initial program 16.8
rmApplied cos-diff3.6
Applied distribute-lft-in3.6
rmApplied expm1-log1p-u3.6
rmApplied acos-asin3.7
rmApplied asin-acos3.6
Final simplification3.6
herbie shell --seed 2020056 +o rules:numerics
(FPCore (R lambda1 lambda2 phi1 phi2)
:name "Spherical law of cosines"
:precision binary64
(* (acos (+ (* (sin phi1) (sin phi2)) (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2))))) R))