Spherical law of cosines

Percentage Accurate: 73.4% → 93.8%
Time: 23.0s
Alternatives: 23
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 23 alternatives:

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

Initial Program: 73.4% 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: 93.8% 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}\;t\_0 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) \leq 1:\\ \;\;\;\;\cos^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\lambda_2 - \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
 (let* ((t_0 (* (sin phi1) (sin phi2))))
   (if (<= (+ t_0 (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))) 1.0)
     (*
      (acos
       (+
        t_0
        (*
         (cos phi1)
         (*
          (cos phi2)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda1) (cos lambda2)))))))
      R)
     (* R (- 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 ((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))) <= 1.0) {
		tmp = acos((t_0 + (cos(phi1) * (cos(phi2) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2))))))) * R;
	} else {
		tmp = R * (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) :: t_0
    real(8) :: tmp
    t_0 = sin(phi1) * sin(phi2)
    if ((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))) <= 1.0d0) then
        tmp = acos((t_0 + (cos(phi1) * (cos(phi2) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2))))))) * r
    else
        tmp = r * (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 t_0 = Math.sin(phi1) * Math.sin(phi2);
	double tmp;
	if ((t_0 + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2)))) <= 1.0) {
		tmp = Math.acos((t_0 + (Math.cos(phi1) * (Math.cos(phi2) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda1) * Math.cos(lambda2))))))) * R;
	} else {
		tmp = R * (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 (t_0 + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2)))) <= 1.0:
		tmp = math.acos((t_0 + (math.cos(phi1) * (math.cos(phi2) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda1) * math.cos(lambda2))))))) * R
	else:
		tmp = R * (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 (Float64(t_0 + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2)))) <= 1.0)
		tmp = Float64(acos(Float64(t_0 + Float64(cos(phi1) * Float64(cos(phi2) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda1) * cos(lambda2))))))) * R);
	else
		tmp = Float64(R * 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 ((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))) <= 1.0)
		tmp = acos((t_0 + (cos(phi1) * (cos(phi2) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2))))))) * R;
	else
		tmp = R * (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[N[(t$95$0 + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 1.0], N[(N[ArcCos[N[(t$95$0 + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(R * N[(lambda2 - lambda1), $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}\;t\_0 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) \leq 1:\\
\;\;\;\;\cos^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left(\lambda_2 - \lambda_1\right)\\


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

    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. 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.f6493.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{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-rr93.1%

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

      \[\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 \left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)}\right)\right), R\right) \]
    6. Step-by-step derivation
      1. *-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_1, \left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      2. 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_1\right), \left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \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(\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, \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. 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_1\right), \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)\right)\right), R\right) \]
      5. +-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{cos.f64}\left(\phi_1\right), \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)\right)\right), R\right) \]
      6. *-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{cos.f64}\left(\phi_1\right), \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)\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{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{+.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)\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{cos.f64}\left(\phi_1\right), \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)\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(\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{+.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)\right)\right), R\right) \]
      10. 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{cos.f64}\left(\phi_1\right), \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)\right)\right), R\right) \]
      11. sin-lowering-sin.f6493.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_1\right), \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)\right)\right), R\right) \]
    7. Simplified93.1%

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

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

    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--.f6442.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. Simplified42.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 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--.f6424.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. Simplified24.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--.f645.7%

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

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

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

