R \cdot \left(2 \cdot \tan^{-1}_* \frac{\sqrt{{\sin \left(\frac{\phi_1 - \phi_2}{2}\right)}^{2} + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \sin \left(\frac{\lambda_1 - \lambda_2}{2}\right)\right) \cdot \sin \left(\frac{\lambda_1 - \lambda_2}{2}\right)}}{\sqrt{1 - \left({\sin \left(\frac{\phi_1 - \phi_2}{2}\right)}^{2} + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \sin \left(\frac{\lambda_1 - \lambda_2}{2}\right)\right) \cdot \sin \left(\frac{\lambda_1 - \lambda_2}{2}\right)\right)}}\right)
\begin{array}{l}
t_0 := \cos \left(\phi_1 \cdot 0.5\right)\\
t_1 := \sin \left(\frac{\lambda_1 - \lambda_2}{2}\right)\\
t_2 := \sin \left(\phi_1 \cdot 0.5\right)\\
t_3 := \sin \left(\phi_2 \cdot 0.5\right)\\
t_4 := \cos \left(\phi_2 \cdot 0.5\right)\\
R \cdot \left(2 \cdot \tan^{-1}_* \frac{\sqrt{\mathsf{fma}\left(t_1, t_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right), {\left(\sin \left(\frac{\phi_1}{2}\right) \cdot \cos \left(\frac{\phi_2}{2}\right) - \cos \left(\frac{\phi_1}{2}\right) \cdot \sin \left(\frac{\phi_2}{2}\right)\right)}^{2}\right)}}{\sqrt{1 - \left(\mathsf{fma}\left({t_3}^{2}, {t_0}^{2}, \cos \phi_2 \cdot \left(\cos \phi_1 \cdot {\sin \left(\left(\lambda_1 - \lambda_2\right) \cdot 0.5\right)}^{2}\right)\right) + t_2 \cdot \left(t_2 \cdot {t_4}^{2} - \left(2 \cdot t_0\right) \cdot \left(t_3 \cdot t_4\right)\right)\right)}}\right)
\end{array}
(FPCore (R lambda1 lambda2 phi1 phi2)
:precision binary64
(*
R
(*
2.0
(atan2
(sqrt
(+
(pow (sin (/ (- phi1 phi2) 2.0)) 2.0)
(*
(* (* (cos phi1) (cos phi2)) (sin (/ (- lambda1 lambda2) 2.0)))
(sin (/ (- lambda1 lambda2) 2.0)))))
(sqrt
(-
1.0
(+
(pow (sin (/ (- phi1 phi2) 2.0)) 2.0)
(*
(* (* (cos phi1) (cos phi2)) (sin (/ (- lambda1 lambda2) 2.0)))
(sin (/ (- lambda1 lambda2) 2.0))))))))))(FPCore (R lambda1 lambda2 phi1 phi2)
:precision binary64
(let* ((t_0 (cos (* phi1 0.5)))
(t_1 (sin (/ (- lambda1 lambda2) 2.0)))
(t_2 (sin (* phi1 0.5)))
(t_3 (sin (* phi2 0.5)))
(t_4 (cos (* phi2 0.5))))
(*
R
(*
2.0
(atan2
(sqrt
(fma
t_1
(* t_1 (* (cos phi1) (cos phi2)))
(pow
(-
(* (sin (/ phi1 2.0)) (cos (/ phi2 2.0)))
(* (cos (/ phi1 2.0)) (sin (/ phi2 2.0))))
2.0)))
(sqrt
(-
1.0
(+
(fma
(pow t_3 2.0)
(pow t_0 2.0)
(*
(cos phi2)
(* (cos phi1) (pow (sin (* (- lambda1 lambda2) 0.5)) 2.0))))
(* t_2 (- (* t_2 (pow t_4 2.0)) (* (* 2.0 t_0) (* t_3 t_4))))))))))))double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
return R * (2.0 * atan2(sqrt(pow(sin((phi1 - phi2) / 2.0), 2.0) + (((cos(phi1) * cos(phi2)) * sin((lambda1 - lambda2) / 2.0)) * sin((lambda1 - lambda2) / 2.0))), sqrt(1.0 - (pow(sin((phi1 - phi2) / 2.0), 2.0) + (((cos(phi1) * cos(phi2)) * sin((lambda1 - lambda2) / 2.0)) * sin((lambda1 - lambda2) / 2.0))))));
}
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
double t_0 = cos(phi1 * 0.5);
double t_1 = sin((lambda1 - lambda2) / 2.0);
double t_2 = sin(phi1 * 0.5);
double t_3 = sin(phi2 * 0.5);
double t_4 = cos(phi2 * 0.5);
return R * (2.0 * atan2(sqrt(fma(t_1, (t_1 * (cos(phi1) * cos(phi2))), pow(((sin(phi1 / 2.0) * cos(phi2 / 2.0)) - (cos(phi1 / 2.0) * sin(phi2 / 2.0))), 2.0))), sqrt(1.0 - (fma(pow(t_3, 2.0), pow(t_0, 2.0), (cos(phi2) * (cos(phi1) * pow(sin((lambda1 - lambda2) * 0.5), 2.0)))) + (t_2 * ((t_2 * pow(t_4, 2.0)) - ((2.0 * t_0) * (t_3 * t_4))))))));
}



Bits error versus R



Bits error versus lambda1



Bits error versus lambda2



Bits error versus phi1



Bits error versus phi2
Initial program 24.5
Simplified24.4
Applied div-sub_binary6424.4
Applied sin-diff_binary6423.8
Applied div-sub_binary6423.8
Applied sin-diff_binary6413.9
Taylor expanded in lambda1 around inf 13.9
Simplified13.9
Final simplification13.9
herbie shell --seed 2022004
(FPCore (R lambda1 lambda2 phi1 phi2)
:name "Distance on a great circle"
:precision binary64
(* R (* 2.0 (atan2 (sqrt (+ (pow (sin (/ (- phi1 phi2) 2.0)) 2.0) (* (* (* (cos phi1) (cos phi2)) (sin (/ (- lambda1 lambda2) 2.0))) (sin (/ (- lambda1 lambda2) 2.0))))) (sqrt (- 1.0 (+ (pow (sin (/ (- phi1 phi2) 2.0)) 2.0) (* (* (* (cos phi1) (cos phi2)) (sin (/ (- lambda1 lambda2) 2.0))) (sin (/ (- lambda1 lambda2) 2.0))))))))))