Spherical law of cosines

Percentage Accurate: 74.1% → 96.5%
Time: 25.4s
Alternatives: 30
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 30 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.1% 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.5% accurate, 0.3× speedup?

\[\begin{array}{l} [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(\mathsf{fma}\left(\cos \lambda_2, t\_1 \cdot \cos \lambda_1, t\_0 + \sin \lambda_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \phi_1 \cdot \sin \lambda_2\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.
(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
       (fma
        (cos lambda2)
        (* t_1 (cos lambda1))
        (+
         t_0
         (* (sin lambda1) (* (cos phi2) (* (cos phi1) (sin lambda2)))))))))))
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(fma(cos(lambda2), (t_1 * cos(lambda1)), (t_0 + (sin(lambda1) * (cos(phi2) * (cos(phi1) * sin(lambda2)))))));
	}
	return tmp;
}
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(fma(cos(lambda2), Float64(t_1 * cos(lambda1)), Float64(t_0 + Float64(sin(lambda1) * Float64(cos(phi2) * Float64(cos(phi1) * sin(lambda2))))))));
	end
	return tmp
end
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[(N[Cos[lambda2], $MachinePrecision] * N[(t$95$1 * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision] + N[(t$95$0 + N[(N[Sin[lambda1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[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(\mathsf{fma}\left(\cos \lambda_2, t\_1 \cdot \cos \lambda_1, t\_0 + \sin \lambda_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \phi_1 \cdot \sin \lambda_2\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 3.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f643.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified3.4%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f643.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified3.4%

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

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

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right), R\right) \]
      3. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right), R\right) \]
      4. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \cos \left(\lambda_2 - \lambda_1\right), R\right) \]
      5. acos-cos-sN/A

        \[\leadsto \mathsf{*.f64}\left(\left(\lambda_2 - \lambda_1\right), R\right) \]
      6. --lowering--.f6431.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right), R\right) \]
    10. Applied egg-rr31.4%

      \[\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 83.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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)\right), R\right) \]
      2. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      3. distribute-rgt-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      4. associate-+l+N/A

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_1\right) \cdot \cos \phi_2 + \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      6. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_1, \cos \phi_2, \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      7. fma-lowering-fma.f64N/A

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \lambda_2 \cdot \cos \lambda_1\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      3. associate-*l*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \lambda_2 \cdot \left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right) + \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      4. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right), \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      5. fma-lowering-fma.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\cos \lambda_2, \left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      6. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{*.f64}\left(\cos \lambda_1, \left(\cos \phi_1 \cdot \cos \phi_2\right)\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      8. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\cos \phi_1 \cdot \cos \phi_2\right)\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      9. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\cos \phi_1, \cos \phi_2\right)\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      10. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \phi_2\right)\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right)\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      12. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right)\right), \mathsf{+.f64}\left(\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), \left(\sin \phi_1 \cdot \sin \phi_2\right)\right)\right)\right), R\right) \]
    6. Applied egg-rr99.0%

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

    \[\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 \lambda_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1, \sin \phi_1 \cdot \sin \phi_2 + \sin \lambda_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \phi_1 \cdot \sin \lambda_2\right)\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 96.4% accurate, 0.4× speedup?

\[\begin{array}{l} [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(\left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\\ \end{array} \end{array} \]
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
       (+
        (+ t_0 (* (cos phi1) (* (cos phi2) (* (sin lambda1) (sin lambda2)))))
        (* (cos phi1) (* (cos phi2) (* (cos lambda2) (cos lambda1))))))))))
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(((t_0 + (cos(phi1) * (cos(phi2) * (sin(lambda1) * sin(lambda2))))) + (cos(phi1) * (cos(phi2) * (cos(lambda2) * cos(lambda1))))));
	}
	return tmp;
}
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 = sin(phi1) * sin(phi2)
    if (acos((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) <= 0.0d0) then
        tmp = (lambda2 - lambda1) * r
    else
        tmp = r * acos(((t_0 + (cos(phi1) * (cos(phi2) * (sin(lambda1) * sin(lambda2))))) + (cos(phi1) * (cos(phi2) * (cos(lambda2) * cos(lambda1))))))
    end if
    code = tmp
end function
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.sin(phi1) * Math.sin(phi2);
	double tmp;
	if (Math.acos((t_0 + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2))))) <= 0.0) {
		tmp = (lambda2 - lambda1) * R;
	} else {
		tmp = R * Math.acos(((t_0 + (Math.cos(phi1) * (Math.cos(phi2) * (Math.sin(lambda1) * Math.sin(lambda2))))) + (Math.cos(phi1) * (Math.cos(phi2) * (Math.cos(lambda2) * Math.cos(lambda1))))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.sin(phi1) * math.sin(phi2)
	tmp = 0
	if math.acos((t_0 + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2))))) <= 0.0:
		tmp = (lambda2 - lambda1) * R
	else:
		tmp = R * math.acos(((t_0 + (math.cos(phi1) * (math.cos(phi2) * (math.sin(lambda1) * math.sin(lambda2))))) + (math.cos(phi1) * (math.cos(phi2) * (math.cos(lambda2) * math.cos(lambda1))))))
	return tmp
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(Float64(Float64(t_0 + Float64(cos(phi1) * Float64(cos(phi2) * Float64(sin(lambda1) * sin(lambda2))))) + Float64(cos(phi1) * Float64(cos(phi2) * Float64(cos(lambda2) * cos(lambda1)))))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = sin(phi1) * sin(phi2);
	tmp = 0.0;
	if (acos((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) <= 0.0)
		tmp = (lambda2 - lambda1) * R;
	else
		tmp = R * acos(((t_0 + (cos(phi1) * (cos(phi2) * (sin(lambda1) * sin(lambda2))))) + (cos(phi1) * (cos(phi2) * (cos(lambda2) * cos(lambda1))))));
	end
	tmp_2 = tmp;
end
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[(t$95$0 + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $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]]]
\begin{array}{l}
[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(\left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\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 3.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f643.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified3.4%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f643.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified3.4%

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

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

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right), R\right) \]
      3. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right), R\right) \]
      4. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \cos \left(\lambda_2 - \lambda_1\right), R\right) \]
      5. acos-cos-sN/A

        \[\leadsto \mathsf{*.f64}\left(\left(\lambda_2 - \lambda_1\right), R\right) \]
      6. --lowering--.f6431.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right), R\right) \]
    10. Applied egg-rr31.4%

      \[\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 83.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      2. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right), R\right) \]
      3. distribute-rgt-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\sin \phi_1 \cdot \sin \phi_2 + \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right), R\right) \]
      4. associate-+r+N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\sin \phi_1 \cdot \sin \phi_2 + \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right) + \left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right), R\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\left(\sin \phi_1 \cdot \sin \phi_2 + \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right), \left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr98.9%

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

    \[\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(\left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 96.5% accurate, 0.4× speedup?

\[\begin{array}{l} [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 + t\_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \end{array} \end{array} \]
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
        (*
         t_1
         (fma
          (cos lambda2)
          (cos lambda1)
          (* (sin lambda1) (sin lambda2))))))))))
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 + (t_1 * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2))))));
	}
	return tmp;
}
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 + Float64(t_1 * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2)))))));
	end
	return tmp
end
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[(t$95$1 * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[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 + t\_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\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 3.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f643.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified3.4%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f643.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified3.4%

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

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

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right), R\right) \]
      3. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right), R\right) \]
      4. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \cos \left(\lambda_2 - \lambda_1\right), R\right) \]
      5. acos-cos-sN/A

        \[\leadsto \mathsf{*.f64}\left(\left(\lambda_2 - \lambda_1\right), R\right) \]
      6. --lowering--.f6431.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right), R\right) \]
    10. Applied egg-rr31.4%

      \[\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 83.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      3. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. fma-lowering-fma.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{fma.f64}\left(\cos \lambda_2, \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sin-lowering-sin.f6498.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    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 \]
  3. Recombined 2 regimes into one program.
  4. Final simplification95.2%

    \[\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 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 96.4% accurate, 0.4× speedup?

\[\begin{array}{l} [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 + t\_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \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.
(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
        (*
         t_1
         (+
          (* (sin lambda1) (sin lambda2))
          (* (cos lambda2) (cos lambda1))))))))))
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 + (t_1 * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1))))));
	}
	return tmp;
}
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) :: t_1
    real(8) :: tmp
    t_0 = sin(phi1) * sin(phi2)
    t_1 = cos(phi1) * cos(phi2)
    if (acos((t_0 + (t_1 * cos((lambda1 - lambda2))))) <= 0.0d0) then
        tmp = (lambda2 - lambda1) * r
    else
        tmp = r * acos((t_0 + (t_1 * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1))))))
    end if
    code = tmp
end function
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.sin(phi1) * Math.sin(phi2);
	double t_1 = Math.cos(phi1) * Math.cos(phi2);
	double tmp;
	if (Math.acos((t_0 + (t_1 * Math.cos((lambda1 - lambda2))))) <= 0.0) {
		tmp = (lambda2 - lambda1) * R;
	} else {
		tmp = R * Math.acos((t_0 + (t_1 * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1))))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.sin(phi1) * math.sin(phi2)
	t_1 = math.cos(phi1) * math.cos(phi2)
	tmp = 0
	if math.acos((t_0 + (t_1 * math.cos((lambda1 - lambda2))))) <= 0.0:
		tmp = (lambda2 - lambda1) * R
	else:
		tmp = R * math.acos((t_0 + (t_1 * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1))))))
	return tmp
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 + Float64(t_1 * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1)))))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = sin(phi1) * sin(phi2);
	t_1 = cos(phi1) * cos(phi2);
	tmp = 0.0;
	if (acos((t_0 + (t_1 * cos((lambda1 - lambda2))))) <= 0.0)
		tmp = (lambda2 - lambda1) * R;
	else
		tmp = R * acos((t_0 + (t_1 * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1))))));
	end
	tmp_2 = tmp;
end
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[(t$95$1 * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[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 + t\_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\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 3.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f643.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified3.4%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f643.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified3.4%

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

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

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right), R\right) \]
      3. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right), R\right) \]
      4. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \cos \left(\lambda_2 - \lambda_1\right), R\right) \]
      5. acos-cos-sN/A

        \[\leadsto \mathsf{*.f64}\left(\left(\lambda_2 - \lambda_1\right), R\right) \]
      6. --lowering--.f6431.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right), R\right) \]
    10. Applied egg-rr31.4%

      \[\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 83.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      2. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      3. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_1, \cos \lambda_2\right)\right)\right)\right)\right), R\right) \]
      8. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \cos \lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f6498.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    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}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification95.2%

    \[\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 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 82.3% accurate, 0.8× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\ \mathbf{if}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\ \;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_0\right)\\ \mathbf{elif}\;\phi_2 \leq 0.052:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right) + \sin \phi_1 \cdot \phi_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2))))))
   (if (<= phi2 -9.9e-82)
     (- (* R (/ PI 2.0)) (* R (asin (+ (* (sin phi1) (sin phi2)) t_0))))
     (if (<= phi2 0.052)
       (*
        R
        (acos
         (+
          (*
           (cos phi1)
           (+ (* (sin lambda1) (sin lambda2)) (* (cos lambda2) (cos lambda1))))
          (* (sin phi1) phi2))))
       (* R (acos (fma (sin phi2) (sin phi1) t_0)))))))
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(phi1) * (cos(phi2) * cos((lambda1 - lambda2)));
	double tmp;
	if (phi2 <= -9.9e-82) {
		tmp = (R * (((double) M_PI) / 2.0)) - (R * asin(((sin(phi1) * sin(phi2)) + t_0)));
	} else if (phi2 <= 0.052) {
		tmp = R * acos(((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))) + (sin(phi1) * phi2)));
	} else {
		tmp = R * acos(fma(sin(phi2), sin(phi1), t_0));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))))
	tmp = 0.0
	if (phi2 <= -9.9e-82)
		tmp = Float64(Float64(R * Float64(pi / 2.0)) - Float64(R * asin(Float64(Float64(sin(phi1) * sin(phi2)) + t_0))));
	elseif (phi2 <= 0.052)
		tmp = Float64(R * acos(Float64(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1)))) + Float64(sin(phi1) * phi2))));
	else
		tmp = Float64(R * acos(fma(sin(phi2), sin(phi1), t_0)));
	end
	return tmp