Alternative 2: 83.0% accurate, 0.8× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \mathsf{fma}\left(0 - \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), R, \frac{R \cdot \pi}{2}\right)\\ \mathbf{if}\;\phi_2 \leq -2.9 \cdot 10^{-8}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 9.8 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right) + \cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\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
         (fma
          (-
           0.0
           (asin
            (+
             (* (sin phi1) (sin phi2))
             (* (cos phi2) (* (cos phi1) (cos (- lambda2 lambda1)))))))
          R
          (/ (* R PI) 2.0))))
   (if (<= phi2 -2.9e-8)
     t_0
     (if (<= phi2 9.8e-11)
       (*
        R
        (acos
         (+
          (* (cos phi1) (* (sin lambda1) (sin lambda2)))
          (* (cos phi1) (* (cos lambda1) (cos lambda2))))))
       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 = fma((0.0 - asin(((sin(phi1) * sin(phi2)) + (cos(phi2) * (cos(phi1) * cos((lambda2 - lambda1))))))), R, ((R * ((double) M_PI)) / 2.0));
	double tmp;
	if (phi2 <= -2.9e-8) {
		tmp = t_0;
	} else if (phi2 <= 9.8e-11) {
		tmp = R * acos(((cos(phi1) * (sin(lambda1) * sin(lambda2))) + (cos(phi1) * (cos(lambda1) * cos(lambda2)))));
	} 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 = fma(Float64(0.0 - asin(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(cos(phi2) * Float64(cos(phi1) * cos(Float64(lambda2 - lambda1))))))), R, Float64(Float64(R * pi) / 2.0))
	tmp = 0.0
	if (phi2 <= -2.9e-8)
		tmp = t_0;
	elseif (phi2 <= 9.8e-11)
		tmp = Float64(R * acos(Float64(Float64(cos(phi1) * Float64(sin(lambda1) * sin(lambda2))) + Float64(cos(phi1) * Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = t_0;
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[(0.0 - N[ArcSin[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * R + N[(N[(R * Pi), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -2.9e-8], t$95$0, If[LessEqual[phi2, 9.8e-11], N[(R * N[ArcCos[N[(N[(N[Cos[phi1], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $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 := \mathsf{fma}\left(0 - \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), R, \frac{R \cdot \pi}{2}\right)\\
\mathbf{if}\;\phi_2 \leq -2.9 \cdot 10^{-8}:\\
\;\;\;\;t\_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -2.9000000000000002e-8 or 9.7999999999999998e-11 < phi2

    1. Initial program 72.1%

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

        \[\leadsto \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. distribute-rgt-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\sin \phi_1 \cdot \sin \phi_2 + \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)\right)\right), R\right) \]
      3. associate-+r+N/A

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(0 - \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), R, \frac{R \cdot \pi}{2}\right)} \]

    if -2.9000000000000002e-8 < phi2 < 9.7999999999999998e-11

    1. Initial program 65.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--.f6465.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. Simplified65.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(\left(\cos \phi_1 \cdot \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(\left(\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      3. *-commutativeN/A

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{cos.f64}\left(\phi_1\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right), \cos \phi_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{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{cos.f64}\left(\phi_1\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right), \cos \phi_1\right)\right)\right), R\right) \]
      16. cos-lowering-cos.f64N/A

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

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

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

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

Alternative 3: 83.0% accurate, 0.8× 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.35 \cdot 10^{-8}:\\ \;\;\;\;\frac{R \cdot \pi}{2} - R \cdot \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)\\ \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\right) + \cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \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
 (if (<= phi2 -1.35e-8)
   (-
    (/ (* R PI) 2.0)
    (*
     R
     (asin
      (+
       (* (sin phi1) (sin phi2))
       (* (cos phi2) (* (cos phi1) (cos (- lambda2 lambda1))))))))
   (if (<= phi2 8e-11)
     (*
      R
      (acos
       (+
        (* (cos phi1) (* (sin lambda1) (sin lambda2)))
        (* (cos phi1) (* (cos lambda1) (cos lambda2))))))
     (*
      R
      (acos
       (fma
        (sin phi2)
        (sin phi1)
        (* (cos phi2) (* (cos phi1) (cos (- lambda1 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 <= -1.35e-8) {
		tmp = ((R * ((double) M_PI)) / 2.0) - (R * asin(((sin(phi1) * sin(phi2)) + (cos(phi2) * (cos(phi1) * cos((lambda2 - lambda1)))))));
	} else if (phi2 <= 8e-11) {
		tmp = R * acos(((cos(phi1) * (sin(lambda1) * sin(lambda2))) + (cos(phi1) * (cos(lambda1) * cos(lambda2)))));
	} else {
		tmp = R * acos(fma(sin(phi2), sin(phi1), (cos(phi2) * (cos(phi1) * cos((lambda1 - 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 <= -1.35e-8)
		tmp = Float64(Float64(Float64(R * pi) / 2.0) - Float64(R * asin(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(cos(phi2) * Float64(cos(phi1) * cos(Float64(lambda2 - lambda1))))))));
	elseif (phi2 <= 8e-11)
		tmp = Float64(R * acos(Float64(Float64(cos(phi1) * Float64(sin(lambda1) * sin(lambda2))) + Float64(cos(phi1) * Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = Float64(R * acos(fma(sin(phi2), sin(phi1), Float64(cos(phi2) * Float64(cos(phi1) * cos(Float64(lambda1 - 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_] := If[LessEqual[phi2, -1.35e-8], N[(N[(N[(R * Pi), $MachinePrecision] / 2.0), $MachinePrecision] - N[(R * N[ArcSin[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 8e-11], N[(R * N[ArcCos[N[(N[(N[Cos[phi1], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda1 - 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}
\mathbf{if}\;\phi_2 \leq -1.35 \cdot 10^{-8}:\\
\;\;\;\;\frac{R \cdot \pi}{2} - R \cdot \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)\\

\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\right) + \cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\

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


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

    1. Initial program 64.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. 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. distribute-rgt-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\sin \phi_1 \cdot \sin \phi_2 + \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)\right)\right), R\right) \]
      3. associate-+r+N/A

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

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

      \[\leadsto \color{blue}{\frac{R \cdot \pi}{2} - R \cdot \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)} \]

    if -1.35000000000000001e-8 < phi2 < 7.99999999999999952e-11

    1. Initial program 65.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--.f6465.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. Simplified65.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(\left(\cos \phi_1 \cdot \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(\left(\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right), R\right) \]
      3. *-commutativeN/A

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{cos.f64}\left(\phi_1\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\cos \lambda_2, \cos \lambda_1\right), \cos \phi_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{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{cos.f64}\left(\phi_1\right)\right), \mathsf{*.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \cos \lambda_1\right), \cos \phi_1\right)\right)\right), R\right) \]
      16. cos-lowering-cos.f64N/A

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

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

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

    if 7.99999999999999952e-11 < phi2

    1. Initial program 78.3%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-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. *-commutativeN/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_2 \cdot \cos \phi_1\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      7. 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_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      8. *-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_2, \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. 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_2\right), \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      10. *-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_2\right), \mathsf{*.f64}\left(\cos \phi_1, \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_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      12. 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_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      13. --lowering--.f6478.2%

        \[\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_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr78.2%

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

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

Alternative 4: 83.0% accurate, 0.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq -2.15 \cdot 10^{-8}:\\ \;\;\;\;\frac{R \cdot \pi}{2} - R \cdot \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)\\ \mathbf{elif}\;\phi_2 \leq 6 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \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
 (if (<= phi2 -2.15e-8)
   (-
    (/ (* R PI) 2.0)
    (*
     R
     (asin
      (+
       (* (sin phi1) (sin phi2))
       (* (cos phi2) (* (cos phi1) (cos (- lambda2 lambda1))))))))
   (if (<= phi2 6e-11)
     (*
      R
      (acos
       (*
        (cos phi1)
        (+ (* (sin lambda1) (sin lambda2)) (* (cos lambda1) (cos lambda2))))))
     (*
      R
      (acos
       (fma
        (sin phi2)
        (sin phi1)
        (* (cos phi2) (* (cos phi1) (cos (- lambda1 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 <= -2.15e-8) {
		tmp = ((R * ((double) M_PI)) / 2.0) - (R * asin(((sin(phi1) * sin(phi2)) + (cos(phi2) * (cos(phi1) * cos((lambda2 - lambda1)))))));
	} else if (phi2 <= 6e-11) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	} else {
		tmp = R * acos(fma(sin(phi2), sin(phi1), (cos(phi2) * (cos(phi1) * cos((lambda1 - 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 <= -2.15e-8)
		tmp = Float64(Float64(Float64(R * pi) / 2.0) - Float64(R * asin(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(cos(phi2) * Float64(cos(phi1) * cos(Float64(lambda2 - lambda1))))))));
	elseif (phi2 <= 6e-11)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = Float64(R * acos(fma(sin(phi2), sin(phi1), Float64(cos(phi2) * Float64(cos(phi1) * cos(Float64(lambda1 - 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_] := If[LessEqual[phi2, -2.15e-8], N[(N[(N[(R * Pi), $MachinePrecision] / 2.0), $MachinePrecision] - N[(R * N[ArcSin[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 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[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda1 - 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}
\mathbf{if}\;\phi_2 \leq -2.15 \cdot 10^{-8}:\\
\;\;\;\;\frac{R \cdot \pi}{2} - R \cdot \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)\\

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

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


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

    1. Initial program 64.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. 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. distribute-rgt-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\sin \phi_1 \cdot \sin \phi_2 + \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)\right)\right), R\right) \]
      3. associate-+r+N/A

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

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

      \[\leadsto \color{blue}{\frac{R \cdot \pi}{2} - R \cdot \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)} \]

    if -2.1500000000000001e-8 < phi2 < 6e-11

    1. Initial program 65.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--.f6465.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. Simplified65.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_1 \cdot \cos \lambda_2 + \sin \lambda_2 \cdot \sin \lambda_1\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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\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.3%

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

      \[\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 6e-11 < phi2

    1. Initial program 78.3%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-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. *-commutativeN/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_2 \cdot \cos \phi_1\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      7. 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_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      8. *-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_2, \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      9. 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_2\right), \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      10. *-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_2\right), \mathsf{*.f64}\left(\cos \phi_1, \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_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right), R\right) \]
      12. 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_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      13. --lowering--.f6478.2%

        \[\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_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr78.2%

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

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

Alternative 5: 83.0% 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 -7.4 \cdot 10^{-9}:\\ \;\;\;\;\frac{R \cdot \pi}{2} - R \cdot \sin^{-1} \left(t\_0 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 1.05 \cdot 10^{-10}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t\_0 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \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))))
   (if (<= phi2 -7.4e-9)
     (-
      (/ (* R PI) 2.0)
      (*
       R
       (asin (+ t_0 (* (cos phi2) (* (cos phi1) (cos (- lambda2 lambda1))))))))
     (if (<= phi2 1.05e-10)
       (*
        R
        (acos
         (*
          (cos phi1)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda1) (cos lambda2))))))
       (*
        R
        (acos
         (+ t_0 (* (* (cos phi1) (cos phi2)) (cos (- lambda1 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 tmp;
	if (phi2 <= -7.4e-9) {
		tmp = ((R * ((double) M_PI)) / 2.0) - (R * asin((t_0 + (cos(phi2) * (cos(phi1) * cos((lambda2 - lambda1)))))));
	} else if (phi2 <= 1.05e-10) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	} else {
		tmp = R * acos((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	}
	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 <= -7.4e-9) {
		tmp = ((R * Math.PI) / 2.0) - (R * Math.asin((t_0 + (Math.cos(phi2) * (Math.cos(phi1) * Math.cos((lambda2 - lambda1)))))));
	} else if (phi2 <= 1.05e-10) {
		tmp = R * Math.acos((Math.cos(phi1) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda1) * Math.cos(lambda2)))));
	} else {
		tmp = R * Math.acos((t_0 + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2)))));
	}
	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 <= -7.4e-9:
		tmp = ((R * math.pi) / 2.0) - (R * math.asin((t_0 + (math.cos(phi2) * (math.cos(phi1) * math.cos((lambda2 - lambda1)))))))
	elif phi2 <= 1.05e-10:
		tmp = R * math.acos((math.cos(phi1) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda1) * math.cos(lambda2)))))
	else:
		tmp = R * math.acos((t_0 + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - 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))
	tmp = 0.0
	if (phi2 <= -7.4e-9)
		tmp = Float64(Float64(Float64(R * pi) / 2.0) - Float64(R * asin(Float64(t_0 + Float64(cos(phi2) * Float64(cos(phi1) * cos(Float64(lambda2 - lambda1))))))));
	elseif (phi2 <= 1.05e-10)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = Float64(R * acos(Float64(t_0 + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - 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)
	t_0 = sin(phi1) * sin(phi2);
	tmp = 0.0;
	if (phi2 <= -7.4e-9)
		tmp = ((R * pi) / 2.0) - (R * asin((t_0 + (cos(phi2) * (cos(phi1) * cos((lambda2 - lambda1)))))));
	elseif (phi2 <= 1.05e-10)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	else
		tmp = R * acos((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -7.4e-9], N[(N[(N[(R * Pi), $MachinePrecision] / 2.0), $MachinePrecision] - N[(R * 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], If[LessEqual[phi2, 1.05e-10], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * 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]), $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 -7.4 \cdot 10^{-9}:\\
\;\;\;\;\frac{R \cdot \pi}{2} - R \cdot \sin^{-1} \left(t\_0 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\\

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

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


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

    1. Initial program 64.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. 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. distribute-rgt-inN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\sin \phi_1 \cdot \sin \phi_2 + \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)\right)\right), R\right) \]
      3. associate-+r+N/A

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

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

      \[\leadsto \color{blue}{\frac{R \cdot \pi}{2} - R \cdot \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)} \]

    if -7.4e-9 < phi2 < 1.05e-10

    1. Initial program 65.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--.f6465.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. Simplified65.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_1 \cdot \cos \lambda_2 + \sin \lambda_2 \cdot \sin \lambda_1\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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\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.3%

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

      \[\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.05e-10 < phi2

    1. Initial program 78.3%

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

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

Alternative 6: 83.0% 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 -2.5 \cdot 10^{-9}:\\ \;\;\;\;\frac{R}{\frac{2}{\pi}} - R \cdot \sin^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 1.05 \cdot 10^{-10}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t\_0 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \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))))
   (if (<= phi2 -2.5e-9)
     (-
      (/ R (/ 2.0 PI))
      (*
       R
       (asin (+ t_0 (* (cos phi1) (* (cos phi2) (cos (- lambda2 lambda1))))))))
     (if (<= phi2 1.05e-10)
       (*
        R
        (acos
         (*
          (cos phi1)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda1) (cos lambda2))))))
       (*
        R
        (acos
         (+ t_0 (* (* (cos phi1) (cos phi2)) (cos (- lambda1 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 tmp;
	if (phi2 <= -2.5e-9) {
		tmp = (R / (2.0 / ((double) M_PI))) - (R * asin((t_0 + (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1)))))));
	} else if (phi2 <= 1.05e-10) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	} else {
		tmp = R * acos((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	}
	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 <= -2.5e-9) {
		tmp = (R / (2.0 / Math.PI)) - (R * Math.asin((t_0 + (Math.cos(phi1) * (Math.cos(phi2) * Math.cos((lambda2 - lambda1)))))));
	} else if (phi2 <= 1.05e-10) {
		tmp = R * Math.acos((Math.cos(phi1) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda1) * Math.cos(lambda2)))));
	} else {
		tmp = R * Math.acos((t_0 + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2)))));
	}
	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 <= -2.5e-9:
		tmp = (R / (2.0 / math.pi)) - (R * math.asin((t_0 + (math.cos(phi1) * (math.cos(phi2) * math.cos((lambda2 - lambda1)))))))
	elif phi2 <= 1.05e-10:
		tmp = R * math.acos((math.cos(phi1) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda1) * math.cos(lambda2)))))
	else:
		tmp = R * math.acos((t_0 + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - 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))
	tmp = 0.0
	if (phi2 <= -2.5e-9)
		tmp = Float64(Float64(R / Float64(2.0 / pi)) - Float64(R * asin(Float64(t_0 + Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda2 - lambda1))))))));
	elseif (phi2 <= 1.05e-10)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = Float64(R * acos(Float64(t_0 + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - 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)
	t_0 = sin(phi1) * sin(phi2);
	tmp = 0.0;
	if (phi2 <= -2.5e-9)
		tmp = (R / (2.0 / pi)) - (R * asin((t_0 + (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1)))))));
	elseif (phi2 <= 1.05e-10)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	else
		tmp = R * acos((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -2.5e-9], N[(N[(R / N[(2.0 / Pi), $MachinePrecision]), $MachinePrecision] - N[(R * N[ArcSin[N[(t$95$0 + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 1.05e-10], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * 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]), $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 -2.5 \cdot 10^{-9}:\\
\;\;\;\;\frac{R}{\frac{2}{\pi}} - R \cdot \sin^{-1} \left(t\_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\\

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

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


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

    1. Initial program 64.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. 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.f6499.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), \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-rr99.0%

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

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

    if -2.5000000000000001e-9 < phi2 < 1.05e-10

    1. Initial program 65.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--.f6465.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. Simplified65.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_1 \cdot \cos \lambda_2 + \sin \lambda_2 \cdot \sin \lambda_1\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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\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.3%

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

      \[\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.05e-10 < phi2

    1. Initial program 78.3%

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

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

Alternative 7: 83.0% 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 \left(\lambda_1 - \lambda_2\right)\right)\\ \mathbf{if}\;\phi_2 \leq -5.1 \cdot 10^{-8}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 9.2 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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
         (*
          R
          (acos
           (+
            (* (sin phi1) (sin phi2))
            (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2))))))))
   (if (<= phi2 -5.1e-8)
     t_0
     (if (<= phi2 9.2e-11)
       (*
        R
        (acos
         (*
          (cos phi1)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda1) (cos lambda2))))))
       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 - lambda2)))));
	double tmp;
	if (phi2 <= -5.1e-8) {
		tmp = t_0;
	} else if (phi2 <= 9.2e-11) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	} 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 = r * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))))
    if (phi2 <= (-5.1d-8)) then
        tmp = t_0
    else if (phi2 <= 9.2d-11) then
        tmp = r * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))))
    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 - lambda2)))));
	double tmp;
	if (phi2 <= -5.1e-8) {
		tmp = t_0;
	} else if (phi2 <= 9.2e-11) {
		tmp = R * Math.acos((Math.cos(phi1) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda1) * Math.cos(lambda2)))));
	} 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 - lambda2)))))
	tmp = 0
	if phi2 <= -5.1e-8:
		tmp = t_0
	elif phi2 <= 9.2e-11:
		tmp = R * math.acos((math.cos(phi1) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda1) * math.cos(lambda2)))))
	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(Float64(lambda1 - lambda2))))))
	tmp = 0.0
	if (phi2 <= -5.1e-8)
		tmp = t_0;
	elseif (phi2 <= 9.2e-11)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda1) * cos(lambda2))))));
	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 - lambda2)))));
	tmp = 0.0;
	if (phi2 <= -5.1e-8)
		tmp = t_0;
	elseif (phi2 <= 9.2e-11)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	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[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -5.1e-8], t$95$0, If[LessEqual[phi2, 9.2e-11], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $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 := R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\
\mathbf{if}\;\phi_2 \leq -5.1 \cdot 10^{-8}:\\
\;\;\;\;t\_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -5.10000000000000001e-8 or 9.20000000000000054e-11 < phi2

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

    if -5.10000000000000001e-8 < phi2 < 9.20000000000000054e-11

    1. Initial program 65.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--.f6465.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. Simplified65.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_1 \cdot \cos \lambda_2 + \sin \lambda_2 \cdot \sin \lambda_1\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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\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.3%

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

      \[\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 simplification79.8%

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

Alternative 8: 74.8% 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_1 \cdot \cos \lambda_2\\ \mathbf{if}\;\phi_1 \leq -1.18 \cdot 10^{-20}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\ \mathbf{elif}\;\phi_1 \leq 3.1 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\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} \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 lambda1) (cos lambda2)))))
   (if (<= phi1 -1.18e-20)
     (* R (acos (* (cos phi1) t_0)))
     (if (<= phi1 3.1e-5)
       (* R (acos (* (cos phi2) t_0)))
       (*
        R
        (acos
         (+
          (* (sin phi1) (sin phi2))
          (* (* (cos phi1) (cos phi2)) (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(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2));
	double tmp;
	if (phi1 <= -1.18e-20) {
		tmp = R * acos((cos(phi1) * t_0));
	} else if (phi1 <= 3.1e-5) {
		tmp = R * acos((cos(phi2) * t_0));
	} else {
		tmp = R * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * 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(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2))
    if (phi1 <= (-1.18d-20)) then
        tmp = r * acos((cos(phi1) * t_0))
    else if (phi1 <= 3.1d-5) then
        tmp = r * acos((cos(phi2) * t_0))
    else
        tmp = r * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * 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(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda1) * Math.cos(lambda2));
	double tmp;
	if (phi1 <= -1.18e-20) {
		tmp = R * Math.acos((Math.cos(phi1) * t_0));
	} else if (phi1 <= 3.1e-5) {
		tmp = R * Math.acos((Math.cos(phi2) * t_0));
	} else {
		tmp = R * Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + ((Math.cos(phi1) * Math.cos(phi2)) * 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(lambda1) * math.sin(lambda2)) + (math.cos(lambda1) * math.cos(lambda2))
	tmp = 0
	if phi1 <= -1.18e-20:
		tmp = R * math.acos((math.cos(phi1) * t_0))
	elif phi1 <= 3.1e-5:
		tmp = R * math.acos((math.cos(phi2) * t_0))
	else:
		tmp = R * math.acos(((math.sin(phi1) * math.sin(phi2)) + ((math.cos(phi1) * math.cos(phi2)) * 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(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda1) * cos(lambda2)))
	tmp = 0.0
	if (phi1 <= -1.18e-20)
		tmp = Float64(R * acos(Float64(cos(phi1) * t_0)));
	elseif (phi1 <= 3.1e-5)
		tmp = Float64(R * acos(Float64(cos(phi2) * t_0)));
	else
		tmp = Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * 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(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2));
	tmp = 0.0;
	if (phi1 <= -1.18e-20)
		tmp = R * acos((cos(phi1) * t_0));
	elseif (phi1 <= 3.1e-5)
		tmp = R * acos((cos(phi2) * t_0));
	else
		tmp = R * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * 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[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -1.18e-20], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 3.1e-5], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 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]]]]
\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_1 \cdot \cos \lambda_2\\
\mathbf{if}\;\phi_1 \leq -1.18 \cdot 10^{-20}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

\mathbf{elif}\;\phi_1 \leq 3.1 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\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}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -1.1800000000000001e-20

    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 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--.f6455.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. Simplified55.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_1 \cdot \cos \lambda_2 + \sin \lambda_2 \cdot \sin \lambda_1\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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\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.f6462.9%

        \[\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-rr62.9%

      \[\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.1800000000000001e-20 < phi1 < 3.10000000000000014e-5

    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. 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.f6488.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{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-rr88.1%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)}\right), R\right) \]
    6. Step-by-step derivation
      1. *-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) \]
      2. 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) \]
      3. +-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) \]
      4. *-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) \]
      5. 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) \]
      6. 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) \]
      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(\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) \]
      8. 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) \]
      9. sin-lowering-sin.f6487.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. Simplified87.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 \]

    if 3.10000000000000014e-5 < phi1

    1. Initial program 73.8%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in 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.f6452.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. Simplified52.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 \]
  3. Recombined 3 regimes into one program.
  4. Final simplification73.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.18 \cdot 10^{-20}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 3.1 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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 9: 72.5% 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_1 \cdot \cos \lambda_2\\ \mathbf{if}\;\phi_1 \leq -1.18 \cdot 10^{-20}:\\ \;\;\;\;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 lambda1) (cos lambda2)))))
   (if (<= phi1 -1.18e-20)
     (* 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(lambda1) * cos(lambda2));
	double tmp;
	if (phi1 <= -1.18e-20) {
		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(lambda1) * cos(lambda2))
    if (phi1 <= (-1.18d-20)) 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(lambda1) * Math.cos(lambda2));
	double tmp;
	if (phi1 <= -1.18e-20) {
		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(lambda1) * math.cos(lambda2))
	tmp = 0
	if phi1 <= -1.18e-20:
		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(lambda1) * cos(lambda2)))
	tmp = 0.0
	if (phi1 <= -1.18e-20)
		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(lambda1) * cos(lambda2));
	tmp = 0.0;
	if (phi1 <= -1.18e-20)
		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[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -1.18e-20], 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_1 \cdot \cos \lambda_2\\
