Spherical law of cosines

Percentage Accurate: 74.2% → 96.8%
Time: 27.6s
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: 74.2% 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.8% accurate, 0.4× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, 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\\ \mathbf{if}\;\cos^{-1} \left(t\_0 + t\_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \leq 0:\\ \;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t\_0 + \mathsf{fma}\left(t\_1 \cdot \sin \lambda_1, \sin \lambda_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))) (t_1 (* (cos phi1) (cos phi2))))
   (if (<= (acos (+ t_0 (* t_1 (cos (- lambda1 lambda2))))) 0.0)
     (* (- lambda2 lambda1) R)
     (*
      R
      (acos
       (+
        t_0
        (fma
         (* t_1 (sin lambda1))
         (sin lambda2)
         (* (cos phi1) (* (cos phi2) (* (cos lambda2) (cos lambda1)))))))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, 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 (acos((t_0 + (t_1 * cos((lambda1 - lambda2))))) <= 0.0) {
		tmp = (lambda2 - lambda1) * R;
	} else {
		tmp = R * acos((t_0 + fma((t_1 * sin(lambda1)), sin(lambda2), (cos(phi1) * (cos(phi2) * (cos(lambda2) * cos(lambda1)))))));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(phi1) * sin(phi2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if (acos(Float64(t_0 + Float64(t_1 * cos(Float64(lambda1 - lambda2))))) <= 0.0)
		tmp = Float64(Float64(lambda2 - lambda1) * R);
	else
		tmp = Float64(R * acos(Float64(t_0 + fma(Float64(t_1 * sin(lambda1)), sin(lambda2), Float64(cos(phi1) * Float64(cos(phi2) * Float64(cos(lambda2) * cos(lambda1))))))));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, 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]}, If[LessEqual[N[ArcCos[N[(t$95$0 + N[(t$95$1 * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0], N[(N[(lambda2 - lambda1), $MachinePrecision] * R), $MachinePrecision], N[(R * N[ArcCos[N[(t$95$0 + N[(N[(t$95$1 * N[Sin[lambda1], $MachinePrecision]), $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, 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\\
\mathbf{if}\;\cos^{-1} \left(t\_0 + t\_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \leq 0:\\
\;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\

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


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

    1. Initial program 17.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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in phi2 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--.f6417.9

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

      \[\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. lift--.f64N/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
      14. acos-cos-s58.9

        \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
    10. Applied egg-rr58.9%

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

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

    1. Initial program 76.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    5. Step-by-step derivation
      1. 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 \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      2. 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 \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. 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 \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      4. 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 \left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \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 \cos \phi_2\right) \cdot \left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lift-sin.f64N/A

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 96.8% accurate, 0.4× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ \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) \leq 0:\\ \;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right), \cos \phi_1, t\_0\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))))
   (if (<=
        (acos (+ t_0 (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
        0.0)
     (* (- lambda2 lambda1) R)
     (*
      R
      (acos
       (fma
        (*
         (cos phi2)
         (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))
        (cos phi1)
        t_0))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, 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))))) <= 0.0) {
		tmp = (lambda2 - lambda1) * R;
	} else {
		tmp = R * acos(fma((cos(phi2) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))), cos(phi1), t_0));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(phi1) * sin(phi2))
	tmp = 0.0
	if (acos(Float64(t_0 + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) <= 0.0)
		tmp = Float64(Float64(lambda2 - lambda1) * R);
	else
		tmp = Float64(R * acos(fma(Float64(cos(phi2) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2)))), cos(phi1), t_0)));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[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], 0.0], N[(N[(lambda2 - lambda1), $MachinePrecision] * R), $MachinePrecision], N[(R * N[ArcCos[N[(N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi1], $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
\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) \leq 0:\\
\;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\

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


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

    1. Initial program 17.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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in phi2 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--.f6417.9

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

      \[\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. lift--.f64N/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
      14. acos-cos-s58.9

        \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
    10. Applied egg-rr58.9%

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

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

    1. Initial program 76.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2 \cdot \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 \]
      3. lower-*.f64N/A

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2} \cdot \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 \]
      5. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \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 \]
      6. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\color{blue}{\cos \lambda_1}, \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 \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \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 \]
      8. lower-*.f64N/A

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

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

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

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

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

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

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

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

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

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

Alternative 3: 83.8% accurate, 0.7× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ t_1 := \sqrt{\pi} \cdot 0.5\\ \mathbf{if}\;\phi_1 \leq -0.0015:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\sqrt{\pi}, t\_1, -\mathsf{fma}\left(t\_1, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 2.1 \cdot 10^{-34}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t\_0 + \left(\cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot \mathsf{fma}\left(-0.5, \phi_1 \cdot \phi_1, 1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\mathsf{fma}\left(t\_1, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), t\_0\right)\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))) (t_1 (* (sqrt PI) 0.5)))
   (if (<= phi1 -0.0015)
     (*
      R
      (fma
       (sqrt PI)
       t_1
       (-
        (fma
         t_1
         (sqrt PI)
         (-
          (acos
           (fma
            (sin phi1)
            (sin phi2)
            (* (cos phi1) (* (cos phi2) (cos (- lambda2 lambda1)))))))))))
     (if (<= phi1 2.1e-34)
       (*
        R
        (acos
         (+
          t_0
          (*
           (*
            (cos phi2)
            (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))
           (fma -0.5 (* phi1 phi1) 1.0)))))
       (fma
        (-
         (fma
          t_1
          (sqrt PI)
          (-
           (acos
            (fma (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2))) t_0)))))
        R
        (* R (* PI 0.5)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin(phi1) * sin(phi2);
	double t_1 = sqrt(((double) M_PI)) * 0.5;
	double tmp;
	if (phi1 <= -0.0015) {
		tmp = R * fma(sqrt(((double) M_PI)), t_1, -fma(t_1, sqrt(((double) M_PI)), -acos(fma(sin(phi1), sin(phi2), (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1))))))));
	} else if (phi1 <= 2.1e-34) {
		tmp = R * acos((t_0 + ((cos(phi2) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))) * fma(-0.5, (phi1 * phi1), 1.0))));
	} else {
		tmp = fma(-fma(t_1, sqrt(((double) M_PI)), -acos(fma(cos(phi1), (cos(phi2) * cos((lambda1 - lambda2))), t_0))), R, (R * (((double) M_PI) * 0.5)));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(phi1) * sin(phi2))
	t_1 = Float64(sqrt(pi) * 0.5)
	tmp = 0.0
	if (phi1 <= -0.0015)
		tmp = Float64(R * fma(sqrt(pi), t_1, Float64(-fma(t_1, sqrt(pi), Float64(-acos(fma(sin(phi1), sin(phi2), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda2 - lambda1)))))))))));
	elseif (phi1 <= 2.1e-34)
		tmp = Float64(R * acos(Float64(t_0 + Float64(Float64(cos(phi2) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2)))) * fma(-0.5, Float64(phi1 * phi1), 1.0)))));
	else
		tmp = fma(Float64(-fma(t_1, sqrt(pi), Float64(-acos(fma(cos(phi1), Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))), t_0))))), R, Float64(R * Float64(pi * 0.5)));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Sqrt[Pi], $MachinePrecision] * 0.5), $MachinePrecision]}, If[LessEqual[phi1, -0.0015], N[(R * N[(N[Sqrt[Pi], $MachinePrecision] * t$95$1 + (-N[(t$95$1 * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])), $MachinePrecision])), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 2.1e-34], N[(R * N[ArcCos[N[(t$95$0 + N[(N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(-0.5 * N[(phi1 * phi1), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[(t$95$1 * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
t_1 := \sqrt{\pi} \cdot 0.5\\
\mathbf{if}\;\phi_1 \leq -0.0015:\\
\;\;\;\;R \cdot \mathsf{fma}\left(\sqrt{\pi}, t\_1, -\mathsf{fma}\left(t\_1, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 2.1 \cdot 10^{-34}:\\
\;\;\;\;R \cdot \cos^{-1} \left(t\_0 + \left(\cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot \mathsf{fma}\left(-0.5, \phi_1 \cdot \phi_1, 1\right)\right)\\

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


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

    1. Initial program 79.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. 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 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      7. 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 \]
      8. 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 \]
      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. acos-asinN/A

        \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} - \sin^{-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)\right)} \cdot R \]
      12. sub-negN/A

        \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} + \left(\mathsf{neg}\left(\sin^{-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)\right)\right)\right)} \cdot R \]
    4. Applied egg-rr78.4%

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

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

    if -0.0015 < phi1 < 2.1000000000000001e-34

    1. Initial program 66.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.1000000000000001e-34 < phi1

    1. Initial program 81.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

Alternative 4: 24.8% accurate, 0.7× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \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 2 \cdot 10^{-298}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} \cos \left(\lambda_2 - \lambda_1\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<=
      (*
       (acos
        (+
         (* (sin phi1) (sin phi2))
         (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
       R)
      2e-298)
   (* R (fabs (remainder (- lambda1 lambda2) (* PI 2.0))))
   (fma (- (asin (cos (- lambda2 lambda1)))) R (* R (* PI 0.5)))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if ((acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R) <= 2e-298) {
		tmp = R * fabs(remainder((lambda1 - lambda2), (((double) M_PI) * 2.0)));
	} else {
		tmp = fma(-asin(cos((lambda2 - lambda1))), R, (R * (((double) M_PI) * 0.5)));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[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], 2e-298], N[(R * N[Abs[N[With[{TMP1 = N[(lambda1 - lambda2), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[ArcSin[N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\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 2 \cdot 10^{-298}:\\
\;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\

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


\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) < 1.99999999999999982e-298

    1. Initial program 71.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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6440.7

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified40.7%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in phi2 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--.f6426.6

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

      \[\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-cosN/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      3. lower-fabs.f64N/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      4. lower-remainder.f64N/A

        \[\leadsto \left|\color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
      5. lift-PI.f64N/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right| \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      7. lower-*.f6423.0

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\pi \cdot 2\right)}\right)\right| \cdot R \]
    10. Applied egg-rr23.0%

      \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|} \cdot R \]

    if 1.99999999999999982e-298 < (*.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 75.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\sin^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)}\right), R, \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R\right) \]
    7. Step-by-step derivation
      1. sub-negN/A

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

        \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\sin^{-1} \left(\cos \phi_2 \cdot \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)\right)\right), R, \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R\right) \]
      3. distribute-neg-inN/A

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\sin^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right), R, \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R\right) \]
    10. Step-by-step derivation
      1. sub-negN/A

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

        \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\sin^{-1} \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)\right), R, \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R\right) \]
      3. mul-1-negN/A

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

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

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

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

        \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\sin^{-1} \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right), R, \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R\right) \]
      8. mul-1-negN/A

        \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\sin^{-1} \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right), R, \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R\right) \]
      9. unsub-negN/A

        \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\sin^{-1} \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right), R, \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R\right) \]
      10. lower--.f6428.2

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

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

    \[\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 2 \cdot 10^{-298}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} \cos \left(\lambda_2 - \lambda_1\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 83.8% accurate, 0.8× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sqrt{\pi} \cdot 0.5\\ \mathbf{if}\;\phi_1 \leq -0.0016:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\sqrt{\pi}, t\_0, -\mathsf{fma}\left(t\_0, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 2.1 \cdot 10^{-34}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\phi_1, \sin \phi_2, \left(\cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot \mathsf{fma}\left(-0.5, \phi_1 \cdot \phi_1, 1\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\mathsf{fma}\left(t\_0, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sqrt PI) 0.5)))
   (if (<= phi1 -0.0016)
     (*
      R
      (fma
       (sqrt PI)
       t_0
       (-
        (fma
         t_0
         (sqrt PI)
         (-
          (acos
           (fma
            (sin phi1)
            (sin phi2)
            (* (cos phi1) (* (cos phi2) (cos (- lambda2 lambda1)))))))))))
     (if (<= phi1 2.1e-34)
       (*
        R
        (acos
         (fma
          phi1
          (sin phi2)
          (*
           (*
            (cos phi2)
            (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))
           (fma -0.5 (* phi1 phi1) 1.0)))))
       (fma
        (-
         (fma
          t_0
          (sqrt PI)
          (-
           (acos
            (fma
             (cos phi1)
             (* (cos phi2) (cos (- lambda1 lambda2)))
             (* (sin phi1) (sin phi2)))))))
        R
        (* R (* PI 0.5)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sqrt(((double) M_PI)) * 0.5;
	double tmp;
	if (phi1 <= -0.0016) {
		tmp = R * fma(sqrt(((double) M_PI)), t_0, -fma(t_0, sqrt(((double) M_PI)), -acos(fma(sin(phi1), sin(phi2), (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1))))))));
	} else if (phi1 <= 2.1e-34) {
		tmp = R * acos(fma(phi1, sin(phi2), ((cos(phi2) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))) * fma(-0.5, (phi1 * phi1), 1.0))));
	} else {
		tmp = fma(-fma(t_0, sqrt(((double) M_PI)), -acos(fma(cos(phi1), (cos(phi2) * cos((lambda1 - lambda2))), (sin(phi1) * sin(phi2))))), R, (R * (((double) M_PI) * 0.5)));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sqrt(pi) * 0.5)
	tmp = 0.0
	if (phi1 <= -0.0016)
		tmp = Float64(R * fma(sqrt(pi), t_0, Float64(-fma(t_0, sqrt(pi), Float64(-acos(fma(sin(phi1), sin(phi2), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda2 - lambda1)))))))))));
	elseif (phi1 <= 2.1e-34)
		tmp = Float64(R * acos(fma(phi1, sin(phi2), Float64(Float64(cos(phi2) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2)))) * fma(-0.5, Float64(phi1 * phi1), 1.0)))));
	else
		tmp = fma(Float64(-fma(t_0, sqrt(pi), Float64(-acos(fma(cos(phi1), Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))), Float64(sin(phi1) * sin(phi2))))))), R, Float64(R * Float64(pi * 0.5)));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sqrt[Pi], $MachinePrecision] * 0.5), $MachinePrecision]}, If[LessEqual[phi1, -0.0016], N[(R * N[(N[Sqrt[Pi], $MachinePrecision] * t$95$0 + (-N[(t$95$0 * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])), $MachinePrecision])), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 2.1e-34], N[(R * N[ArcCos[N[(phi1 * N[Sin[phi2], $MachinePrecision] + N[(N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(-0.5 * N[(phi1 * phi1), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[(t$95$0 * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \sqrt{\pi} \cdot 0.5\\
\mathbf{if}\;\phi_1 \leq -0.0016:\\
\;\;\;\;R \cdot \mathsf{fma}\left(\sqrt{\pi}, t\_0, -\mathsf{fma}\left(t\_0, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\right)\right)\\

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

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


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

    1. Initial program 79.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. 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 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      7. 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 \]
      8. 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 \]
      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. acos-asinN/A

        \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} - \sin^{-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)\right)} \cdot R \]
      12. sub-negN/A

        \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} + \left(\mathsf{neg}\left(\sin^{-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)\right)\right)\right)} \cdot R \]
    4. Applied egg-rr78.4%

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

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

    if -0.00160000000000000008 < phi1 < 2.1000000000000001e-34

    1. Initial program 66.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_1 \cdot \left(\sin \phi_2 + \frac{-1}{2} \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right) + \cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    6. Step-by-step derivation
      1. distribute-rgt-inN/A

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\phi_1 \cdot \sin \phi_2 + \left(\left(\frac{-1}{2} \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right) \cdot \phi_1 + \cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)} \cdot R \]
    7. Simplified90.0%

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

    if 2.1000000000000001e-34 < phi1

    1. Initial program 81.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

Alternative 6: 83.7% accurate, 0.8× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sqrt{\pi} \cdot 0.5\\ \mathbf{if}\;\phi_1 \leq -0.000116:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\sqrt{\pi}, t\_0, -\mathsf{fma}\left(t\_0, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 2.1 \cdot 10^{-34}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right), \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\mathsf{fma}\left(t\_0, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sqrt PI) 0.5)))
   (if (<= phi1 -0.000116)
     (*
      R
      (fma
       (sqrt PI)
       t_0
       (-
        (fma
         t_0
         (sqrt PI)
         (-
          (acos
           (fma
            (sin phi1)
            (sin phi2)
            (* (cos phi1) (* (cos phi2) (cos (- lambda2 lambda1)))))))))))
     (if (<= phi1 2.1e-34)
       (*
        R
        (acos
         (fma
          (cos phi2)
          (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2)))
          (* phi1 (sin phi2)))))
       (fma
        (-
         (fma
          t_0
          (sqrt PI)
          (-
           (acos
            (fma
             (cos phi1)
             (* (cos phi2) (cos (- lambda1 lambda2)))
             (* (sin phi1) (sin phi2)))))))
        R
        (* R (* PI 0.5)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sqrt(((double) M_PI)) * 0.5;
	double tmp;
	if (phi1 <= -0.000116) {
		tmp = R * fma(sqrt(((double) M_PI)), t_0, -fma(t_0, sqrt(((double) M_PI)), -acos(fma(sin(phi1), sin(phi2), (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1))))))));
	} else if (phi1 <= 2.1e-34) {
		tmp = R * acos(fma(cos(phi2), fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2))), (phi1 * sin(phi2))));
	} else {
		tmp = fma(-fma(t_0, sqrt(((double) M_PI)), -acos(fma(cos(phi1), (cos(phi2) * cos((lambda1 - lambda2))), (sin(phi1) * sin(phi2))))), R, (R * (((double) M_PI) * 0.5)));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sqrt(pi) * 0.5)
	tmp = 0.0
	if (phi1 <= -0.000116)
		tmp = Float64(R * fma(sqrt(pi), t_0, Float64(-fma(t_0, sqrt(pi), Float64(-acos(fma(sin(phi1), sin(phi2), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda2 - lambda1)))))))))));
	elseif (phi1 <= 2.1e-34)
		tmp = Float64(R * acos(fma(cos(phi2), fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))), Float64(phi1 * sin(phi2)))));
	else
		tmp = fma(Float64(-fma(t_0, sqrt(pi), Float64(-acos(fma(cos(phi1), Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))), Float64(sin(phi1) * sin(phi2))))))), R, Float64(R * Float64(pi * 0.5)));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sqrt[Pi], $MachinePrecision] * 0.5), $MachinePrecision]}, If[LessEqual[phi1, -0.000116], N[(R * N[(N[Sqrt[Pi], $MachinePrecision] * t$95$0 + (-N[(t$95$0 * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])), $MachinePrecision])), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 2.1e-34], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(phi1 * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[(t$95$0 * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \sqrt{\pi} \cdot 0.5\\
\mathbf{if}\;\phi_1 \leq -0.000116:\\
\;\;\;\;R \cdot \mathsf{fma}\left(\sqrt{\pi}, t\_0, -\mathsf{fma}\left(t\_0, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\right)\right)\\

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

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


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

    1. Initial program 79.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. 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 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      7. 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 \]
      8. 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 \]
      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. acos-asinN/A

        \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} - \sin^{-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)\right)} \cdot R \]
      12. sub-negN/A

        \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} + \left(\mathsf{neg}\left(\sin^{-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)\right)\right)\right)} \cdot R \]
    4. Applied egg-rr78.4%

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

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

    if -1.16e-4 < phi1 < 2.1000000000000001e-34

    1. Initial program 66.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.1000000000000001e-34 < phi1

    1. Initial program 81.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