end
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[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -9.9e-82], N[(N[(R * N[(Pi / 2.0), $MachinePrecision]), $MachinePrecision] - N[(R * N[ArcSin[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 0.052], N[(R * N[ArcCos[N[(N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * phi2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\
\mathbf{if}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\
\;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_0\right)\\

\mathbf{elif}\;\phi_2 \leq 0.052:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right) + \sin \phi_1 \cdot \phi_2\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -9.9000000000000001e-82

    1. Initial program 82.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. *-commutativeN/A

        \[\leadsto R \cdot \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)} \]
      2. acos-asinN/A

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

        \[\leadsto R \cdot \left(\frac{\mathsf{PI}\left(\right)}{2} + \color{blue}{\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) \]
      4. distribute-rgt-inN/A

        \[\leadsto \frac{\mathsf{PI}\left(\right)}{2} \cdot R + \color{blue}{\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) \cdot R} \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot R\right), \color{blue}{\left(\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) \cdot R\right)}\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2}\right), R\right), \left(\color{blue}{\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)} \cdot R\right)\right) \]
      7. /-lowering-/.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI}\left(\right), 2\right), R\right), \left(\left(\mathsf{neg}\left(\color{blue}{\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) \cdot R\right)\right) \]
      8. PI-lowering-PI.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \left(\left(\mathsf{neg}\left(\sin^{-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)}\right)\right) \cdot R\right)\right) \]
      9. distribute-lft-neg-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \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) \cdot R\right)\right)\right) \]
      10. neg-lowering-neg.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \mathsf{neg.f64}\left(\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) \cdot R\right)\right)\right) \]
    4. Applied egg-rr82.0%

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

    if -9.9000000000000001e-82 < phi2 < 0.0519999999999999976

    1. Initial program 70.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. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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)\right), R\right) \]
      2. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      3. distribute-lft-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      4. associate-+l+N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      6. associate-*r*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      8. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2, \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      9. fma-lowering-fma.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right), \cos \lambda_1, \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
    4. Applied egg-rr87.7%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_1\right) + \cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \phi_2 \cdot \sin \phi_1\right)\right), R\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \cos \phi_1\right) + \cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      3. associate-*r*N/A

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_1 + \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      5. distribute-rgt-outN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\left(\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      8. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      9. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\cos \lambda_1, \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      10. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      12. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right)\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      13. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right)\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      14. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right), \left(\phi_2 \cdot \sin \phi_1\right)\right)\right), R\right) \]
      15. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right), \mathsf{*.f64}\left(\phi_2, \sin \phi_1\right)\right)\right), R\right) \]
    7. Simplified87.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) + \phi_2 \cdot \sin \phi_1\right)} \cdot R \]

    if 0.0519999999999999976 < phi2

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\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)\right), R\right) \]
      2. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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)\right), R\right) \]
      3. fma-lowering-fma.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\sin \phi_2, \sin \phi_1, \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      4. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \sin \phi_1, \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      5. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      6. associate-*l*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\cos \phi_1, \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      8. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\cos \phi_2, \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      10. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6490.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr90.1%

      \[\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 simplification86.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\ \;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 0.052:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right) + \sin \phi_1 \cdot \phi_2\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 6: 82.3% accurate, 0.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\ \mathbf{if}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\ \;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_0\right)\\ \mathbf{elif}\;\phi_2 \leq 2.3 \cdot 10^{-10}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2))))))
   (if (<= phi2 -9.9e-82)
     (- (* R (/ PI 2.0)) (* R (asin (+ (* (sin phi1) (sin phi2)) t_0))))
     (if (<= phi2 2.3e-10)
       (*
        R
        (acos
         (*
          (cos phi1)
          (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2))))))
       (* R (acos (fma (sin phi2) (sin phi1) t_0)))))))
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(phi1) * (cos(phi2) * cos((lambda1 - lambda2)));
	double tmp;
	if (phi2 <= -9.9e-82) {
		tmp = (R * (((double) M_PI) / 2.0)) - (R * asin(((sin(phi1) * sin(phi2)) + t_0)));
	} else if (phi2 <= 2.3e-10) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2)))));
	} else {
		tmp = R * acos(fma(sin(phi2), sin(phi1), t_0));
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))))
	tmp = 0.0
	if (phi2 <= -9.9e-82)
		tmp = Float64(Float64(R * Float64(pi / 2.0)) - Float64(R * asin(Float64(Float64(sin(phi1) * sin(phi2)) + t_0))));
	elseif (phi2 <= 2.3e-10)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = Float64(R * acos(fma(sin(phi2), sin(phi1), t_0)));
	end
	return tmp
end
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[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -9.9e-82], N[(N[(R * N[(Pi / 2.0), $MachinePrecision]), $MachinePrecision] - N[(R * N[ArcSin[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 2.3e-10], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\
\mathbf{if}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\
\;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_0\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -9.9000000000000001e-82

    1. Initial program 82.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. *-commutativeN/A

        \[\leadsto R \cdot \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)} \]
      2. acos-asinN/A

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

        \[\leadsto R \cdot \left(\frac{\mathsf{PI}\left(\right)}{2} + \color{blue}{\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) \]
      4. distribute-rgt-inN/A

        \[\leadsto \frac{\mathsf{PI}\left(\right)}{2} \cdot R + \color{blue}{\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) \cdot R} \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot R\right), \color{blue}{\left(\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) \cdot R\right)}\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2}\right), R\right), \left(\color{blue}{\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)} \cdot R\right)\right) \]
      7. /-lowering-/.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI}\left(\right), 2\right), R\right), \left(\left(\mathsf{neg}\left(\color{blue}{\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) \cdot R\right)\right) \]
      8. PI-lowering-PI.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \left(\left(\mathsf{neg}\left(\sin^{-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)}\right)\right) \cdot R\right)\right) \]
      9. distribute-lft-neg-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \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) \cdot R\right)\right)\right) \]
      10. neg-lowering-neg.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \mathsf{neg.f64}\left(\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) \cdot R\right)\right)\right) \]
    4. Applied egg-rr82.0%

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

    if -9.9000000000000001e-82 < phi2 < 2.30000000000000007e-10

    1. Initial program 70.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6470.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified70.2%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      3. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      4. fma-lowering-fma.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\cos \lambda_2, \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      5. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right)\right)\right)\right), R\right) \]
      9. sin-lowering-sin.f6487.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.1%

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

    if 2.30000000000000007e-10 < phi2

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\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)\right), R\right) \]
      2. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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)\right), R\right) \]
      3. fma-lowering-fma.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\sin \phi_2, \sin \phi_1, \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      4. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \sin \phi_1, \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      5. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      6. associate-*l*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\cos \phi_1, \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      8. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\cos \phi_2, \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      10. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6489.3%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr89.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 simplification86.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\ \;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 2.3 \cdot 10^{-10}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\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 7: 82.3% accurate, 0.9× speedup?

\[\begin{array}{l} [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}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\ \;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 1.9 \cdot 10^{-10}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t\_0 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))))
   (if (<= phi2 -9.9e-82)
     (-
      (* R (/ PI 2.0))
      (*
       R
       (asin (+ t_0 (* (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2))))))))
     (if (<= phi2 1.9e-10)
       (*
        R
        (acos
         (*
          (cos phi1)
          (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2))))))
       (*
        R
        (-
         (/ PI 2.0)
         (asin
          (+
           t_0
           (* (cos phi2) (* (cos phi1) (cos (- lambda2 lambda1))))))))))))
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 (phi2 <= -9.9e-82) {
		tmp = (R * (((double) M_PI) / 2.0)) - (R * asin((t_0 + (cos(phi1) * (cos(phi2) * cos((lambda1 - lambda2)))))));
	} else if (phi2 <= 1.9e-10) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2)))));
	} else {
		tmp = R * ((((double) M_PI) / 2.0) - asin((t_0 + (cos(phi2) * (cos(phi1) * cos((lambda2 - lambda1)))))));
	}
	return tmp;
}
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 (phi2 <= -9.9e-82)
		tmp = Float64(Float64(R * Float64(pi / 2.0)) - Float64(R * asin(Float64(t_0 + Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))))))));
	elseif (phi2 <= 1.9e-10)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = Float64(R * Float64(Float64(pi / 2.0) - asin(Float64(t_0 + Float64(cos(phi2) * Float64(cos(phi1) * cos(Float64(lambda2 - lambda1))))))));
	end
	return tmp
end
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[phi2, -9.9e-82], N[(N[(R * N[(Pi / 2.0), $MachinePrecision]), $MachinePrecision] - N[(R * N[ArcSin[N[(t$95$0 + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 1.9e-10], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[(N[(Pi / 2.0), $MachinePrecision] - N[ArcSin[N[(t$95$0 + N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[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}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\
\;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\

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

\mathbf{else}:\\
\;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t\_0 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -9.9000000000000001e-82

    1. Initial program 82.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. *-commutativeN/A

        \[\leadsto R \cdot \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)} \]
      2. acos-asinN/A

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

        \[\leadsto R \cdot \left(\frac{\mathsf{PI}\left(\right)}{2} + \color{blue}{\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) \]
      4. distribute-rgt-inN/A

        \[\leadsto \frac{\mathsf{PI}\left(\right)}{2} \cdot R + \color{blue}{\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) \cdot R} \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot R\right), \color{blue}{\left(\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) \cdot R\right)}\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2}\right), R\right), \left(\color{blue}{\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)} \cdot R\right)\right) \]
      7. /-lowering-/.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI}\left(\right), 2\right), R\right), \left(\left(\mathsf{neg}\left(\color{blue}{\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) \cdot R\right)\right) \]
      8. PI-lowering-PI.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \left(\left(\mathsf{neg}\left(\sin^{-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)}\right)\right) \cdot R\right)\right) \]
      9. distribute-lft-neg-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \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) \cdot R\right)\right)\right) \]
      10. neg-lowering-neg.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \mathsf{neg.f64}\left(\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) \cdot R\right)\right)\right) \]
    4. Applied egg-rr82.0%

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

    if -9.9000000000000001e-82 < phi2 < 1.8999999999999999e-10

    1. Initial program 70.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6470.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified70.2%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      3. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      4. fma-lowering-fma.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\cos \lambda_2, \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      5. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right)\right)\right)\right), R\right) \]
      9. sin-lowering-sin.f6487.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{fma.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.1%

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

    if 1.8999999999999999e-10 < phi2

    1. Initial program 89.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. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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)\right), R\right) \]
      2. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      3. distribute-lft-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      4. associate-+l+N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      6. associate-*r*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      8. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2, \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      9. fma-lowering-fma.f64N/A

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

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

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

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

Alternative 8: 82.3% accurate, 1.0× speedup?

\[\begin{array}{l} [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}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\ \;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t\_0 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))))
   (if (<= phi2 -9.9e-82)
     (-
      (* R (/ PI 2.0))
      (*
       R
       (asin (+ t_0 (* (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2))))))))
     (if (<= phi2 8e-11)
       (*
        R
        (acos
         (*
          (cos phi1)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda2) (cos lambda1))))))
       (*
        R
        (-
         (/ PI 2.0)
         (asin
          (+
           t_0
           (* (cos phi2) (* (cos phi1) (cos (- lambda2 lambda1))))))))))))
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 (phi2 <= -9.9e-82) {
		tmp = (R * (((double) M_PI) / 2.0)) - (R * asin((t_0 + (cos(phi1) * (cos(phi2) * cos((lambda1 - lambda2)))))));
	} else if (phi2 <= 8e-11) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = R * ((((double) M_PI) / 2.0) - asin((t_0 + (cos(phi2) * (cos(phi1) * cos((lambda2 - lambda1)))))));
	}
	return tmp;
}
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.sin(phi1) * Math.sin(phi2);
	double tmp;
	if (phi2 <= -9.9e-82) {
		tmp = (R * (Math.PI / 2.0)) - (R * Math.asin((t_0 + (Math.cos(phi1) * (Math.cos(phi2) * Math.cos((lambda1 - lambda2)))))));
	} else if (phi2 <= 8e-11) {
		tmp = R * Math.acos((Math.cos(phi1) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1)))));
	} else {
		tmp = R * ((Math.PI / 2.0) - Math.asin((t_0 + (Math.cos(phi2) * (Math.cos(phi1) * Math.cos((lambda2 - lambda1)))))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.sin(phi1) * math.sin(phi2)
	tmp = 0
	if phi2 <= -9.9e-82:
		tmp = (R * (math.pi / 2.0)) - (R * math.asin((t_0 + (math.cos(phi1) * (math.cos(phi2) * math.cos((lambda1 - lambda2)))))))
	elif phi2 <= 8e-11:
		tmp = R * math.acos((math.cos(phi1) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1)))))
	else:
		tmp = R * ((math.pi / 2.0) - math.asin((t_0 + (math.cos(phi2) * (math.cos(phi1) * math.cos((lambda2 - lambda1)))))))
	return tmp
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 (phi2 <= -9.9e-82)
		tmp = Float64(Float64(R * Float64(pi / 2.0)) - Float64(R * asin(Float64(t_0 + Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))))))));
	elseif (phi2 <= 8e-11)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = Float64(R * Float64(Float64(pi / 2.0) - asin(Float64(t_0 + Float64(cos(phi2) * Float64(cos(phi1) * cos(Float64(lambda2 - lambda1))))))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = sin(phi1) * sin(phi2);
	tmp = 0.0;
	if (phi2 <= -9.9e-82)
		tmp = (R * (pi / 2.0)) - (R * asin((t_0 + (cos(phi1) * (cos(phi2) * cos((lambda1 - lambda2)))))));
	elseif (phi2 <= 8e-11)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	else
		tmp = R * ((pi / 2.0) - asin((t_0 + (cos(phi2) * (cos(phi1) * 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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -9.9e-82], N[(N[(R * N[(Pi / 2.0), $MachinePrecision]), $MachinePrecision] - N[(R * N[ArcSin[N[(t$95$0 + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 8e-11], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[(N[(Pi / 2.0), $MachinePrecision] - N[ArcSin[N[(t$95$0 + N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[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}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\
\;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\

\mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{-11}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t\_0 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -9.9000000000000001e-82

    1. Initial program 82.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. *-commutativeN/A

        \[\leadsto R \cdot \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)} \]
      2. acos-asinN/A

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

        \[\leadsto R \cdot \left(\frac{\mathsf{PI}\left(\right)}{2} + \color{blue}{\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) \]
      4. distribute-rgt-inN/A

        \[\leadsto \frac{\mathsf{PI}\left(\right)}{2} \cdot R + \color{blue}{\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) \cdot R} \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot R\right), \color{blue}{\left(\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) \cdot R\right)}\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2}\right), R\right), \left(\color{blue}{\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)} \cdot R\right)\right) \]
      7. /-lowering-/.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI}\left(\right), 2\right), R\right), \left(\left(\mathsf{neg}\left(\color{blue}{\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) \cdot R\right)\right) \]
      8. PI-lowering-PI.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \left(\left(\mathsf{neg}\left(\sin^{-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)}\right)\right) \cdot R\right)\right) \]
      9. distribute-lft-neg-outN/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \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) \cdot R\right)\right)\right) \]
      10. neg-lowering-neg.f64N/A

        \[\leadsto \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), R\right), \mathsf{neg.f64}\left(\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) \cdot R\right)\right)\right) \]
    4. Applied egg-rr82.0%

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

    if -9.9000000000000001e-82 < phi2 < 7.99999999999999952e-11

    1. Initial program 70.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6470.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified70.2%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      3. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right), R\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      7. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f6487.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.1%

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

    if 7.99999999999999952e-11 < phi2

    1. Initial program 89.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. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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)\right), R\right) \]
      2. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      3. distribute-lft-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      4. associate-+l+N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      6. associate-*r*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      8. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2, \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      9. fma-lowering-fma.f64N/A

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -9.9 \cdot 10^{-82}:\\ \;\;\;\;R \cdot \frac{\pi}{2} - R \cdot \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 83.3% accurate, 1.0× speedup?