\mathbf{if}\;\phi_1 \leq -1.18 \cdot 10^{-20}:\\
\;\;\;\;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 phi1 < -1.1800000000000001e-20

    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 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--.f6455.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. Simplified55.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_1 \cdot \cos \lambda_2 + \sin \lambda_2 \cdot \sin \lambda_1\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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\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.f6462.9%

        \[\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-rr62.9%

      \[\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.1800000000000001e-20 < phi1

    1. Initial program 66.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. 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.f6491.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{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-rr91.6%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)}\right), R\right) \]
    6. Step-by-step derivation
      1. *-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) \]
      2. 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) \]
      3. +-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) \]
      4. *-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) \]
      5. 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) \]
      6. 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) \]
      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(\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) \]
      8. 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) \]
      9. sin-lowering-sin.f6466.2%

        \[\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. Simplified66.2%

      \[\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 2 regimes into one program.
  4. Final simplification65.3%

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

Alternative 10: 67.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.05 \cdot 10^{-10}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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.05e-10)
   (*
    R
    (acos
     (*
      (cos phi1)
      (+ (* (sin lambda1) (sin lambda2)) (* (cos lambda1) (cos lambda2))))))
   (* 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.05e-10) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	} 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.05d-10) then
        tmp = r * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))))
    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.05e-10) {
		tmp = R * Math.acos((Math.cos(phi1) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda1) * Math.cos(lambda2)))));
	} 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.05e-10:
		tmp = R * math.acos((math.cos(phi1) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda1) * math.cos(lambda2)))))
	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.05e-10)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda1) * cos(lambda2))))));
	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.05e-10)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	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.05e-10], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $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.05 \cdot 10^{-10}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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 2 regimes
  2. if phi2 < 1.05e-10

    1. Initial program 65.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 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--.f6451.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. Simplified51.3%

      \[\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_1 \cdot \cos \lambda_2 + \sin \lambda_2 \cdot \sin \lambda_1\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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\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.f6466.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-rr66.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.05e-10 < phi2

    1. Initial program 78.3%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in 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--.f6456.9%

        \[\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. Simplified56.9%

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

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

Alternative 11: 57.8% 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 \left(\lambda_2 - \lambda_1\right)\\ \mathbf{if}\;\phi_2 \leq 2 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \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 2e-14)
     (* R (acos (+ (* (sin phi1) (sin phi2)) (* (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 <= 2e-14) {
		tmp = R * acos(((sin(phi1) * sin(phi2)) + (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 <= 2d-14) then
        tmp = r * acos(((sin(phi1) * sin(phi2)) + (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 <= 2e-14) {
		tmp = R * Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + (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 <= 2e-14:
		tmp = R * math.acos(((math.sin(phi1) * math.sin(phi2)) + (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 <= 2e-14)
		tmp = Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + 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 <= 2e-14)
		tmp = R * acos(((sin(phi1) * sin(phi2)) + (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, 2e-14], N[(R * N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]), $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 2 \cdot 10^{-14}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \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 < 2e-14

    1. Initial program 65.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 phi2 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 \left(\lambda_1 - \lambda_2\right)\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(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      2. 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_1\right), \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      3. sub-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{cos.f64}\left(\phi_1\right), \cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      4. remove-double-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{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)\right), R\right) \]
      5. mul-1-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{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)\right), R\right) \]
      6. distribute-neg-inN/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_1\right), \cos \left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      7. +-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{cos.f64}\left(\phi_1\right), \cos \left(\mathsf{neg}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right)\right), R\right) \]
      8. 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{cos.f64}\left(\phi_1\right), \cos \left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right), R\right) \]
      9. 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_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)\right)\right)\right), R\right) \]
      10. mul-1-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{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)\right), R\right) \]
      11. unsub-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{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6451.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_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right)\right), R\right) \]
    5. Simplified51.1%

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

    if 2e-14 < phi2

    1. Initial program 78.3%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in 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--.f6456.9%

        \[\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. Simplified56.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2 \cdot 10^{-14}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \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 12: 57.9% 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_1 \leq -3.7 \cdot 10^{-7}:\\ \;\;\;\;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 (<= phi1 -3.7e-7)
     (* 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 (phi1 <= -3.7e-7) {
		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 (phi1 <= (-3.7d-7)) 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 (phi1 <= -3.7e-7) {
		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 phi1 <= -3.7e-7:
		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 (phi1 <= -3.7e-7)
		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 (phi1 <= -3.7e-7)
		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[phi1, -3.7e-7], 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_1 \leq -3.7 \cdot 10^{-7}:\\
\;\;\;\;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 phi1 < -3.70000000000000004e-7

    1. Initial program 76.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. 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--.f6454.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. Simplified54.5%

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

    if -3.70000000000000004e-7 < phi1

    1. Initial program 66.5%

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

      \[\leadsto \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--.f6449.2%

        \[\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. Simplified49.2%

      \[\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 simplification50.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -3.7 \cdot 10^{-7}:\\ \;\;\;\;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 13: 52.8% 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 3.3 \cdot 10^{-6}:\\ \;\;\;\;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 3.3e-6)
   (* 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 <= 3.3e-6) {
		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 <= 3.3d-6) 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 <= 3.3e-6) {
		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 <= 3.3e-6:
		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 <= 3.3e-6)
		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 <= 3.3e-6)
		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, 3.3e-6], 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 3.3 \cdot 10^{-6}:\\
\;\;\;\;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 < 3.30000000000000017e-6

    1. Initial program 65.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--.f6451.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. Simplified51.5%

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

    if 3.30000000000000017e-6 < phi2

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_2, \cos \lambda_2\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_2\right), \cos \lambda_2\right)\right), R\right) \]
      4. cos-lowering-cos.f6444.9%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right), R\right) \]
    8. Simplified44.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 3.3 \cdot 10^{-6}:\\ \;\;\;\;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 14: 42.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 -1 \cdot 10^{-26}:\\ \;\;\;\;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 (<= phi1 -1e-26)
   (* 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 (phi1 <= -1e-26) {
		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 (phi1 <= (-1d-26)) 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 (phi1 <= -1e-26) {
		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 phi1 <= -1e-26:
		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 (phi1 <= -1e-26)
		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 (phi1 <= -1e-26)
		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[phi1, -1e-26], 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}\;\phi_1 \leq -1 \cdot 10^{-26}:\\
\;\;\;\;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 phi1 < -1e-26

    1. Initial program 76.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. 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--.f6455.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. Simplified55.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 lambda2 around 0

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \lambda_1\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), \cos \lambda_1\right)\right), R\right) \]
      4. cos-lowering-cos.f6442.5%

        \[\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. Simplified42.5%

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

    if -1e-26 < phi1

    1. Initial program 66.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in 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.f6443.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. Simplified43.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. *-commutativeN/A

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_2, \cos \lambda_2\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_2\right), \cos \lambda_2\right)\right), R\right) \]
      4. cos-lowering-cos.f6432.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right), R\right) \]
    8. Simplified32.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1 \cdot 10^{-26}:\\ \;\;\;\;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 15: 36.4% 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_1 \leq -2.3 \cdot 10^{+23}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \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 -2.3e+23)
   (* R (acos (* (cos phi2) (cos lambda1))))
   (* R (acos (* (cos phi1) (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.3e+23) {
		tmp = R * acos((cos(phi2) * cos(lambda1)));
	} else {
		tmp = R * acos((cos(phi1) * 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.3d+23)) then
        tmp = r * acos((cos(phi2) * cos(lambda1)))
    else
        tmp = r * acos((cos(phi1) * 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.3e+23) {
		tmp = R * Math.acos((Math.cos(phi2) * Math.cos(lambda1)));
	} else {
		tmp = R * Math.acos((Math.cos(phi1) * 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.3e+23:
		tmp = R * math.acos((math.cos(phi2) * math.cos(lambda1)))
	else:
		tmp = R * math.acos((math.cos(phi1) * 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.3e+23)
		tmp = Float64(R * acos(Float64(cos(phi2) * cos(lambda1))));
	else
		tmp = Float64(R * acos(Float64(cos(phi1) * 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.3e+23)
		tmp = R * acos((cos(phi2) * cos(lambda1)));
	else
		tmp = R * acos((cos(phi1) * 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.3e+23], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi1], $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 -2.3 \cdot 10^{+23}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\right)\\


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

    1. Initial program 55.0%

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

        \[\leadsto \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.f6499.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{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-rr99.1%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)}\right), R\right) \]
    6. Step-by-step derivation
      1. *-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) \]
      2. 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) \]
      3. +-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) \]
      4. *-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) \]
      5. 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) \]
      6. 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) \]
      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(\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) \]
      8. 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) \]
      9. sin-lowering-sin.f6466.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. Simplified66.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 \]
    8. Taylor expanded in lambda2 around 0

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \lambda_1, \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_1\right), \cos \phi_2\right)\right), R\right) \]
      3. cos-lowering-cos.f6439.8%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right)\right), R\right) \]
    10. Simplified39.8%

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

    if -2.3e23 < lambda1

    1. Initial program 74.1%

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

      \[\leadsto \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.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. Simplified45.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 lambda1 around 0

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \lambda_2\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), \cos \lambda_2\right)\right), R\right) \]
      4. cos-lowering-cos.f6434.8%

        \[\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. Simplified34.8%

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

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

Alternative 16: 42.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 -2.9 \cdot 10^{-6}:\\ \;\;\;\;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_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 (<= phi1 -2.9e-6)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (* R (acos (* (cos phi2) (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 (phi1 <= -2.9e-6) {
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	} else {
		tmp = R * acos((cos(phi2) * 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 (phi1 <= (-2.9d-6)) then
        tmp = r * acos((cos(phi1) * cos(lambda1)))
    else
        tmp = r * acos((cos(phi2) * 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 (phi1 <= -2.9e-6) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda1)));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * 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 phi1 <= -2.9e-6:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda1)))
	else:
		tmp = R * math.acos((math.cos(phi2) * 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 (phi1 <= -2.9e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda1))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * 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 (phi1 <= -2.9e-6)
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	else
		tmp = R * acos((cos(phi2) * 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[phi1, -2.9e-6], 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[lambda1], $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 -2.9 \cdot 10^{-6}:\\
\;\;\;\;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_1\right)\\


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

    1. Initial program 76.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. 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--.f6454.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. Simplified54.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(\color{blue}{\left(\cos \phi_1 \cdot \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(\left(\cos \phi_1 \cdot \cos \lambda_1\right)\right), R\right) \]
      2. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \phi_1, \cos \lambda_1\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), \cos \lambda_1\right)\right), R\right) \]
      4. cos-lowering-cos.f6443.3%

        \[\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. Simplified43.3%

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

    if -2.9000000000000002e-6 < phi1

    1. Initial program 66.5%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. 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.f6491.3%

        \[\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-rr91.3%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)}\right), R\right) \]
    6. Step-by-step derivation
      1. *-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) \]
      2. 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) \]
      3. +-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) \]
      4. *-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) \]
      5. 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) \]
      6. 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) \]
      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(\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) \]
      8. 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) \]
      9. sin-lowering-sin.f6466.2%

        \[\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. Simplified66.2%

      \[\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 \]
    8. Taylor expanded in lambda2 around 0

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \lambda_1, \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_1\right), \cos \phi_2\right)\right), R\right) \]
      3. cos-lowering-cos.f6436.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right)\right), R\right) \]
    10. Simplified36.6%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.9 \cdot 10^{-6}:\\ \;\;\;\;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_1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 17: 37.7% 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 -3.5 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -3.5e-5)
   (* R (acos (cos phi1)))
   (* R (acos (* (cos phi2) (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 (phi1 <= -3.5e-5) {
		tmp = R * acos(cos(phi1));
	} else {
		tmp = R * acos((cos(phi2) * 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 (phi1 <= (-3.5d-5)) then
        tmp = r * acos(cos(phi1))
    else
        tmp = r * acos((cos(phi2) * 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 (phi1 <= -3.5e-5) {
		tmp = R * Math.acos(Math.cos(phi1));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * 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 phi1 <= -3.5e-5:
		tmp = R * math.acos(math.cos(phi1))
	else:
		tmp = R * math.acos((math.cos(phi2) * 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 (phi1 <= -3.5e-5)
		tmp = Float64(R * acos(cos(phi1)));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * 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 (phi1 <= -3.5e-5)
		tmp = R * acos(cos(phi1));
	else
		tmp = R * acos((cos(phi2) * 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[phi1, -3.5e-5], N[(R * N[ArcCos[N[Cos[phi1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -3.5 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\

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


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

    1. Initial program 76.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. 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--.f6454.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. Simplified54.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. mul-1-negN/A

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

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

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

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

        \[\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. Simplified34.9%

      \[\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.f6432.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\phi_1\right)\right), R\right) \]
    11. Simplified32.0%

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]

    if -3.4999999999999997e-5 < phi1

    1. Initial program 66.5%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. 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.f6491.3%

        \[\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-rr91.3%

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)}\right), R\right) \]
    6. Step-by-step derivation
      1. *-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) \]
      2. 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) \]
      3. +-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) \]
      4. *-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) \]
      5. 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) \]
      6. 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) \]
      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(\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) \]
      8. 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) \]
      9. sin-lowering-sin.f6466.2%

        \[\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. Simplified66.2%

      \[\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 \]
    8. Taylor expanded in lambda2 around 0

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\cos \lambda_1, \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_1\right), \cos \phi_2\right)\right), R\right) \]
      3. cos-lowering-cos.f6436.6%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\phi_2\right)\right)\right), R\right) \]
    10. Simplified36.6%

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

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