Alternative 7: 83.6% accurate, 0.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sqrt{\pi} \cdot 0.5\\ \mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\sqrt{\pi}, t\_0, -\mathsf{fma}\left(t\_0, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 2.1 \cdot 10^{-34}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\mathsf{fma}\left(t\_0, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sqrt PI) 0.5)))
   (if (<= phi1 -6.6e-15)
     (*
      R
      (fma
       (sqrt PI)
       t_0
       (-
        (fma
         t_0
         (sqrt PI)
         (-
          (acos
           (fma
            (sin phi1)
            (sin phi2)
            (* (cos phi1) (* (cos phi2) (cos (- lambda2 lambda1)))))))))))
     (if (<= phi1 2.1e-34)
       (*
        R
        (acos
         (*
          (cos phi2)
          (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))
       (fma
        (-
         (fma
          t_0
          (sqrt PI)
          (-
           (acos
            (fma
             (cos phi1)
             (* (cos phi2) (cos (- lambda1 lambda2)))
             (* (sin phi1) (sin phi2)))))))
        R
        (* R (* PI 0.5)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sqrt(((double) M_PI)) * 0.5;
	double tmp;
	if (phi1 <= -6.6e-15) {
		tmp = R * fma(sqrt(((double) M_PI)), t_0, -fma(t_0, sqrt(((double) M_PI)), -acos(fma(sin(phi1), sin(phi2), (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1))))))));
	} else if (phi1 <= 2.1e-34) {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = fma(-fma(t_0, sqrt(((double) M_PI)), -acos(fma(cos(phi1), (cos(phi2) * cos((lambda1 - lambda2))), (sin(phi1) * sin(phi2))))), R, (R * (((double) M_PI) * 0.5)));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sqrt(pi) * 0.5)
	tmp = 0.0
	if (phi1 <= -6.6e-15)
		tmp = Float64(R * fma(sqrt(pi), t_0, Float64(-fma(t_0, sqrt(pi), Float64(-acos(fma(sin(phi1), sin(phi2), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda2 - lambda1)))))))))));
	elseif (phi1 <= 2.1e-34)
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = fma(Float64(-fma(t_0, sqrt(pi), Float64(-acos(fma(cos(phi1), Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))), Float64(sin(phi1) * sin(phi2))))))), R, Float64(R * Float64(pi * 0.5)));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sqrt[Pi], $MachinePrecision] * 0.5), $MachinePrecision]}, If[LessEqual[phi1, -6.6e-15], N[(R * N[(N[Sqrt[Pi], $MachinePrecision] * t$95$0 + (-N[(t$95$0 * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])), $MachinePrecision])), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 2.1e-34], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[(t$95$0 * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \sqrt{\pi} \cdot 0.5\\
\mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\
\;\;\;\;R \cdot \mathsf{fma}\left(\sqrt{\pi}, t\_0, -\mathsf{fma}\left(t\_0, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\right)\right)\\

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

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


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

    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. 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 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      7. 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 \]
      8. 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 \]
      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. acos-asinN/A

        \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} - \sin^{-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)\right)} \cdot R \]
      12. sub-negN/A

        \[\leadsto \color{blue}{\left(\frac{\mathsf{PI}\left(\right)}{2} + \left(\mathsf{neg}\left(\sin^{-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)\right)\right)\right)} \cdot R \]
    4. Applied egg-rr77.5%

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

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

    if -6.6e-15 < phi1 < 2.1000000000000001e-34

    1. Initial program 66.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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6466.3

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified66.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.1000000000000001e-34 < phi1

    1. Initial program 81.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