\[\begin{array}{l} [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}\;\phi_2 \leq -1.42 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 9.6 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t\_0 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))))
   (if (<= phi2 -1.42e-14)
     (*
      R
      (-
       (/ PI 2.0)
       (asin (+ t_0 (* (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2))))))))
     (if (<= phi2 9.6e-11)
       (*
        R
        (acos
         (*
          (cos phi1)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda2) (cos lambda1))))))
       (*
        R
        (-
         (/ PI 2.0)
         (asin
          (+
           t_0
           (* (cos phi2) (* (cos phi1) (cos (- lambda2 lambda1))))))))))))
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 (phi2 <= -1.42e-14) {
		tmp = R * ((((double) M_PI) / 2.0) - asin((t_0 + (cos(phi1) * (cos(phi2) * cos((lambda1 - lambda2)))))));
	} else if (phi2 <= 9.6e-11) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = R * ((((double) M_PI) / 2.0) - asin((t_0 + (cos(phi2) * (cos(phi1) * cos((lambda2 - lambda1)))))));
	}
	return tmp;
}
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.sin(phi1) * Math.sin(phi2);
	double tmp;
	if (phi2 <= -1.42e-14) {
		tmp = R * ((Math.PI / 2.0) - Math.asin((t_0 + (Math.cos(phi1) * (Math.cos(phi2) * Math.cos((lambda1 - lambda2)))))));
	} else if (phi2 <= 9.6e-11) {
		tmp = R * Math.acos((Math.cos(phi1) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1)))));
	} else {
		tmp = R * ((Math.PI / 2.0) - Math.asin((t_0 + (Math.cos(phi2) * (Math.cos(phi1) * Math.cos((lambda2 - lambda1)))))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.sin(phi1) * math.sin(phi2)
	tmp = 0
	if phi2 <= -1.42e-14:
		tmp = R * ((math.pi / 2.0) - math.asin((t_0 + (math.cos(phi1) * (math.cos(phi2) * math.cos((lambda1 - lambda2)))))))
	elif phi2 <= 9.6e-11:
		tmp = R * math.acos((math.cos(phi1) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1)))))
	else:
		tmp = R * ((math.pi / 2.0) - math.asin((t_0 + (math.cos(phi2) * (math.cos(phi1) * math.cos((lambda2 - lambda1)))))))
	return tmp
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 (phi2 <= -1.42e-14)
		tmp = Float64(R * Float64(Float64(pi / 2.0) - asin(Float64(t_0 + Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))))))));
	elseif (phi2 <= 9.6e-11)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = Float64(R * Float64(Float64(pi / 2.0) - asin(Float64(t_0 + Float64(cos(phi2) * Float64(cos(phi1) * cos(Float64(lambda2 - lambda1))))))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = sin(phi1) * sin(phi2);
	tmp = 0.0;
	if (phi2 <= -1.42e-14)
		tmp = R * ((pi / 2.0) - asin((t_0 + (cos(phi1) * (cos(phi2) * cos((lambda1 - lambda2)))))));
	elseif (phi2 <= 9.6e-11)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	else
		tmp = R * ((pi / 2.0) - asin((t_0 + (cos(phi2) * (cos(phi1) * 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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -1.42e-14], N[(R * N[(N[(Pi / 2.0), $MachinePrecision] - N[ArcSin[N[(t$95$0 + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 9.6e-11], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[(N[(Pi / 2.0), $MachinePrecision] - N[ArcSin[N[(t$95$0 + N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[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}\;\phi_2 \leq -1.42 \cdot 10^{-14}:\\
\;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\

\mathbf{elif}\;\phi_2 \leq 9.6 \cdot 10^{-11}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t\_0 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -1.42000000000000004e-14

    1. Initial program 85.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. acos-asinN/A

        \[\leadsto \mathsf{*.f64}\left(\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), R\right) \]
      2. --lowering--.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2}\right), \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), R\right) \]
      3. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI}\left(\right), 2\right), \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), R\right) \]
      4. PI-lowering-PI.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \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), R\right) \]
      5. asin-lowering-asin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\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), R\right) \]
      6. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\left(\sin \phi_1 \cdot \sin \phi_2\right), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \phi_1, \sin \phi_2\right), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \sin \phi_2\right), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      10. associate-*l*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      11. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\cos \phi_1, \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      13. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\cos \phi_2, \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      14. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      15. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right)\right), R\right) \]
      16. --lowering--.f6485.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr85.6%

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

    if -1.42000000000000004e-14 < phi2 < 9.6000000000000005e-11

    1. Initial program 68.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6468.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified68.9%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      3. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right), R\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      7. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f6487.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.2%

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

    if 9.6000000000000005e-11 < phi2

    1. Initial program 89.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. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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)\right), R\right) \]
      2. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      3. distribute-lft-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      4. associate-+l+N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      6. associate-*r*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      8. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2, \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      9. fma-lowering-fma.f64N/A

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.42 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 9.6 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 10: 73.1% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1\right)\\ t_1 := \sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\\ \mathbf{if}\;\phi_2 \leq -1.06 \cdot 10^{-7}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_1\right)\\ \mathbf{elif}\;\phi_2 \leq 3.45 \cdot 10^{+218}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_1\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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (*
          R
          (acos
           (+
            (* (sin phi1) (sin phi2))
            (* (* (cos phi1) (cos phi2)) (cos lambda1))))))
        (t_1
         (+ (* (sin lambda1) (sin lambda2)) (* (cos lambda2) (cos lambda1)))))
   (if (<= phi2 -1.06e-7)
     t_0
     (if (<= phi2 8e-6)
       (* R (acos (* (cos phi1) t_1)))
       (if (<= phi2 3.45e+218) (* R (acos (* (cos phi2) t_1))) t_0)))))
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(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos(lambda1))));
	double t_1 = (sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1));
	double tmp;
	if (phi2 <= -1.06e-7) {
		tmp = t_0;
	} else if (phi2 <= 8e-6) {
		tmp = R * acos((cos(phi1) * t_1));
	} else if (phi2 <= 3.45e+218) {
		tmp = R * acos((cos(phi2) * t_1));
	} else {
		tmp = t_0;
	}
	return tmp;
}
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) :: t_1
    real(8) :: tmp
    t_0 = r * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos(lambda1))))
    t_1 = (sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1))
    if (phi2 <= (-1.06d-7)) then
        tmp = t_0
    else if (phi2 <= 8d-6) then
        tmp = r * acos((cos(phi1) * t_1))
    else if (phi2 <= 3.45d+218) then
        tmp = r * acos((cos(phi2) * t_1))
    else
        tmp = t_0
    end if
    code = tmp
end function
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 = R * Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos(lambda1))));
	double t_1 = (Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1));
	double tmp;
	if (phi2 <= -1.06e-7) {
		tmp = t_0;
	} else if (phi2 <= 8e-6) {
		tmp = R * Math.acos((Math.cos(phi1) * t_1));
	} else if (phi2 <= 3.45e+218) {
		tmp = R * Math.acos((Math.cos(phi2) * t_1));
	} else {
		tmp = t_0;
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = R * math.acos(((math.sin(phi1) * math.sin(phi2)) + ((math.cos(phi1) * math.cos(phi2)) * math.cos(lambda1))))
	t_1 = (math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1))
	tmp = 0
	if phi2 <= -1.06e-7:
		tmp = t_0
	elif phi2 <= 8e-6:
		tmp = R * math.acos((math.cos(phi1) * t_1))
	elif phi2 <= 3.45e+218:
		tmp = R * math.acos((math.cos(phi2) * t_1))
	else:
		tmp = t_0
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(lambda1)))))
	t_1 = Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1)))
	tmp = 0.0
	if (phi2 <= -1.06e-7)
		tmp = t_0;
	elseif (phi2 <= 8e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * t_1)));
	elseif (phi2 <= 3.45e+218)
		tmp = Float64(R * acos(Float64(cos(phi2) * t_1)));
	else
		tmp = t_0;
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = R * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos(lambda1))));
	t_1 = (sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1));
	tmp = 0.0;
	if (phi2 <= -1.06e-7)
		tmp = t_0;
	elseif (phi2 <= 8e-6)
		tmp = R * acos((cos(phi1) * t_1));
	elseif (phi2 <= 3.45e+218)
		tmp = R * acos((cos(phi2) * t_1));
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
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[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -1.06e-7], t$95$0, If[LessEqual[phi2, 8e-6], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 3.45e+218], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * t$95$1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1\right)\\
t_1 := \sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\\
\mathbf{if}\;\phi_2 \leq -1.06 \cdot 10^{-7}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{-6}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_1\right)\\

\mathbf{elif}\;\phi_2 \leq 3.45 \cdot 10^{+218}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -1.06e-7 or 3.4500000000000001e218 < phi2

    1. Initial program 87.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 \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \color{blue}{\cos \lambda_1}\right)\right)\right), R\right) \]
    4. Step-by-step derivation
      1. cos-lowering-cos.f6464.8%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified64.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 \]

    if -1.06e-7 < phi2 < 7.99999999999999964e-6

    1. Initial program 69.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 phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6469.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified69.0%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      3. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right), R\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      7. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f6487.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.0%

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

    if 7.99999999999999964e-6 < phi2 < 3.4500000000000001e218

    1. Initial program 87.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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)\right), R\right) \]
      2. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      3. distribute-lft-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      4. associate-+l+N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      6. associate-*r*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      8. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2, \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      9. fma-lowering-fma.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right), \cos \lambda_1, \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
    4. Applied egg-rr98.8%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2\right)\right), R\right) \]
      3. distribute-rgt-outN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_2, \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      5. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      6. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\cos \lambda_1, \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      8. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right)\right)\right)\right), R\right) \]
      11. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right)\right)\right)\right), R\right) \]
      12. sin-lowering-sin.f6462.5%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Simplified62.5%

      \[\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)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification74.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.06 \cdot 10^{-7}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1\right)\\ \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 3.45 \cdot 10^{+218}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 11: 83.3% accurate, 1.0× speedup?

