Spherical law of cosines

Percentage Accurate: 73.8% → 96.2%
Time: 24.3s
Alternatives: 32
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \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 \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (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 acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
}
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * r
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2))))) * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return math.acos(((math.sin(phi1) * math.sin(phi2)) + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2))))) * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\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
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 32 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 73.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \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 \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (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 acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
}
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * r
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2))))) * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return math.acos(((math.sin(phi1) * math.sin(phi2)) + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2))))) * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\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
\end{array}

Alternative 1: 96.2% accurate, 0.4× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\cos^{-1} \left(t\_0 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\_m \leq 0:\\ \;\;\;\;\lambda_1 \cdot \left(\frac{\lambda_2 \cdot R\_m}{\lambda_1} - R\_m\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, t\_0\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))))
   (*
    R_s
    (if (<=
         (*
          (acos
           (+ t_0 (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
          R_m)
         0.0)
      (* lambda1 (- (/ (* lambda2 R_m) lambda1) R_m))
      (*
       R_m
       (acos
        (fma
         (*
          (cos phi2)
          (fma (sin lambda2) (sin lambda1) (* (cos lambda1) (cos lambda2))))
         (cos phi1)
         t_0)))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin(phi1) * sin(phi2);
	double tmp;
	if ((acos((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R_m) <= 0.0) {
		tmp = lambda1 * (((lambda2 * R_m) / lambda1) - R_m);
	} else {
		tmp = R_m * acos(fma((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda1) * cos(lambda2)))), cos(phi1), t_0));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(phi1) * sin(phi2))
	tmp = 0.0
	if (Float64(acos(Float64(t_0 + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) * R_m) <= 0.0)
		tmp = Float64(lambda1 * Float64(Float64(Float64(lambda2 * R_m) / lambda1) - R_m));
	else
		tmp = Float64(R_m * acos(fma(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda1) * cos(lambda2)))), cos(phi1), t_0)));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[N[(N[ArcCos[N[(t$95$0 + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R$95$m), $MachinePrecision], 0.0], N[(lambda1 * N[(N[(N[(lambda2 * R$95$m), $MachinePrecision] / lambda1), $MachinePrecision] - R$95$m), $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi1], $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\cos^{-1} \left(t\_0 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\_m \leq 0:\\
\;\;\;\;\lambda_1 \cdot \left(\frac{\lambda_2 \cdot R\_m}{\lambda_1} - R\_m\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, t\_0\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (acos.f64 (+.f64 (*.f64 (sin.f64 phi1) (sin.f64 phi2)) (*.f64 (*.f64 (cos.f64 phi1) (cos.f64 phi2)) (cos.f64 (-.f64 lambda1 lambda2))))) R) < 0.0

    1. Initial program 70.2%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6439.9

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified39.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6425.6

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified25.6%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    9. Step-by-step derivation
      1. flip--N/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}{\lambda_1 + \lambda_2}\right)} \cdot R \]
      2. clear-numN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right)} \cdot R \]
      3. lower-/.f64N/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right)} \cdot R \]
      4. lower-/.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\color{blue}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}}\right) \cdot R \]
      5. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\color{blue}{\lambda_2 + \lambda_1}}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right) \cdot R \]
      6. lower-+.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\color{blue}{\lambda_2 + \lambda_1}}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right) \cdot R \]
      7. difference-of-squaresN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_1 + \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      8. lift--.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      9. acos-cos-sN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      10. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      11. lift-acos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      12. lower-*.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_1 + \lambda_2\right) \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      13. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_2 + \lambda_1\right)} \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}\right) \cdot R \]
      14. lower-+.f6425.6

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_2 + \lambda_1\right)} \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}\right) \cdot R \]
      15. lift-acos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      16. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      17. lift--.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      18. cos-diffN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}}\right) \cdot R \]
      19. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      20. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      21. *-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      22. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      23. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      24. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      25. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)}}\right) \cdot R \]
      26. *-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right)}}\right) \cdot R \]
      27. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right)}}\right) \cdot R \]
      28. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right)}}\right) \cdot R \]
    10. Applied egg-rr13.6%

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \left(\lambda_2 - \lambda_1\right)}}\right)} \cdot R \]
    11. Taylor expanded in lambda1 around inf

      \[\leadsto \color{blue}{\lambda_1 \cdot \left(-1 \cdot R + \frac{R \cdot \lambda_2}{\lambda_1}\right)} \]
    12. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \color{blue}{\lambda_1 \cdot \left(-1 \cdot R + \frac{R \cdot \lambda_2}{\lambda_1}\right)} \]
      2. +-commutativeN/A

        \[\leadsto \lambda_1 \cdot \color{blue}{\left(\frac{R \cdot \lambda_2}{\lambda_1} + -1 \cdot R\right)} \]
      3. mul-1-negN/A

        \[\leadsto \lambda_1 \cdot \left(\frac{R \cdot \lambda_2}{\lambda_1} + \color{blue}{\left(\mathsf{neg}\left(R\right)\right)}\right) \]
      4. unsub-negN/A

        \[\leadsto \lambda_1 \cdot \color{blue}{\left(\frac{R \cdot \lambda_2}{\lambda_1} - R\right)} \]
      5. lower--.f64N/A

        \[\leadsto \lambda_1 \cdot \color{blue}{\left(\frac{R \cdot \lambda_2}{\lambda_1} - R\right)} \]
      6. lower-/.f64N/A

        \[\leadsto \lambda_1 \cdot \left(\color{blue}{\frac{R \cdot \lambda_2}{\lambda_1}} - R\right) \]
      7. *-commutativeN/A

        \[\leadsto \lambda_1 \cdot \left(\frac{\color{blue}{\lambda_2 \cdot R}}{\lambda_1} - R\right) \]
      8. lower-*.f647.9

        \[\leadsto \lambda_1 \cdot \left(\frac{\color{blue}{\lambda_2 \cdot R}}{\lambda_1} - R\right) \]
    13. Simplified7.9%

      \[\leadsto \color{blue}{\lambda_1 \cdot \left(\frac{\lambda_2 \cdot R}{\lambda_1} - R\right)} \]

    if 0.0 < (*.f64 (acos.f64 (+.f64 (*.f64 (sin.f64 phi1) (sin.f64 phi2)) (*.f64 (*.f64 (cos.f64 phi1) (cos.f64 phi2)) (cos.f64 (-.f64 lambda1 lambda2))))) R)

    1. Initial program 83.9%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      13. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      16. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      17. lower-*.f6483.9

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    4. Applied egg-rr83.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1 \cdot \cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_1} \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\sin \lambda_1 \cdot \color{blue}{\sin \lambda_2} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_2 \cdot \sin \lambda_1} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-fma.f6498.7

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Applied egg-rr98.7%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification55.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\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 \leq 0:\\ \;\;\;\;\lambda_1 \cdot \left(\frac{\lambda_2 \cdot R}{\lambda_1} - R\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 83.9% accurate, 0.7× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ t_2 := \sin \phi_1 \cdot \sin \phi_2\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, t\_2\right)\right) \cdot \left(-R\_m\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.057:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.001388888888888889, 0.041666666666666664\right), -0.5\right), 1\right), t\_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2)))
        (t_1 (* (cos phi1) (cos phi2)))
        (t_2 (* (sin phi1) (sin phi2))))
   (*
    R_s
    (if (<= phi1 -2.6e-21)
      (fma (* PI 0.5) R_m (* (asin (fma t_0 t_1 t_2)) (- R_m)))
      (if (<= phi1 0.057)
        (*
         R_m
         (acos
          (fma
           (*
            (cos phi2)
            (fma (sin lambda2) (sin lambda1) (* (cos lambda1) (cos lambda2))))
           (fma
            (* phi1 phi1)
            (fma
             (* phi1 phi1)
             (fma (* phi1 phi1) -0.001388888888888889 0.041666666666666664)
             -0.5)
            1.0)
           t_2)))
        (* R_m (acos (fma (sin phi2) (sin phi1) (* t_1 t_0)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = cos(phi1) * cos(phi2);
	double t_2 = sin(phi1) * sin(phi2);
	double tmp;
	if (phi1 <= -2.6e-21) {
		tmp = fma((((double) M_PI) * 0.5), R_m, (asin(fma(t_0, t_1, t_2)) * -R_m));
	} else if (phi1 <= 0.057) {
		tmp = R_m * acos(fma((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda1) * cos(lambda2)))), fma((phi1 * phi1), fma((phi1 * phi1), fma((phi1 * phi1), -0.001388888888888889, 0.041666666666666664), -0.5), 1.0), t_2));
	} else {
		tmp = R_m * acos(fma(sin(phi2), sin(phi1), (t_1 * t_0)));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	t_2 = Float64(sin(phi1) * sin(phi2))
	tmp = 0.0
	if (phi1 <= -2.6e-21)
		tmp = fma(Float64(pi * 0.5), R_m, Float64(asin(fma(t_0, t_1, t_2)) * Float64(-R_m)));
	elseif (phi1 <= 0.057)
		tmp = Float64(R_m * acos(fma(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda1) * cos(lambda2)))), fma(Float64(phi1 * phi1), fma(Float64(phi1 * phi1), fma(Float64(phi1 * phi1), -0.001388888888888889, 0.041666666666666664), -0.5), 1.0), t_2)));
	else
		tmp = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(t_1 * t_0))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi1, -2.6e-21], N[(N[(Pi * 0.5), $MachinePrecision] * R$95$m + N[(N[ArcSin[N[(t$95$0 * t$95$1 + t$95$2), $MachinePrecision]], $MachinePrecision] * (-R$95$m)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 0.057], N[(R$95$m * N[ArcCos[N[(N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(N[(phi1 * phi1), $MachinePrecision] * N[(N[(phi1 * phi1), $MachinePrecision] * N[(N[(phi1 * phi1), $MachinePrecision] * -0.001388888888888889 + 0.041666666666666664), $MachinePrecision] + -0.5), $MachinePrecision] + 1.0), $MachinePrecision] + t$95$2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
t_2 := \sin \phi_1 \cdot \sin \phi_2\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\
\;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, t\_2\right)\right) \cdot \left(-R\_m\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 0.057:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.001388888888888889, 0.041666666666666664\right), -0.5\right), 1\right), t\_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -2.60000000000000017e-21

    1. Initial program 86.4%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lift-+.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\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 \]
      11. lift-acos.f64N/A

        \[\leadsto \color{blue}{\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 \]
      12. *-commutativeN/A

        \[\leadsto \color{blue}{R \cdot \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)} \]
    4. Applied egg-rr86.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\pi \cdot 0.5, R, \left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\right) \cdot R\right)} \]

    if -2.60000000000000017e-21 < phi1 < 0.0570000000000000021

    1. Initial program 69.7%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      13. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      16. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      17. lower-*.f6469.7

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    4. Applied egg-rr69.7%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1 \cdot \cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_1} \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\sin \lambda_1 \cdot \color{blue}{\sin \lambda_2} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_2 \cdot \sin \lambda_1} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-fma.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Applied egg-rr89.2%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    7. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{1 + {\phi_1}^{2} \cdot \left({\phi_1}^{2} \cdot \left(\frac{1}{24} + \frac{-1}{720} \cdot {\phi_1}^{2}\right) - \frac{1}{2}\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    8. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{{\phi_1}^{2} \cdot \left({\phi_1}^{2} \cdot \left(\frac{1}{24} + \frac{-1}{720} \cdot {\phi_1}^{2}\right) - \frac{1}{2}\right) + 1}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{\mathsf{fma}\left({\phi_1}^{2}, {\phi_1}^{2} \cdot \left(\frac{1}{24} + \frac{-1}{720} \cdot {\phi_1}^{2}\right) - \frac{1}{2}, 1\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. unpow2N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, {\phi_1}^{2} \cdot \left(\frac{1}{24} + \frac{-1}{720} \cdot {\phi_1}^{2}\right) - \frac{1}{2}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, {\phi_1}^{2} \cdot \left(\frac{1}{24} + \frac{-1}{720} \cdot {\phi_1}^{2}\right) - \frac{1}{2}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. sub-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \color{blue}{{\phi_1}^{2} \cdot \left(\frac{1}{24} + \frac{-1}{720} \cdot {\phi_1}^{2}\right) + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right)}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. metadata-evalN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, {\phi_1}^{2} \cdot \left(\frac{1}{24} + \frac{-1}{720} \cdot {\phi_1}^{2}\right) + \color{blue}{\frac{-1}{2}}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \color{blue}{\mathsf{fma}\left({\phi_1}^{2}, \frac{1}{24} + \frac{-1}{720} \cdot {\phi_1}^{2}, \frac{-1}{2}\right)}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. unpow2N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, \frac{1}{24} + \frac{-1}{720} \cdot {\phi_1}^{2}, \frac{-1}{2}\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, \frac{1}{24} + \frac{-1}{720} \cdot {\phi_1}^{2}, \frac{-1}{2}\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \color{blue}{\frac{-1}{720} \cdot {\phi_1}^{2} + \frac{1}{24}}, \frac{-1}{2}\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      11. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \color{blue}{{\phi_1}^{2} \cdot \frac{-1}{720}} + \frac{1}{24}, \frac{-1}{2}\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      12. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \color{blue}{\mathsf{fma}\left({\phi_1}^{2}, \frac{-1}{720}, \frac{1}{24}\right)}, \frac{-1}{2}\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      13. unpow2N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, \frac{-1}{720}, \frac{1}{24}\right), \frac{-1}{2}\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      14. lower-*.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, -0.001388888888888889, 0.041666666666666664\right), -0.5\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    9. Simplified89.2%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{\mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.001388888888888889, 0.041666666666666664\right), -0.5\right), 1\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]

    if 0.0570000000000000021 < phi1

    1. Initial program 82.3%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\sin \phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lower-fma.f6482.3

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      13. lower-*.f6482.3

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
    4. Applied egg-rr82.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R, \sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.057:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.001388888888888889, 0.041666666666666664\right), -0.5\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 83.9% accurate, 0.7× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ t_2 := \sin \phi_1 \cdot \sin \phi_2\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, t\_2\right)\right) \cdot \left(-R\_m\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.045:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, 0.041666666666666664, -0.5\right), 1\right), t\_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2)))
        (t_1 (* (cos phi1) (cos phi2)))
        (t_2 (* (sin phi1) (sin phi2))))
   (*
    R_s
    (if (<= phi1 -2.6e-21)
      (fma (* PI 0.5) R_m (* (asin (fma t_0 t_1 t_2)) (- R_m)))
      (if (<= phi1 0.045)
        (*
         R_m
         (acos
          (fma
           (*
            (cos phi2)
            (fma (sin lambda2) (sin lambda1) (* (cos lambda1) (cos lambda2))))
           (fma
            (* phi1 phi1)
            (fma (* phi1 phi1) 0.041666666666666664 -0.5)
            1.0)
           t_2)))
        (* R_m (acos (fma (sin phi2) (sin phi1) (* t_1 t_0)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = cos(phi1) * cos(phi2);
	double t_2 = sin(phi1) * sin(phi2);
	double tmp;
	if (phi1 <= -2.6e-21) {
		tmp = fma((((double) M_PI) * 0.5), R_m, (asin(fma(t_0, t_1, t_2)) * -R_m));
	} else if (phi1 <= 0.045) {
		tmp = R_m * acos(fma((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda1) * cos(lambda2)))), fma((phi1 * phi1), fma((phi1 * phi1), 0.041666666666666664, -0.5), 1.0), t_2));
	} else {
		tmp = R_m * acos(fma(sin(phi2), sin(phi1), (t_1 * t_0)));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	t_2 = Float64(sin(phi1) * sin(phi2))
	tmp = 0.0
	if (phi1 <= -2.6e-21)
		tmp = fma(Float64(pi * 0.5), R_m, Float64(asin(fma(t_0, t_1, t_2)) * Float64(-R_m)));
	elseif (phi1 <= 0.045)
		tmp = Float64(R_m * acos(fma(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda1) * cos(lambda2)))), fma(Float64(phi1 * phi1), fma(Float64(phi1 * phi1), 0.041666666666666664, -0.5), 1.0), t_2)));
	else
		tmp = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(t_1 * t_0))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi1, -2.6e-21], N[(N[(Pi * 0.5), $MachinePrecision] * R$95$m + N[(N[ArcSin[N[(t$95$0 * t$95$1 + t$95$2), $MachinePrecision]], $MachinePrecision] * (-R$95$m)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 0.045], N[(R$95$m * N[ArcCos[N[(N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(N[(phi1 * phi1), $MachinePrecision] * N[(N[(phi1 * phi1), $MachinePrecision] * 0.041666666666666664 + -0.5), $MachinePrecision] + 1.0), $MachinePrecision] + t$95$2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
t_2 := \sin \phi_1 \cdot \sin \phi_2\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\
\;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, t\_2\right)\right) \cdot \left(-R\_m\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 0.045:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, 0.041666666666666664, -0.5\right), 1\right), t\_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -2.60000000000000017e-21

    1. Initial program 86.4%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lift-+.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\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 \]
      11. lift-acos.f64N/A

        \[\leadsto \color{blue}{\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 \]
      12. *-commutativeN/A

        \[\leadsto \color{blue}{R \cdot \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)} \]
    4. Applied egg-rr86.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\pi \cdot 0.5, R, \left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\right) \cdot R\right)} \]

    if -2.60000000000000017e-21 < phi1 < 0.044999999999999998

    1. Initial program 69.7%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      13. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      16. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      17. lower-*.f6469.7

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    4. Applied egg-rr69.7%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1 \cdot \cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_1} \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\sin \lambda_1 \cdot \color{blue}{\sin \lambda_2} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_2 \cdot \sin \lambda_1} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-fma.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Applied egg-rr89.2%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    7. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{1 + {\phi_1}^{2} \cdot \left(\frac{1}{24} \cdot {\phi_1}^{2} - \frac{1}{2}\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    8. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{{\phi_1}^{2} \cdot \left(\frac{1}{24} \cdot {\phi_1}^{2} - \frac{1}{2}\right) + 1}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{\mathsf{fma}\left({\phi_1}^{2}, \frac{1}{24} \cdot {\phi_1}^{2} - \frac{1}{2}, 1\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. unpow2N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, \frac{1}{24} \cdot {\phi_1}^{2} - \frac{1}{2}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, \frac{1}{24} \cdot {\phi_1}^{2} - \frac{1}{2}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. sub-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \color{blue}{\frac{1}{24} \cdot {\phi_1}^{2} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right)}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \color{blue}{{\phi_1}^{2} \cdot \frac{1}{24}} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. metadata-evalN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, {\phi_1}^{2} \cdot \frac{1}{24} + \color{blue}{\frac{-1}{2}}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \color{blue}{\mathsf{fma}\left({\phi_1}^{2}, \frac{1}{24}, \frac{-1}{2}\right)}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. unpow2N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, \frac{1}{24}, \frac{-1}{2}\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      10. lower-*.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, 0.041666666666666664, -0.5\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    9. Simplified89.2%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{\mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, 0.041666666666666664, -0.5\right), 1\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]

    if 0.044999999999999998 < phi1

    1. Initial program 82.3%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\sin \phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lower-fma.f6482.3

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      13. lower-*.f6482.3

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
    4. Applied egg-rr82.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R, \sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.045:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \mathsf{fma}\left(\phi_1 \cdot \phi_1, \mathsf{fma}\left(\phi_1 \cdot \phi_1, 0.041666666666666664, -0.5\right), 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 83.8% accurate, 0.7× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\_m\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.045:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \phi_1 \cdot \left(\sin \phi_2 \cdot \mathsf{fma}\left(-0.16666666666666666, \phi_1 \cdot \phi_1, 1\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))) (t_1 (* (cos phi1) (cos phi2))))
   (*
    R_s
    (if (<= phi1 -2.6e-21)
      (fma
       (* PI 0.5)
       R_m
       (* (asin (fma t_0 t_1 (* (sin phi1) (sin phi2)))) (- R_m)))
      (if (<= phi1 0.045)
        (*
         R_m
         (acos
          (fma
           (*
            (cos phi2)
            (fma (sin lambda2) (sin lambda1) (* (cos lambda1) (cos lambda2))))
           (cos phi1)
           (*
            phi1
            (* (sin phi2) (fma -0.16666666666666666 (* phi1 phi1) 1.0))))))
        (* R_m (acos (fma (sin phi2) (sin phi1) (* t_1 t_0)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = cos(phi1) * cos(phi2);
	double tmp;
	if (phi1 <= -2.6e-21) {
		tmp = fma((((double) M_PI) * 0.5), R_m, (asin(fma(t_0, t_1, (sin(phi1) * sin(phi2)))) * -R_m));
	} else if (phi1 <= 0.045) {
		tmp = R_m * acos(fma((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda1) * cos(lambda2)))), cos(phi1), (phi1 * (sin(phi2) * fma(-0.16666666666666666, (phi1 * phi1), 1.0)))));
	} else {
		tmp = R_m * acos(fma(sin(phi2), sin(phi1), (t_1 * t_0)));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if (phi1 <= -2.6e-21)
		tmp = fma(Float64(pi * 0.5), R_m, Float64(asin(fma(t_0, t_1, Float64(sin(phi1) * sin(phi2)))) * Float64(-R_m)));
	elseif (phi1 <= 0.045)
		tmp = Float64(R_m * acos(fma(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda1) * cos(lambda2)))), cos(phi1), Float64(phi1 * Float64(sin(phi2) * fma(-0.16666666666666666, Float64(phi1 * phi1), 1.0))))));
	else
		tmp = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(t_1 * t_0))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi1, -2.6e-21], N[(N[(Pi * 0.5), $MachinePrecision] * R$95$m + N[(N[ArcSin[N[(t$95$0 * t$95$1 + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * (-R$95$m)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 0.045], N[(R$95$m * N[ArcCos[N[(N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi1], $MachinePrecision] + N[(phi1 * N[(N[Sin[phi2], $MachinePrecision] * N[(-0.16666666666666666 * N[(phi1 * phi1), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\
\;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\_m\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 0.045:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \phi_1 \cdot \left(\sin \phi_2 \cdot \mathsf{fma}\left(-0.16666666666666666, \phi_1 \cdot \phi_1, 1\right)\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -2.60000000000000017e-21

    1. Initial program 86.4%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lift-+.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\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 \]
      11. lift-acos.f64N/A

        \[\leadsto \color{blue}{\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 \]
      12. *-commutativeN/A

        \[\leadsto \color{blue}{R \cdot \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)} \]
    4. Applied egg-rr86.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\pi \cdot 0.5, R, \left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\right) \cdot R\right)} \]

    if -2.60000000000000017e-21 < phi1 < 0.044999999999999998

    1. Initial program 69.7%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      13. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      16. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      17. lower-*.f6469.7

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    4. Applied egg-rr69.7%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1 \cdot \cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_1} \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\sin \lambda_1 \cdot \color{blue}{\sin \lambda_2} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_2 \cdot \sin \lambda_1} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-fma.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Applied egg-rr89.2%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    7. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \color{blue}{\phi_1 \cdot \left(\sin \phi_2 + \frac{-1}{6} \cdot \left({\phi_1}^{2} \cdot \sin \phi_2\right)\right)}\right)\right) \cdot R \]
    8. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \color{blue}{\phi_1 \cdot \left(\sin \phi_2 + \frac{-1}{6} \cdot \left({\phi_1}^{2} \cdot \sin \phi_2\right)\right)}\right)\right) \cdot R \]
      2. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \phi_1 \cdot \left(\sin \phi_2 + \color{blue}{\left(\frac{-1}{6} \cdot {\phi_1}^{2}\right) \cdot \sin \phi_2}\right)\right)\right) \cdot R \]
      3. distribute-rgt1-inN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \phi_1 \cdot \color{blue}{\left(\left(\frac{-1}{6} \cdot {\phi_1}^{2} + 1\right) \cdot \sin \phi_2\right)}\right)\right) \cdot R \]
      4. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \phi_1 \cdot \color{blue}{\left(\left(\frac{-1}{6} \cdot {\phi_1}^{2} + 1\right) \cdot \sin \phi_2\right)}\right)\right) \cdot R \]
      5. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \phi_1 \cdot \left(\color{blue}{\mathsf{fma}\left(\frac{-1}{6}, {\phi_1}^{2}, 1\right)} \cdot \sin \phi_2\right)\right)\right) \cdot R \]
      6. unpow2N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \phi_1 \cdot \left(\mathsf{fma}\left(\frac{-1}{6}, \color{blue}{\phi_1 \cdot \phi_1}, 1\right) \cdot \sin \phi_2\right)\right)\right) \cdot R \]
      7. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \phi_1 \cdot \left(\mathsf{fma}\left(\frac{-1}{6}, \color{blue}{\phi_1 \cdot \phi_1}, 1\right) \cdot \sin \phi_2\right)\right)\right) \cdot R \]
      8. lower-sin.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \phi_1 \cdot \left(\mathsf{fma}\left(-0.16666666666666666, \phi_1 \cdot \phi_1, 1\right) \cdot \color{blue}{\sin \phi_2}\right)\right)\right) \cdot R \]
    9. Simplified89.2%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \color{blue}{\phi_1 \cdot \left(\mathsf{fma}\left(-0.16666666666666666, \phi_1 \cdot \phi_1, 1\right) \cdot \sin \phi_2\right)}\right)\right) \cdot R \]

    if 0.044999999999999998 < phi1

    1. Initial program 82.3%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\sin \phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lower-fma.f6482.3

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      13. lower-*.f6482.3

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
    4. Applied egg-rr82.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R, \sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.045:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \phi_1 \cdot \left(\sin \phi_2 \cdot \mathsf{fma}\left(-0.16666666666666666, \phi_1 \cdot \phi_1, 1\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 83.8% accurate, 0.7× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ t_2 := \sin \phi_1 \cdot \sin \phi_2\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, t\_2\right)\right) \cdot \left(-R\_m\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.0155:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.5, 1\right), t\_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2)))
        (t_1 (* (cos phi1) (cos phi2)))
        (t_2 (* (sin phi1) (sin phi2))))
   (*
    R_s
    (if (<= phi1 -2.6e-21)
      (fma (* PI 0.5) R_m (* (asin (fma t_0 t_1 t_2)) (- R_m)))
      (if (<= phi1 0.0155)
        (*
         R_m
         (acos
          (fma
           (*
            (cos phi2)
            (fma (sin lambda2) (sin lambda1) (* (cos lambda1) (cos lambda2))))
           (fma (* phi1 phi1) -0.5 1.0)
           t_2)))
        (* R_m (acos (fma (sin phi2) (sin phi1) (* t_1 t_0)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = cos(phi1) * cos(phi2);
	double t_2 = sin(phi1) * sin(phi2);
	double tmp;
	if (phi1 <= -2.6e-21) {
		tmp = fma((((double) M_PI) * 0.5), R_m, (asin(fma(t_0, t_1, t_2)) * -R_m));
	} else if (phi1 <= 0.0155) {
		tmp = R_m * acos(fma((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda1) * cos(lambda2)))), fma((phi1 * phi1), -0.5, 1.0), t_2));
	} else {
		tmp = R_m * acos(fma(sin(phi2), sin(phi1), (t_1 * t_0)));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	t_2 = Float64(sin(phi1) * sin(phi2))
	tmp = 0.0
	if (phi1 <= -2.6e-21)
		tmp = fma(Float64(pi * 0.5), R_m, Float64(asin(fma(t_0, t_1, t_2)) * Float64(-R_m)));
	elseif (phi1 <= 0.0155)
		tmp = Float64(R_m * acos(fma(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda1) * cos(lambda2)))), fma(Float64(phi1 * phi1), -0.5, 1.0), t_2)));
	else
		tmp = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(t_1 * t_0))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi1, -2.6e-21], N[(N[(Pi * 0.5), $MachinePrecision] * R$95$m + N[(N[ArcSin[N[(t$95$0 * t$95$1 + t$95$2), $MachinePrecision]], $MachinePrecision] * (-R$95$m)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 0.0155], N[(R$95$m * N[ArcCos[N[(N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(N[(phi1 * phi1), $MachinePrecision] * -0.5 + 1.0), $MachinePrecision] + t$95$2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
t_2 := \sin \phi_1 \cdot \sin \phi_2\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\
\;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, t\_2\right)\right) \cdot \left(-R\_m\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 0.0155:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.5, 1\right), t\_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -2.60000000000000017e-21

    1. Initial program 86.4%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lift-+.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\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 \]
      11. lift-acos.f64N/A

        \[\leadsto \color{blue}{\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 \]
      12. *-commutativeN/A

        \[\leadsto \color{blue}{R \cdot \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)} \]
    4. Applied egg-rr86.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\pi \cdot 0.5, R, \left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\right) \cdot R\right)} \]

    if -2.60000000000000017e-21 < phi1 < 0.0155

    1. Initial program 69.7%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      13. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      16. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      17. lower-*.f6469.7

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    4. Applied egg-rr69.7%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1 \cdot \cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_1} \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\sin \lambda_1 \cdot \color{blue}{\sin \lambda_2} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_2 \cdot \sin \lambda_1} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-fma.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Applied egg-rr89.2%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    7. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{1 + \frac{-1}{2} \cdot {\phi_1}^{2}}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    8. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{\frac{-1}{2} \cdot {\phi_1}^{2} + 1}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{{\phi_1}^{2} \cdot \frac{-1}{2}} + 1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{\mathsf{fma}\left({\phi_1}^{2}, \frac{-1}{2}, 1\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. unpow2N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, \frac{-1}{2}, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. lower-*.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, -0.5, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    9. Simplified89.2%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \color{blue}{\mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.5, 1\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]

    if 0.0155 < phi1

    1. Initial program 82.3%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\sin \phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lower-fma.f6482.3

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      13. lower-*.f6482.3

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
    4. Applied egg-rr82.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R, \sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.0155:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.5, 1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 83.8% accurate, 0.7× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\_m\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.0155:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))) (t_1 (* (cos phi1) (cos phi2))))
   (*
    R_s
    (if (<= phi1 -2.6e-21)
      (fma
       (* PI 0.5)
       R_m
       (* (asin (fma t_0 t_1 (* (sin phi1) (sin phi2)))) (- R_m)))
      (if (<= phi1 0.0155)
        (*
         R_m
         (acos
          (fma
           (*
            (cos phi2)
            (fma (sin lambda2) (sin lambda1) (* (cos lambda1) (cos lambda2))))
           (cos phi1)
           (* phi1 (sin phi2)))))
        (* R_m (acos (fma (sin phi2) (sin phi1) (* t_1 t_0)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = cos(phi1) * cos(phi2);
	double tmp;
	if (phi1 <= -2.6e-21) {
		tmp = fma((((double) M_PI) * 0.5), R_m, (asin(fma(t_0, t_1, (sin(phi1) * sin(phi2)))) * -R_m));
	} else if (phi1 <= 0.0155) {
		tmp = R_m * acos(fma((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda1) * cos(lambda2)))), cos(phi1), (phi1 * sin(phi2))));
	} else {
		tmp = R_m * acos(fma(sin(phi2), sin(phi1), (t_1 * t_0)));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if (phi1 <= -2.6e-21)
		tmp = fma(Float64(pi * 0.5), R_m, Float64(asin(fma(t_0, t_1, Float64(sin(phi1) * sin(phi2)))) * Float64(-R_m)));
	elseif (phi1 <= 0.0155)
		tmp = Float64(R_m * acos(fma(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda1) * cos(lambda2)))), cos(phi1), Float64(phi1 * sin(phi2)))));
	else
		tmp = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(t_1 * t_0))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi1, -2.6e-21], N[(N[(Pi * 0.5), $MachinePrecision] * R$95$m + N[(N[ArcSin[N[(t$95$0 * t$95$1 + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * (-R$95$m)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 0.0155], N[(R$95$m * N[ArcCos[N[(N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi1], $MachinePrecision] + N[(phi1 * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\
\;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\_m\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 0.0155:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \phi_1 \cdot \sin \phi_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -2.60000000000000017e-21

    1. Initial program 86.4%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lift-+.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\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 \]
      11. lift-acos.f64N/A

        \[\leadsto \color{blue}{\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 \]
      12. *-commutativeN/A

        \[\leadsto \color{blue}{R \cdot \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)} \]
    4. Applied egg-rr86.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\pi \cdot 0.5, R, \left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\right) \cdot R\right)} \]

    if -2.60000000000000017e-21 < phi1 < 0.0155

    1. Initial program 69.7%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      13. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      16. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      17. lower-*.f6469.7

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    4. Applied egg-rr69.7%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\cos \lambda_1 \cdot \cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_1} \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\sin \lambda_1 \cdot \color{blue}{\sin \lambda_2} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\color{blue}{\sin \lambda_2 \cdot \sin \lambda_1} + \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-fma.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Applied egg-rr89.2%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    7. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \color{blue}{\phi_1 \cdot \sin \phi_2}\right)\right) \cdot R \]
    8. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \color{blue}{\phi_1 \cdot \sin \phi_2}\right)\right) \cdot R \]
      2. lower-sin.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \phi_1 \cdot \color{blue}{\sin \phi_2}\right)\right) \cdot R \]
    9. Simplified89.2%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \color{blue}{\phi_1 \cdot \sin \phi_2}\right)\right) \cdot R \]

    if 0.0155 < phi1

    1. Initial program 82.3%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\sin \phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lower-fma.f6482.3

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      13. lower-*.f6482.3

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
    4. Applied egg-rr82.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R, \sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.0155:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 83.7% accurate, 0.8× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\_m\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.0155:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))) (t_1 (* (cos phi1) (cos phi2))))
   (*
    R_s
    (if (<= phi1 -2.6e-21)
      (fma
       (* PI 0.5)
       R_m
       (* (asin (fma t_0 t_1 (* (sin phi1) (sin phi2)))) (- R_m)))
      (if (<= phi1 0.0155)
        (*
         R_m
         (acos
          (fma
           phi1
           (sin phi2)
           (*
            (cos phi2)
            (fma
             (sin lambda1)
             (sin lambda2)
             (* (cos lambda1) (cos lambda2)))))))
        (* R_m (acos (fma (sin phi2) (sin phi1) (* t_1 t_0)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = cos(phi1) * cos(phi2);
	double tmp;
	if (phi1 <= -2.6e-21) {
		tmp = fma((((double) M_PI) * 0.5), R_m, (asin(fma(t_0, t_1, (sin(phi1) * sin(phi2)))) * -R_m));
	} else if (phi1 <= 0.0155) {
		tmp = R_m * acos(fma(phi1, sin(phi2), (cos(phi2) * fma(sin(lambda1), sin(lambda2), (cos(lambda1) * cos(lambda2))))));
	} else {
		tmp = R_m * acos(fma(sin(phi2), sin(phi1), (t_1 * t_0)));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if (phi1 <= -2.6e-21)
		tmp = fma(Float64(pi * 0.5), R_m, Float64(asin(fma(t_0, t_1, Float64(sin(phi1) * sin(phi2)))) * Float64(-R_m)));
	elseif (phi1 <= 0.0155)
		tmp = Float64(R_m * acos(fma(phi1, sin(phi2), Float64(cos(phi2) * fma(sin(lambda1), sin(lambda2), Float64(cos(lambda1) * cos(lambda2)))))));
	else
		tmp = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(t_1 * t_0))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi1, -2.6e-21], N[(N[(Pi * 0.5), $MachinePrecision] * R$95$m + N[(N[ArcSin[N[(t$95$0 * t$95$1 + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * (-R$95$m)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 0.0155], N[(R$95$m * N[ArcCos[N[(phi1 * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\
\;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\_m\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 0.0155:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -2.60000000000000017e-21

    1. Initial program 86.4%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lift-+.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\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 \]
      11. lift-acos.f64N/A

        \[\leadsto \color{blue}{\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 \]
      12. *-commutativeN/A

        \[\leadsto \color{blue}{R \cdot \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)} \]
    4. Applied egg-rr86.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\pi \cdot 0.5, R, \left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\right) \cdot R\right)} \]

    if -2.60000000000000017e-21 < phi1 < 0.0155

    1. Initial program 69.7%

      \[\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 \]
    2. Add Preprocessing
    3. Applied egg-rr89.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \mathsf{fma}\left(\cos \phi_1, \sin \lambda_1 \cdot \left(\sin \lambda_2 \cdot \cos \phi_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_1 \cdot \sin \phi_2 + \left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right) + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right) + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)} \cdot R \]
      2. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \color{blue}{\sin \phi_2}, \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right) + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R \]
      3. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \color{blue}{\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      4. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2} + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right)\right) \cdot R \]
      5. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2 + \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2}\right)\right) \cdot R \]
      6. distribute-rgt-outN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \color{blue}{\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)}\right)\right) \cdot R \]
      7. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \color{blue}{\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)}\right)\right) \cdot R \]
      8. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \color{blue}{\cos \phi_2} \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right) \cdot R \]
      9. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right)\right) \cdot R \]
      10. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \phi_2 \cdot \mathsf{fma}\left(\color{blue}{\sin \lambda_1}, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right) \cdot R \]
      11. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right) \cdot R \]
      12. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1 \cdot \cos \lambda_2}\right)\right)\right) \cdot R \]
      13. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1} \cdot \cos \lambda_2\right)\right)\right) \cdot R \]
      14. lower-cos.f6489.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \color{blue}{\cos \lambda_2}\right)\right)\right) \cdot R \]
    6. Simplified89.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)} \cdot R \]

    if 0.0155 < phi1

    1. Initial program 82.3%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\sin \phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lower-fma.f6482.3

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      13. lower-*.f6482.3

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
    4. Applied egg-rr82.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R, \sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.0155:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 83.5% accurate, 1.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\_m\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))) (t_1 (* (cos phi1) (cos phi2))))
   (*
    R_s
    (if (<= phi1 -2.6e-21)
      (fma
       (* PI 0.5)
       R_m
       (* (asin (fma t_0 t_1 (* (sin phi1) (sin phi2)))) (- R_m)))
      (if (<= phi1 3.7e-18)
        (*
         R_m
         (acos
          (*
           (cos phi2)
           (fma (sin lambda1) (sin lambda2) (* (cos lambda1) (cos lambda2))))))
        (* R_m (acos (fma (sin phi2) (sin phi1) (* t_1 t_0)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = cos(phi1) * cos(phi2);
	double tmp;
	if (phi1 <= -2.6e-21) {
		tmp = fma((((double) M_PI) * 0.5), R_m, (asin(fma(t_0, t_1, (sin(phi1) * sin(phi2)))) * -R_m));
	} else if (phi1 <= 3.7e-18) {
		tmp = R_m * acos((cos(phi2) * fma(sin(lambda1), sin(lambda2), (cos(lambda1) * cos(lambda2)))));
	} else {
		tmp = R_m * acos(fma(sin(phi2), sin(phi1), (t_1 * t_0)));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if (phi1 <= -2.6e-21)
		tmp = fma(Float64(pi * 0.5), R_m, Float64(asin(fma(t_0, t_1, Float64(sin(phi1) * sin(phi2)))) * Float64(-R_m)));
	elseif (phi1 <= 3.7e-18)
		tmp = Float64(R_m * acos(Float64(cos(phi2) * fma(sin(lambda1), sin(lambda2), Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(t_1 * t_0))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi1, -2.6e-21], N[(N[(Pi * 0.5), $MachinePrecision] * R$95$m + N[(N[ArcSin[N[(t$95$0 * t$95$1 + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * (-R$95$m)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 3.7e-18], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\
\;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R\_m, \sin^{-1} \left(\mathsf{fma}\left(t\_0, t\_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\_m\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot t\_0\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -2.60000000000000017e-21

    1. Initial program 86.4%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lift-+.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\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 \]
      11. lift-acos.f64N/A

        \[\leadsto \color{blue}{\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 \]
      12. *-commutativeN/A

        \[\leadsto \color{blue}{R \cdot \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)} \]
    4. Applied egg-rr86.4%

      \[\leadsto \color{blue}{\mathsf{fma}\left(\pi \cdot 0.5, R, \left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\right) \cdot R\right)} \]

    if -2.60000000000000017e-21 < phi1 < 3.7000000000000003e-18

    1. Initial program 69.0%

      \[\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 \]
    2. Add Preprocessing
    3. Applied egg-rr88.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \mathsf{fma}\left(\cos \phi_1, \sin \lambda_1 \cdot \left(\sin \lambda_2 \cdot \cos \phi_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right) + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right)} \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2} + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right) \cdot R \]
      3. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2 + \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2}\right) \cdot R \]
      4. distribute-rgt-outN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      5. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      6. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      7. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
      8. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\color{blue}{\sin \lambda_1}, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      9. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      10. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1 \cdot \cos \lambda_2}\right)\right) \cdot R \]
      11. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1} \cdot \cos \lambda_2\right)\right) \cdot R \]
      12. lower-cos.f6488.9

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \color{blue}{\cos \lambda_2}\right)\right) \cdot R \]
    6. Simplified88.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]

    if 3.7000000000000003e-18 < phi1

    1. Initial program 83.0%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\sin \phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lower-fma.f6483.0

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      13. lower-*.f6483.0

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
    4. Applied egg-rr83.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.6 \cdot 10^{-21}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R, \sin^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot \left(-R\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 83.7% accurate, 1.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.72 \cdot 10^{-9}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot t\_0, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot t\_0\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (*
    R_s
    (if (<= phi1 -1.72e-9)
      (*
       R_m
       (acos (fma (* (cos phi2) t_0) (cos phi1) (* (sin phi1) (sin phi2)))))
      (if (<= phi1 3.7e-18)
        (*
         R_m
         (acos
          (*
           (cos phi2)
           (fma (sin lambda1) (sin lambda2) (* (cos lambda1) (cos lambda2))))))
        (*
         R_m
         (acos
          (fma (sin phi2) (sin phi1) (* (* (cos phi1) (cos phi2)) t_0)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (phi1 <= -1.72e-9) {
		tmp = R_m * acos(fma((cos(phi2) * t_0), cos(phi1), (sin(phi1) * sin(phi2))));
	} else if (phi1 <= 3.7e-18) {
		tmp = R_m * acos((cos(phi2) * fma(sin(lambda1), sin(lambda2), (cos(lambda1) * cos(lambda2)))));
	} else {
		tmp = R_m * acos(fma(sin(phi2), sin(phi1), ((cos(phi1) * cos(phi2)) * t_0)));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi1 <= -1.72e-9)
		tmp = Float64(R_m * acos(fma(Float64(cos(phi2) * t_0), cos(phi1), Float64(sin(phi1) * sin(phi2)))));
	elseif (phi1 <= 3.7e-18)
		tmp = Float64(R_m * acos(Float64(cos(phi2) * fma(sin(lambda1), sin(lambda2), Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(Float64(cos(phi1) * cos(phi2)) * t_0))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi1, -1.72e-9], N[(R$95$m * N[ArcCos[N[(N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision] * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 3.7e-18], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -1.72 \cdot 10^{-9}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot t\_0, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot t\_0\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -1.72000000000000006e-9

    1. Initial program 86.2%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      13. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      16. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      17. lower-*.f6486.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    4. Applied egg-rr86.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]

    if -1.72000000000000006e-9 < phi1 < 3.7000000000000003e-18

    1. Initial program 69.2%

      \[\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 \]
    2. Add Preprocessing
    3. Applied egg-rr89.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \mathsf{fma}\left(\cos \phi_1, \sin \lambda_1 \cdot \left(\sin \lambda_2 \cdot \cos \phi_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right) + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right)} \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2} + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right) \cdot R \]
      3. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2 + \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2}\right) \cdot R \]
      4. distribute-rgt-outN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      5. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      6. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      7. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
      8. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\color{blue}{\sin \lambda_1}, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      9. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      10. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1 \cdot \cos \lambda_2}\right)\right) \cdot R \]
      11. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1} \cdot \cos \lambda_2\right)\right) \cdot R \]
      12. lower-cos.f6489.0

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \color{blue}{\cos \lambda_2}\right)\right) \cdot R \]
    6. Simplified89.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]

    if 3.7000000000000003e-18 < phi1

    1. Initial program 83.0%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\sin \phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lower-fma.f6483.0

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      13. lower-*.f6483.0

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
    4. Applied egg-rr83.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.72 \cdot 10^{-9}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 10: 83.7% accurate, 1.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.72 \cdot 10^{-9}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi1 -1.72e-9)
    (*
     R_m
     (acos
      (fma
       (sin phi2)
       (sin phi1)
       (* (cos phi1) (* (cos phi2) (cos (- lambda2 lambda1)))))))
    (if (<= phi1 3.7e-18)
      (*
       R_m
       (acos
        (*
         (cos phi2)
         (fma (sin lambda1) (sin lambda2) (* (cos lambda1) (cos lambda2))))))
      (*
       R_m
       (acos
        (fma
         (sin phi2)
         (sin phi1)
         (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2))))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -1.72e-9) {
		tmp = R_m * acos(fma(sin(phi2), sin(phi1), (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1))))));
	} else if (phi1 <= 3.7e-18) {
		tmp = R_m * acos((cos(phi2) * fma(sin(lambda1), sin(lambda2), (cos(lambda1) * cos(lambda2)))));
	} else {
		tmp = R_m * acos(fma(sin(phi2), sin(phi1), ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -1.72e-9)
		tmp = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda2 - lambda1)))))));
	elseif (phi1 <= 3.7e-18)
		tmp = Float64(R_m * acos(Float64(cos(phi2) * fma(sin(lambda1), sin(lambda2), Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi1, -1.72e-9], N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 3.7e-18], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -1.72 \cdot 10^{-9}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -1.72000000000000006e-9

    1. Initial program 86.2%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      13. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      16. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      17. lower-*.f6486.2

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    4. Applied egg-rr86.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. cos-diffN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      10. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\color{blue}{\cos \lambda_1 \cdot \cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      11. distribute-rgt-inN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      12. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \left(\color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      13. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \left(\sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      14. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \color{blue}{\sin \lambda_1 \cdot \left(\sin \lambda_2 \cdot \cos \phi_2\right)}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      15. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \sin \lambda_1 \cdot \color{blue}{\left(\sin \lambda_2 \cdot \cos \phi_2\right)}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      16. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \color{blue}{\sin \lambda_1 \cdot \left(\sin \lambda_2 \cdot \cos \phi_2\right)}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Applied egg-rr99.1%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\mathsf{fma}\left(\cos \phi_2 \cdot \cos \lambda_2, \cos \lambda_1, \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    7. Applied egg-rr86.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right) \cdot \cos \phi_1\right)\right)} \cdot R \]

    if -1.72000000000000006e-9 < phi1 < 3.7000000000000003e-18

    1. Initial program 69.2%

      \[\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 \]
    2. Add Preprocessing
    3. Applied egg-rr89.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \mathsf{fma}\left(\cos \phi_1, \sin \lambda_1 \cdot \left(\sin \lambda_2 \cdot \cos \phi_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right) + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right)} \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2} + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right) \cdot R \]
      3. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2 + \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2}\right) \cdot R \]
      4. distribute-rgt-outN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      5. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      6. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      7. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
      8. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\color{blue}{\sin \lambda_1}, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      9. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      10. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1 \cdot \cos \lambda_2}\right)\right) \cdot R \]
      11. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1} \cdot \cos \lambda_2\right)\right) \cdot R \]
      12. lower-cos.f6489.0

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \color{blue}{\cos \lambda_2}\right)\right) \cdot R \]
    6. Simplified89.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]

    if 3.7000000000000003e-18 < phi1

    1. Initial program 83.0%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\sin \phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lower-fma.f6483.0

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      13. lower-*.f6483.0

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
    4. Applied egg-rr83.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.72 \cdot 10^{-9}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 11: 83.7% accurate, 1.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.72 \cdot 10^{-9}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (*
          R_m
          (acos
           (fma
            (sin phi2)
            (sin phi1)
            (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2))))))))
   (*
    R_s
    (if (<= phi1 -1.72e-9)
      t_0
      (if (<= phi1 3.7e-18)
        (*
         R_m
         (acos
          (*
           (cos phi2)
           (fma (sin lambda1) (sin lambda2) (* (cos lambda1) (cos lambda2))))))
        t_0)))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = R_m * acos(fma(sin(phi2), sin(phi1), ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	double tmp;
	if (phi1 <= -1.72e-9) {
		tmp = t_0;
	} else if (phi1 <= 3.7e-18) {
		tmp = R_m * acos((cos(phi2) * fma(sin(lambda1), sin(lambda2), (cos(lambda1) * cos(lambda2)))));
	} else {
		tmp = t_0;
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(R_m * acos(fma(sin(phi2), sin(phi1), Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))))
	tmp = 0.0
	if (phi1 <= -1.72e-9)
		tmp = t_0;
	elseif (phi1 <= 3.7e-18)
		tmp = Float64(R_m * acos(Float64(cos(phi2) * fma(sin(lambda1), sin(lambda2), Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = t_0;
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(R$95$m * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi1, -1.72e-9], t$95$0, If[LessEqual[phi1, 3.7e-18], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$0]]), $MachinePrecision]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -1.72 \cdot 10^{-9}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -1.72000000000000006e-9 or 3.7000000000000003e-18 < phi1

    1. Initial program 84.5%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\sin \phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. lower-fma.f6484.5

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
      13. lower-*.f6484.5

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right) \cdot R \]
    4. Applied egg-rr84.5%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)} \cdot R \]

    if -1.72000000000000006e-9 < phi1 < 3.7000000000000003e-18

    1. Initial program 69.2%

      \[\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 \]
    2. Add Preprocessing
    3. Applied egg-rr89.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \mathsf{fma}\left(\cos \phi_1, \sin \lambda_1 \cdot \left(\sin \lambda_2 \cdot \cos \phi_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right) + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right)} \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2} + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right) \cdot R \]
      3. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2 + \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2}\right) \cdot R \]
      4. distribute-rgt-outN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      5. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      6. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      7. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
      8. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\color{blue}{\sin \lambda_1}, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      9. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      10. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1 \cdot \cos \lambda_2}\right)\right) \cdot R \]
      11. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1} \cdot \cos \lambda_2\right)\right) \cdot R \]
      12. lower-cos.f6489.0

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \color{blue}{\cos \lambda_2}\right)\right) \cdot R \]
    6. Simplified89.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification86.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.72 \cdot 10^{-9}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 12: 76.0% accurate, 1.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\lambda_2 \leq -6.7 \cdot 10^{-6}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\\ \mathbf{elif}\;\lambda_2 \leq 9 \cdot 10^{-5}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, t\_1, t\_0\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, t\_1, t\_0\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))) (t_1 (* (cos phi1) (cos phi2))))
   (*
    R_s
    (if (<= lambda2 -6.7e-6)
      (*
       R_m
       (acos
        (*
         (cos phi1)
         (fma (cos lambda2) (cos lambda1) (* (sin lambda2) (sin lambda1))))))
      (if (<= lambda2 9e-5)
        (* R_m (acos (fma (cos lambda1) t_1 t_0)))
        (* R_m (acos (fma (cos lambda2) t_1 t_0))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin(phi1) * sin(phi2);
	double t_1 = cos(phi1) * cos(phi2);
	double tmp;
	if (lambda2 <= -6.7e-6) {
		tmp = R_m * acos((cos(phi1) * fma(cos(lambda2), cos(lambda1), (sin(lambda2) * sin(lambda1)))));
	} else if (lambda2 <= 9e-5) {
		tmp = R_m * acos(fma(cos(lambda1), t_1, t_0));
	} else {
		tmp = R_m * acos(fma(cos(lambda2), t_1, t_0));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(phi1) * sin(phi2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if (lambda2 <= -6.7e-6)
		tmp = Float64(R_m * acos(Float64(cos(phi1) * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda2) * sin(lambda1))))));
	elseif (lambda2 <= 9e-5)
		tmp = Float64(R_m * acos(fma(cos(lambda1), t_1, t_0)));
	else
		tmp = Float64(R_m * acos(fma(cos(lambda2), t_1, t_0)));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[lambda2, -6.7e-6], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda2, 9e-5], N[(R$95$m * N[ArcCos[N[(N[Cos[lambda1], $MachinePrecision] * t$95$1 + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Cos[lambda2], $MachinePrecision] * t$95$1 + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\lambda_2 \leq -6.7 \cdot 10^{-6}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\\

\mathbf{elif}\;\lambda_2 \leq 9 \cdot 10^{-5}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, t\_1, t\_0\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, t\_1, t\_0\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if lambda2 < -6.7e-6

    1. Initial program 62.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6438.8

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified38.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_2 \cdot \sin \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
      7. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      8. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
      9. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      11. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. lower-*.f6459.1

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
    7. Applied egg-rr59.1%

      \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]

    if -6.7e-6 < lambda2 < 9.00000000000000057e-5

    1. Initial program 90.3%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \lambda_1}, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \phi_1 \cdot \cos \phi_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \phi_1} \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \color{blue}{\cos \phi_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \color{blue}{\sin \phi_1 \cdot \sin \phi_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \color{blue}{\sin \phi_1} \cdot \sin \phi_2\right)\right) \cdot R \]
      8. lower-sin.f6490.4

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \color{blue}{\sin \phi_2}\right)\right) \cdot R \]
    5. Simplified90.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]

    if 9.00000000000000057e-5 < lambda2

    1. Initial program 58.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    4. Step-by-step derivation
      1. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\mathsf{neg}\left(\lambda_2\right)\right), \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      4. cos-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \lambda_2}, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \lambda_2}, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \color{blue}{\cos \phi_1 \cdot \cos \phi_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \color{blue}{\cos \phi_1} \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \phi_1 \cdot \color{blue}{\cos \phi_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \phi_1 \cdot \cos \phi_2, \color{blue}{\sin \phi_1 \cdot \sin \phi_2}\right)\right) \cdot R \]
      10. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \phi_1 \cdot \cos \phi_2, \color{blue}{\sin \phi_1} \cdot \sin \phi_2\right)\right) \cdot R \]
      11. lower-sin.f6459.0

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \color{blue}{\sin \phi_2}\right)\right) \cdot R \]
    5. Simplified59.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_2, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification76.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_2 \leq -6.7 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\\ \mathbf{elif}\;\lambda_2 \leq 9 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 13: 73.4% accurate, 1.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -6.2 \cdot 10^{-6}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (*
          R_m
          (acos
           (fma
            (cos lambda1)
            (* (cos phi1) (cos phi2))
            (* (sin phi1) (sin phi2)))))))
   (*
    R_s
    (if (<= phi1 -6.2e-6)
      t_0
      (if (<= phi1 3.7e-18)
        (*
         R_m
         (acos
          (*
           (cos phi2)
           (fma (sin lambda1) (sin lambda2) (* (cos lambda1) (cos lambda2))))))
        t_0)))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = R_m * acos(fma(cos(lambda1), (cos(phi1) * cos(phi2)), (sin(phi1) * sin(phi2))));
	double tmp;
	if (phi1 <= -6.2e-6) {
		tmp = t_0;
	} else if (phi1 <= 3.7e-18) {
		tmp = R_m * acos((cos(phi2) * fma(sin(lambda1), sin(lambda2), (cos(lambda1) * cos(lambda2)))));
	} else {
		tmp = t_0;
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(R_m * acos(fma(cos(lambda1), Float64(cos(phi1) * cos(phi2)), Float64(sin(phi1) * sin(phi2)))))
	tmp = 0.0
	if (phi1 <= -6.2e-6)
		tmp = t_0;
	elseif (phi1 <= 3.7e-18)
		tmp = Float64(R_m * acos(Float64(cos(phi2) * fma(sin(lambda1), sin(lambda2), Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = t_0;
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(R$95$m * N[ArcCos[N[(N[Cos[lambda1], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi1, -6.2e-6], t$95$0, If[LessEqual[phi1, 3.7e-18], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$0]]), $MachinePrecision]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -6.2 \cdot 10^{-6}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -6.1999999999999999e-6 or 3.7000000000000003e-18 < phi1

    1. Initial program 84.3%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \lambda_1}, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \phi_1 \cdot \cos \phi_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \phi_1} \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \color{blue}{\cos \phi_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \color{blue}{\sin \phi_1 \cdot \sin \phi_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \color{blue}{\sin \phi_1} \cdot \sin \phi_2\right)\right) \cdot R \]
      8. lower-sin.f6466.4

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \color{blue}{\sin \phi_2}\right)\right) \cdot R \]
    5. Simplified66.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]

    if -6.1999999999999999e-6 < phi1 < 3.7000000000000003e-18

    1. Initial program 69.7%

      \[\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 \]
    2. Add Preprocessing
    3. Applied egg-rr89.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \mathsf{fma}\left(\cos \phi_1, \sin \lambda_1 \cdot \left(\sin \lambda_2 \cdot \cos \phi_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right) + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right)} \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2} + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right) \cdot R \]
      3. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2 + \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2}\right) \cdot R \]
      4. distribute-rgt-outN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      5. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      6. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      7. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
      8. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\color{blue}{\sin \lambda_1}, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      9. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      10. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1 \cdot \cos \lambda_2}\right)\right) \cdot R \]
      11. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1} \cdot \cos \lambda_2\right)\right) \cdot R \]
      12. lower-cos.f6488.6

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \color{blue}{\cos \lambda_2}\right)\right) \cdot R \]
    6. Simplified88.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification76.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -6.2 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 3.7 \cdot 10^{-18}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 14: 63.2% accurate, 1.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_2 \leq 6.5 \cdot 10^{-6}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi2 6.5e-6)
    (*
     R_m
     (acos
      (*
       (cos phi1)
       (fma (cos lambda2) (cos lambda1) (* (sin lambda2) (sin lambda1))))))
    (*
     R_m
     (acos
      (*
       (cos phi2)
       (fma (sin lambda1) (sin lambda2) (* (cos lambda1) (cos lambda2)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 6.5e-6) {
		tmp = R_m * acos((cos(phi1) * fma(cos(lambda2), cos(lambda1), (sin(lambda2) * sin(lambda1)))));
	} else {
		tmp = R_m * acos((cos(phi2) * fma(sin(lambda1), sin(lambda2), (cos(lambda1) * cos(lambda2)))));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 6.5e-6)
		tmp = Float64(R_m * acos(Float64(cos(phi1) * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda2) * sin(lambda1))))));
	else
		tmp = Float64(R_m * acos(Float64(cos(phi2) * fma(sin(lambda1), sin(lambda2), Float64(cos(lambda1) * cos(lambda2))))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi2, 6.5e-6], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_2 \leq 6.5 \cdot 10^{-6}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < 6.4999999999999996e-6

    1. Initial program 77.5%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6451.2

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified51.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_2 \cdot \sin \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
      7. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      8. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
      9. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      11. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. lower-*.f6461.8

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
    7. Applied egg-rr61.8%

      \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]

    if 6.4999999999999996e-6 < phi2

    1. Initial program 77.0%

      \[\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 \]
    2. Add Preprocessing
    3. Applied egg-rr99.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \mathsf{fma}\left(\cos \phi_1, \sin \lambda_1 \cdot \left(\sin \lambda_2 \cdot \cos \phi_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right) + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right)} \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2} + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right) \cdot R \]
      3. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2 + \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2}\right) \cdot R \]
      4. distribute-rgt-outN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      5. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      6. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      7. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
      8. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\color{blue}{\sin \lambda_1}, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      9. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      10. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1 \cdot \cos \lambda_2}\right)\right) \cdot R \]
      11. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1} \cdot \cos \lambda_2\right)\right) \cdot R \]
      12. lower-cos.f6458.9

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \color{blue}{\cos \lambda_2}\right)\right) \cdot R \]
    6. Simplified58.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification61.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 6.5 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 15: 60.9% accurate, 1.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.00011:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi1 -0.00011)
    (* R_m (acos (* (cos phi1) (cos (- lambda2 lambda1)))))
    (*
     R_m
     (acos
      (*
       (cos phi2)
       (fma (sin lambda1) (sin lambda2) (* (cos lambda1) (cos lambda2)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -0.00011) {
		tmp = R_m * acos((cos(phi1) * cos((lambda2 - lambda1))));
	} else {
		tmp = R_m * acos((cos(phi2) * fma(sin(lambda1), sin(lambda2), (cos(lambda1) * cos(lambda2)))));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -0.00011)
		tmp = Float64(R_m * acos(Float64(cos(phi1) * cos(Float64(lambda2 - lambda1)))));
	else
		tmp = Float64(R_m * acos(Float64(cos(phi2) * fma(sin(lambda1), sin(lambda2), Float64(cos(lambda1) * cos(lambda2))))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi1, -0.00011], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -0.00011:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -1.10000000000000004e-4

    1. Initial program 85.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6447.5

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified47.5%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]

    if -1.10000000000000004e-4 < phi1

    1. Initial program 74.7%

      \[\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 \]
    2. Add Preprocessing
    3. Applied egg-rr92.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right), \cos \phi_1, \mathsf{fma}\left(\cos \phi_1, \sin \lambda_1 \cdot \left(\sin \lambda_2 \cdot \cos \phi_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right) + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    5. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right)} \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2} + \cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_2\right)\right) \cdot R \]
      3. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2 + \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2}\right) \cdot R \]
      4. distribute-rgt-outN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      5. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
      6. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      7. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
      8. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\color{blue}{\sin \lambda_1}, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      9. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R \]
      10. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1 \cdot \cos \lambda_2}\right)\right) \cdot R \]
      11. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_1} \cdot \cos \lambda_2\right)\right) \cdot R \]
      12. lower-cos.f6462.8

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \color{blue}{\cos \lambda_2}\right)\right) \cdot R \]
    6. Simplified62.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification59.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.00011:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 16: 56.1% accurate, 1.1× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.25 \cdot 10^{-5}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \frac{\cos \phi_2}{\lambda_2}, \sin \phi_1 \cdot \frac{\sin \phi_2}{\lambda_2}\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 0.00065:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \phi_1, \sin \phi_1 \cdot \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 1.5 \cdot 10^{+67}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda2 lambda1))))
   (*
    R_s
    (if (<= phi2 -1.25e-5)
      (*
       R_m
       (acos
        (*
         lambda2
         (fma
          (cos phi1)
          (/ (cos phi2) lambda2)
          (* (sin phi1) (/ (sin phi2) lambda2))))))
      (if (<= phi2 0.00065)
        (* R_m (acos (fma t_0 (cos phi1) (* (sin phi1) phi2))))
        (if (<= phi2 1.5e+67)
          (* R_m (acos (fma (cos phi1) (cos phi2) (* (sin phi1) (sin phi2)))))
          (* R_m (acos (* (cos phi2) t_0)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda2 - lambda1));
	double tmp;
	if (phi2 <= -1.25e-5) {
		tmp = R_m * acos((lambda2 * fma(cos(phi1), (cos(phi2) / lambda2), (sin(phi1) * (sin(phi2) / lambda2)))));
	} else if (phi2 <= 0.00065) {
		tmp = R_m * acos(fma(t_0, cos(phi1), (sin(phi1) * phi2)));
	} else if (phi2 <= 1.5e+67) {
		tmp = R_m * acos(fma(cos(phi1), cos(phi2), (sin(phi1) * sin(phi2))));
	} else {
		tmp = R_m * acos((cos(phi2) * t_0));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda2 - lambda1))
	tmp = 0.0
	if (phi2 <= -1.25e-5)
		tmp = Float64(R_m * acos(Float64(lambda2 * fma(cos(phi1), Float64(cos(phi2) / lambda2), Float64(sin(phi1) * Float64(sin(phi2) / lambda2))))));
	elseif (phi2 <= 0.00065)
		tmp = Float64(R_m * acos(fma(t_0, cos(phi1), Float64(sin(phi1) * phi2))));
	elseif (phi2 <= 1.5e+67)
		tmp = Float64(R_m * acos(fma(cos(phi1), cos(phi2), Float64(sin(phi1) * sin(phi2)))));
	else
		tmp = Float64(R_m * acos(Float64(cos(phi2) * t_0)));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi2, -1.25e-5], N[(R$95$m * N[ArcCos[N[(lambda2 * N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] / lambda2), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[(N[Sin[phi2], $MachinePrecision] / lambda2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 0.00065], N[(R$95$m * N[ArcCos[N[(t$95$0 * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * phi2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 1.5e+67], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]), $MachinePrecision]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_2 \leq -1.25 \cdot 10^{-5}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \frac{\cos \phi_2}{\lambda_2}, \sin \phi_1 \cdot \frac{\sin \phi_2}{\lambda_2}\right)\right)\\

\mathbf{elif}\;\phi_2 \leq 0.00065:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \phi_1, \sin \phi_1 \cdot \phi_2\right)\right)\\

\mathbf{elif}\;\phi_2 \leq 1.5 \cdot 10^{+67}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if phi2 < -1.25000000000000006e-5

    1. Initial program 86.6%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right) + \left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. associate-+r+N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right) + \cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      2. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1} + \lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      4. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right) \cdot \lambda_2}\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      5. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \sin \lambda_1\right)} \cdot \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      6. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \lambda_2\right)}\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      7. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\lambda_2 \cdot \sin \lambda_1\right)}\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      8. distribute-lft-outN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 + \lambda_2 \cdot \sin \lambda_1\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      9. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1 \cdot \cos \phi_2, \cos \lambda_1 + \lambda_2 \cdot \sin \lambda_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Simplified60.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1 \cdot \cos \phi_2, \mathsf{fma}\left(\lambda_2, \sin \lambda_1, \cos \lambda_1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    6. Taylor expanded in lambda2 around inf

      \[\leadsto \cos^{-1} \color{blue}{\left(\lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right) + \left(\frac{\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}{\lambda_2} + \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2}\right)\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right) + \left(\frac{\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}{\lambda_2} + \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2}\right)\right)\right)} \cdot R \]
      2. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \color{blue}{\left(\left(\frac{\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}{\lambda_2} + \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2}\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)}\right) \cdot R \]
      3. associate-+l+N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \color{blue}{\left(\frac{\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)}{\lambda_2} + \left(\frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2} + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right)}\right) \cdot R \]
      4. associate-/l*N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \left(\color{blue}{\cos \lambda_1 \cdot \frac{\cos \phi_1 \cdot \cos \phi_2}{\lambda_2}} + \left(\frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2} + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right)\right) \cdot R \]
      5. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \frac{\cos \phi_1 \cdot \cos \phi_2}{\lambda_2}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2} + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)}\right) \cdot R \]
      6. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\color{blue}{\cos \lambda_1}, \frac{\cos \phi_1 \cdot \cos \phi_2}{\lambda_2}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2} + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right) \cdot R \]
      7. associate-/l*N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \phi_1 \cdot \frac{\cos \phi_2}{\lambda_2}}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2} + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right) \cdot R \]
      8. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \phi_1 \cdot \frac{\cos \phi_2}{\lambda_2}}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2} + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \phi_1} \cdot \frac{\cos \phi_2}{\lambda_2}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2} + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right) \cdot R \]
      10. lower-/.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \color{blue}{\frac{\cos \phi_2}{\lambda_2}}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2} + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right) \cdot R \]
      11. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \frac{\color{blue}{\cos \phi_2}}{\lambda_2}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2} + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right) \cdot R \]
    8. Simplified60.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\lambda_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \frac{\cos \phi_2}{\lambda_2}, \mathsf{fma}\left(\sin \phi_1, \frac{\sin \phi_2}{\lambda_2}, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right)\right)} \cdot R \]
    9. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\lambda_2 \cdot \left(\frac{\cos \phi_1 \cdot \cos \phi_2}{\lambda_2} + \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2}\right)\right)} \cdot R \]
    10. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\lambda_2 \cdot \left(\frac{\cos \phi_1 \cdot \cos \phi_2}{\lambda_2} + \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2}\right)\right)} \cdot R \]
      2. associate-/l*N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \left(\color{blue}{\cos \phi_1 \cdot \frac{\cos \phi_2}{\lambda_2}} + \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2}\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \color{blue}{\mathsf{fma}\left(\cos \phi_1, \frac{\cos \phi_2}{\lambda_2}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2}\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\color{blue}{\cos \phi_1}, \frac{\cos \phi_2}{\lambda_2}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2}\right)\right) \cdot R \]
      5. lower-/.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \color{blue}{\frac{\cos \phi_2}{\lambda_2}}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2}\right)\right) \cdot R \]
      6. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \frac{\color{blue}{\cos \phi_2}}{\lambda_2}, \frac{\sin \phi_1 \cdot \sin \phi_2}{\lambda_2}\right)\right) \cdot R \]
      7. associate-/l*N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \frac{\cos \phi_2}{\lambda_2}, \color{blue}{\sin \phi_1 \cdot \frac{\sin \phi_2}{\lambda_2}}\right)\right) \cdot R \]
      8. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \frac{\cos \phi_2}{\lambda_2}, \color{blue}{\sin \phi_1 \cdot \frac{\sin \phi_2}{\lambda_2}}\right)\right) \cdot R \]
      9. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \frac{\cos \phi_2}{\lambda_2}, \color{blue}{\sin \phi_1} \cdot \frac{\sin \phi_2}{\lambda_2}\right)\right) \cdot R \]
      10. lower-/.f64N/A

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \frac{\cos \phi_2}{\lambda_2}, \sin \phi_1 \cdot \color{blue}{\frac{\sin \phi_2}{\lambda_2}}\right)\right) \cdot R \]
      11. lower-sin.f6447.4

        \[\leadsto \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \frac{\cos \phi_2}{\lambda_2}, \sin \phi_1 \cdot \frac{\color{blue}{\sin \phi_2}}{\lambda_2}\right)\right) \cdot R \]
    11. Simplified47.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \frac{\cos \phi_2}{\lambda_2}, \sin \phi_1 \cdot \frac{\sin \phi_2}{\lambda_2}\right)\right)} \cdot R \]

    if -1.25000000000000006e-5 < phi2 < 6.4999999999999997e-4

    1. Initial program 71.9%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_2 \cdot \sin \phi_1 + \cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right) + \phi_2 \cdot \sin \phi_1\right)} \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1} + \phi_2 \cdot \sin \phi_1\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right)} \cdot R \]
      4. sub-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      5. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      6. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      7. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      8. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      9. cos-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      10. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      11. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      12. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      13. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      14. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_2 - \lambda_1\right), \color{blue}{\cos \phi_1}, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      15. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_2 - \lambda_1\right), \cos \phi_1, \color{blue}{\phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      16. lower-sin.f6471.9

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_2 - \lambda_1\right), \cos \phi_1, \phi_2 \cdot \color{blue}{\sin \phi_1}\right)\right) \cdot R \]
    5. Simplified71.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_2 - \lambda_1\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right)} \cdot R \]

    if 6.4999999999999997e-4 < phi2 < 1.50000000000000005e67

    1. Initial program 78.3%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right) + \left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. associate-+r+N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right) + \cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      2. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1} + \lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      4. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right) \cdot \lambda_2}\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      5. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \sin \lambda_1\right)} \cdot \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      6. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \lambda_2\right)}\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      7. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\lambda_2 \cdot \sin \lambda_1\right)}\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      8. distribute-lft-outN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 + \lambda_2 \cdot \sin \lambda_1\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      9. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1 \cdot \cos \phi_2, \cos \lambda_1 + \lambda_2 \cdot \sin \lambda_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Simplified47.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1 \cdot \cos \phi_2, \mathsf{fma}\left(\lambda_2, \sin \lambda_1, \cos \lambda_1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    6. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2 + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    7. Step-by-step derivation
      1. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_1}, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \color{blue}{\cos \phi_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \color{blue}{\sin \phi_1 \cdot \sin \phi_2}\right)\right) \cdot R \]
      5. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \color{blue}{\sin \phi_1} \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lower-sin.f6448.8

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \color{blue}{\sin \phi_2}\right)\right) \cdot R \]
    8. Simplified48.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]

    if 1.50000000000000005e67 < phi2

    1. Initial program 76.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_2\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_2\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_2\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      13. lower-cos.f6450.4

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_2}\right) \cdot R \]
    5. Simplified50.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_2\right)} \cdot R \]
  3. Recombined 4 regimes into one program.
  4. Final simplification59.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.25 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\lambda_2 \cdot \mathsf{fma}\left(\cos \phi_1, \frac{\cos \phi_2}{\lambda_2}, \sin \phi_1 \cdot \frac{\sin \phi_2}{\lambda_2}\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 0.00065:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_2 - \lambda_1\right), \cos \phi_1, \sin \phi_1 \cdot \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 1.5 \cdot 10^{+67}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 17: 56.1% accurate, 1.2× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ t_1 := \cos \left(\lambda_2 - \lambda_1\right)\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.25 \cdot 10^{-5}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 0.00065:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_1, \cos \phi_1, \sin \phi_1 \cdot \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 1.5 \cdot 10^{+67}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_1\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (* R_m (acos (fma (cos phi1) (cos phi2) (* (sin phi1) (sin phi2))))))
        (t_1 (cos (- lambda2 lambda1))))
   (*
    R_s
    (if (<= phi2 -1.25e-5)
      t_0
      (if (<= phi2 0.00065)
        (* R_m (acos (fma t_1 (cos phi1) (* (sin phi1) phi2))))
        (if (<= phi2 1.5e+67) t_0 (* R_m (acos (* (cos phi2) t_1)))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = R_m * acos(fma(cos(phi1), cos(phi2), (sin(phi1) * sin(phi2))));
	double t_1 = cos((lambda2 - lambda1));
	double tmp;
	if (phi2 <= -1.25e-5) {
		tmp = t_0;
	} else if (phi2 <= 0.00065) {
		tmp = R_m * acos(fma(t_1, cos(phi1), (sin(phi1) * phi2)));
	} else if (phi2 <= 1.5e+67) {
		tmp = t_0;
	} else {
		tmp = R_m * acos((cos(phi2) * t_1));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(R_m * acos(fma(cos(phi1), cos(phi2), Float64(sin(phi1) * sin(phi2)))))
	t_1 = cos(Float64(lambda2 - lambda1))
	tmp = 0.0
	if (phi2 <= -1.25e-5)
		tmp = t_0;
	elseif (phi2 <= 0.00065)
		tmp = Float64(R_m * acos(fma(t_1, cos(phi1), Float64(sin(phi1) * phi2))));
	elseif (phi2 <= 1.5e+67)
		tmp = t_0;
	else
		tmp = Float64(R_m * acos(Float64(cos(phi2) * t_1)));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi2, -1.25e-5], t$95$0, If[LessEqual[phi2, 0.00065], N[(R$95$m * N[ArcCos[N[(t$95$1 * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * phi2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 1.5e+67], t$95$0, N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * t$95$1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]), $MachinePrecision]]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\
t_1 := \cos \left(\lambda_2 - \lambda_1\right)\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_2 \leq -1.25 \cdot 10^{-5}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\phi_2 \leq 0.00065:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_1, \cos \phi_1, \sin \phi_1 \cdot \phi_2\right)\right)\\

\mathbf{elif}\;\phi_2 \leq 1.5 \cdot 10^{+67}:\\
\;\;\;\;t\_0\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_1\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -1.25000000000000006e-5 or 6.4999999999999997e-4 < phi2 < 1.50000000000000005e67

    1. Initial program 85.3%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right) + \left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. associate-+r+N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right) + \cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      2. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      3. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1} + \lambda_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      4. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \sin \lambda_1\right)\right) \cdot \lambda_2}\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      5. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \sin \lambda_1\right)} \cdot \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      6. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \lambda_2\right)}\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      7. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\lambda_2 \cdot \sin \lambda_1\right)}\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      8. distribute-lft-outN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 + \lambda_2 \cdot \sin \lambda_1\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      9. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1 \cdot \cos \phi_2, \cos \lambda_1 + \lambda_2 \cdot \sin \lambda_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Simplified58.5%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1 \cdot \cos \phi_2, \mathsf{fma}\left(\lambda_2, \sin \lambda_1, \cos \lambda_1\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    6. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2 + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    7. Step-by-step derivation
      1. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_1}, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \color{blue}{\cos \phi_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \color{blue}{\sin \phi_1 \cdot \sin \phi_2}\right)\right) \cdot R \]
      5. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \color{blue}{\sin \phi_1} \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lower-sin.f6447.7

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \color{blue}{\sin \phi_2}\right)\right) \cdot R \]
    8. Simplified47.7%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]

    if -1.25000000000000006e-5 < phi2 < 6.4999999999999997e-4

    1. Initial program 71.9%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_2 \cdot \sin \phi_1 + \cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right) + \phi_2 \cdot \sin \phi_1\right)} \cdot R \]
      2. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1} + \phi_2 \cdot \sin \phi_1\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right)} \cdot R \]
      4. sub-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      5. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      6. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      7. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      8. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      9. cos-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      10. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      11. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      12. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      13. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}, \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      14. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_2 - \lambda_1\right), \color{blue}{\cos \phi_1}, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      15. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_2 - \lambda_1\right), \cos \phi_1, \color{blue}{\phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      16. lower-sin.f6471.9

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_2 - \lambda_1\right), \cos \phi_1, \phi_2 \cdot \color{blue}{\sin \phi_1}\right)\right) \cdot R \]
    5. Simplified71.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_2 - \lambda_1\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right)} \cdot R \]

    if 1.50000000000000005e67 < phi2

    1. Initial program 76.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_2\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_2\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_2\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      13. lower-cos.f6450.4

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_2}\right) \cdot R \]
    5. Simplified50.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_2\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification59.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.25 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 0.00065:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \left(\lambda_2 - \lambda_1\right), \cos \phi_1, \sin \phi_1 \cdot \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 1.5 \cdot 10^{+67}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 18: 33.3% accurate, 1.9× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;t\_0 \leq 0.999999:\\ \;\;\;\;R\_m \cdot \cos^{-1} t\_0\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, -0.5, \lambda_1\right), 1\right)\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (*
    R_s
    (if (<= t_0 0.999999)
      (* R_m (acos t_0))
      (*
       R_m
       (acos (* (cos phi1) (fma lambda2 (fma lambda2 -0.5 lambda1) 1.0))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.999999) {
		tmp = R_m * acos(t_0);
	} else {
		tmp = R_m * acos((cos(phi1) * fma(lambda2, fma(lambda2, -0.5, lambda1), 1.0)));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (t_0 <= 0.999999)
		tmp = Float64(R_m * acos(t_0));
	else
		tmp = Float64(R_m * acos(Float64(cos(phi1) * fma(lambda2, fma(lambda2, -0.5, lambda1), 1.0))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, N[(R$95$s * If[LessEqual[t$95$0, 0.999999], N[(R$95$m * N[ArcCos[t$95$0], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(lambda2 * N[(lambda2 * -0.5 + lambda1), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;t\_0 \leq 0.999999:\\
\;\;\;\;R\_m \cdot \cos^{-1} t\_0\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, -0.5, \lambda_1\right), 1\right)\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (cos.f64 (-.f64 lambda1 lambda2)) < 0.999998999999999971

    1. Initial program 74.9%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6446.0

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified46.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6432.8

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified32.8%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]

    if 0.999998999999999971 < (cos.f64 (-.f64 lambda1 lambda2))

    1. Initial program 83.5%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6435.6

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified35.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \lambda_2 + \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
    7. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\lambda_1, \sin \lambda_2, \cos \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      4. lower-cos.f6435.5

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
    8. Simplified35.5%

      \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\lambda_1, \sin \lambda_2, \cos \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
    9. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \left(\color{blue}{\left(1 + \lambda_2 \cdot \left(\lambda_1 + \frac{-1}{2} \cdot \lambda_2\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    10. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\lambda_2 \cdot \left(\lambda_1 + \frac{-1}{2} \cdot \lambda_2\right) + 1\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\lambda_2, \lambda_1 + \frac{-1}{2} \cdot \lambda_2, 1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\lambda_2, \color{blue}{\frac{-1}{2} \cdot \lambda_2 + \lambda_1}, 1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\lambda_2, \color{blue}{\lambda_2 \cdot \frac{-1}{2}} + \lambda_1, 1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. lower-fma.f6435.5

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\lambda_2, \color{blue}{\mathsf{fma}\left(\lambda_2, -0.5, \lambda_1\right)}, 1\right) \cdot \cos \phi_1\right) \cdot R \]
    11. Simplified35.5%

      \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, -0.5, \lambda_1\right), 1\right)} \cdot \cos \phi_1\right) \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification33.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\lambda_1 - \lambda_2\right) \leq 0.999999:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, -0.5, \lambda_1\right), 1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 19: 36.7% accurate, 1.9× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.002:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{elif}\;\phi_1 \leq -5.2 \cdot 10^{-109}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.5, 1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi1 -0.002)
    (* R_m (acos (* (cos phi1) (cos lambda1))))
    (if (<= phi1 -5.2e-109)
      (* R_m (acos (* (cos (- lambda1 lambda2)) (fma (* phi1 phi1) -0.5 1.0))))
      (* R_m (acos (* (cos phi2) (cos lambda1))))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -0.002) {
		tmp = R_m * acos((cos(phi1) * cos(lambda1)));
	} else if (phi1 <= -5.2e-109) {
		tmp = R_m * acos((cos((lambda1 - lambda2)) * fma((phi1 * phi1), -0.5, 1.0)));
	} else {
		tmp = R_m * acos((cos(phi2) * cos(lambda1)));
	}
	return R_s * tmp;
}
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -0.002)
		tmp = Float64(R_m * acos(Float64(cos(phi1) * cos(lambda1))));
	elseif (phi1 <= -5.2e-109)
		tmp = Float64(R_m * acos(Float64(cos(Float64(lambda1 - lambda2)) * fma(Float64(phi1 * phi1), -0.5, 1.0))));
	else
		tmp = Float64(R_m * acos(Float64(cos(phi2) * cos(lambda1))));
	end
	return Float64(R_s * tmp)
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi1, -0.002], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, -5.2e-109], N[(R$95$m * N[ArcCos[N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[(N[(phi1 * phi1), $MachinePrecision] * -0.5 + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -0.002:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

\mathbf{elif}\;\phi_1 \leq -5.2 \cdot 10^{-109}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.5, 1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -2e-3

    1. Initial program 85.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6447.5

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified47.5%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\mathsf{neg}\left(\lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]
      2. lower-cos.f6443.6

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]
    8. Simplified43.6%

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]

    if -2e-3 < phi1 < -5.1999999999999997e-109

    1. Initial program 84.1%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6452.4

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified52.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) + \frac{-1}{2} \cdot \left({\phi_1}^{2} \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. associate-*r*N/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) + \color{blue}{\left(\frac{-1}{2} \cdot {\phi_1}^{2}\right) \cdot \cos \left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      2. distribute-rgt1-inN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\frac{-1}{2} \cdot {\phi_1}^{2} + 1\right) \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
      3. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(1 + \frac{-1}{2} \cdot {\phi_1}^{2}\right)} \cdot \cos \left(\lambda_2 - \lambda_1\right)\right) \cdot R \]
      4. sub-negN/A

        \[\leadsto \cos^{-1} \left(\left(1 + \frac{-1}{2} \cdot {\phi_1}^{2}\right) \cdot \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)}\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \left(\left(1 + \frac{-1}{2} \cdot {\phi_1}^{2}\right) \cdot \cos \left(\lambda_2 + \color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \left(\left(1 + \frac{-1}{2} \cdot {\phi_1}^{2}\right) \cdot \color{blue}{\cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)}\right) \cdot R \]
      7. neg-mul-1N/A

        \[\leadsto \cos^{-1} \left(\left(1 + \frac{-1}{2} \cdot {\phi_1}^{2}\right) \cdot \cos \left(\mathsf{neg}\left(\left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right)\right)\right) \cdot R \]
      8. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(1 + \frac{-1}{2} \cdot {\phi_1}^{2}\right) \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\left(\mathsf{neg}\left(\lambda_1\right)\right) + \lambda_2\right)}\right)\right)\right) \cdot R \]
      9. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\left(1 + \frac{-1}{2} \cdot {\phi_1}^{2}\right) \cdot \cos \color{blue}{\left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      10. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\left(1 + \frac{-1}{2} \cdot {\phi_1}^{2}\right) \cdot \cos \left(\color{blue}{\lambda_1} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      11. sub-negN/A

        \[\leadsto \cos^{-1} \left(\left(1 + \frac{-1}{2} \cdot {\phi_1}^{2}\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      12. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(1 + \frac{-1}{2} \cdot {\phi_1}^{2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      13. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\frac{-1}{2} \cdot {\phi_1}^{2} + 1\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\left(\color{blue}{{\phi_1}^{2} \cdot \frac{-1}{2}} + 1\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left({\phi_1}^{2}, \frac{-1}{2}, 1\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      16. unpow2N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, \frac{-1}{2}, 1\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      17. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\phi_1 \cdot \phi_1}, \frac{-1}{2}, 1\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      18. sub-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1 \cdot \phi_1, \frac{-1}{2}, 1\right) \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      19. neg-mul-1N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1 \cdot \phi_1, \frac{-1}{2}, 1\right) \cdot \cos \left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right) \cdot R \]
      20. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1 \cdot \phi_1, \frac{-1}{2}, 1\right) \cdot \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)}\right) \cdot R \]
      21. neg-mul-1N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_1 \cdot \phi_1, \frac{-1}{2}, 1\right) \cdot \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right)\right) \cdot R \]
    8. Simplified52.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.5, 1\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]

    if -5.1999999999999997e-109 < phi1

    1. Initial program 73.6%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      13. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      16. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      17. lower-*.f6473.6

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    4. Applied egg-rr73.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Step-by-step derivation
      1. lower-cos.f6455.8

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    7. Simplified55.8%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    8. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \phi_2\right)} \cdot R \]
    9. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_1\right)} \cdot R \]
      3. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \lambda_1\right) \cdot R \]
      4. lower-cos.f6436.2

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    10. Simplified36.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_1\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification39.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.002:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{elif}\;\phi_1 \leq -5.2 \cdot 10^{-109}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \mathsf{fma}\left(\phi_1 \cdot \phi_1, -0.5, 1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 20: 50.7% accurate, 2.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_2 \leq 10^{-5}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\right)\\ \end{array} \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda2 lambda1))))
   (*
    R_s
    (if (<= phi2 1e-5)
      (* R_m (acos (* (cos phi1) t_0)))
      (* R_m (acos (* (cos phi2) t_0)))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda2 - lambda1));
	double tmp;
	if (phi2 <= 1e-5) {
		tmp = R_m * acos((cos(phi1) * t_0));
	} else {
		tmp = R_m * acos((cos(phi2) * t_0));
	}
	return R_s * tmp;
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos((lambda2 - lambda1))
    if (phi2 <= 1d-5) then
        tmp = r_m * acos((cos(phi1) * t_0))
    else
        tmp = r_m * acos((cos(phi2) * t_0))
    end if
    code = r_s * tmp
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda2 - lambda1));
	double tmp;
	if (phi2 <= 1e-5) {
		tmp = R_m * Math.acos((Math.cos(phi1) * t_0));
	} else {
		tmp = R_m * Math.acos((Math.cos(phi2) * t_0));
	}
	return R_s * tmp;
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda2 - lambda1))
	tmp = 0
	if phi2 <= 1e-5:
		tmp = R_m * math.acos((math.cos(phi1) * t_0))
	else:
		tmp = R_m * math.acos((math.cos(phi2) * t_0))
	return R_s * tmp
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda2 - lambda1))
	tmp = 0.0
	if (phi2 <= 1e-5)
		tmp = Float64(R_m * acos(Float64(cos(phi1) * t_0)));
	else
		tmp = Float64(R_m * acos(Float64(cos(phi2) * t_0)));
	end
	return Float64(R_s * tmp)
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda2 - lambda1));
	tmp = 0.0;
	if (phi2 <= 1e-5)
		tmp = R_m * acos((cos(phi1) * t_0));
	else
		tmp = R_m * acos((cos(phi2) * t_0));
	end
	tmp_2 = R_s * tmp;
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]}, N[(R$95$s * If[LessEqual[phi2, 1e-5], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_2 \leq 10^{-5}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\right)\\


\end{array}
\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < 1.00000000000000008e-5

    1. Initial program 77.5%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6451.2

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified51.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]

    if 1.00000000000000008e-5 < phi2

    1. Initial program 77.0%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_2\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_2\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_2\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      13. lower-cos.f6446.1

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_2}\right) \cdot R \]
    5. Simplified46.1%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_2\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification49.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 21: 48.4% accurate, 2.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.3 \cdot 10^{-5}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi2 1.3e-5)
    (* R_m (acos (* (cos phi1) (cos (- lambda2 lambda1)))))
    (* R_m (acos (* (cos phi2) (cos lambda1)))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 1.3e-5) {
		tmp = R_m * acos((cos(phi1) * cos((lambda2 - lambda1))));
	} else {
		tmp = R_m * acos((cos(phi2) * cos(lambda1)));
	}
	return R_s * tmp;
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (phi2 <= 1.3d-5) then
        tmp = r_m * acos((cos(phi1) * cos((lambda2 - lambda1))))
    else
        tmp = r_m * acos((cos(phi2) * cos(lambda1)))
    end if
    code = r_s * tmp
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 1.3e-5) {
		tmp = R_m * Math.acos((Math.cos(phi1) * Math.cos((lambda2 - lambda1))));
	} else {
		tmp = R_m * Math.acos((Math.cos(phi2) * Math.cos(lambda1)));
	}
	return R_s * tmp;
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 1.3e-5:
		tmp = R_m * math.acos((math.cos(phi1) * math.cos((lambda2 - lambda1))))
	else:
		tmp = R_m * math.acos((math.cos(phi2) * math.cos(lambda1)))
	return R_s * tmp
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 1.3e-5)
		tmp = Float64(R_m * acos(Float64(cos(phi1) * cos(Float64(lambda2 - lambda1)))));
	else
		tmp = Float64(R_m * acos(Float64(cos(phi2) * cos(lambda1))));
	end
	return Float64(R_s * tmp)
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 1.3e-5)
		tmp = R_m * acos((cos(phi1) * cos((lambda2 - lambda1))));
	else
		tmp = R_m * acos((cos(phi2) * cos(lambda1)));
	end
	tmp_2 = R_s * tmp;
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi2, 1.3e-5], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_2 \leq 1.3 \cdot 10^{-5}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < 1.29999999999999992e-5

    1. Initial program 77.5%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6451.2

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified51.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]

    if 1.29999999999999992e-5 < phi2

    1. Initial program 77.0%

      \[\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 \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      4. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\cos \phi_1} \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      5. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      7. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      10. +-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      13. associate-*l*N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      14. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      16. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      17. lower-*.f6477.1

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    4. Applied egg-rr77.1%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Step-by-step derivation
      1. lower-cos.f6458.8

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    7. Simplified58.8%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    8. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \phi_2\right)} \cdot R \]
    9. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_1\right)} \cdot R \]
      3. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \lambda_1\right) \cdot R \]
      4. lower-cos.f6439.5

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    10. Simplified39.5%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_1\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification48.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.3 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 22: 42.6% accurate, 2.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1.05 \cdot 10^{-42}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\right)\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= lambda1 -1.05e-42)
    (* R_m (acos (* (cos phi1) (cos lambda1))))
    (* R_m (acos (* (cos phi1) (cos lambda2)))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1.05e-42) {
		tmp = R_m * acos((cos(phi1) * cos(lambda1)));
	} else {
		tmp = R_m * acos((cos(phi1) * cos(lambda2)));
	}
	return R_s * tmp;
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (lambda1 <= (-1.05d-42)) then
        tmp = r_m * acos((cos(phi1) * cos(lambda1)))
    else
        tmp = r_m * acos((cos(phi1) * cos(lambda2)))
    end if
    code = r_s * tmp
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1.05e-42) {
		tmp = R_m * Math.acos((Math.cos(phi1) * Math.cos(lambda1)));
	} else {
		tmp = R_m * Math.acos((Math.cos(phi1) * Math.cos(lambda2)));
	}
	return R_s * tmp;
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -1.05e-42:
		tmp = R_m * math.acos((math.cos(phi1) * math.cos(lambda1)))
	else:
		tmp = R_m * math.acos((math.cos(phi1) * math.cos(lambda2)))
	return R_s * tmp
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -1.05e-42)
		tmp = Float64(R_m * acos(Float64(cos(phi1) * cos(lambda1))));
	else
		tmp = Float64(R_m * acos(Float64(cos(phi1) * cos(lambda2))));
	end
	return Float64(R_s * tmp)
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda1 <= -1.05e-42)
		tmp = R_m * acos((cos(phi1) * cos(lambda1)));
	else
		tmp = R_m * acos((cos(phi1) * cos(lambda2)));
	end
	tmp_2 = R_s * tmp;
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[lambda1, -1.05e-42], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -1.05 \cdot 10^{-42}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda1 < -1.05000000000000003e-42

    1. Initial program 66.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6436.6

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified36.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\mathsf{neg}\left(\lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]
      2. lower-cos.f6436.9

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]
    8. Simplified36.9%

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]

    if -1.05000000000000003e-42 < lambda1

    1. Initial program 81.8%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6445.5

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified45.5%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_2} \cdot \cos \phi_1\right) \cdot R \]
    7. Step-by-step derivation
      1. lower-cos.f6435.7

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_2} \cdot \cos \phi_1\right) \cdot R \]
    8. Simplified35.7%

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_2} \cdot \cos \phi_1\right) \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification36.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1.05 \cdot 10^{-42}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 23: 32.6% accurate, 2.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -3.8 \cdot 10^{-8}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi1 -3.8e-8)
    (* R_m (acos (* (cos phi1) (cos lambda1))))
    (* R_m (acos (cos (- lambda1 lambda2)))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -3.8e-8) {
		tmp = R_m * acos((cos(phi1) * cos(lambda1)));
	} else {
		tmp = R_m * acos(cos((lambda1 - lambda2)));
	}
	return R_s * tmp;
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (phi1 <= (-3.8d-8)) then
        tmp = r_m * acos((cos(phi1) * cos(lambda1)))
    else
        tmp = r_m * acos(cos((lambda1 - lambda2)))
    end if
    code = r_s * tmp
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -3.8e-8) {
		tmp = R_m * Math.acos((Math.cos(phi1) * Math.cos(lambda1)));
	} else {
		tmp = R_m * Math.acos(Math.cos((lambda1 - lambda2)));
	}
	return R_s * tmp;
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi1 <= -3.8e-8:
		tmp = R_m * math.acos((math.cos(phi1) * math.cos(lambda1)))
	else:
		tmp = R_m * math.acos(math.cos((lambda1 - lambda2)))
	return R_s * tmp
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -3.8e-8)
		tmp = Float64(R_m * acos(Float64(cos(phi1) * cos(lambda1))));
	else
		tmp = Float64(R_m * acos(cos(Float64(lambda1 - lambda2))));
	end
	return Float64(R_s * tmp)
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi1 <= -3.8e-8)
		tmp = R_m * acos((cos(phi1) * cos(lambda1)));
	else
		tmp = R_m * acos(cos((lambda1 - lambda2)));
	end
	tmp_2 = R_s * tmp;
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi1, -3.8e-8], N[(R$95$m * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -3.8 \cdot 10^{-8}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -3.80000000000000028e-8

    1. Initial program 86.2%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6447.9

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified47.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\mathsf{neg}\left(\lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]
      2. lower-cos.f6444.1

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]
    8. Simplified44.1%

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]

    if -3.80000000000000028e-8 < phi1

    1. Initial program 74.5%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6441.3

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified41.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6427.6

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified27.6%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification31.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -3.8 \cdot 10^{-8}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 24: 28.2% accurate, 2.9× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\lambda_2 \leq -1.9 \cdot 10^{-299}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{elif}\;\lambda_2 \leq 4.5 \cdot 10^{-7}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \cos \phi_1\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= lambda2 -1.9e-299)
    (* R_m (acos (cos lambda1)))
    (if (<= lambda2 4.5e-7)
      (* R_m (acos (cos phi1)))
      (* R_m (acos (cos lambda2)))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= -1.9e-299) {
		tmp = R_m * acos(cos(lambda1));
	} else if (lambda2 <= 4.5e-7) {
		tmp = R_m * acos(cos(phi1));
	} else {
		tmp = R_m * acos(cos(lambda2));
	}
	return R_s * tmp;
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (lambda2 <= (-1.9d-299)) then
        tmp = r_m * acos(cos(lambda1))
    else if (lambda2 <= 4.5d-7) then
        tmp = r_m * acos(cos(phi1))
    else
        tmp = r_m * acos(cos(lambda2))
    end if
    code = r_s * tmp
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= -1.9e-299) {
		tmp = R_m * Math.acos(Math.cos(lambda1));
	} else if (lambda2 <= 4.5e-7) {
		tmp = R_m * Math.acos(Math.cos(phi1));
	} else {
		tmp = R_m * Math.acos(Math.cos(lambda2));
	}
	return R_s * tmp;
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= -1.9e-299:
		tmp = R_m * math.acos(math.cos(lambda1))
	elif lambda2 <= 4.5e-7:
		tmp = R_m * math.acos(math.cos(phi1))
	else:
		tmp = R_m * math.acos(math.cos(lambda2))
	return R_s * tmp
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= -1.9e-299)
		tmp = Float64(R_m * acos(cos(lambda1)));
	elseif (lambda2 <= 4.5e-7)
		tmp = Float64(R_m * acos(cos(phi1)));
	else
		tmp = Float64(R_m * acos(cos(lambda2)));
	end
	return Float64(R_s * tmp)
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= -1.9e-299)
		tmp = R_m * acos(cos(lambda1));
	elseif (lambda2 <= 4.5e-7)
		tmp = R_m * acos(cos(phi1));
	else
		tmp = R_m * acos(cos(lambda2));
	end
	tmp_2 = R_s * tmp;
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[lambda2, -1.9e-299], N[(R$95$m * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda2, 4.5e-7], N[(R$95$m * N[ArcCos[N[Cos[phi1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[Cos[lambda2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\lambda_2 \leq -1.9 \cdot 10^{-299}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{elif}\;\lambda_2 \leq 4.5 \cdot 10^{-7}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \cos \phi_1\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \cos \lambda_2\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if lambda2 < -1.9000000000000001e-299

    1. Initial program 76.1%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6443.2

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified43.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6427.4

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified27.4%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    9. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6418.1

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    11. Simplified18.1%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if -1.9000000000000001e-299 < lambda2 < 4.4999999999999998e-7

    1. Initial program 91.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6448.3

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified48.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \lambda_2 + \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
    7. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\lambda_1, \sin \lambda_2, \cos \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      4. lower-cos.f6422.0

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
    8. Simplified22.0%

      \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\lambda_1, \sin \lambda_2, \cos \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
    9. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6425.6

        \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]
    11. Simplified25.6%

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]

    if 4.4999999999999998e-7 < lambda2

    1. Initial program 58.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6433.8

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified33.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6424.9

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified24.9%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    9. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)} \cdot R \]
    10. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
      2. lower-cos.f6424.7

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
    11. Simplified24.7%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification21.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_2 \leq -1.9 \cdot 10^{-299}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{elif}\;\lambda_2 \leq 4.5 \cdot 10^{-7}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \]
  5. Add Preprocessing

Alternative 25: 30.0% accurate, 2.9× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.00045:\\ \;\;\;\;R\_m \cdot \cos^{-1} \cos \phi_1\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi1 -0.00045)
    (* R_m (acos (cos phi1)))
    (* R_m (acos (cos (- lambda1 lambda2)))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -0.00045) {
		tmp = R_m * acos(cos(phi1));
	} else {
		tmp = R_m * acos(cos((lambda1 - lambda2)));
	}
	return R_s * tmp;
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (phi1 <= (-0.00045d0)) then
        tmp = r_m * acos(cos(phi1))
    else
        tmp = r_m * acos(cos((lambda1 - lambda2)))
    end if
    code = r_s * tmp
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -0.00045) {
		tmp = R_m * Math.acos(Math.cos(phi1));
	} else {
		tmp = R_m * Math.acos(Math.cos((lambda1 - lambda2)));
	}
	return R_s * tmp;
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi1 <= -0.00045:
		tmp = R_m * math.acos(math.cos(phi1))
	else:
		tmp = R_m * math.acos(math.cos((lambda1 - lambda2)))
	return R_s * tmp
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -0.00045)
		tmp = Float64(R_m * acos(cos(phi1)));
	else
		tmp = Float64(R_m * acos(cos(Float64(lambda1 - lambda2))));
	end
	return Float64(R_s * tmp)
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi1 <= -0.00045)
		tmp = R_m * acos(cos(phi1));
	else
		tmp = R_m * acos(cos((lambda1 - lambda2)));
	end
	tmp_2 = R_s * tmp;
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi1, -0.00045], N[(R$95$m * N[ArcCos[N[Cos[phi1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -0.00045:\\
\;\;\;\;R\_m \cdot \cos^{-1} \cos \phi_1\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -4.4999999999999999e-4

    1. Initial program 85.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6447.5

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified47.5%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \lambda_2 + \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
    7. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\lambda_1, \sin \lambda_2, \cos \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\lambda_1, \color{blue}{\sin \lambda_2}, \cos \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      4. lower-cos.f6426.9

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\lambda_1, \sin \lambda_2, \color{blue}{\cos \lambda_2}\right) \cdot \cos \phi_1\right) \cdot R \]
    8. Simplified26.9%

      \[\leadsto \cos^{-1} \left(\color{blue}{\mathsf{fma}\left(\lambda_1, \sin \lambda_2, \cos \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
    9. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6430.1

        \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]
    11. Simplified30.1%

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]

    if -4.4999999999999999e-4 < phi1

    1. Initial program 74.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6441.5

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified41.5%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6427.9

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified27.9%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification28.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.00045:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 26: 26.4% accurate, 3.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 9 \cdot 10^{-5}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= lambda2 9e-5)
    (* R_m (acos (cos lambda1)))
    (* R_m (acos (cos lambda2))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 9e-5) {
		tmp = R_m * acos(cos(lambda1));
	} else {
		tmp = R_m * acos(cos(lambda2));
	}
	return R_s * tmp;
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (lambda2 <= 9d-5) then
        tmp = r_m * acos(cos(lambda1))
    else
        tmp = r_m * acos(cos(lambda2))
    end if
    code = r_s * tmp
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 9e-5) {
		tmp = R_m * Math.acos(Math.cos(lambda1));
	} else {
		tmp = R_m * Math.acos(Math.cos(lambda2));
	}
	return R_s * tmp;
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= 9e-5:
		tmp = R_m * math.acos(math.cos(lambda1))
	else:
		tmp = R_m * math.acos(math.cos(lambda2))
	return R_s * tmp
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 9e-5)
		tmp = Float64(R_m * acos(cos(lambda1)));
	else
		tmp = Float64(R_m * acos(cos(lambda2)));
	end
	return Float64(R_s * tmp)
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= 9e-5)
		tmp = R_m * acos(cos(lambda1));
	else
		tmp = R_m * acos(cos(lambda2));
	end
	tmp_2 = R_s * tmp;
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[lambda2, 9e-5], N[(R$95$m * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[ArcCos[N[Cos[lambda2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 9 \cdot 10^{-5}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \cos^{-1} \cos \lambda_2\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda2 < 9.00000000000000057e-5

    1. Initial program 81.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6445.0

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified45.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6424.8

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified24.8%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    9. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6418.8

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    11. Simplified18.8%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if 9.00000000000000057e-5 < lambda2

    1. Initial program 58.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6433.8

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified33.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6424.9

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified24.9%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    9. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)} \cdot R \]
    10. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
      2. lower-cos.f6424.7

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
    11. Simplified24.7%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification19.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 9 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \]
  5. Add Preprocessing

Alternative 27: 24.7% accurate, 3.0× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -6200000000:\\ \;\;\;\;R\_m \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R\_m \cdot \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= lambda1 -6200000000.0)
    (* R_m (acos (cos lambda1)))
    (* R_m (fabs (remainder (- lambda2 lambda1) (* PI 2.0)))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -6200000000.0) {
		tmp = R_m * acos(cos(lambda1));
	} else {
		tmp = R_m * fabs(remainder((lambda2 - lambda1), (((double) M_PI) * 2.0)));
	}
	return R_s * tmp;
}
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -6200000000.0) {
		tmp = R_m * Math.acos(Math.cos(lambda1));
	} else {
		tmp = R_m * Math.abs(Math.IEEEremainder((lambda2 - lambda1), (Math.PI * 2.0)));
	}
	return R_s * tmp;
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -6200000000.0:
		tmp = R_m * math.acos(math.cos(lambda1))
	else:
		tmp = R_m * math.fabs(math.remainder((lambda2 - lambda1), (math.pi * 2.0)))
	return R_s * tmp
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[lambda1, -6200000000.0], N[(R$95$m * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[Abs[N[With[{TMP1 = N[(lambda2 - lambda1), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -6200000000:\\
\;\;\;\;R\_m \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{else}:\\
\;\;\;\;R\_m \cdot \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda1 < -6.2e9

    1. Initial program 64.7%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6438.4

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified38.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6430.6

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified30.6%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    9. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6430.8

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    11. Simplified30.8%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if -6.2e9 < lambda1

    1. Initial program 81.5%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6444.4

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified44.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6422.9

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified22.9%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    9. Step-by-step derivation
      1. lift--.f64N/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      2. acos-cos-sN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
      4. lift-acos.f64N/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
      5. acos-cosN/A

        \[\leadsto \color{blue}{\left|\left(\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      6. lower-fabs.f64N/A

        \[\leadsto \color{blue}{\left|\left(\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      7. lower-remainder.f64N/A

        \[\leadsto \left|\color{blue}{\left(\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
    10. Applied egg-rr18.6%

      \[\leadsto \color{blue}{\left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification21.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -6200000000:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \]
  5. Add Preprocessing

Alternative 28: 19.7% accurate, 5.4× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \left(R\_m \cdot \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\right) \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R_s (* R_m (fabs (remainder (- lambda2 lambda1) (* PI 2.0))))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (R_m * fabs(remainder((lambda2 - lambda1), (((double) M_PI) * 2.0))));
}
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (R_m * Math.abs(Math.IEEEremainder((lambda2 - lambda1), (Math.PI * 2.0))));
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	return R_s * (R_m * math.fabs(math.remainder((lambda2 - lambda1), (math.pi * 2.0))))
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * N[(R$95$m * N[Abs[N[With[{TMP1 = N[(lambda2 - lambda1), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \left(R\_m \cdot \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\right)
\end{array}
Derivation
  1. Initial program 77.4%

    \[\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 \]
  2. Add Preprocessing
  3. Taylor expanded in phi2 around 0

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    2. lower-*.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    3. sub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    4. remove-double-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    5. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    6. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    7. +-commutativeN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    8. cos-negN/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    9. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    10. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
    11. unsub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    12. lower--.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    13. lower-cos.f6442.9

      \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
  5. Simplified42.9%

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
  6. Taylor expanded in phi1 around 0

    \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
  7. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
    2. remove-double-negN/A

      \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
    3. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
    4. +-commutativeN/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
    5. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
    6. cos-negN/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    7. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    8. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
    9. sub-negN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    10. lower--.f6424.8

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
  8. Simplified24.8%

    \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
  9. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    2. acos-cos-sN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    3. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \cos \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    4. lift-acos.f64N/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    5. acos-cosN/A

      \[\leadsto \color{blue}{\left|\left(\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
    6. lower-fabs.f64N/A

      \[\leadsto \color{blue}{\left|\left(\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
    7. lower-remainder.f64N/A

      \[\leadsto \left|\color{blue}{\left(\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
  10. Applied egg-rr18.9%

    \[\leadsto \color{blue}{\left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|} \cdot R \]
  11. Final simplification18.9%

    \[\leadsto R \cdot \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right| \]
  12. Add Preprocessing

Alternative 29: 8.8% accurate, 44.7× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -3 \cdot 10^{-172}:\\ \;\;\;\;\lambda_1 \cdot \left(-R\_m\right)\\ \mathbf{else}:\\ \;\;\;\;\lambda_2 \cdot R\_m\\ \end{array} \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R_s (if (<= lambda1 -3e-172) (* lambda1 (- R_m)) (* lambda2 R_m))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -3e-172) {
		tmp = lambda1 * -R_m;
	} else {
		tmp = lambda2 * R_m;
	}
	return R_s * tmp;
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (lambda1 <= (-3d-172)) then
        tmp = lambda1 * -r_m
    else
        tmp = lambda2 * r_m
    end if
    code = r_s * tmp
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -3e-172) {
		tmp = lambda1 * -R_m;
	} else {
		tmp = lambda2 * R_m;
	}
	return R_s * tmp;
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -3e-172:
		tmp = lambda1 * -R_m
	else:
		tmp = lambda2 * R_m
	return R_s * tmp
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -3e-172)
		tmp = Float64(lambda1 * Float64(-R_m));
	else
		tmp = Float64(lambda2 * R_m);
	end
	return Float64(R_s * tmp)
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda1 <= -3e-172)
		tmp = lambda1 * -R_m;
	else
		tmp = lambda2 * R_m;
	end
	tmp_2 = R_s * tmp;
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[lambda1, -3e-172], N[(lambda1 * (-R$95$m)), $MachinePrecision], N[(lambda2 * R$95$m), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -3 \cdot 10^{-172}:\\
\;\;\;\;\lambda_1 \cdot \left(-R\_m\right)\\

\mathbf{else}:\\
\;\;\;\;\lambda_2 \cdot R\_m\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda1 < -2.99999999999999984e-172

    1. Initial program 73.8%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6442.7

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified42.7%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6427.2

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified27.2%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    9. Step-by-step derivation
      1. flip--N/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}{\lambda_1 + \lambda_2}\right)} \cdot R \]
      2. clear-numN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right)} \cdot R \]
      3. lower-/.f64N/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right)} \cdot R \]
      4. lower-/.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\color{blue}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}}\right) \cdot R \]
      5. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\color{blue}{\lambda_2 + \lambda_1}}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right) \cdot R \]
      6. lower-+.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\color{blue}{\lambda_2 + \lambda_1}}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right) \cdot R \]
      7. difference-of-squaresN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_1 + \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      8. lift--.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      9. acos-cos-sN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      10. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      11. lift-acos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      12. lower-*.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_1 + \lambda_2\right) \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      13. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_2 + \lambda_1\right)} \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}\right) \cdot R \]
      14. lower-+.f6427.1

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_2 + \lambda_1\right)} \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}\right) \cdot R \]
      15. lift-acos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      16. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      17. lift--.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      18. cos-diffN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}}\right) \cdot R \]
      19. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      20. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      21. *-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      22. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      23. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      24. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      25. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)}}\right) \cdot R \]
      26. *-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right)}}\right) \cdot R \]
      27. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right)}}\right) \cdot R \]
      28. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right)}}\right) \cdot R \]
    10. Applied egg-rr9.6%

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \left(\lambda_2 - \lambda_1\right)}}\right)} \cdot R \]
    11. Taylor expanded in lambda2 around 0

      \[\leadsto \color{blue}{\left(-1 \cdot \lambda_1\right)} \cdot R \]
    12. Step-by-step derivation
      1. neg-mul-1N/A

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)} \cdot R \]
      2. lower-neg.f648.9

        \[\leadsto \color{blue}{\left(-\lambda_1\right)} \cdot R \]
    13. Simplified8.9%

      \[\leadsto \color{blue}{\left(-\lambda_1\right)} \cdot R \]

    if -2.99999999999999984e-172 < lambda1

    1. Initial program 79.9%

      \[\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 \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      13. lower-cos.f6443.1

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified43.1%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      10. lower--.f6423.2

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified23.2%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    9. Step-by-step derivation
      1. flip--N/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}{\lambda_1 + \lambda_2}\right)} \cdot R \]
      2. clear-numN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right)} \cdot R \]
      3. lower-/.f64N/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right)} \cdot R \]
      4. lower-/.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\color{blue}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}}\right) \cdot R \]
      5. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\color{blue}{\lambda_2 + \lambda_1}}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right) \cdot R \]
      6. lower-+.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\color{blue}{\lambda_2 + \lambda_1}}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right) \cdot R \]
      7. difference-of-squaresN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_1 + \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      8. lift--.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      9. acos-cos-sN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      10. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      11. lift-acos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      12. lower-*.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_1 + \lambda_2\right) \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      13. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_2 + \lambda_1\right)} \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}\right) \cdot R \]
      14. lower-+.f6423.2

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_2 + \lambda_1\right)} \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}\right) \cdot R \]
      15. lift-acos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      16. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      17. lift--.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
      18. cos-diffN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}}\right) \cdot R \]
      19. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      20. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      21. *-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      22. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      23. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      24. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)}}\right) \cdot R \]
      25. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)}}\right) \cdot R \]
      26. *-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right)}}\right) \cdot R \]
      27. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right)}}\right) \cdot R \]
      28. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right)}}\right) \cdot R \]
    10. Applied egg-rr12.0%

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \left(\lambda_2 - \lambda_1\right)}}\right)} \cdot R \]
    11. Taylor expanded in lambda2 around inf

      \[\leadsto \color{blue}{R \cdot \lambda_2} \]
    12. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
      2. lower-*.f645.6

        \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
    13. Simplified5.6%

      \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification6.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -3 \cdot 10^{-172}:\\ \;\;\;\;\lambda_1 \cdot \left(-R\right)\\ \mathbf{else}:\\ \;\;\;\;\lambda_2 \cdot R\\ \end{array} \]
  5. Add Preprocessing