Alternative 8: 83.5% accurate, 0.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := R \cdot \left(\pi \cdot 0.5\right)\\ \mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right), R, t\_0\right)\\ \mathbf{elif}\;\phi_1 \leq 2.1 \cdot 10^{-34}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\mathsf{fma}\left(\sqrt{\pi} \cdot 0.5, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R, t\_0\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* R (* PI 0.5))))
   (if (<= phi1 -6.6e-15)
     (fma
      (-
       (asin
        (fma
         (sin phi1)
         (sin phi2)
         (* (cos phi1) (* (cos phi2) (cos (- lambda2 lambda1)))))))
      R
      t_0)
     (if (<= phi1 2.1e-34)
       (*
        R
        (acos
         (*
          (cos phi2)
          (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))
       (fma
        (-
         (fma
          (* (sqrt PI) 0.5)
          (sqrt PI)
          (-
           (acos
            (fma
             (cos phi1)
             (* (cos phi2) (cos (- lambda1 lambda2)))
             (* (sin phi1) (sin phi2)))))))
        R
        t_0)))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = R * (((double) M_PI) * 0.5);
	double tmp;
	if (phi1 <= -6.6e-15) {
		tmp = fma(-asin(fma(sin(phi1), sin(phi2), (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1)))))), R, t_0);
	} else if (phi1 <= 2.1e-34) {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = fma(-fma((sqrt(((double) M_PI)) * 0.5), sqrt(((double) M_PI)), -acos(fma(cos(phi1), (cos(phi2) * cos((lambda1 - lambda2))), (sin(phi1) * sin(phi2))))), R, t_0);
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(R * Float64(pi * 0.5))
	tmp = 0.0
	if (phi1 <= -6.6e-15)
		tmp = fma(Float64(-asin(fma(sin(phi1), sin(phi2), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda2 - lambda1))))))), R, t_0);
	elseif (phi1 <= 2.1e-34)
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = fma(Float64(-fma(Float64(sqrt(pi) * 0.5), sqrt(pi), Float64(-acos(fma(cos(phi1), Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))), Float64(sin(phi1) * sin(phi2))))))), R, t_0);
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -6.6e-15], N[((-N[ArcSin[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]) * R + t$95$0), $MachinePrecision], If[LessEqual[phi1, 2.1e-34], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[(N[(N[Sqrt[Pi], $MachinePrecision] * 0.5), $MachinePrecision] * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]) * R + t$95$0), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := R \cdot \left(\pi \cdot 0.5\right)\\
\mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\
\;\;\;\;\mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right), R, t\_0\right)\\

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

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


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

    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. Step-by-step derivation
      1. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

    if -6.6e-15 < phi1 < 2.1000000000000001e-34

    1. Initial program 66.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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6466.3

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified66.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.1000000000000001e-34 < phi1

    1. Initial program 81.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