\[\begin{array}{l} [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 \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq -2.3 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot t\_1\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 1.15 \cdot 10^{-10}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(t\_0 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot t\_1\right) \cdot R\\ \end{array} \end{array} \]
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 (- lambda1 lambda2))))
   (if (<= phi2 -2.3e-14)
     (* R (- (/ PI 2.0) (asin (+ t_0 (* (cos phi1) (* (cos phi2) t_1))))))
     (if (<= phi2 1.15e-10)
       (*
        R
        (acos
         (*
          (cos phi1)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda2) (cos lambda1))))))
       (* (acos (+ t_0 (* (* (cos phi1) (cos phi2)) t_1))) R)))))
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((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -2.3e-14) {
		tmp = R * ((((double) M_PI) / 2.0) - asin((t_0 + (cos(phi1) * (cos(phi2) * t_1)))));
	} else if (phi2 <= 1.15e-10) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = acos((t_0 + ((cos(phi1) * cos(phi2)) * t_1))) * R;
	}
	return tmp;
}
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.sin(phi1) * Math.sin(phi2);
	double t_1 = Math.cos((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -2.3e-14) {
		tmp = R * ((Math.PI / 2.0) - Math.asin((t_0 + (Math.cos(phi1) * (Math.cos(phi2) * t_1)))));
	} else if (phi2 <= 1.15e-10) {
		tmp = R * Math.acos((Math.cos(phi1) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1)))));
	} else {
		tmp = Math.acos((t_0 + ((Math.cos(phi1) * Math.cos(phi2)) * t_1))) * R;
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.sin(phi1) * math.sin(phi2)
	t_1 = math.cos((lambda1 - lambda2))
	tmp = 0
	if phi2 <= -2.3e-14:
		tmp = R * ((math.pi / 2.0) - math.asin((t_0 + (math.cos(phi1) * (math.cos(phi2) * t_1)))))
	elif phi2 <= 1.15e-10:
		tmp = R * math.acos((math.cos(phi1) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1)))))
	else:
		tmp = math.acos((t_0 + ((math.cos(phi1) * math.cos(phi2)) * t_1))) * R
	return tmp
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 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi2 <= -2.3e-14)
		tmp = Float64(R * Float64(Float64(pi / 2.0) - asin(Float64(t_0 + Float64(cos(phi1) * Float64(cos(phi2) * t_1))))));
	elseif (phi2 <= 1.15e-10)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = Float64(acos(Float64(t_0 + Float64(Float64(cos(phi1) * cos(phi2)) * t_1))) * R);
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = sin(phi1) * sin(phi2);
	t_1 = cos((lambda1 - lambda2));
	tmp = 0.0;
	if (phi2 <= -2.3e-14)
		tmp = R * ((pi / 2.0) - asin((t_0 + (cos(phi1) * (cos(phi2) * t_1)))));
	elseif (phi2 <= 1.15e-10)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	else
		tmp = acos((t_0 + ((cos(phi1) * cos(phi2)) * t_1))) * R;
	end
	tmp_2 = tmp;
end
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[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, -2.3e-14], N[(R * N[(N[(Pi / 2.0), $MachinePrecision] - N[ArcSin[N[(t$95$0 + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 1.15e-10], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[ArcCos[N[(t$95$0 + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]]]
\begin{array}{l}
[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 \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq -2.3 \cdot 10^{-14}:\\
\;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot t\_1\right)\right)\right)\\

\mathbf{elif}\;\phi_2 \leq 1.15 \cdot 10^{-10}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(t\_0 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot t\_1\right) \cdot R\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -2.29999999999999998e-14

    1. Initial program 85.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. acos-asinN/A

        \[\leadsto \mathsf{*.f64}\left(\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), R\right) \]
      2. --lowering--.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2}\right), \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), R\right) \]
      3. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI}\left(\right), 2\right), \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), R\right) \]
      4. PI-lowering-PI.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \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), R\right) \]
      5. asin-lowering-asin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\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), R\right) \]
      6. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\left(\sin \phi_1 \cdot \sin \phi_2\right), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \phi_1, \sin \phi_2\right), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \sin \phi_2\right), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      10. associate-*l*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      11. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\cos \phi_1, \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      13. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\cos \phi_2, \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      14. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      15. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right)\right), R\right) \]
      16. --lowering--.f6485.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\mathsf{/.f64}\left(\mathsf{PI.f64}\left(\right), 2\right), \mathsf{asin.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr85.6%

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

    if -2.29999999999999998e-14 < phi2 < 1.15000000000000004e-10

    1. Initial program 68.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6468.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified68.9%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      3. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right), R\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      7. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f6487.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.2%

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

    if 1.15000000000000004e-10 < phi2

    1. Initial program 89.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. Recombined 3 regimes into one program.
  4. Final simplification87.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -2.3 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 1.15 \cdot 10^{-10}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\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} \]
  5. Add Preprocessing

Alternative 12: 83.3% accurate, 1.0× speedup?

\[\begin{array}{l} [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 := \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq -2.3 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t\_1 + \frac{1}{\frac{1}{t\_0}}\right)\\ \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(t\_0 + t\_1\right) \cdot R\\ \end{array} \end{array} \]
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)) (cos (- lambda1 lambda2)))))
   (if (<= phi2 -2.3e-14)
     (* R (acos (+ t_1 (/ 1.0 (/ 1.0 t_0)))))
     (if (<= phi2 8e-11)
       (*
        R
        (acos
         (*
          (cos phi1)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda2) (cos lambda1))))))
       (* (acos (+ t_0 t_1)) R)))))
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)) * cos((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -2.3e-14) {
		tmp = R * acos((t_1 + (1.0 / (1.0 / t_0))));
	} else if (phi2 <= 8e-11) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = acos((t_0 + t_1)) * R;
	}
	return tmp;
}
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) :: t_1
    real(8) :: tmp
    t_0 = sin(phi1) * sin(phi2)
    t_1 = (cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))
    if (phi2 <= (-2.3d-14)) then
        tmp = r * acos((t_1 + (1.0d0 / (1.0d0 / t_0))))
    else if (phi2 <= 8d-11) then
        tmp = r * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))))
    else
        tmp = acos((t_0 + t_1)) * r
    end if
    code = tmp
end function
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.sin(phi1) * Math.sin(phi2);
	double t_1 = (Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -2.3e-14) {
		tmp = R * Math.acos((t_1 + (1.0 / (1.0 / t_0))));
	} else if (phi2 <= 8e-11) {
		tmp = R * Math.acos((Math.cos(phi1) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1)))));
	} else {
		tmp = Math.acos((t_0 + t_1)) * R;
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.sin(phi1) * math.sin(phi2)
	t_1 = (math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2))
	tmp = 0
	if phi2 <= -2.3e-14:
		tmp = R * math.acos((t_1 + (1.0 / (1.0 / t_0))))
	elif phi2 <= 8e-11:
		tmp = R * math.acos((math.cos(phi1) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1)))))
	else:
		tmp = math.acos((t_0 + t_1)) * R
	return tmp
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(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2)))
	tmp = 0.0
	if (phi2 <= -2.3e-14)
		tmp = Float64(R * acos(Float64(t_1 + Float64(1.0 / Float64(1.0 / t_0)))));
	elseif (phi2 <= 8e-11)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = Float64(acos(Float64(t_0 + t_1)) * R);
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = sin(phi1) * sin(phi2);
	t_1 = (cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2));
	tmp = 0.0;
	if (phi2 <= -2.3e-14)
		tmp = R * acos((t_1 + (1.0 / (1.0 / t_0))));
	elseif (phi2 <= 8e-11)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	else
		tmp = acos((t_0 + t_1)) * R;
	end
	tmp_2 = tmp;
end
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[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -2.3e-14], N[(R * N[ArcCos[N[(t$95$1 + N[(1.0 / N[(1.0 / t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 8e-11], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[ArcCos[N[(t$95$0 + t$95$1), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]]]
\begin{array}{l}
[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 := \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq -2.3 \cdot 10^{-14}:\\
\;\;\;\;R \cdot \cos^{-1} \left(t\_1 + \frac{1}{\frac{1}{t\_0}}\right)\\

\mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{-11}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(t\_0 + t\_1\right) \cdot R\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -2.29999999999999998e-14

    1. Initial program 85.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. sin-multN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\left(\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2}\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right), R\right) \]
      2. clear-numN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\left(\frac{1}{\frac{2}{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}}\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right), R\right) \]
      3. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{/.f64}\left(1, \left(\frac{2}{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. clear-numN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{/.f64}\left(1, \left(\frac{1}{\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2}}\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. sin-multN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{/.f64}\left(1, \left(\frac{1}{\sin \phi_1 \cdot \sin \phi_2}\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(1, \left(\sin \phi_1 \cdot \sin \phi_2\right)\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\sin \phi_1, \sin \phi_2\right)\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \sin \phi_2\right)\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sin-lowering-sin.f6485.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right)\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr85.6%

      \[\leadsto \cos^{-1} \left(\color{blue}{\frac{1}{\frac{1}{\sin \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 \]

    if -2.29999999999999998e-14 < phi2 < 7.99999999999999952e-11

    1. Initial program 68.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6468.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified68.9%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      3. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right), R\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      7. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f6487.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.2%

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

    if 7.99999999999999952e-11 < phi2

    1. Initial program 89.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. Recombined 3 regimes into one program.
  4. Final simplification87.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -2.3 \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) + \frac{1}{\frac{1}{\sin \phi_1 \cdot \sin \phi_2}}\right)\\ \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\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} \]
  5. Add Preprocessing

Alternative 13: 83.3% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_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\\ \mathbf{if}\;\phi_2 \leq -2.3 \cdot 10^{-14}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 3.8 \cdot 10^{-9}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (*
          (acos
           (+
            (* (sin phi1) (sin phi2))
            (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
          R)))
   (if (<= phi2 -2.3e-14)
     t_0
     (if (<= phi2 3.8e-9)
       (*
        R
        (acos
         (*
          (cos phi1)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda2) (cos lambda1))))))
       t_0))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
	double tmp;
	if (phi2 <= -2.3e-14) {
		tmp = t_0;
	} else if (phi2 <= 3.8e-9) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = t_0;
	}
	return tmp;
}
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 = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * r
    if (phi2 <= (-2.3d-14)) then
        tmp = t_0
    else if (phi2 <= 3.8d-9) then
        tmp = r * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))))
    else
        tmp = t_0
    end if
    code = tmp
end function
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.acos(((Math.sin(phi1) * Math.sin(phi2)) + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2))))) * R;
	double tmp;
	if (phi2 <= -2.3e-14) {
		tmp = t_0;
	} else if (phi2 <= 3.8e-9) {
		tmp = R * Math.acos((Math.cos(phi1) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1)))));
	} else {
		tmp = t_0;
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.acos(((math.sin(phi1) * math.sin(phi2)) + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2))))) * R
	tmp = 0
	if phi2 <= -2.3e-14:
		tmp = t_0
	elif phi2 <= 3.8e-9:
		tmp = R * math.acos((math.cos(phi1) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1)))))
	else:
		tmp = t_0
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) * R)
	tmp = 0.0
	if (phi2 <= -2.3e-14)
		tmp = t_0;
	elseif (phi2 <= 3.8e-9)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = t_0;
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
	tmp = 0.0;
	if (phi2 <= -2.3e-14)
		tmp = t_0;
	elseif (phi2 <= 3.8e-9)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
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[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]}, If[LessEqual[phi2, -2.3e-14], t$95$0, If[LessEqual[phi2, 3.8e-9], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $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])\\
\\
\begin{array}{l}
t_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\\
\mathbf{if}\;\phi_2 \leq -2.3 \cdot 10^{-14}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\phi_2 \leq 3.8 \cdot 10^{-9}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \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 phi2 < -2.29999999999999998e-14 or 3.80000000000000011e-9 < phi2

    1. Initial program 87.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

    if -2.29999999999999998e-14 < phi2 < 3.80000000000000011e-9

    1. Initial program 68.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6468.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified68.9%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      3. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right), R\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      7. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f6487.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -2.3 \cdot 10^{-14}:\\ \;\;\;\;\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\\ \mathbf{elif}\;\phi_2 \leq 3.8 \cdot 10^{-9}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\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} \]
  5. Add Preprocessing

Alternative 14: 76.2% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \phi_1 \cdot \cos \phi_2\\ t_1 := \sin \phi_1 \cdot \sin \phi_2\\ \mathbf{if}\;\lambda_1 \leq -4.4 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t\_1 + t\_0 \cdot \cos \lambda_1\right)\\ \mathbf{elif}\;\lambda_1 \leq 1300000000000:\\ \;\;\;\;R \cdot \cos^{-1} \left(t\_1 + t\_0 \cdot \cos \lambda_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi1) (cos phi2))) (t_1 (* (sin phi1) (sin phi2))))
   (if (<= lambda1 -4.4e-6)
     (* R (acos (+ t_1 (* t_0 (cos lambda1)))))
     (if (<= lambda1 1300000000000.0)
       (* R (acos (+ t_1 (* t_0 (cos lambda2)))))
       (*
        R
        (acos
         (*
          (cos phi2)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda2) (cos lambda1))))))))))
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(phi1) * cos(phi2);
	double t_1 = sin(phi1) * sin(phi2);
	double tmp;
	if (lambda1 <= -4.4e-6) {
		tmp = R * acos((t_1 + (t_0 * cos(lambda1))));
	} else if (lambda1 <= 1300000000000.0) {
		tmp = R * acos((t_1 + (t_0 * cos(lambda2))));
	} else {
		tmp = R * acos((cos(phi2) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	}
	return tmp;
}
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) :: t_1
    real(8) :: tmp
    t_0 = cos(phi1) * cos(phi2)
    t_1 = sin(phi1) * sin(phi2)
    if (lambda1 <= (-4.4d-6)) then
        tmp = r * acos((t_1 + (t_0 * cos(lambda1))))
    else if (lambda1 <= 1300000000000.0d0) then
        tmp = r * acos((t_1 + (t_0 * cos(lambda2))))
    else
        tmp = r * acos((cos(phi2) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))))
    end if
    code = tmp