Alternative 30: 9.0% accurate, 69.7× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \left(R\_m \cdot \left(\lambda_2 - \lambda_1\right)\right) \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R_s (* R_m (- lambda2 lambda1))))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (R_m * (lambda2 - lambda1));
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = r_s * (r_m * (lambda2 - lambda1))
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (R_m * (lambda2 - lambda1));
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	return R_s * (R_m * (lambda2 - lambda1))
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	return Float64(R_s * Float64(R_m * Float64(lambda2 - lambda1)))
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = R_s * (R_m * (lambda2 - lambda1));
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * N[(R$95$m * N[(lambda2 - lambda1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \left(R\_m \cdot \left(\lambda_2 - \lambda_1\right)\right)
\end{array}
Derivation
  1. Initial program 77.4%

    \[\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 \]
  2. Add Preprocessing
  3. Taylor expanded in phi2 around 0

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    2. lower-*.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    3. sub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    4. remove-double-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    5. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    6. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    7. +-commutativeN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    8. cos-negN/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    9. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    10. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
    11. unsub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    12. lower--.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    13. lower-cos.f6442.9

      \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
  5. Simplified42.9%

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
  6. Taylor expanded in phi1 around 0

    \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
  7. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
    2. remove-double-negN/A

      \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
    3. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
    4. +-commutativeN/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
    5. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
    6. cos-negN/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    7. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    8. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
    9. sub-negN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    10. lower--.f6424.8

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
  8. Simplified24.8%

    \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
  9. Step-by-step derivation
    1. cos-diffN/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)} \cdot R \]
    2. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot R \]
    3. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot R \]
    4. *-commutativeN/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot R \]
    5. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot R \]
    6. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot R \]
    7. lift-sin.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right) \cdot R \]
    8. lift-sin.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right) \cdot R \]
    9. *-commutativeN/A

      \[\leadsto \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right) \cdot R \]
    10. lift-sin.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right) \cdot R \]
    11. lift-sin.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right) \cdot R \]
    12. cos-diffN/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
    13. acos-cos-sN/A

      \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
    14. lower--.f645.9

      \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
  10. Applied egg-rr5.9%

    \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
  11. Final simplification5.9%

    \[\leadsto R \cdot \left(\lambda_2 - \lambda_1\right) \]
  12. Add Preprocessing