Alternative 9: 83.8% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 7.2 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\pi \cdot 0.5, R, \sin^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right) \cdot \left(-R\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -6.6e-15)
   (fma
    (-
     (asin
      (fma
       (sin phi1)
       (sin phi2)
       (* (cos phi1) (* (cos phi2) (cos (- lambda2 lambda1)))))))
    R
    (* R (* PI 0.5)))
   (if (<= phi1 7.2e-14)
     (*
      R
      (acos
       (*
        (cos phi2)
        (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))
     (fma
      (* PI 0.5)
      R
      (*
       (asin
        (fma
         (sin phi1)
         (sin phi2)
         (* (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2))))))
       (- R))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -6.6e-15) {
		tmp = fma(-asin(fma(sin(phi1), sin(phi2), (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1)))))), R, (R * (((double) M_PI) * 0.5)));
	} else if (phi1 <= 7.2e-14) {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = fma((((double) M_PI) * 0.5), R, (asin(fma(sin(phi1), sin(phi2), (cos(phi1) * (cos(phi2) * cos((lambda1 - lambda2)))))) * -R));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -6.6e-15)
		tmp = fma(Float64(-asin(fma(sin(phi1), sin(phi2), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda2 - lambda1))))))), R, Float64(R * Float64(pi * 0.5)));
	elseif (phi1 <= 7.2e-14)
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = fma(Float64(pi * 0.5), R, Float64(asin(fma(sin(phi1), sin(phi2), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda1 - lambda2)))))) * Float64(-R)));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -6.6e-15], N[((-N[ArcSin[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 7.2e-14], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(Pi * 0.5), $MachinePrecision] * R + N[(N[ArcSin[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * (-R)), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\
\;\;\;\;\mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\

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

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


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

    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. Step-by-step derivation
      1. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

    if -6.6e-15 < phi1 < 7.1999999999999996e-14

    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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6466.7

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified66.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 7.1999999999999996e-14 < phi1

    1. Initial program 81.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. 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 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      7. 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 \]
      8. 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 \]
      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-rr81.2%

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

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

Alternative 10: 83.8% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_1 \leq 7.2 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (fma
          (-
           (asin
            (fma
             (sin phi1)
             (sin phi2)
             (* (cos phi1) (* (cos phi2) (cos (- lambda2 lambda1)))))))
          R
          (* R (* PI 0.5)))))
   (if (<= phi1 -6.6e-15)
     t_0
     (if (<= phi1 7.2e-14)
       (*
        R
        (acos
         (*
          (cos phi2)
          (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))
       t_0))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = fma(-asin(fma(sin(phi1), sin(phi2), (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1)))))), R, (R * (((double) M_PI) * 0.5)));
	double tmp;
	if (phi1 <= -6.6e-15) {
		tmp = t_0;
	} else if (phi1 <= 7.2e-14) {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = t_0;
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = fma(Float64(-asin(fma(sin(phi1), sin(phi2), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda2 - lambda1))))))), R, Float64(R * Float64(pi * 0.5)))
	tmp = 0.0
	if (phi1 <= -6.6e-15)
		tmp = t_0;
	elseif (phi1 <= 7.2e-14)
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = t_0;
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[((-N[ArcSin[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -6.6e-15], t$95$0, If[LessEqual[phi1, 7.2e-14], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\
\mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\
\;\;\;\;t\_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -6.6e-15 or 7.1999999999999996e-14 < phi1

    1. Initial program 79.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

    if -6.6e-15 < phi1 < 7.1999999999999996e-14

    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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6466.7

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified66.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 11: 83.6% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 1.75 \cdot 10^{-34}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_1 \cdot t\_0\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi2) (cos (- lambda1 lambda2)))))
   (if (<= phi1 -6.6e-15)
     (* R (acos (fma t_0 (cos phi1) (* (sin phi1) (sin phi2)))))
     (if (<= phi1 1.75e-34)
       (*
        R
        (acos
         (*
          (cos phi2)
          (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))
       (* R (acos (fma (sin phi2) (sin phi1) (* (cos phi1) t_0))))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi2) * cos((lambda1 - lambda2));
	double tmp;
	if (phi1 <= -6.6e-15) {
		tmp = R * acos(fma(t_0, cos(phi1), (sin(phi1) * sin(phi2))));
	} else if (phi1 <= 1.75e-34) {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = R * acos(fma(sin(phi2), sin(phi1), (cos(phi1) * t_0)));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi2) * cos(Float64(lambda1 - lambda2)))
	tmp = 0.0
	if (phi1 <= -6.6e-15)
		tmp = Float64(R * acos(fma(t_0, cos(phi1), Float64(sin(phi1) * sin(phi2)))));
	elseif (phi1 <= 1.75e-34)
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = Float64(R * acos(fma(sin(phi2), sin(phi1), Float64(cos(phi1) * t_0))));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -6.6e-15], N[(R * N[ArcCos[N[(t$95$0 * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 1.75e-34], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\

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

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


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

    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. 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 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      7. 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 \]
      8. 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 \]
      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. lower-*.f6478.3

        \[\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 \]
    4. Applied egg-rr78.3%

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

    if -6.6e-15 < phi1 < 1.75e-34

    1. Initial program 66.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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified66.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.75e-34 < phi1

    1. Initial program 81.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 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      7. 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 \]
      8. 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 \]
      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.f6481.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. 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 \]
      13. associate-*l*N/A

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

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

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

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

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

Alternative 12: 83.6% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := 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_1 - \lambda_2\right)\right)\right)\right)\\ \mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_1 \leq 1.75 \cdot 10^{-34}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (*
          R
          (acos
           (fma
            (sin phi2)
            (sin phi1)
            (* (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2)))))))))
   (if (<= phi1 -6.6e-15)
     t_0
     (if (<= phi1 1.75e-34)
       (*
        R
        (acos
         (*
          (cos phi2)
          (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))
       t_0))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = R * acos(fma(sin(phi2), sin(phi1), (cos(phi1) * (cos(phi2) * cos((lambda1 - lambda2))))));
	double tmp;
	if (phi1 <= -6.6e-15) {
		tmp = t_0;
	} else if (phi1 <= 1.75e-34) {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = t_0;
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(R * acos(fma(sin(phi2), sin(phi1), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda1 - lambda2)))))))
	tmp = 0.0
	if (phi1 <= -6.6e-15)
		tmp = t_0;
	elseif (phi1 <= 1.75e-34)
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = t_0;
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(R * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -6.6e-15], t$95$0, If[LessEqual[phi1, 1.75e-34], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := 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_1 - \lambda_2\right)\right)\right)\right)\\
\mathbf{if}\;\phi_1 \leq -6.6 \cdot 10^{-15}:\\
\;\;\;\;t\_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -6.6e-15 or 1.75e-34 < phi1

    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. 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 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      7. 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 \]
      8. 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 \]
      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.f6479.9

        \[\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. 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 \]
      13. associate-*l*N/A

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

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

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

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

    if -6.6e-15 < phi1 < 1.75e-34

    1. Initial program 66.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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified66.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 13: 76.2% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ \mathbf{if}\;\lambda_1 \leq -0.007:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_1, t\_0\right)\right)\\ \mathbf{elif}\;\lambda_1 \leq 1.05 \cdot 10^{-9}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, t\_0\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))))
   (if (<= lambda1 -0.007)
     (* R (acos (fma (cos phi2) (* (cos phi1) (cos lambda1)) t_0)))
     (if (<= lambda1 1.05e-9)
       (* R (acos (fma (cos phi2) (* (cos phi1) (cos lambda2)) t_0)))
       (*
        R
        (acos
         (*
          (cos phi2)
          (fma
           (sin lambda2)
           (sin lambda1)
           (* (cos lambda2) (cos lambda1))))))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin(phi1) * sin(phi2);
	double tmp;
	if (lambda1 <= -0.007) {
		tmp = R * acos(fma(cos(phi2), (cos(phi1) * cos(lambda1)), t_0));
	} else if (lambda1 <= 1.05e-9) {
		tmp = R * acos(fma(cos(phi2), (cos(phi1) * cos(lambda2)), t_0));
	} else {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(phi1) * sin(phi2))
	tmp = 0.0
	if (lambda1 <= -0.007)
		tmp = Float64(R * acos(fma(cos(phi2), Float64(cos(phi1) * cos(lambda1)), t_0)));
	elseif (lambda1 <= 1.05e-9)
		tmp = Float64(R * acos(fma(cos(phi2), Float64(cos(phi1) * cos(lambda2)), t_0)));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[lambda1, -0.007], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda1, 1.05e-9], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
\mathbf{if}\;\lambda_1 \leq -0.007:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_1, t\_0\right)\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if lambda1 < -0.00700000000000000015

    1. Initial program 58.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 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. associate-*r*N/A

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \phi_1\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 \phi_2, \cos \lambda_1 \cdot \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

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

    if -0.00700000000000000015 < lambda1 < 1.0500000000000001e-9

    1. Initial program 88.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 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}{\left(\cos \phi_2 \cdot \cos \phi_1\right)} \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      3. associate-*l*N/A

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

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

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

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

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

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

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

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

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

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

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

    if 1.0500000000000001e-9 < lambda1

    1. Initial program 61.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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6440.8

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 14: 73.3% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq -37000000000000:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{elif}\;\lambda_2 \leq 1.7 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda2 -37000000000000.0)
   (*
    R
    (acos
     (*
      (cos phi1)
      (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
   (if (<= lambda2 1.7e-5)
     (*
      R
      (acos
       (fma
        (cos phi2)
        (* (cos phi1) (cos lambda1))
        (* (sin phi1) (sin phi2)))))
     (*
      R
      (acos
       (*
        (cos phi2)
        (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1)))))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= -37000000000000.0) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))));
	} else if (lambda2 <= 1.7e-5) {
		tmp = R * acos(fma(cos(phi2), (cos(phi1) * cos(lambda1)), (sin(phi1) * sin(phi2))));
	} else {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= -37000000000000.0)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	elseif (lambda2 <= 1.7e-5)
		tmp = Float64(R * acos(fma(cos(phi2), Float64(cos(phi1) * cos(lambda1)), Float64(sin(phi1) * sin(phi2)))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, -37000000000000.0], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda2, 1.7e-5], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq -37000000000000:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if lambda2 < -3.7e13

    1. Initial program 60.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. Step-by-step derivation
      1. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -3.7e13 < lambda2 < 1.7e-5

    1. Initial program 89.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 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. associate-*r*N/A

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \phi_1\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 \phi_2, \cos \lambda_1 \cdot \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

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

    if 1.7e-5 < lambda2

    1. Initial program 58.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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6434.3

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified34.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 15: 72.9% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.7 \cdot 10^{-30}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 1.7e-30)
   (*
    R
    (acos
     (*
      (cos phi1)
      (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
   (*
    R
    (acos
     (*
      (cos phi2)
      (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 1.7e-30) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))));
	} else {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 1.7e-30)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 1.7e-30], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 1.7 \cdot 10^{-30}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < 1.7000000000000001e-30

    1. Initial program 74.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. Step-by-step derivation
      1. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.7000000000000001e-30 < phi2

    1. Initial program 72.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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6442.3

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified42.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 16: 68.6% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.00155:\\ \;\;\;\;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_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -0.00155)
   (* R (acos (* (cos phi1) (cos (- lambda2 lambda1)))))
   (*
    R
    (acos
     (*
      (cos phi2)
      (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -0.00155) {
		tmp = R * acos((cos(phi1) * cos((lambda2 - lambda1))));
	} else {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -0.00155)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(Float64(lambda2 - lambda1)))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -0.00155], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -0.00155:\\
\;\;\;\;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_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -0.00154999999999999995

    1. Initial program 79.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.f6444.0

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

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

    if -0.00154999999999999995 < phi1

    1. Initial program 71.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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified49.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\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.00155:\\ \;\;\;\;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_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 17: 60.4% accurate, 1.1× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.8:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 7.2 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \phi_1 \cdot \left(\sin \phi_2 \cdot \left(1 + \left(\phi_1 \cdot \phi_1\right) \cdot \mathsf{fma}\left(\phi_1 \cdot \phi_1, 0.008333333333333333, -0.16666666666666666\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -2.8)
   (* R (acos (* (cos phi1) (cos (- lambda2 lambda1)))))
   (if (<= phi1 7.2e-14)
     (*
      R
      (acos
       (+
        (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))
        (*
         phi1
         (*
          (sin phi2)
          (+
           1.0
           (*
            (* phi1 phi1)
            (fma
             (* phi1 phi1)
             0.008333333333333333
             -0.16666666666666666))))))))
     (fma
      (- (asin (fma (cos phi1) (cos phi2) (* (sin phi1) (sin phi2)))))
      R
      (* R (* PI 0.5))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -2.8) {
		tmp = R * acos((cos(phi1) * cos((lambda2 - lambda1))));
	} else if (phi1 <= 7.2e-14) {
		tmp = R * acos((((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))) + (phi1 * (sin(phi2) * (1.0 + ((phi1 * phi1) * fma((phi1 * phi1), 0.008333333333333333, -0.16666666666666666)))))));
	} else {
		tmp = fma(-asin(fma(cos(phi1), cos(phi2), (sin(phi1) * sin(phi2)))), R, (R * (((double) M_PI) * 0.5)));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -2.8)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(Float64(lambda2 - lambda1)))));
	elseif (phi1 <= 7.2e-14)
		tmp = Float64(R * acos(Float64(Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))) + Float64(phi1 * Float64(sin(phi2) * Float64(1.0 + Float64(Float64(phi1 * phi1) * fma(Float64(phi1 * phi1), 0.008333333333333333, -0.16666666666666666))))))));
	else
		tmp = fma(Float64(-asin(fma(cos(phi1), cos(phi2), Float64(sin(phi1) * sin(phi2))))), R, Float64(R * Float64(pi * 0.5)));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -2.8], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 7.2e-14], N[(R * N[ArcCos[N[(N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(phi1 * N[(N[Sin[phi2], $MachinePrecision] * N[(1.0 + N[(N[(phi1 * phi1), $MachinePrecision] * N[(N[(phi1 * phi1), $MachinePrecision] * 0.008333333333333333 + -0.16666666666666666), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[ArcSin[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -2.8:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\

\mathbf{elif}\;\phi_1 \leq 7.2 \cdot 10^{-14}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \phi_1 \cdot \left(\sin \phi_2 \cdot \left(1 + \left(\phi_1 \cdot \phi_1\right) \cdot \mathsf{fma}\left(\phi_1 \cdot \phi_1, 0.008333333333333333, -0.16666666666666666\right)\right)\right)\right)\\

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


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

    1. Initial program 79.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.f6444.0

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

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

    if -2.7999999999999998 < phi1 < 7.1999999999999996e-14

    1. Initial program 66.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 phi1 around 0

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\phi_1 \cdot \left(\color{blue}{\sin \phi_2} \cdot \left(\left(\frac{-1}{6} \cdot {\phi_1}^{2} + 1\right) + {\phi_1}^{2} \cdot \left(\frac{1}{120} \cdot {\phi_1}^{2}\right)\right)\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    5. Simplified66.5%

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

    if 7.1999999999999996e-14 < phi1

    1. Initial program 81.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. Step-by-step derivation
      1. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 18: 60.4% accurate, 1.1× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.5:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 7.2 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \left(\phi_1 \cdot \sin \phi_2\right) \cdot \mathsf{fma}\left(\phi_1, \phi_1 \cdot -0.16666666666666666, 1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -2.5)
   (* R (acos (* (cos phi1) (cos (- lambda2 lambda1)))))
   (if (<= phi1 7.2e-14)
     (*
      R
      (acos
       (+
        (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))
        (* (* phi1 (sin phi2)) (fma phi1 (* phi1 -0.16666666666666666) 1.0)))))
     (fma
      (- (asin (fma (cos phi1) (cos phi2) (* (sin phi1) (sin phi2)))))
      R
      (* R (* PI 0.5))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -2.5) {
		tmp = R * acos((cos(phi1) * cos((lambda2 - lambda1))));
	} else if (phi1 <= 7.2e-14) {
		tmp = R * acos((((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))) + ((phi1 * sin(phi2)) * fma(phi1, (phi1 * -0.16666666666666666), 1.0))));
	} else {
		tmp = fma(-asin(fma(cos(phi1), cos(phi2), (sin(phi1) * sin(phi2)))), R, (R * (((double) M_PI) * 0.5)));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -2.5)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(Float64(lambda2 - lambda1)))));
	elseif (phi1 <= 7.2e-14)
		tmp = Float64(R * acos(Float64(Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))) + Float64(Float64(phi1 * sin(phi2)) * fma(phi1, Float64(phi1 * -0.16666666666666666), 1.0)))));
	else
		tmp = fma(Float64(-asin(fma(cos(phi1), cos(phi2), Float64(sin(phi1) * sin(phi2))))), R, Float64(R * Float64(pi * 0.5)));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -2.5], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 7.2e-14], N[(R * N[ArcCos[N[(N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[(phi1 * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] * N[(phi1 * N[(phi1 * -0.16666666666666666), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[ArcSin[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -2.5:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\

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

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


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

    1. Initial program 79.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.f6444.0

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

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

    if -2.5 < phi1 < 7.1999999999999996e-14

    1. Initial program 66.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 phi1 around 0

      \[\leadsto \cos^{-1} \left(\color{blue}{\phi_1 \cdot \left(\sin \phi_2 + \frac{-1}{6} \cdot \left({\phi_1}^{2} \cdot \sin \phi_2\right)\right)} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    4. Step-by-step derivation
      1. distribute-lft-inN/A

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

        \[\leadsto \cos^{-1} \left(\left(\phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\phi_1 \cdot \frac{-1}{6}\right) \cdot \left({\phi_1}^{2} \cdot \sin \phi_2\right)}\right) + \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(\left(\phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\frac{-1}{6} \cdot \phi_1\right)} \cdot \left({\phi_1}^{2} \cdot \sin \phi_2\right)\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      4. *-commutativeN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 7.1999999999999996e-14 < phi1

    1. Initial program 81.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. Step-by-step derivation
      1. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 19: 60.3% accurate, 1.2× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\ \mathbf{if}\;\phi_1 \leq -0.016:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\ \mathbf{elif}\;\phi_1 \leq 7.2 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\phi_1, \phi_1 \cdot -0.5, 1\right), \cos \phi_2 \cdot t\_0, \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda2 lambda1))))
   (if (<= phi1 -0.016)
     (* R (acos (* (cos phi1) t_0)))
     (if (<= phi1 7.2e-14)
       (*
        R
        (acos
         (fma
          (fma phi1 (* phi1 -0.5) 1.0)
          (* (cos phi2) t_0)
          (* phi1 (sin phi2)))))
       (fma
        (- (asin (fma (cos phi1) (cos phi2) (* (sin phi1) (sin phi2)))))
        R
        (* R (* PI 0.5)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda2 - lambda1));
	double tmp;
	if (phi1 <= -0.016) {
		tmp = R * acos((cos(phi1) * t_0));
	} else if (phi1 <= 7.2e-14) {
		tmp = R * acos(fma(fma(phi1, (phi1 * -0.5), 1.0), (cos(phi2) * t_0), (phi1 * sin(phi2))));
	} else {
		tmp = fma(-asin(fma(cos(phi1), cos(phi2), (sin(phi1) * sin(phi2)))), R, (R * (((double) M_PI) * 0.5)));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda2 - lambda1))
	tmp = 0.0
	if (phi1 <= -0.016)
		tmp = Float64(R * acos(Float64(cos(phi1) * t_0)));
	elseif (phi1 <= 7.2e-14)
		tmp = Float64(R * acos(fma(fma(phi1, Float64(phi1 * -0.5), 1.0), Float64(cos(phi2) * t_0), Float64(phi1 * sin(phi2)))));
	else
		tmp = fma(Float64(-asin(fma(cos(phi1), cos(phi2), Float64(sin(phi1) * sin(phi2))))), R, Float64(R * Float64(pi * 0.5)));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi1, -0.016], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 7.2e-14], N[(R * N[ArcCos[N[(N[(phi1 * N[(phi1 * -0.5), $MachinePrecision] + 1.0), $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision] + N[(phi1 * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[ArcSin[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
\mathbf{if}\;\phi_1 \leq -0.016:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

\mathbf{elif}\;\phi_1 \leq 7.2 \cdot 10^{-14}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\phi_1, \phi_1 \cdot -0.5, 1\right), \cos \phi_2 \cdot t\_0, \phi_1 \cdot \sin \phi_2\right)\right)\\

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


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

    1. Initial program 79.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.f6444.0

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

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

    if -0.016 < phi1 < 7.1999999999999996e-14

    1. Initial program 66.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 phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_1 \cdot \left(\sin \phi_2 + \frac{-1}{2} \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right) + \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. distribute-lft-inN/A

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

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

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

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

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

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

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

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

    if 7.1999999999999996e-14 < phi1

    1. Initial program 81.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. Step-by-step derivation
      1. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 20: 60.3% accurate, 1.2× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\ \mathbf{if}\;\phi_1 \leq -0.019:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\ \mathbf{elif}\;\phi_1 \leq 2.2 \cdot 10^{-20}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\phi_1, \phi_1 \cdot -0.5, 1\right), \cos \phi_2 \cdot t\_0, \left(\phi_1 \cdot \sin \phi_2\right) \cdot \mathsf{fma}\left(\phi_1, \phi_1 \cdot -0.16666666666666666, 1\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda2 lambda1))))
   (if (<= phi1 -0.019)
     (* R (acos (* (cos phi1) t_0)))
     (if (<= phi1 2.2e-20)
       (*
        R
        (acos
         (fma
          (fma phi1 (* phi1 -0.5) 1.0)
          (* (cos phi2) t_0)
          (*
           (* phi1 (sin phi2))
           (fma phi1 (* phi1 -0.16666666666666666) 1.0)))))
       (* R (acos (fma (cos phi2) (cos phi1) (* (sin phi1) (sin phi2)))))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda2 - lambda1));
	double tmp;
	if (phi1 <= -0.019) {
		tmp = R * acos((cos(phi1) * t_0));
	} else if (phi1 <= 2.2e-20) {
		tmp = R * acos(fma(fma(phi1, (phi1 * -0.5), 1.0), (cos(phi2) * t_0), ((phi1 * sin(phi2)) * fma(phi1, (phi1 * -0.16666666666666666), 1.0))));
	} else {
		tmp = R * acos(fma(cos(phi2), cos(phi1), (sin(phi1) * sin(phi2))));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda2 - lambda1))
	tmp = 0.0
	if (phi1 <= -0.019)
		tmp = Float64(R * acos(Float64(cos(phi1) * t_0)));
	elseif (phi1 <= 2.2e-20)
		tmp = Float64(R * acos(fma(fma(phi1, Float64(phi1 * -0.5), 1.0), Float64(cos(phi2) * t_0), Float64(Float64(phi1 * sin(phi2)) * fma(phi1, Float64(phi1 * -0.16666666666666666), 1.0)))));
	else
		tmp = Float64(R * acos(fma(cos(phi2), cos(phi1), Float64(sin(phi1) * sin(phi2)))));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi1, -0.019], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 2.2e-20], N[(R * N[ArcCos[N[(N[(phi1 * N[(phi1 * -0.5), $MachinePrecision] + 1.0), $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision] + N[(N[(phi1 * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] * N[(phi1 * N[(phi1 * -0.16666666666666666), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
\mathbf{if}\;\phi_1 \leq -0.019:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

\mathbf{elif}\;\phi_1 \leq 2.2 \cdot 10^{-20}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\phi_1, \phi_1 \cdot -0.5, 1\right), \cos \phi_2 \cdot t\_0, \left(\phi_1 \cdot \sin \phi_2\right) \cdot \mathsf{fma}\left(\phi_1, \phi_1 \cdot -0.16666666666666666, 1\right)\right)\right)\\

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


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

    1. Initial program 79.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.f6444.0

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

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

    if -0.0189999999999999995 < phi1 < 2.19999999999999991e-20

    1. Initial program 66.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 phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_1 \cdot \left(\sin \phi_2 + \phi_1 \cdot \left(\frac{-1}{2} \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) + \frac{-1}{6} \cdot \left(\phi_1 \cdot \sin \phi_2\right)\right)\right) + \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Simplified66.2%

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

    if 2.19999999999999991e-20 < phi1

    1. Initial program 81.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 lambda2 around 0

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    5. Simplified60.1%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \lambda_1}\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. *-commutativeN/A

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

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

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

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

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

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

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

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

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

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

Alternative 21: 58.3% accurate, 1.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.7 \cdot 10^{-30}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 1.7e-30)
   (* R (acos (* (cos phi1) (cos (- lambda2 lambda1)))))
   (fma
    (- (asin (* (cos phi2) (cos (- lambda1 lambda2)))))
    R
    (* R (* PI 0.5)))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 1.7e-30) {
		tmp = R * acos((cos(phi1) * cos((lambda2 - lambda1))));
	} else {
		tmp = fma(-asin((cos(phi2) * cos((lambda1 - lambda2)))), R, (R * (((double) M_PI) * 0.5)));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 1.7e-30)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(Float64(lambda2 - lambda1)))));
	else
		tmp = fma(Float64(-asin(Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))))), R, Float64(R * Float64(pi * 0.5)));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 1.7e-30], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[ArcSin[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 1.7 \cdot 10^{-30}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < 1.7000000000000001e-30

    1. Initial program 74.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.f6451.5

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

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

    if 1.7000000000000001e-30 < phi2

    1. Initial program 72.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. cos-diffN/A

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\color{blue}{\cos \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.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}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
      4. lower-cos.f64N/A

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

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

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

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

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

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

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

      \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\sin^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)}\right), R, \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R\right) \]
    7. Step-by-step derivation
      1. sub-negN/A

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

        \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\sin^{-1} \left(\cos \phi_2 \cdot \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)\right)\right), R, \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R\right) \]
      3. distribute-neg-inN/A

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(\mathsf{neg}\left(\sin^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right)\right), R, \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R\right) \]
      12. lower--.f6442.3

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

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

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

Alternative 22: 42.8% accurate, 1.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 7.2 \cdot 10^{-276}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{elif}\;\phi_2 \leq 0.0265:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \phi_2, \mathsf{fma}\left(\phi_2 \cdot \phi_2, 0.041666666666666664, -0.5\right), 1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 7.2e-276)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (if (<= phi2 0.0265)
     (*
      R
      (acos
       (*
        (cos (- lambda2 lambda1))
        (fma
         (* phi2 phi2)
         (fma (* phi2 phi2) 0.041666666666666664 -0.5)
         1.0))))
     (* R (acos (* (cos phi2) (cos lambda1)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 7.2e-276) {
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	} else if (phi2 <= 0.0265) {
		tmp = R * acos((cos((lambda2 - lambda1)) * fma((phi2 * phi2), fma((phi2 * phi2), 0.041666666666666664, -0.5), 1.0)));
	} else {
		tmp = R * acos((cos(phi2) * cos(lambda1)));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 7.2e-276)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda1))));
	elseif (phi2 <= 0.0265)
		tmp = Float64(R * acos(Float64(cos(Float64(lambda2 - lambda1)) * fma(Float64(phi2 * phi2), fma(Float64(phi2 * phi2), 0.041666666666666664, -0.5), 1.0))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * cos(lambda1))));
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 7.2e-276], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 0.0265], N[(R * N[ArcCos[N[(N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision] * N[(N[(phi2 * phi2), $MachinePrecision] * N[(N[(phi2 * phi2), $MachinePrecision] * 0.041666666666666664 + -0.5), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 7.2 \cdot 10^{-276}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

\mathbf{elif}\;\phi_2 \leq 0.0265:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \phi_2, \mathsf{fma}\left(\phi_2 \cdot \phi_2, 0.041666666666666664, -0.5\right), 1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R \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 phi2 < 7.19999999999999988e-276

    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 lambda2 around 0

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

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

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

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

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

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

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

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

    if 7.19999999999999988e-276 < phi2 < 0.0264999999999999993

    1. Initial program 71.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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6446.1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 0.0264999999999999993 < phi2

    1. Initial program 71.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 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. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified42.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\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.f6433.8

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    8. Simplified33.8%

      \[\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 simplification36.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 7.2 \cdot 10^{-276}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{elif}\;\phi_2 \leq 0.0265:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \phi_2, \mathsf{fma}\left(\phi_2 \cdot \phi_2, 0.041666666666666664, -0.5\right), 1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 23: 58.2% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\ \mathbf{if}\;\phi_2 \leq 1.7 \cdot 10^{-30}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda2 lambda1))))
   (if (<= phi2 1.7e-30)
     (* R (acos (* (cos phi1) t_0)))
     (* R (acos (* (cos phi2) t_0))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda2 - lambda1));
	double tmp;
	if (phi2 <= 1.7e-30) {
		tmp = R * acos((cos(phi1) * t_0));
	} else {
		tmp = R * acos((cos(phi2) * t_0));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos((lambda2 - lambda1))
    if (phi2 <= 1.7d-30) then
        tmp = r * acos((cos(phi1) * t_0))
    else
        tmp = r * acos((cos(phi2) * t_0))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda2 - lambda1));
	double tmp;
	if (phi2 <= 1.7e-30) {
		tmp = R * Math.acos((Math.cos(phi1) * t_0));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * t_0));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda2 - lambda1))
	tmp = 0
	if phi2 <= 1.7e-30:
		tmp = R * math.acos((math.cos(phi1) * t_0))
	else:
		tmp = R * math.acos((math.cos(phi2) * t_0))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda2 - lambda1))
	tmp = 0.0
	if (phi2 <= 1.7e-30)
		tmp = Float64(R * acos(Float64(cos(phi1) * t_0)));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * t_0)));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda2 - lambda1));
	tmp = 0.0;
	if (phi2 <= 1.7e-30)
		tmp = R * acos((cos(phi1) * t_0));
	else
		tmp = R * acos((cos(phi2) * t_0));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, 1.7e-30], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