end function
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(phi1) * Math.cos(phi2);
	double t_1 = Math.sin(phi1) * Math.sin(phi2);
	double tmp;
	if (lambda1 <= -4.4e-6) {
		tmp = R * Math.acos((t_1 + (t_0 * Math.cos(lambda1))));
	} else if (lambda1 <= 1300000000000.0) {
		tmp = R * Math.acos((t_1 + (t_0 * Math.cos(lambda2))));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1)))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos(phi1) * math.cos(phi2)
	t_1 = math.sin(phi1) * math.sin(phi2)
	tmp = 0
	if lambda1 <= -4.4e-6:
		tmp = R * math.acos((t_1 + (t_0 * math.cos(lambda1))))
	elif lambda1 <= 1300000000000.0:
		tmp = R * math.acos((t_1 + (t_0 * math.cos(lambda2))))
	else:
		tmp = R * math.acos((math.cos(phi2) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1)))))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi1) * cos(phi2))
	t_1 = Float64(sin(phi1) * sin(phi2))
	tmp = 0.0
	if (lambda1 <= -4.4e-6)
		tmp = Float64(R * acos(Float64(t_1 + Float64(t_0 * cos(lambda1)))));
	elseif (lambda1 <= 1300000000000.0)
		tmp = Float64(R * acos(Float64(t_1 + Float64(t_0 * cos(lambda2)))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1))))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(phi1) * cos(phi2);
	t_1 = sin(phi1) * sin(phi2);
	tmp = 0.0;
	if (lambda1 <= -4.4e-6)
		tmp = R * acos((t_1 + (t_0 * cos(lambda1))));
	elseif (lambda1 <= 1300000000000.0)
		tmp = R * acos((t_1 + (t_0 * cos(lambda2))));
	else
		tmp = R * acos((cos(phi2) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1)))));
	end
	tmp_2 = tmp;
end
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[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[lambda1, -4.4e-6], N[(R * N[ArcCos[N[(t$95$1 + N[(t$95$0 * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda1, 1300000000000.0], N[(R * N[ArcCos[N[(t$95$1 + N[(t$95$0 * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $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])\\
\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \cos \phi_2\\
t_1 := \sin \phi_1 \cdot \sin \phi_2\\
\mathbf{if}\;\lambda_1 \leq -4.4 \cdot 10^{-6}:\\
\;\;\;\;R \cdot \cos^{-1} \left(t\_1 + t\_0 \cdot \cos \lambda_1\right)\\

\mathbf{elif}\;\lambda_1 \leq 1300000000000:\\
\;\;\;\;R \cdot \cos^{-1} \left(t\_1 + t\_0 \cdot \cos \lambda_2\right)\\

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


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

    1. Initial program 73.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 lambda2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \color{blue}{\cos \lambda_1}\right)\right)\right), R\right) \]
    4. Step-by-step derivation
      1. cos-lowering-cos.f6473.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified73.6%

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

    if -4.4000000000000002e-6 < lambda1 < 1.3e12

    1. Initial program 88.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 lambda1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right)\right)\right), R\right) \]
    4. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \cos \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f6486.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right), R\right) \]
    5. Simplified86.9%

      \[\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_2}\right) \cdot R \]

    if 1.3e12 < lambda1

    1. Initial program 63.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. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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)\right), R\right) \]
      2. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      3. distribute-lft-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      4. associate-+l+N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      6. associate-*r*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      8. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2, \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      9. fma-lowering-fma.f64N/A

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2\right)\right), R\right) \]
      3. distribute-rgt-outN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_2, \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      5. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      6. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\cos \lambda_1, \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      8. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right)\right)\right)\right), R\right) \]
      11. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right)\right)\right)\right), R\right) \]
      12. sin-lowering-sin.f6458.8%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Simplified58.8%

      \[\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)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification77.1%

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

Alternative 15: 68.3% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\\ \mathbf{if}\;\phi_2 \leq -1.7 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \cos \phi_2\right)\\ \mathbf{elif}\;\phi_2 \leq 2.95 \cdot 10^{-5}:\\ \;\;\;\;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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (+ (* (sin lambda1) (sin lambda2)) (* (cos lambda2) (cos lambda1)))))
   (if (<= phi2 -1.7e-5)
     (* R (acos (+ (* (sin phi1) (sin phi2)) (* (cos phi1) (cos phi2)))))
     (if (<= phi2 2.95e-5)
       (* R (acos (* (cos phi1) t_0)))
       (* R (acos (* (cos phi2) t_0)))))))
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(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1));
	double tmp;
	if (phi2 <= -1.7e-5) {
		tmp = R * acos(((sin(phi1) * sin(phi2)) + (cos(phi1) * cos(phi2))));
	} else if (phi2 <= 2.95e-5) {
		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.
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 = (sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1))
    if (phi2 <= (-1.7d-5)) then
        tmp = r * acos(((sin(phi1) * sin(phi2)) + (cos(phi1) * cos(phi2))))
    else if (phi2 <= 2.95d-5) 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;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1));
	double tmp;
	if (phi2 <= -1.7e-5) {
		tmp = R * Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + (Math.cos(phi1) * Math.cos(phi2))));
	} else if (phi2 <= 2.95e-5) {
		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])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = (math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1))
	tmp = 0
	if phi2 <= -1.7e-5:
		tmp = R * math.acos(((math.sin(phi1) * math.sin(phi2)) + (math.cos(phi1) * math.cos(phi2))))
	elif phi2 <= 2.95e-5:
		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])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1)))
	tmp = 0.0
	if (phi2 <= -1.7e-5)
		tmp = Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(cos(phi1) * cos(phi2)))));
	elseif (phi2 <= 2.95e-5)
		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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = (sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1));
	tmp = 0.0;
	if (phi2 <= -1.7e-5)
		tmp = R * acos(((sin(phi1) * sin(phi2)) + (cos(phi1) * cos(phi2))));
	elseif (phi2 <= 2.95e-5)
		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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -1.7e-5], N[(R * N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 2.95e-5], 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])\\
\\
\begin{array}{l}
t_0 := \sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\\
\mathbf{if}\;\phi_2 \leq -1.7 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \cos \phi_2\right)\\

\mathbf{elif}\;\phi_2 \leq 2.95 \cdot 10^{-5}:\\
\;\;\;\;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 3 regimes
  2. if phi2 < -1.7e-5

    1. Initial program 85.7%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in lambda1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right)\right)\right), R\right) \]
    4. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \cos \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f6462.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right), R\right) \]
    5. Simplified62.2%

      \[\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_2}\right) \cdot R \]
    6. Taylor expanded in lambda2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \left(\cos \phi_2 \cdot \cos \phi_1\right)\right)\right), R\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\cos \phi_2, \cos \phi_1\right)\right)\right), R\right) \]
      3. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \phi_1\right)\right)\right), R\right) \]
      4. cos-lowering-cos.f6439.5%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\phi_1\right)\right)\right)\right), R\right) \]
    8. Simplified39.5%

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

    if -1.7e-5 < phi2 < 2.9499999999999999e-5

    1. Initial program 69.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 phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6469.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified69.0%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      3. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right), R\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      7. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f6487.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.0%

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

    if 2.9499999999999999e-5 < phi2

    1. Initial program 89.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. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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)\right), R\right) \]
      2. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      3. distribute-lft-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right), R\right) \]
      4. associate-+l+N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      5. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right) + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      6. associate-*r*N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      7. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\right) \cdot \cos \lambda_1 + \left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      8. fma-defineN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2, \cos \lambda_1, \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)\right)\right), R\right) \]
      9. fma-lowering-fma.f64N/A

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \cos \phi_2 + \left(\sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2\right)\right), R\right) \]
      3. distribute-rgt-outN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      4. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_2, \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      5. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      6. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\cos \lambda_1, \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      8. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right)\right)\right)\right), R\right) \]
      11. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right)\right)\right)\right), R\right) \]
      12. sin-lowering-sin.f6456.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Simplified56.1%

      \[\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)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification65.6%

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

Alternative 16: 65.7% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.5 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \cos \phi_2\right)\\ \mathbf{elif}\;\phi_2 \leq 0.052:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \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} \end{array} \]
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.5e-5)
   (* R (acos (+ (* (sin phi1) (sin phi2)) (* (cos phi1) (cos phi2)))))
   (if (<= phi2 0.052)
     (*
      R
      (acos
       (*
        (cos phi1)
        (+ (* (sin lambda1) (sin lambda2)) (* (cos lambda2) (cos lambda1))))))
     (* R (acos (* (cos phi2) (cos (- lambda2 lambda1))))))))
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.5e-5) {
		tmp = R * acos(((sin(phi1) * sin(phi2)) + (cos(phi1) * cos(phi2))));
	} else if (phi2 <= 0.052) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * 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.
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 (phi2 <= (-1.5d-5)) then
        tmp = r * acos(((sin(phi1) * sin(phi2)) + (cos(phi1) * cos(phi2))))
    else if (phi2 <= 0.052d0) then
        tmp = r * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * 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;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= -1.5e-5) {
		tmp = R * Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + (Math.cos(phi1) * Math.cos(phi2))));
	} else if (phi2 <= 0.052) {
		tmp = R * Math.acos((Math.cos(phi1) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * 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])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= -1.5e-5:
		tmp = R * math.acos(((math.sin(phi1) * math.sin(phi2)) + (math.cos(phi1) * math.cos(phi2))))
	elif phi2 <= 0.052:
		tmp = R * math.acos((math.cos(phi1) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * 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])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= -1.5e-5)
		tmp = Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(cos(phi1) * cos(phi2)))));
	elseif (phi2 <= 0.052)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * 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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= -1.5e-5)
		tmp = R * acos(((sin(phi1) * sin(phi2)) + (cos(phi1) * cos(phi2))));
	elseif (phi2 <= 0.052)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * 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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, -1.5e-5], N[(R * N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 0.052], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $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])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq -1.5 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \cos \phi_2\right)\\

\mathbf{elif}\;\phi_2 \leq 0.052:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \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}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -1.50000000000000004e-5

    1. Initial program 85.7%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in lambda1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right)\right)\right), R\right) \]
    4. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \cos \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f6462.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right), R\right) \]
    5. Simplified62.2%

      \[\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_2}\right) \cdot R \]
    6. Taylor expanded in lambda2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \left(\cos \phi_2 \cdot \cos \phi_1\right)\right)\right), R\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\cos \phi_2, \cos \phi_1\right)\right)\right), R\right) \]
      3. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \phi_1\right)\right)\right), R\right) \]
      4. cos-lowering-cos.f6439.5%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\phi_1\right)\right)\right)\right), R\right) \]
    8. Simplified39.5%

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

    if -1.50000000000000004e-5 < phi2 < 0.0519999999999999976

    1. Initial program 69.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6468.7%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified68.7%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), R\right) \]
      3. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      4. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right), R\right) \]
      5. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      6. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      7. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      8. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      9. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      10. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f6486.5%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr86.5%

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

    if 0.0519999999999999976 < phi2

    1. Initial program 89.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 \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_2, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6450.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified50.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.5 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \cos \phi_2\right)\\ \mathbf{elif}\;\phi_2 \leq 0.052:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \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 17: 63.6% accurate, 1.2× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \phi_1 \cdot \cos \phi_2\\ \mathbf{if}\;\lambda_1 - \lambda_2 \leq -1 \cdot 10^{-7}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\frac{1}{\frac{4}{0 \cdot 2 + 2 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(t\_0 \cdot 2\right)\right)}}\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_0\right)\\ \end{array} \end{array} \]
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 phi1) (cos phi2))))
   (if (<= (- lambda1 lambda2) -1e-7)
     (*
      R
      (acos
       (/
        1.0
        (/
         4.0
         (+ (* 0.0 2.0) (* 2.0 (* (cos (- lambda1 lambda2)) (* t_0 2.0))))))))
     (* R (acos (+ (* (sin phi1) (sin phi2)) t_0))))))
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(phi1) * cos(phi2);
	double tmp;
	if ((lambda1 - lambda2) <= -1e-7) {
		tmp = R * acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (cos((lambda1 - lambda2)) * (t_0 * 2.0)))))));
	} else {
		tmp = R * acos(((sin(phi1) * sin(phi2)) + t_0));
	}
	return tmp;
}
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(phi1) * cos(phi2)
    if ((lambda1 - lambda2) <= (-1d-7)) then
        tmp = r * acos((1.0d0 / (4.0d0 / ((0.0d0 * 2.0d0) + (2.0d0 * (cos((lambda1 - lambda2)) * (t_0 * 2.0d0)))))))
    else
        tmp = r * acos(((sin(phi1) * sin(phi2)) + t_0))
    end if
    code = tmp