Alternative 31: 6.8% accurate, 104.5× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \left(\lambda_2 \cdot R\_m\right) \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R_s (* lambda2 R_m)))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (lambda2 * R_m);
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = r_s * (lambda2 * r_m)
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (lambda2 * R_m);
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	return R_s * (lambda2 * R_m)
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	return Float64(R_s * Float64(lambda2 * R_m))
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = R_s * (lambda2 * R_m);
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * N[(lambda2 * R$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \left(\lambda_2 \cdot R\_m\right)
\end{array}
Derivation
  1. Initial program 77.4%

    \[\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 \]
  2. Add Preprocessing
  3. Taylor expanded in phi2 around 0

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    2. lower-*.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    3. sub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    4. remove-double-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    5. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    6. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    7. +-commutativeN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    8. cos-negN/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    9. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    10. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
    11. unsub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    12. lower--.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    13. lower-cos.f6442.9

      \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
  5. Simplified42.9%

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
  6. Taylor expanded in phi1 around 0

    \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
  7. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
    2. remove-double-negN/A

      \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
    3. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
    4. +-commutativeN/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
    5. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
    6. cos-negN/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    7. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    8. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
    9. sub-negN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    10. lower--.f6424.8

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
  8. Simplified24.8%

    \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
  9. Step-by-step derivation
    1. flip--N/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}{\lambda_1 + \lambda_2}\right)} \cdot R \]
    2. clear-numN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right)} \cdot R \]
    3. lower-/.f64N/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right)} \cdot R \]
    4. lower-/.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\color{blue}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}}\right) \cdot R \]
    5. +-commutativeN/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\color{blue}{\lambda_2 + \lambda_1}}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right) \cdot R \]
    6. lower-+.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\color{blue}{\lambda_2 + \lambda_1}}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}\right) \cdot R \]
    7. difference-of-squaresN/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_1 + \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
    8. lift--.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
    9. acos-cos-sN/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
    10. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
    11. lift-acos.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_1 + \lambda_2\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
    12. lower-*.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_1 + \lambda_2\right) \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
    13. +-commutativeN/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_2 + \lambda_1\right)} \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}\right) \cdot R \]
    14. lower-+.f6424.8

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\color{blue}{\left(\lambda_2 + \lambda_1\right)} \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}\right) \cdot R \]
    15. lift-acos.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \color{blue}{\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
    16. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
    17. lift--.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}}}\right) \cdot R \]
    18. cos-diffN/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}}\right) \cdot R \]
    19. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
    20. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_1 \cdot \color{blue}{\cos \lambda_2} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
    21. *-commutativeN/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
    22. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
    23. lift-cos.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)}}\right) \cdot R \]
    24. lift-sin.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)}}\right) \cdot R \]
    25. lift-sin.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)}}\right) \cdot R \]
    26. *-commutativeN/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right)}}\right) \cdot R \]
    27. lift-sin.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right)}}\right) \cdot R \]
    28. lift-sin.f64N/A

      \[\leadsto \cos^{-1} \cos \left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right)}}\right) \cdot R \]
  10. Applied egg-rr11.0%

    \[\leadsto \cos^{-1} \cos \color{blue}{\left(\frac{1}{\frac{\lambda_2 + \lambda_1}{\left(\lambda_2 + \lambda_1\right) \cdot \left(\lambda_2 - \lambda_1\right)}}\right)} \cdot R \]
  11. Taylor expanded in lambda2 around inf

    \[\leadsto \color{blue}{R \cdot \lambda_2} \]
  12. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
    2. lower-*.f644.9

      \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
  13. Simplified4.9%

    \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
  14. Add Preprocessing