\mathbf{if}\;\phi_2 \leq 1.7 \cdot 10^{-30}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < 1.7000000000000001e-30

    1. Initial program 74.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.f6451.5

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    5. Simplified51.5%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]

    if 1.7000000000000001e-30 < phi2

    1. Initial program 72.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6442.3

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified42.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification48.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.7 \cdot 10^{-30}:\\ \;\;\;\;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 24: 53.5% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.25:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -1.25)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (* R (acos (* (cos phi2) (cos (- lambda2 lambda1)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -1.25) {
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	} else {
		tmp = R * acos((cos(phi2) * cos((lambda2 - lambda1))));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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
    real(8) :: tmp
    if (phi1 <= (-1.25d0)) then
        tmp = r * acos((cos(phi1) * cos(lambda1)))
    else
        tmp = r * acos((cos(phi2) * cos((lambda2 - lambda1))))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -1.25) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda1)));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * Math.cos((lambda2 - lambda1))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi1 <= -1.25:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda1)))
	else:
		tmp = R * math.acos((math.cos(phi2) * math.cos((lambda2 - lambda1))))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -1.25)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda1))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * cos(Float64(lambda2 - lambda1)))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi1 <= -1.25)
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	else
		tmp = R * acos((cos(phi2) * cos((lambda2 - lambda1))));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -1.25], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -1.25:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -1.25

    1. Initial program 79.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 lambda2 around 0

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    4. Step-by-step derivation
      1. lower-cos.f6455.4

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    5. Simplified55.4%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    6. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \phi_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]
      3. lower-cos.f6434.2

        \[\leadsto \cos^{-1} \left(\cos \lambda_1 \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    8. Simplified34.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \phi_1\right)} \cdot R \]

    if -1.25 < phi1

    1. Initial program 71.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6449.0

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified49.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification45.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.25:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\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 25: 29.6% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;t\_0 \leq 0.999:\\ \;\;\;\;R \cdot \cos^{-1} t\_0\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (if (<= t_0 0.999)
     (* R (acos t_0))
     (* R (fabs (remainder (- lambda1 lambda2) (* PI 2.0)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.999) {
		tmp = R * acos(t_0);
	} else {
		tmp = R * fabs(remainder((lambda1 - lambda2), (((double) M_PI) * 2.0)));
	}
	return tmp;
}
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.999) {
		tmp = R * Math.acos(t_0);
	} else {
		tmp = R * Math.abs(Math.IEEEremainder((lambda1 - lambda2), (Math.PI * 2.0)));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	tmp = 0
	if t_0 <= 0.999:
		tmp = R * math.acos(t_0)
	else:
		tmp = R * math.fabs(math.remainder((lambda1 - lambda2), (math.pi * 2.0)))
	return tmp
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[t$95$0, 0.999], N[(R * N[ArcCos[t$95$0], $MachinePrecision]), $MachinePrecision], N[(R * N[Abs[N[With[{TMP1 = N[(lambda1 - lambda2), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;t\_0 \leq 0.999:\\
\;\;\;\;R \cdot \cos^{-1} t\_0\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (cos.f64 (-.f64 lambda1 lambda2)) < 0.998999999999999999

    1. Initial program 72.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6443.7

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified43.7%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in phi2 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.7

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified32.7%

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]

    if 0.998999999999999999 < (cos.f64 (-.f64 lambda1 lambda2))

    1. Initial program 77.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6434.1

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified34.1%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in phi2 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--.f648.1

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified8.1%

      \[\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-cosN/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      3. lower-fabs.f64N/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      4. lower-remainder.f64N/A

        \[\leadsto \left|\color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
      5. lift-PI.f64N/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right| \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      7. lower-*.f6421.0

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\pi \cdot 2\right)}\right)\right| \cdot R \]
    10. Applied egg-rr21.0%

      \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification30.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\lambda_1 - \lambda_2\right) \leq 0.999:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \]
  5. Add Preprocessing

Alternative 26: 42.8% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1550:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_2\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -1550.0)
   (* R (acos (* (cos phi2) (cos lambda1))))
   (* R (acos (* (cos phi2) (cos lambda2))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1550.0) {
		tmp = R * acos((cos(phi2) * cos(lambda1)));
	} else {
		tmp = R * acos((cos(phi2) * cos(lambda2)));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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
    real(8) :: tmp
    if (lambda1 <= (-1550.0d0)) then
        tmp = r * acos((cos(phi2) * cos(lambda1)))
    else
        tmp = r * acos((cos(phi2) * cos(lambda2)))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1550.0) {
		tmp = R * Math.acos((Math.cos(phi2) * Math.cos(lambda1)));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * Math.cos(lambda2)));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -1550.0:
		tmp = R * math.acos((math.cos(phi2) * math.cos(lambda1)))
	else:
		tmp = R * math.acos((math.cos(phi2) * math.cos(lambda2)))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -1550.0)
		tmp = Float64(R * acos(Float64(cos(phi2) * cos(lambda1))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * cos(lambda2))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda1 <= -1550.0)
		tmp = R * acos((cos(phi2) * cos(lambda1)));
	else
		tmp = R * acos((cos(phi2) * cos(lambda2)));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -1550.0], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -1550:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda1 < -1550

    1. Initial program 58.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6438.8

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified38.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\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.f6438.7

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    8. Simplified38.7%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_1\right)} \cdot R \]

    if -1550 < lambda1

    1. Initial program 79.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6442.6

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified42.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_2 \cdot \cos \phi_2\right)} \cdot R \]
    7. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_2\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_2\right)} \cdot R \]
      3. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \lambda_2\right) \cdot R \]
      4. lower-cos.f6434.0

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \lambda_2}\right) \cdot R \]
    8. Simplified34.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_2\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification35.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1550:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 27: 37.7% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 0.003:\\ \;\;\;\;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} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda2 0.003)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (* R (acos (cos (- lambda1 lambda2))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 0.003) {
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	} else {
		tmp = R * acos(cos((lambda1 - lambda2)));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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
    real(8) :: tmp
    if (lambda2 <= 0.003d0) then
        tmp = r * acos((cos(phi1) * cos(lambda1)))
    else
        tmp = r * acos(cos((lambda1 - lambda2)))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 0.003) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda1)));
	} else {
		tmp = R * Math.acos(Math.cos((lambda1 - lambda2)));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= 0.003:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda1)))
	else:
		tmp = R * math.acos(math.cos((lambda1 - lambda2)))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 0.003)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda1))));
	else
		tmp = Float64(R * acos(cos(Float64(lambda1 - lambda2))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= 0.003)
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	else
		tmp = R * acos(cos((lambda1 - lambda2)));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, 0.003], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 0.003:\\