end function
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(phi1) * Math.cos(phi2);
	double tmp;
	if ((lambda1 - lambda2) <= -1e-7) {
		tmp = R * Math.acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (Math.cos((lambda1 - lambda2)) * (t_0 * 2.0)))))));
	} else {
		tmp = R * Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + t_0));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos(phi1) * math.cos(phi2)
	tmp = 0
	if (lambda1 - lambda2) <= -1e-7:
		tmp = R * math.acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (math.cos((lambda1 - lambda2)) * (t_0 * 2.0)))))))
	else:
		tmp = R * math.acos(((math.sin(phi1) * math.sin(phi2)) + t_0))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if (Float64(lambda1 - lambda2) <= -1e-7)
		tmp = Float64(R * acos(Float64(1.0 / Float64(4.0 / Float64(Float64(0.0 * 2.0) + Float64(2.0 * Float64(cos(Float64(lambda1 - lambda2)) * Float64(t_0 * 2.0))))))));
	else
		tmp = Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + t_0)));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(phi1) * cos(phi2);
	tmp = 0.0;
	if ((lambda1 - lambda2) <= -1e-7)
		tmp = R * acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (cos((lambda1 - lambda2)) * (t_0 * 2.0)))))));
	else
		tmp = R * acos(((sin(phi1) * sin(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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(lambda1 - lambda2), $MachinePrecision], -1e-7], N[(R * N[ArcCos[N[(1.0 / N[(4.0 / N[(N[(0.0 * 2.0), $MachinePrecision] + N[(2.0 * N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[(t$95$0 * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \cos \phi_2\\
\mathbf{if}\;\lambda_1 - \lambda_2 \leq -1 \cdot 10^{-7}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\frac{1}{\frac{4}{0 \cdot 2 + 2 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(t\_0 \cdot 2\right)\right)}}\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 lambda1 lambda2) < -9.9999999999999995e-8

    1. Initial program 82.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. Step-by-step derivation
      1. sin-multN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-multN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2} + \frac{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}{2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. associate-*l/N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2} + \frac{\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}{2}\right)\right), R\right) \]
      4. frac-addN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}{2 \cdot 2}\right)\right), R\right) \]
      5. clear-numN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{1}{\frac{2 \cdot 2}{\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}}\right)\right), R\right) \]
      6. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \left(\frac{2 \cdot 2}{\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right)\right)\right), R\right) \]
      7. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(\left(2 \cdot 2\right), \left(\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      8. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \left(\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      9. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\left(\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2\right), \left(2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr67.9%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\mathsf{*.f64}\left(\color{blue}{\left(\cos \left(\mathsf{neg}\left(\phi_2\right)\right) - \cos \phi_2\right)}, 2\right), \mathsf{*.f64}\left(2, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), 2\right)\right)\right)\right)\right)\right)\right), R\right) \]
    6. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\mathsf{*.f64}\left(\left(\cos \phi_2 - \cos \phi_2\right), 2\right), \mathsf{*.f64}\left(2, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), 2\right)\right)\right)\right)\right)\right)\right), R\right) \]
      2. +-inverses67.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\mathsf{*.f64}\left(0, 2\right), \mathsf{*.f64}\left(2, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), 2\right)\right)\right)\right)\right)\right)\right), R\right) \]
    7. Simplified67.9%

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

    if -9.9999999999999995e-8 < (-.f64 lambda1 lambda2)

    1. Initial program 76.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 lambda1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right)\right)\right), R\right) \]
    4. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \cos \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f6458.7%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right), R\right) \]
    5. Simplified58.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_2}\right) \cdot R \]
    6. Taylor expanded in lambda2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \left(\cos \phi_2 \cdot \cos \phi_1\right)\right)\right), R\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\cos \phi_2, \cos \phi_1\right)\right)\right), R\right) \]
      3. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \phi_1\right)\right)\right), R\right) \]
      4. cos-lowering-cos.f6441.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\phi_1\right)\right)\right)\right), R\right) \]
    8. Simplified41.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -1 \cdot 10^{-7}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\frac{1}{\frac{4}{0 \cdot 2 + 2 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot 2\right)\right)}}\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \cos \phi_2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 18: 59.8% accurate, 1.2× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq 3.95:\\ \;\;\;\;R \cdot \cos^{-1} \left(\frac{1}{\frac{4}{0 \cdot 2 + 2 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot 2\right)\right)}}\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 3.95)
   (*
    R
    (acos
     (/
      1.0
      (/
       4.0
       (+
        (* 0.0 2.0)
        (*
         2.0
         (* (cos (- lambda1 lambda2)) (* (* (cos phi1) (cos phi2)) 2.0))))))))
   (*
    R
    (acos
     (+ (* (sin lambda1) (sin lambda2)) (* (cos lambda2) (cos lambda1)))))))
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 <= 3.95) {
		tmp = R * acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (cos((lambda1 - lambda2)) * ((cos(phi1) * cos(phi2)) * 2.0)))))));
	} else {
		tmp = R * acos(((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1))));
	}
	return tmp;
}
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 <= 3.95d0) then
        tmp = r * acos((1.0d0 / (4.0d0 / ((0.0d0 * 2.0d0) + (2.0d0 * (cos((lambda1 - lambda2)) * ((cos(phi1) * cos(phi2)) * 2.0d0)))))))
    else
        tmp = r * acos(((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1))))
    end if
    code = tmp
end function
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 <= 3.95) {
		tmp = R * Math.acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (Math.cos((lambda1 - lambda2)) * ((Math.cos(phi1) * Math.cos(phi2)) * 2.0)))))));
	} else {
		tmp = R * Math.acos(((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= 3.95:
		tmp = R * math.acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (math.cos((lambda1 - lambda2)) * ((math.cos(phi1) * math.cos(phi2)) * 2.0)))))))
	else:
		tmp = R * math.acos(((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1))))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= 3.95)
		tmp = Float64(R * acos(Float64(1.0 / Float64(4.0 / Float64(Float64(0.0 * 2.0) + Float64(2.0 * Float64(cos(Float64(lambda1 - lambda2)) * Float64(Float64(cos(phi1) * cos(phi2)) * 2.0))))))));
	else
		tmp = Float64(R * acos(Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1)))));
	end
	return tmp
end
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 <= 3.95)
		tmp = R * acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (cos((lambda1 - lambda2)) * ((cos(phi1) * cos(phi2)) * 2.0)))))));
	else
		tmp = R * acos(((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1))));
	end
	tmp_2 = tmp;
end
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, 3.95], N[(R * N[ArcCos[N[(1.0 / N[(4.0 / N[(N[(0.0 * 2.0), $MachinePrecision] + N[(2.0 * N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq 3.95:\\
\;\;\;\;R \cdot \cos^{-1} \left(\frac{1}{\frac{4}{0 \cdot 2 + 2 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot 2\right)\right)}}\right)\\

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


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

    1. Initial program 84.1%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. sin-multN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-multN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2} + \frac{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}{2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. associate-*l/N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2} + \frac{\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}{2}\right)\right), R\right) \]
      4. frac-addN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}{2 \cdot 2}\right)\right), R\right) \]
      5. clear-numN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{1}{\frac{2 \cdot 2}{\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}}\right)\right), R\right) \]
      6. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \left(\frac{2 \cdot 2}{\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right)\right)\right), R\right) \]
      7. /-lowering-/.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(\left(2 \cdot 2\right), \left(\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      8. metadata-evalN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \left(\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      9. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\left(\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2\right), \left(2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr63.4%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\mathsf{*.f64}\left(\color{blue}{\left(\cos \left(\mathsf{neg}\left(\phi_2\right)\right) - \cos \phi_2\right)}, 2\right), \mathsf{*.f64}\left(2, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), 2\right)\right)\right)\right)\right)\right)\right), R\right) \]
    6. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\mathsf{*.f64}\left(\left(\cos \phi_2 - \cos \phi_2\right), 2\right), \mathsf{*.f64}\left(2, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), 2\right)\right)\right)\right)\right)\right)\right), R\right) \]
      2. +-inverses63.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\mathsf{*.f64}\left(0, 2\right), \mathsf{*.f64}\left(2, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), 2\right)\right)\right)\right)\right)\right)\right), R\right) \]
    7. Simplified63.4%

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

    if 3.9500000000000002 < lambda1

    1. Initial program 63.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 phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6440.3%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified40.3%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f6429.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified29.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 \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right), R\right) \]
      2. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right), R\right) \]
      3. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right), R\right) \]
      4. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 - \lambda_1\right)\right), R\right) \]
      5. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      6. neg-sub0N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(0 - \lambda_1\right)\right)\right), R\right) \]
      7. cos-PI/2N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right) - \lambda_1\right)\right)\right), R\right) \]
      8. associate-+r-N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right) - \lambda_1\right)\right), R\right) \]
      9. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right) \cdot \cos \lambda_1 + \sin \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right) \cdot \sin \lambda_1\right)\right), R\right) \]
      10. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\left(\cos \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right) \cdot \cos \lambda_1\right), \left(\sin \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right) \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      11. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\cos \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right), \cos \lambda_1\right), \left(\sin \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right) \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      12. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right)\right), \cos \lambda_1\right), \left(\sin \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right) \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      13. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{+.f64}\left(\lambda_2, \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right)\right), \cos \lambda_1\right), \left(\sin \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right) \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      14. cos-PI/2N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{+.f64}\left(\lambda_2, 0\right)\right), \cos \lambda_1\right), \left(\sin \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right) \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      15. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{+.f64}\left(\lambda_2, 0\right)\right), \mathsf{cos.f64}\left(\lambda_1\right)\right), \left(\sin \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right) \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      16. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{+.f64}\left(\lambda_2, 0\right)\right), \mathsf{cos.f64}\left(\lambda_1\right)\right), \mathsf{*.f64}\left(\sin \left(\lambda_2 + \cos \left(\frac{\mathsf{PI}\left(\right)}{2}\right)\right), \sin \lambda_1\right)\right)\right), R\right) \]
    10. Applied egg-rr39.4%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\sin \left(\lambda_2 + 0\right) \cdot \sin \lambda_1 + \cos \left(\lambda_2 + 0\right) \cdot \cos \lambda_1\right)\right), R\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\left(\sin \left(\lambda_2 + 0\right) \cdot \sin \lambda_1\right), \left(\cos \left(\lambda_2 + 0\right) \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      3. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \left(\lambda_2 + 0\right)\right), \left(\cos \left(\lambda_2 + 0\right) \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      4. +-rgt-identityN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \left(\lambda_2 + 0\right) \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      5. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right), \left(\cos \left(\lambda_2 + 0\right) \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      6. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \left(\lambda_2 + 0\right) \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      7. sin-lowering-sin.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \left(\lambda_2 + 0\right) \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      8. +-rgt-identityN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right), R\right) \]
      9. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right)\right)\right), R\right) \]
      10. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f6439.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right), R\right) \]
    12. Applied egg-rr39.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq 3.95:\\ \;\;\;\;R \cdot \cos^{-1} \left(\frac{1}{\frac{4}{0 \cdot 2 + 2 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot 2\right)\right)}}\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 19: 58.5% accurate, 1.5× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ R \cdot \cos^{-1} \left(\frac{1}{\frac{4}{0 \cdot 2 + 2 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot 2\right)\right)}}\right) \end{array} \]
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
  (acos
   (/
    1.0
    (/
     4.0
     (+
      (* 0.0 2.0)
      (*
       2.0
       (* (cos (- lambda1 lambda2)) (* (* (cos phi1) (cos phi2)) 2.0)))))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (cos((lambda1 - lambda2)) * ((cos(phi1) * cos(phi2)) * 2.0)))))));
}
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 = r * acos((1.0d0 / (4.0d0 / ((0.0d0 * 2.0d0) + (2.0d0 * (cos((lambda1 - lambda2)) * ((cos(phi1) * cos(phi2)) * 2.0d0)))))))
end function
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.acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (Math.cos((lambda1 - lambda2)) * ((Math.cos(phi1) * Math.cos(phi2)) * 2.0)))))));
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	return R * math.acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (math.cos((lambda1 - lambda2)) * ((math.cos(phi1) * math.cos(phi2)) * 2.0)))))))
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(R * acos(Float64(1.0 / Float64(4.0 / Float64(Float64(0.0 * 2.0) + Float64(2.0 * Float64(cos(Float64(lambda1 - lambda2)) * Float64(Float64(cos(phi1) * cos(phi2)) * 2.0))))))))
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = R * acos((1.0 / (4.0 / ((0.0 * 2.0) + (2.0 * (cos((lambda1 - lambda2)) * ((cos(phi1) * cos(phi2)) * 2.0)))))));
end
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[ArcCos[N[(1.0 / N[(4.0 / N[(N[(0.0 * 2.0), $MachinePrecision] + N[(2.0 * N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
R \cdot \cos^{-1} \left(\frac{1}{\frac{4}{0 \cdot 2 + 2 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot 2\right)\right)}}\right)
\end{array}
Derivation
  1. Initial program 78.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. sin-multN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    2. cos-multN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2} + \frac{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}{2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    3. associate-*l/N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}{2} + \frac{\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}{2}\right)\right), R\right) \]
    4. frac-addN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}{2 \cdot 2}\right)\right), R\right) \]
    5. clear-numN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\frac{1}{\frac{2 \cdot 2}{\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}}\right)\right), R\right) \]
    6. /-lowering-/.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \left(\frac{2 \cdot 2}{\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right)\right)\right), R\right) \]
    7. /-lowering-/.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(\left(2 \cdot 2\right), \left(\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    8. metadata-evalN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \left(\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2 + 2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    9. +-lowering-+.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\left(\left(\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)\right) \cdot 2\right), \left(2 \cdot \left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right)\right), R\right) \]
  4. Applied egg-rr60.4%

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

    \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\mathsf{*.f64}\left(\color{blue}{\left(\cos \left(\mathsf{neg}\left(\phi_2\right)\right) - \cos \phi_2\right)}, 2\right), \mathsf{*.f64}\left(2, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), 2\right)\right)\right)\right)\right)\right)\right), R\right) \]
  6. Step-by-step derivation
    1. cos-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\mathsf{*.f64}\left(\left(\cos \phi_2 - \cos \phi_2\right), 2\right), \mathsf{*.f64}\left(2, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), 2\right)\right)\right)\right)\right)\right)\right), R\right) \]
    2. +-inverses60.4%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(1, \mathsf{/.f64}\left(4, \mathsf{+.f64}\left(\mathsf{*.f64}\left(0, 2\right), \mathsf{*.f64}\left(2, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), 2\right)\right)\right)\right)\right)\right)\right), R\right) \]
  7. Simplified60.4%

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

    \[\leadsto R \cdot \cos^{-1} \left(\frac{1}{\frac{4}{0 \cdot 2 + 2 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot 2\right)\right)}}\right) \]
  9. Add Preprocessing