Alternative 18: 21.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_2 \leq -2.1 \cdot 10^{-237}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{elif}\;\lambda_2 \leq 1950:\\ \;\;\;\;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 (<= lambda2 -2.1e-237)
   (* R (acos (cos lambda1)))
   (if (<= lambda2 1950.0)
     (* 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 (lambda2 <= -2.1e-237) {
		tmp = R * acos(cos(lambda1));
	} else if (lambda2 <= 1950.0) {
		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 (lambda2 <= (-2.1d-237)) then
        tmp = r * acos(cos(lambda1))
    else if (lambda2 <= 1950.0d0) 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 (lambda2 <= -2.1e-237) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else if (lambda2 <= 1950.0) {
		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 lambda2 <= -2.1e-237:
		tmp = R * math.acos(math.cos(lambda1))
	elif lambda2 <= 1950.0:
		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 (lambda2 <= -2.1e-237)
		tmp = Float64(R * acos(cos(lambda1)));
	elseif (lambda2 <= 1950.0)
		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 (lambda2 <= -2.1e-237)
		tmp = R * acos(cos(lambda1));
	elseif (lambda2 <= 1950.0)
		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[lambda2, -2.1e-237], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda2, 1950.0], 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_2 \leq -2.1 \cdot 10^{-237}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{elif}\;\lambda_2 \leq 1950:\\
\;\;\;\;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 lambda2 < -2.1000000000000001e-237

    1. Initial program 68.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--.f6444.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. Simplified44.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--.f6427.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. Simplified27.1%

      \[\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.f6419.3%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right)\right), R\right) \]
    11. Simplified19.3%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if -2.1000000000000001e-237 < lambda2 < 1950

    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. 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--.f6443.6%

        \[\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. Simplified43.6%

      \[\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. mul-1-negN/A

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\phi_1\right)\right), R\right) \]
    11. Simplified19.3%

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]

    if 1950 < lambda2

    1. Initial program 53.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--.f6438.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. Simplified38.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 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.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. Simplified26.6%

      \[\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.f6427.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right)\right), R\right) \]
    11. Simplified27.0%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification21.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_2 \leq -2.1 \cdot 10^{-237}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{elif}\;\lambda_2 \leq 1950:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \]
  5. Add Preprocessing