\;\;\;\;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}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda2 < 0.0030000000000000001

    1. Initial program 80.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 lambda2 around 0

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    4. Step-by-step derivation
      1. lower-cos.f6466.7

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    5. Simplified66.7%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
    6. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \phi_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_1} \cdot \cos \phi_1\right) \cdot R \]
      3. lower-cos.f6437.2

        \[\leadsto \cos^{-1} \left(\cos \lambda_1 \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    8. Simplified37.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \cos \phi_1\right)} \cdot R \]

    if 0.0030000000000000001 < lambda2

    1. Initial program 57.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6434.4

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified34.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in phi2 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 \]
  3. Recombined 2 regimes into one program.
  4. Final simplification34.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 0.003:\\ \;\;\;\;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 28: 26.4% accurate, 3.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 4.2 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda2 4.2e-5)
   (* R (acos (cos lambda1)))
   (* R (acos (cos lambda2)))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 4.2e-5) {
		tmp = R * acos(cos(lambda1));
	} else {
		tmp = R * acos(cos(lambda2));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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
    real(8) :: tmp
    if (lambda2 <= 4.2d-5) then
        tmp = r * acos(cos(lambda1))
    else
        tmp = r * acos(cos(lambda2))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 4.2e-5) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else {
		tmp = R * Math.acos(Math.cos(lambda2));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= 4.2e-5:
		tmp = R * math.acos(math.cos(lambda1))
	else:
		tmp = R * math.acos(math.cos(lambda2))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 4.2e-5)
		tmp = Float64(R * acos(cos(lambda1)));
	else
		tmp = Float64(R * acos(cos(lambda2)));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= 4.2e-5)
		tmp = R * acos(cos(lambda1));
	else
		tmp = R * acos(cos(lambda2));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, 4.2e-5], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[Cos[lambda2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 4.2 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda2 < 4.19999999999999977e-5

    1. Initial program 80.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6444.6

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified44.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in phi2 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. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6422.3

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    11. Simplified22.3%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if 4.19999999999999977e-5 < lambda2

    1. Initial program 58.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6434.2

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified34.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in phi2 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. 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.f6427.4

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
    11. Simplified27.4%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification23.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 4.2 \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 29: 24.9% accurate, 3.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1550:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -1550.0)
   (* R (acos (cos lambda1)))
   (* R (fabs (remainder (- lambda1 lambda2) (* PI 2.0))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1550.0) {
		tmp = R * acos(cos(lambda1));
	} else {
		tmp = R * fabs(remainder((lambda1 - lambda2), (((double) M_PI) * 2.0)));
	}
	return tmp;
}
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1550.0) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else {
		tmp = R * Math.abs(Math.IEEEremainder((lambda1 - lambda2), (Math.PI * 2.0)));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -1550.0:
		tmp = R * math.acos(math.cos(lambda1))
	else:
		tmp = R * math.fabs(math.remainder((lambda1 - lambda2), (math.pi * 2.0)))
	return tmp
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -1550.0], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[Abs[N[With[{TMP1 = N[(lambda1 - lambda2), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -1550:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda1 < -1550

    1. Initial program 58.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6438.8

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified38.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in phi2 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.1

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified30.1%

      \[\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.0

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    11. Simplified30.0%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if -1550 < lambda1

    1. Initial program 79.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 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. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
      5. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      6. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6442.6

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified42.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    6. Taylor expanded in phi2 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--.f6426.1

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    8. Simplified26.1%

      \[\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-cosN/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      3. lower-fabs.f64N/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      4. lower-remainder.f64N/A

        \[\leadsto \left|\color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
      5. lift-PI.f64N/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right| \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      7. lower-*.f6420.1

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\pi \cdot 2\right)}\right)\right| \cdot R \]
    10. Applied egg-rr20.1%

      \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification22.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1550:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \]
  5. Add Preprocessing

Alternative 30: 20.1% accurate, 5.4× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right| \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R (fabs (remainder (- lambda1 lambda2) (* PI 2.0)))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * fabs(remainder((lambda1 - lambda2), (((double) M_PI) * 2.0)));
}
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * Math.abs(Math.IEEEremainder((lambda1 - lambda2), (Math.PI * 2.0)));
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	return R * math.fabs(math.remainder((lambda1 - lambda2), (math.pi * 2.0)))
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * N[Abs[N[With[{TMP1 = N[(lambda1 - lambda2), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|
\end{array}
Derivation
  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. 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. lower-*.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    2. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    3. sub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
    4. remove-double-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
    5. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
    6. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
    7. +-commutativeN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
    8. cos-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
    9. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
    10. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
    11. unsub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    12. lower--.f6441.6

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
  5. Simplified41.6%

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
  6. Taylor expanded in phi2 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. lift--.f64N/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    2. acos-cosN/A

      \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
    3. lower-fabs.f64N/A

      \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
    4. lower-remainder.f64N/A

      \[\leadsto \left|\color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
    5. lift-PI.f64N/A

      \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right| \cdot R \]
    6. *-commutativeN/A

      \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
    7. lower-*.f6420.3

      \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\pi \cdot 2\right)}\right)\right| \cdot R \]
  10. Applied egg-rr20.3%

    \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|} \cdot R \]
  11. Final simplification20.3%

    \[\leadsto R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right| \]
  12. Add Preprocessing

Alternative 31: 9.4% accurate, 69.7× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \left(\lambda_2 - \lambda_1\right) \cdot R \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* (- lambda2 lambda1) R))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return (lambda2 - lambda1) * R;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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 = (lambda2 - lambda1) * r
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return (lambda2 - lambda1) * R;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	return (lambda2 - lambda1) * R
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(Float64(lambda2 - lambda1) * R)
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = (lambda2 - lambda1) * R;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[(lambda2 - lambda1), $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\left(\lambda_2 - \lambda_1\right) \cdot R
\end{array}
Derivation
  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. 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. lower-*.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    2. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    3. sub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
    4. remove-double-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
    5. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
    6. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
    7. +-commutativeN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
    8. cos-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
    9. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
    10. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
    11. unsub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    12. lower--.f6441.6

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
  5. Simplified41.6%

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
  6. Taylor expanded in phi2 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. 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. lift--.f64N/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
    14. acos-cos-s6.8

      \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
  10. Applied egg-rr6.8%

    \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
  11. Add Preprocessing

Alternative 32: 3.6% accurate, 104.5× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \lambda_1 \cdot R \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2) :precision binary64 (* lambda1 R))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return lambda1 * R;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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 = lambda1 * r
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return lambda1 * R;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	return lambda1 * R
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(lambda1 * R)
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = lambda1 * R;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(lambda1 * R), $MachinePrecision]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\lambda_1 \cdot R
\end{array}
Derivation
  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. 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. lower-*.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    2. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    3. sub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
    4. remove-double-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \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)\right) \cdot R \]
    5. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\left(\mathsf{neg}\left(\color{blue}{-1 \cdot \lambda_1}\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
    6. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
    7. +-commutativeN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
    8. cos-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
    9. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
    10. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
    11. unsub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    12. lower--.f6441.6

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
  5. Simplified41.6%

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
  6. Taylor expanded in phi2 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. 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.2

      \[\leadsto \color{blue}{\lambda_1 \cdot R} \]
  11. Simplified5.2%

    \[\leadsto \color{blue}{\lambda_1 \cdot R} \]
  12. Add Preprocessing

Reproduce

?
herbie shell --seed 2024207 
(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))