Alternative 20: 42.7% accurate, 1.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1.7 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{elif}\;\lambda_1 \leq 2.9 \cdot 10^{-273}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -1.7e-5)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (if (<= lambda1 2.9e-273)
     (* R (acos (* (cos phi1) (cos lambda2))))
     (* R (acos (* (cos phi2) (cos lambda2)))))))
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 <= -1.7e-5) {
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	} else if (lambda1 <= 2.9e-273) {
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	} 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.
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 <= (-1.7d-5)) then
        tmp = r * acos((cos(phi1) * cos(lambda1)))
    else if (lambda1 <= 2.9d-273) then
        tmp = r * acos((cos(phi1) * cos(lambda2)))
    else
        tmp = r * acos((cos(phi2) * cos(lambda2)))
    end if
    code = tmp
end function
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 <= -1.7e-5) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda1)));
	} else if (lambda1 <= 2.9e-273) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda2)));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * Math.cos(lambda2)));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -1.7e-5:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda1)))
	elif lambda1 <= 2.9e-273:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda2)))
	else:
		tmp = R * math.acos((math.cos(phi2) * math.cos(lambda2)))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -1.7e-5)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda1))));
	elseif (lambda1 <= 2.9e-273)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda2))));
	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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda1 <= -1.7e-5)
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	elseif (lambda1 <= 2.9e-273)
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -1.7e-5], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda1, 2.9e-273], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda2], $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])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -1.7 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

\mathbf{elif}\;\lambda_1 \leq 2.9 \cdot 10^{-273}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\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 3 regimes
  2. if lambda1 < -1.7e-5

    1. Initial program 73.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6447.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified47.1%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \lambda_1\right)\right), R\right) \]
      2. cos-lowering-cos.f6446.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right), R\right) \]
    8. Simplified46.6%

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

    if -1.7e-5 < lambda1 < 2.89999999999999986e-273

    1. Initial program 92.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6441.7%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified41.7%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \color{blue}{\cos \lambda_2}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. cos-lowering-cos.f6441.7%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right), R\right) \]
    8. Simplified41.7%

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

    if 2.89999999999999986e-273 < lambda1

    1. Initial program 73.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 lambda1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right)\right)\right), R\right) \]
    4. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \cos \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f6449.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right), R\right) \]
    5. Simplified49.9%

      \[\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_2}\right) \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \lambda_2 \cdot \cos \phi_2\right)}\right), R\right) \]
    7. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \lambda_2, \cos \phi_2\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \phi_2\right)\right), R\right) \]
      3. cos-lowering-cos.f6429.8%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\phi_2\right)\right)\right), R\right) \]
    8. Simplified29.8%

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

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

Alternative 21: 50.6% accurate, 2.0× speedup?

\[\begin{array}{l} [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 132000000:\\ \;\;\;\;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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda2 lambda1))))
   (if (<= phi2 132000000.0)
     (* R (acos (* (cos phi1) t_0)))
     (* R (acos (* (cos phi2) t_0))))))
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 <= 132000000.0) {
		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.
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 <= 132000000.0d0) 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;
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 <= 132000000.0) {
		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])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda2 - lambda1))
	tmp = 0
	if phi2 <= 132000000.0:
		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])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda2 - lambda1))
	tmp = 0.0
	if (phi2 <= 132000000.0)
		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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda2 - lambda1));
	tmp = 0.0;
	if (phi2 <= 132000000.0)
		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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, 132000000.0], 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])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
\mathbf{if}\;\phi_2 \leq 132000000:\\
\;\;\;\;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.32e8

    1. Initial program 75.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6449.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified49.4%

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

    if 1.32e8 < phi2

    1. Initial program 89.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 \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_2, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6450.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified50.4%

      \[\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 simplification49.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 132000000:\\ \;\;\;\;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 22: 48.5% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 0.052:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_2\right)\\ \end{array} \end{array} \]
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 0.052)
   (* R (acos (* (cos phi1) (cos (- lambda2 lambda1)))))
   (* R (acos (* (cos phi2) (cos lambda2))))))
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 <= 0.052) {
		tmp = R * acos((cos(phi1) * cos((lambda2 - 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.
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 (phi2 <= 0.052d0) then
        tmp = r * acos((cos(phi1) * cos((lambda2 - lambda1))))
    else
        tmp = r * acos((cos(phi2) * cos(lambda2)))
    end if
    code = tmp
end function
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 (phi2 <= 0.052) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos((lambda2 - 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])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 0.052:
		tmp = R * math.acos((math.cos(phi1) * math.cos((lambda2 - 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])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 0.052)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(Float64(lambda2 - 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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 0.052)
		tmp = R * acos((cos(phi1) * cos((lambda2 - 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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 0.052], 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[Cos[lambda2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 0.052:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < 0.0519999999999999976

    1. Initial program 75.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6449.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified49.4%

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

    if 0.0519999999999999976 < phi2

    1. Initial program 89.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 lambda1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right)\right)\right), R\right) \]
    4. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \cos \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f6470.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right), R\right) \]
    5. Simplified70.2%

      \[\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_2}\right) \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \lambda_2 \cdot \cos \phi_2\right)}\right), R\right) \]
    7. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \lambda_2, \cos \phi_2\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \phi_2\right)\right), R\right) \]
      3. cos-lowering-cos.f6440.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\phi_2\right)\right)\right), R\right) \]
    8. Simplified40.6%

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

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

Alternative 23: 42.9% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 0.000105:\\ \;\;\;\;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 \lambda_2\right)\\ \end{array} \end{array} \]
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.000105)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (* R (acos (* (cos phi2) (cos lambda2))))))
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.000105) {
		tmp = R * acos((cos(phi1) * 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.
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.000105d0) then
        tmp = r * acos((cos(phi1) * 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;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 0.000105) {
		tmp = R * Math.acos((Math.cos(phi1) * 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])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= 0.000105:
		tmp = R * math.acos((math.cos(phi1) * 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])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 0.000105)
		tmp = Float64(R * acos(Float64(cos(phi1) * 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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= 0.000105)
		tmp = R * acos((cos(phi1) * 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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, 0.000105], 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[lambda2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 0.000105:\\
\;\;\;\;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 \lambda_2\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda2 < 1.05e-4

    1. Initial program 80.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6442.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified42.0%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \lambda_1\right)\right), R\right) \]
      2. cos-lowering-cos.f6435.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right), R\right) \]
    8. Simplified35.1%

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

    if 1.05e-4 < lambda2

    1. Initial program 71.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 lambda1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right)\right)\right), R\right) \]
    4. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \cos \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f6471.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right), R\right) \]
    5. Simplified71.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_2}\right) \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \lambda_2 \cdot \cos \phi_2\right)}\right), R\right) \]
    7. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \lambda_2, \cos \phi_2\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \phi_2\right)\right), R\right) \]
      3. cos-lowering-cos.f6451.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\phi_2\right)\right)\right), R\right) \]
    8. Simplified51.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 0.000105:\\ \;\;\;\;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 \lambda_2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 24: 34.5% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.0039:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -0.0039)
   (* R (acos (cos phi1)))
   (* R (acos (* (cos phi2) (cos lambda2))))))
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.0039) {
		tmp = R * acos(cos(phi1));
	} 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.
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 <= (-0.0039d0)) then
        tmp = r * acos(cos(phi1))
    else
        tmp = r * acos((cos(phi2) * cos(lambda2)))
    end if
    code = tmp
end function
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 <= -0.0039) {
		tmp = R * Math.acos(Math.cos(phi1));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * Math.cos(lambda2)));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi1 <= -0.0039:
		tmp = R * math.acos(math.cos(phi1))
	else:
		tmp = R * math.acos((math.cos(phi2) * math.cos(lambda2)))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -0.0039)
		tmp = Float64(R * acos(cos(phi1)));
	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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi1 <= -0.0039)
		tmp = R * acos(cos(phi1));
	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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -0.0039], N[(R * N[ArcCos[N[Cos[phi1], $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])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -0.0039:\\
\;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\

\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 phi1 < -0.0038999999999999998

    1. Initial program 84.8%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6445.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified45.1%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \color{blue}{\left(\cos \left(\mathsf{neg}\left(\lambda_1\right)\right) + -1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_1 + -1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\cos \lambda_1, \left(-1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right), R\right) \]
      3. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(-1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right), R\right) \]
      4. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\mathsf{neg}\left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right), R\right) \]
      5. distribute-rgt-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\lambda_2 \cdot \left(\mathsf{neg}\left(\sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right)\right), R\right) \]
      6. sin-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\lambda_2 \cdot \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\sin \lambda_1\right)\right)\right)\right)\right)\right)\right)\right), R\right) \]
      7. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\lambda_2 \cdot \sin \lambda_1\right)\right)\right)\right), R\right) \]
      8. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\lambda_2, \sin \lambda_1\right)\right)\right)\right), R\right) \]
      9. sin-lowering-sin.f6425.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\lambda_2, \mathsf{sin.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    8. Simplified25.0%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\cos \phi_1}\right), R\right) \]
    10. Step-by-step derivation
      1. cos-lowering-cos.f6425.3%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\phi_1\right)\right), R\right) \]
    11. Simplified25.3%

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]

    if -0.0038999999999999998 < phi1

    1. Initial program 76.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 lambda1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right)\right)\right), R\right) \]
    4. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \cos \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f6450.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right), R\right) \]
    5. Simplified50.2%

      \[\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_2}\right) \cdot R \]
    6. Taylor expanded in phi1 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \lambda_2 \cdot \cos \phi_2\right)}\right), R\right) \]
    7. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \lambda_2, \cos \phi_2\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \phi_2\right)\right), R\right) \]
      3. cos-lowering-cos.f6438.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{cos.f64}\left(\phi_2\right)\right)\right), R\right) \]
    8. Simplified38.4%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.0039:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 25: 27.5% accurate, 2.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -0.0155:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{elif}\;\lambda_1 \leq -8.2 \cdot 10^{-224}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -0.0155)
   (* R (acos (cos lambda1)))
   (if (<= lambda1 -8.2e-224)
     (* R (acos (cos phi1)))
     (* R (acos (cos lambda2))))))
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 <= -0.0155) {
		tmp = R * acos(cos(lambda1));
	} else if (lambda1 <= -8.2e-224) {
		tmp = R * acos(cos(phi1));
	} 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.
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 <= (-0.0155d0)) then
        tmp = r * acos(cos(lambda1))
    else if (lambda1 <= (-8.2d-224)) then
        tmp = r * acos(cos(phi1))
    else
        tmp = r * acos(cos(lambda2))
    end if
    code = tmp
end function
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 <= -0.0155) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else if (lambda1 <= -8.2e-224) {
		tmp = R * Math.acos(Math.cos(phi1));
	} else {
		tmp = R * Math.acos(Math.cos(lambda2));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -0.0155:
		tmp = R * math.acos(math.cos(lambda1))
	elif lambda1 <= -8.2e-224:
		tmp = R * math.acos(math.cos(phi1))
	else:
		tmp = R * math.acos(math.cos(lambda2))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -0.0155)
		tmp = Float64(R * acos(cos(lambda1)));
	elseif (lambda1 <= -8.2e-224)
		tmp = Float64(R * acos(cos(phi1)));
	else
		tmp = Float64(R * acos(cos(lambda2)));
	end
	return tmp