Alternative 32: 3.6% accurate, 104.5× speedup?

\[\begin{array}{l} R\_m = \left|R\right| \\ R\_s = \mathsf{copysign}\left(1, R\right) \\ [R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\ \\ R\_s \cdot \left(\lambda_1 \cdot R\_m\right) \end{array} \]
R\_m = (fabs.f64 R)
R\_s = (copysign.f64 #s(literal 1 binary64) R)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R_s (* lambda1 R_m)))
R\_m = fabs(R);
R\_s = copysign(1.0, R);
assert(R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (lambda1 * R_m);
}
R\_m = abs(r)
R\_s = copysign(1.0d0, r)
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = r_s * (lambda1 * r_m)
end function
R\_m = Math.abs(R);
R\_s = Math.copySign(1.0, R);
assert R_m < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (lambda1 * R_m);
}
R\_m = math.fabs(R)
R\_s = math.copysign(1.0, R)
[R_m, lambda1, lambda2, phi1, phi2] = sort([R_m, lambda1, lambda2, phi1, phi2])
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	return R_s * (lambda1 * R_m)
R\_m = abs(R)
R\_s = copysign(1.0, R)
R_m, lambda1, lambda2, phi1, phi2 = sort([R_m, lambda1, lambda2, phi1, phi2])
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	return Float64(R_s * Float64(lambda1 * R_m))
end
R\_m = abs(R);
R\_s = sign(R) * abs(1.0);
R_m, lambda1, lambda2, phi1, phi2 = num2cell(sort([R_m, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = R_s * (lambda1 * R_m);
end
R\_m = N[Abs[R], $MachinePrecision]
R\_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
NOTE: R_m, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * N[(lambda1 * R$95$m), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
R\_m = \left|R\right|
\\
R\_s = \mathsf{copysign}\left(1, R\right)
\\
[R_m, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R_m, lambda1, lambda2, phi1, phi2])\\
\\
R\_s \cdot \left(\lambda_1 \cdot R\_m\right)
\end{array}
Derivation
  1. Initial program 77.4%

    \[\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 \]
  2. Add Preprocessing
  3. Taylor expanded in phi2 around 0

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
  4. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    2. lower-*.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    3. sub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    4. remove-double-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    5. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    6. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
    7. +-commutativeN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    8. cos-negN/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    9. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    10. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
    11. unsub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    12. lower--.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    13. lower-cos.f6442.9

      \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
  5. Simplified42.9%

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
  6. Taylor expanded in phi1 around 0

    \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \cdot R \]
  7. Step-by-step derivation
    1. sub-negN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
    2. remove-double-negN/A

      \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
    3. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
    4. +-commutativeN/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
    5. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
    6. cos-negN/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    7. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    8. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
    9. sub-negN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    10. lower--.f6424.8

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
  8. Simplified24.8%

    \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
  9. Taylor expanded in lambda1 around inf

    \[\leadsto \color{blue}{R \cdot \lambda_1} \]
  10. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{\lambda_1 \cdot R} \]
    2. lower-*.f645.0

      \[\leadsto \color{blue}{\lambda_1 \cdot R} \]
  11. Simplified5.0%

    \[\leadsto \color{blue}{\lambda_1 \cdot R} \]
  12. Add Preprocessing

Reproduce

?
herbie shell --seed 2024221 
(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))