Alternative 19: 33.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}\;\phi_1 \leq -3.5 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -3.5e-5)
   (* R (acos (cos phi1)))
   (* R (acos (cos (- lambda1 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 <= -3.5e-5) {
		tmp = R * acos(cos(phi1));
	} else {
		tmp = R * acos(cos((lambda1 - lambda2)));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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 <= (-3.5d-5)) then
        tmp = r * acos(cos(phi1))
    else
        tmp = r * acos(cos((lambda1 - 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 <= -3.5e-5) {
		tmp = R * Math.acos(Math.cos(phi1));
	} else {
		tmp = R * Math.acos(Math.cos((lambda1 - 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 <= -3.5e-5:
		tmp = R * math.acos(math.cos(phi1))
	else:
		tmp = R * math.acos(math.cos((lambda1 - 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 <= -3.5e-5)
		tmp = Float64(R * acos(cos(phi1)));
	else
		tmp = Float64(R * acos(cos(Float64(lambda1 - lambda2))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi1 <= -3.5e-5)
		tmp = R * acos(cos(phi1));
	else
		tmp = R * acos(cos((lambda1 - lambda2)));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -3.5e-5], N[(R * N[ArcCos[N[Cos[phi1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -3.5 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\

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


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

    1. Initial program 76.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. 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--.f6454.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. Simplified54.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. mul-1-negN/A

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

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

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

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

        \[\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. Simplified34.9%

      \[\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.f6432.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\phi_1\right)\right), R\right) \]
    11. Simplified32.0%

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]

    if -3.4999999999999997e-5 < phi1

    1. Initial program 66.5%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in 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--.f6438.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. Simplified38.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--.f6427.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. Simplified27.9%

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

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

Alternative 20: 21.7% 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_2 \leq 1.1 \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 (<= lambda2 1.1e-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 (lambda2 <= 1.1e-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 (lambda2 <= 1.1d-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 (lambda2 <= 1.1e-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 lambda2 <= 1.1e-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 (lambda2 <= 1.1e-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 (lambda2 <= 1.1e-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[lambda2, 1.1e-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_2 \leq 1.1 \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 lambda2 < 1.1000000000000001e-6

    1. Initial program 75.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--.f6444.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. Simplified44.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.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. Simplified23.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.f6419.4%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right)\right), R\right) \]
    11. Simplified19.4%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if 1.1000000000000001e-6 < lambda2

    1. Initial program 53.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--.f6438.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. Simplified38.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--.f6426.5%

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

      \[\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.f6426.8%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right)\right), R\right) \]
    11. Simplified26.8%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification21.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 1.1 \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 21: 12.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 \leq -2 \cdot 10^{-8}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\lambda_2 - \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 -2e-8) (* R (acos (cos lambda1))) (* R (- 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 (lambda1 <= -2e-8) {
		tmp = R * acos(cos(lambda1));
	} else {
		tmp = R * (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 (lambda1 <= (-2d-8)) then
        tmp = r * acos(cos(lambda1))
    else
        tmp = r * (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 (lambda1 <= -2e-8) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else {
		tmp = R * (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 lambda1 <= -2e-8:
		tmp = R * math.acos(math.cos(lambda1))
	else:
		tmp = R * (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 (lambda1 <= -2e-8)
		tmp = Float64(R * acos(cos(lambda1)));
	else
		tmp = Float64(R * 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 (lambda1 <= -2e-8)
		tmp = R * acos(cos(lambda1));
	else
		tmp = R * (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[lambda1, -2e-8], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[(lambda2 - lambda1), $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 \cdot 10^{-8}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left(\lambda_2 - \lambda_1\right)\\


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

    1. Initial program 56.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.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. Simplified37.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 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--.f6427.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. Simplified27.6%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right)\right), R\right) \]
    11. Simplified27.7%

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]

    if -2e-8 < lambda1

    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--.f6445.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. Simplified45.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 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.5%

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

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

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

      \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification12.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -2 \cdot 10^{-8}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\lambda_2 - \lambda_1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 22: 5.7% accurate, 122.6× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ R \cdot \left(\lambda_2 - \lambda_1\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 (- lambda2 lambda1)))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * (lambda2 - lambda1);
}
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 * (lambda2 - lambda1)
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 * (lambda2 - lambda1);
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	return R * (lambda2 - lambda1)
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(R * Float64(lambda2 - lambda1))
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = R * (lambda2 - lambda1);
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[(lambda2 - lambda1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
R \cdot \left(\lambda_2 - \lambda_1\right)
\end{array}
Derivation
  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--.f6442.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. Simplified42.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 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--.f6424.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. Simplified24.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--.f645.7%

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

    \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
  11. Final simplification5.7%

    \[\leadsto R \cdot \left(\lambda_2 - \lambda_1\right) \]
  12. Add Preprocessing

Alternative 23: 5.3% 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 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--.f6442.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. Simplified42.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 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--.f6424.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. Simplified24.6%

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

    \[\leadsto \mathsf{*.f64}\left(\color{blue}{\lambda_1}, R\right) \]
  10. Step-by-step derivation
    1. Simplified4.4%

      \[\leadsto \color{blue}{\lambda_1} \cdot R \]
    2. Add Preprocessing

    Reproduce

    ?
    herbie shell --seed 2024139 
    (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))