end
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 <= -0.0155)
		tmp = R * acos(cos(lambda1));
	elseif (lambda1 <= -8.2e-224)
		tmp = R * acos(cos(phi1));
	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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -0.0155], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda1, -8.2e-224], N[(R * N[ArcCos[N[Cos[phi1], $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])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -0.0155:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{elif}\;\lambda_1 \leq -8.2 \cdot 10^{-224}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\


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

    1. Initial program 73.7%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6448.5%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified48.5%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f6433.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified33.2%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\cos \lambda_1}\right), R\right) \]
    10. Step-by-step derivation
      1. cos-lowering-cos.f6433.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right)\right), R\right) \]
    11. Simplified33.1%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if -0.0155 < lambda1 < -8.19999999999999972e-224

    1. Initial program 93.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 \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6437.5%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified37.5%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \color{blue}{\left(\cos \left(\mathsf{neg}\left(\lambda_1\right)\right) + -1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_1 + -1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\cos \lambda_1, \left(-1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right), R\right) \]
      3. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(-1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right), R\right) \]
      4. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\mathsf{neg}\left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right), R\right) \]
      5. distribute-rgt-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\lambda_2 \cdot \left(\mathsf{neg}\left(\sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right)\right), R\right) \]
      6. sin-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\lambda_2 \cdot \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\sin \lambda_1\right)\right)\right)\right)\right)\right)\right)\right), R\right) \]
      7. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\lambda_2 \cdot \sin \lambda_1\right)\right)\right)\right), R\right) \]
      8. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\lambda_2, \sin \lambda_1\right)\right)\right)\right), R\right) \]
      9. sin-lowering-sin.f6416.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\lambda_2, \mathsf{sin.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    8. Simplified16.6%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\cos \phi_1}\right), R\right) \]
    10. Step-by-step derivation
      1. cos-lowering-cos.f6420.5%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\phi_1\right)\right), R\right) \]
    11. Simplified20.5%

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]

    if -8.19999999999999972e-224 < lambda1

    1. Initial program 76.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 phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6440.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified40.9%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f6426.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified26.1%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right), R\right) \]
    10. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \lambda_2\right), R\right) \]
      2. cos-lowering-cos.f6418.5%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right)\right), R\right) \]
    11. Simplified18.5%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification22.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -0.0155:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{elif}\;\lambda_1 \leq -8.2 \cdot 10^{-224}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \]
  5. Add Preprocessing

Alternative 26: 32.8% accurate, 2.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -0.02:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \end{array} \end{array} \]
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 lambda2) -0.02)
   (* R (acos (cos (- lambda1 lambda2))))
   (* R (acos (cos phi1)))))
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 - lambda2) <= -0.02) {
		tmp = R * acos(cos((lambda1 - lambda2)));
	} else {
		tmp = R * acos(cos(phi1));
	}
	return tmp;
}
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 - lambda2) <= (-0.02d0)) then
        tmp = r * acos(cos((lambda1 - lambda2)))
    else
        tmp = r * acos(cos(phi1))
    end if
    code = tmp
end function
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 - lambda2) <= -0.02) {
		tmp = R * Math.acos(Math.cos((lambda1 - lambda2)));
	} else {
		tmp = R * Math.acos(Math.cos(phi1));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if (lambda1 - lambda2) <= -0.02:
		tmp = R * math.acos(math.cos((lambda1 - lambda2)))
	else:
		tmp = R * math.acos(math.cos(phi1))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (Float64(lambda1 - lambda2) <= -0.02)
		tmp = Float64(R * acos(cos(Float64(lambda1 - lambda2))));
	else
		tmp = Float64(R * acos(cos(phi1)));
	end
	return tmp
end
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 - lambda2) <= -0.02)
		tmp = R * acos(cos((lambda1 - lambda2)));
	else
		tmp = R * acos(cos(phi1));
	end
	tmp_2 = tmp;
end
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[(lambda1 - lambda2), $MachinePrecision], -0.02], N[(R * N[ArcCos[N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[Cos[phi1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 - \lambda_2 \leq -0.02:\\
\;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 lambda1 lambda2) < -0.0200000000000000004

    1. Initial program 82.3%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6450.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified50.2%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f6436.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified36.1%

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

    if -0.0200000000000000004 < (-.f64 lambda1 lambda2)

    1. Initial program 76.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6437.2%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified37.2%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \color{blue}{\left(\cos \left(\mathsf{neg}\left(\lambda_1\right)\right) + -1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_1 + -1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      2. +-lowering-+.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\cos \lambda_1, \left(-1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right), R\right) \]
      3. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(-1 \cdot \left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right), R\right) \]
      4. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\mathsf{neg}\left(\lambda_2 \cdot \sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right), R\right) \]
      5. distribute-rgt-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\lambda_2 \cdot \left(\mathsf{neg}\left(\sin \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right)\right)\right), R\right) \]
      6. sin-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\lambda_2 \cdot \left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\sin \lambda_1\right)\right)\right)\right)\right)\right)\right)\right), R\right) \]
      7. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\lambda_2 \cdot \sin \lambda_1\right)\right)\right)\right), R\right) \]
      8. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\lambda_2, \sin \lambda_1\right)\right)\right)\right), R\right) \]
      9. sin-lowering-sin.f6423.3%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\lambda_2, \mathsf{sin.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    8. Simplified23.3%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\cos \phi_1}\right), R\right) \]
    10. Step-by-step derivation
      1. cos-lowering-cos.f6417.7%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\phi_1\right)\right), R\right) \]
    11. Simplified17.7%

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification24.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -0.02:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \end{array} \]
  5. Add Preprocessing

Alternative 27: 20.0% accurate, 2.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -100000000:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\ \end{array} \end{array} \]
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 lambda2) -100000000.0)
   (* R (acos (cos lambda1)))
   (* (- lambda2 lambda1) R)))
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 - lambda2) <= -100000000.0) {
		tmp = R * acos(cos(lambda1));
	} else {
		tmp = (lambda2 - lambda1) * R;
	}
	return tmp;
}
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 - lambda2) <= (-100000000.0d0)) then
        tmp = r * acos(cos(lambda1))
    else
        tmp = (lambda2 - lambda1) * r
    end if
    code = tmp
end function
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 - lambda2) <= -100000000.0) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else {
		tmp = (lambda2 - lambda1) * R;
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if (lambda1 - lambda2) <= -100000000.0:
		tmp = R * math.acos(math.cos(lambda1))
	else:
		tmp = (lambda2 - lambda1) * R
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (Float64(lambda1 - lambda2) <= -100000000.0)
		tmp = Float64(R * acos(cos(lambda1)));
	else
		tmp = Float64(Float64(lambda2 - lambda1) * R);
	end
	return tmp
end
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 - lambda2) <= -100000000.0)
		tmp = R * acos(cos(lambda1));
	else
		tmp = (lambda2 - lambda1) * R;
	end
	tmp_2 = tmp;
end
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[(lambda1 - lambda2), $MachinePrecision], -100000000.0], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(lambda2 - lambda1), $MachinePrecision] * R), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 - \lambda_2 \leq -100000000:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{else}:\\
\;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 lambda1 lambda2) < -1e8

    1. Initial program 82.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 \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6449.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified49.4%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f6434.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified34.9%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\cos \lambda_1}\right), R\right) \]
    10. Step-by-step derivation
      1. cos-lowering-cos.f6421.5%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right)\right), R\right) \]
    11. Simplified21.5%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if -1e8 < (-.f64 lambda1 lambda2)

    1. Initial program 77.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 \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6437.8%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified37.8%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f6420.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified20.6%

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

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

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right), R\right) \]
      3. *-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right), R\right) \]
      4. cos-diffN/A

        \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \cos \left(\lambda_2 - \lambda_1\right), R\right) \]
      5. acos-cos-sN/A

        \[\leadsto \mathsf{*.f64}\left(\left(\lambda_2 - \lambda_1\right), R\right) \]
      6. --lowering--.f646.3%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right), R\right) \]
    10. Applied egg-rr6.3%

      \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification11.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -100000000:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\ \end{array} \]
  5. Add Preprocessing

Alternative 28: 26.1% accurate, 2.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -2.9 \cdot 10^{-6}:\\ \;\;\;\;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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -2.9e-6)
   (* R (acos (cos lambda1)))
   (* R (acos (cos lambda2)))))
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 <= -2.9e-6) {
		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.
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 <= (-2.9d-6)) 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;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -2.9e-6) {
		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])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -2.9e-6:
		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])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -2.9e-6)
		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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda1 <= -2.9e-6)
		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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -2.9e-6], 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])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -2.9 \cdot 10^{-6}:\\
\;\;\;\;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 lambda1 < -2.9000000000000002e-6

    1. Initial program 73.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6447.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified47.1%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f6432.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified32.4%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\cos \lambda_1}\right), R\right) \]
    10. Step-by-step derivation
      1. cos-lowering-cos.f6432.1%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right)\right), R\right) \]
    11. Simplified32.1%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if -2.9000000000000002e-6 < lambda1

    1. Initial program 80.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      2. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      3. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
      7. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      8. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      10. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      11. unsub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6440.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    5. Simplified40.4%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      2. remove-double-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
      3. distribute-neg-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
      4. +-commutativeN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      5. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
      6. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
      7. cos-lowering-cos.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
      8. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. sub-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
      10. --lowering--.f6423.7%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
    8. Simplified23.7%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right), R\right) \]
    10. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \lambda_2\right), R\right) \]
      2. cos-lowering-cos.f6418.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right)\right), R\right) \]
    11. Simplified18.0%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification21.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -2.9 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \]
  5. Add Preprocessing

Alternative 29: 9.1% accurate, 122.6× speedup?

\[\begin{array}{l} [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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* (- lambda2 lambda1) R))
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.
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;
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])
def code(R, lambda1, lambda2, phi1, phi2):
	return (lambda2 - lambda1) * R
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])){:}
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.
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])\\
\\
\left(\lambda_2 - \lambda_1\right) \cdot R
\end{array}
Derivation
  1. Initial program 78.9%

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Add Preprocessing
  3. Taylor expanded in phi2 around 0

    \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
  4. Step-by-step derivation
    1. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    2. cos-lowering-cos.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    3. sub-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    4. remove-double-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    5. mul-1-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    6. distribute-neg-inN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. +-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
    8. cos-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
    9. cos-lowering-cos.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
    10. mul-1-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
    11. unsub-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
    12. --lowering--.f6441.9%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
  5. Simplified41.9%

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
    2. remove-double-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
    3. distribute-neg-inN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
    4. +-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    5. neg-mul-1N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
    6. cos-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
    7. cos-lowering-cos.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
    8. neg-mul-1N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    9. sub-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    10. --lowering--.f6425.7%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
  8. Simplified25.7%

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

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

      \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \sin \lambda_2\right), R\right) \]
    3. *-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right), R\right) \]
    4. cos-diffN/A

      \[\leadsto \mathsf{*.f64}\left(\cos^{-1} \cos \left(\lambda_2 - \lambda_1\right), R\right) \]
    5. acos-cos-sN/A

      \[\leadsto \mathsf{*.f64}\left(\left(\lambda_2 - \lambda_1\right), R\right) \]
    6. --lowering--.f645.9%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right), R\right) \]
  10. Applied egg-rr5.9%

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

Alternative 30: 3.5% accurate, 204.3× speedup?

\[\begin{array}{l} [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.
(FPCore (R lambda1 lambda2 phi1 phi2) :precision binary64 (* lambda1 R))
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.
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;
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])
def code(R, lambda1, lambda2, phi1, phi2):
	return lambda1 * R
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])){:}
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.
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])\\
\\
\lambda_1 \cdot R
\end{array}
Derivation
  1. Initial program 78.9%

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Add Preprocessing
  3. Taylor expanded in phi2 around 0

    \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right), R\right) \]
  4. Step-by-step derivation
    1. *-lowering-*.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    2. cos-lowering-cos.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    3. sub-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    4. remove-double-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    5. mul-1-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\left(\mathsf{neg}\left(-1 \cdot \lambda_1\right)\right) + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    6. distribute-neg-inN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. +-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
    8. cos-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right), R\right) \]
    9. cos-lowering-cos.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
    10. mul-1-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
    11. unsub-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
    12. --lowering--.f6441.9%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
  5. Simplified41.9%

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
    2. remove-double-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right), R\right) \]
    3. distribute-neg-inN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)\right), R\right) \]
    4. +-commutativeN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    5. neg-mul-1N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
    6. cos-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
    7. cos-lowering-cos.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
    8. neg-mul-1N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    9. sub-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    10. --lowering--.f6425.7%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
  8. Simplified25.7%

    \[\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 \lambda_1 \cdot \color{blue}{R} \]
    2. *-lowering-*.f645.3%

      \[\leadsto \mathsf{*.f64}\left(\lambda_1, \color{blue}{R}\right) \]
  11. Simplified5.3%

    \[\leadsto \color{blue}{\lambda_1 \cdot R} \]
  12. Add Preprocessing

Reproduce

?
herbie shell --seed 2024164 
(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))