Spherical law of cosines

Percentage Accurate: 73.4% → 96.5%
Time: 24.5s
Alternatives: 28
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 28 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: 96.5% accurate, 0.4× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ \mathbf{if}\;\cos^{-1} \left(t\_0 + t\_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \leq 0.0004:\\ \;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t\_0 + \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \cos \lambda_1 \cdot \left(t\_1 \cdot \cos \lambda_2\right)\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))) (t_1 (* (cos phi1) (cos phi2))))
   (if (<= (acos (+ t_0 (* t_1 (cos (- lambda1 lambda2))))) 0.0004)
     (* (- lambda2 lambda1) R)
     (*
      R
      (acos
       (+
        t_0
        (+
         (* (cos phi1) (* (cos phi2) (* (sin lambda1) (sin lambda2))))
         (* (cos lambda1) (* t_1 (cos lambda2))))))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin(phi1) * sin(phi2);
	double t_1 = cos(phi1) * cos(phi2);
	double tmp;
	if (acos((t_0 + (t_1 * cos((lambda1 - lambda2))))) <= 0.0004) {
		tmp = (lambda2 - lambda1) * R;
	} else {
		tmp = R * acos((t_0 + ((cos(phi1) * (cos(phi2) * (sin(lambda1) * sin(lambda2)))) + (cos(lambda1) * (t_1 * cos(lambda2))))));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = sin(phi1) * sin(phi2)
    t_1 = cos(phi1) * cos(phi2)
    if (acos((t_0 + (t_1 * cos((lambda1 - lambda2))))) <= 0.0004d0) then
        tmp = (lambda2 - lambda1) * r
    else
        tmp = r * acos((t_0 + ((cos(phi1) * (cos(phi2) * (sin(lambda1) * sin(lambda2)))) + (cos(lambda1) * (t_1 * cos(lambda2))))))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.sin(phi1) * Math.sin(phi2);
	double t_1 = Math.cos(phi1) * Math.cos(phi2);
	double tmp;
	if (Math.acos((t_0 + (t_1 * Math.cos((lambda1 - lambda2))))) <= 0.0004) {
		tmp = (lambda2 - lambda1) * R;
	} else {
		tmp = R * Math.acos((t_0 + ((Math.cos(phi1) * (Math.cos(phi2) * (Math.sin(lambda1) * Math.sin(lambda2)))) + (Math.cos(lambda1) * (t_1 * Math.cos(lambda2))))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.sin(phi1) * math.sin(phi2)
	t_1 = math.cos(phi1) * math.cos(phi2)
	tmp = 0
	if math.acos((t_0 + (t_1 * math.cos((lambda1 - lambda2))))) <= 0.0004:
		tmp = (lambda2 - lambda1) * R
	else:
		tmp = R * math.acos((t_0 + ((math.cos(phi1) * (math.cos(phi2) * (math.sin(lambda1) * math.sin(lambda2)))) + (math.cos(lambda1) * (t_1 * math.cos(lambda2))))))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(phi1) * sin(phi2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if (acos(Float64(t_0 + Float64(t_1 * cos(Float64(lambda1 - lambda2))))) <= 0.0004)
		tmp = Float64(Float64(lambda2 - lambda1) * R);
	else
		tmp = Float64(R * acos(Float64(t_0 + Float64(Float64(cos(phi1) * Float64(cos(phi2) * Float64(sin(lambda1) * sin(lambda2)))) + Float64(cos(lambda1) * Float64(t_1 * cos(lambda2)))))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = sin(phi1) * sin(phi2);
	t_1 = cos(phi1) * cos(phi2);
	tmp = 0.0;
	if (acos((t_0 + (t_1 * cos((lambda1 - lambda2))))) <= 0.0004)
		tmp = (lambda2 - lambda1) * R;
	else
		tmp = R * acos((t_0 + ((cos(phi1) * (cos(phi2) * (sin(lambda1) * sin(lambda2)))) + (cos(lambda1) * (t_1 * cos(lambda2))))));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[ArcCos[N[(t$95$0 + N[(t$95$1 * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 0.0004], N[(N[(lambda2 - lambda1), $MachinePrecision] * R), $MachinePrecision], N[(R * N[ArcCos[N[(t$95$0 + N[(N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[(t$95$1 * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
\mathbf{if}\;\cos^{-1} \left(t\_0 + t\_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \leq 0.0004:\\
\;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\

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


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

    1. Initial program 8.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--.f648.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. Simplified8.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
      9. sin-lowering-sin.f648.3%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
    10. Applied egg-rr8.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    11. Step-by-step derivation
      1. *-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) \]
      2. cos-diffN/A

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

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

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

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

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

    1. Initial program 77.5%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right), \left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right)\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{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\sin \lambda_1, \sin \lambda_2\right)\right)\right), \left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right)\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{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right)\right)\right), \left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right)\right), R\right) \]
      11. 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{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right), \left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right)\right), R\right) \]
      12. associate-*l*N/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{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right), \left(\cos \lambda_1 \cdot \left(\cos \lambda_2 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right)\right)\right), R\right) \]
      13. *-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{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right), \mathsf{*.f64}\left(\cos \lambda_1, \left(\cos \lambda_2 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right)\right)\right), R\right) \]
      14. 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{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \left(\cos \lambda_2 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right)\right)\right), R\right) \]
      15. *-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{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\cos \lambda_2, \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\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{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{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right)\right)\right), R\right) \]
      17. *-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{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{*.f64}\left(\cos \phi_1, \cos \phi_2\right)\right)\right)\right)\right)\right), R\right) \]
      18. 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{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right)\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \cos \phi_2\right)\right)\right)\right)\right)\right), R\right) \]
      19. cos-lowering-cos.f6498.9%

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

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

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

Alternative 2: 96.5% accurate, 0.4× speedup?

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

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


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

    1. Initial program 8.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--.f648.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. Simplified8.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
      9. sin-lowering-sin.f648.3%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
    10. Applied egg-rr8.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    11. Step-by-step derivation
      1. *-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) \]
      2. cos-diffN/A

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

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

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

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

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

    1. Initial program 77.5%

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 3: 83.5% accurate, 0.7× speedup?

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

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

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


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

    1. Initial program 77.7%

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

        \[\leadsto \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-lft-inN/A

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

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

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

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

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

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

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

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

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

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

    if -0.0040000000000000001 < phi2 < 1.65000000000000008e-6

    1. Initial program 69.6%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{+.f64}\left(\left(\frac{-1}{2} \cdot {\phi_2}^{2}\right), 1\right), \left(\cos \phi_1 \cdot \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{+.f64}\left(\mathsf{*.f64}\left(\frac{-1}{2}, \left({\phi_2}^{2}\right)\right), 1\right), \left(\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      6. unpow2N/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{*.f64}\left(\frac{-1}{2}, \left(\phi_2 \cdot \phi_2\right)\right), 1\right), \left(\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \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{*.f64}\left(\frac{-1}{2}, \mathsf{*.f64}\left(\phi_2, \phi_2\right)\right), 1\right), \left(\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      8. *-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{*.f64}\left(\frac{-1}{2}, \mathsf{*.f64}\left(\phi_2, \phi_2\right)\right), 1\right), \mathsf{*.f64}\left(\cos \phi_1, \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\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{+.f64}\left(\mathsf{*.f64}\left(\frac{-1}{2}, \mathsf{*.f64}\left(\phi_2, \phi_2\right)\right), 1\right), \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)\right)\right), R\right) \]
      10. +-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{*.f64}\left(\frac{-1}{2}, \mathsf{*.f64}\left(\phi_2, \phi_2\right)\right), 1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{+.f64}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2\right), \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    7. Simplified88.8%

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

    if 1.65000000000000008e-6 < phi2

    1. Initial program 78.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 4: 83.5% accurate, 0.8× speedup?

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

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

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


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

    1. Initial program 77.7%

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

        \[\leadsto \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-lft-inN/A

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

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

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

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

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

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

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

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

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

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

    if -0.0030000000000000001 < phi2 < 1.65000000000000008e-6

    1. Initial program 69.6%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\phi_2 \cdot \left(\sin \phi_1 + \frac{-1}{2} \cdot \left(\phi_2 \cdot \left(\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right) + \cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)}\right), R\right) \]
    6. Simplified88.7%

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

    if 1.65000000000000008e-6 < phi2

    1. Initial program 78.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 83.4% accurate, 0.8× speedup?

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

\mathbf{elif}\;\phi_2 \leq 1.35 \cdot 10^{-6}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \phi_2 + \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(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \cos \phi_1, t\_0\right)\right)\\


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

    1. Initial program 77.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. 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-lft-inN/A

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

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

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

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

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

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

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

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

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

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

    if -6.49999999999999943e-5 < phi2 < 1.34999999999999999e-6

    1. Initial program 69.7%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. 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-lft-inN/A

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

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

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

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

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

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

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

    if 1.34999999999999999e-6 < phi2

    1. Initial program 78.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 6: 83.3% accurate, 0.9× speedup?

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

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

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


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

    1. Initial program 76.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(\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-lft-inN/A

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. associate-*r*N/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 \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. associate-*r*N/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 \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      6. distribute-lft-inN/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 \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      7. cos-diffN/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) \]
      8. 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) \]
      9. 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) \]
      10. 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. Applied egg-rr76.5%

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

    if -1.06e-13 < phi2 < 1.3000000000000001e-9

    1. Initial program 69.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--.f6469.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. Simplified69.7%

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

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

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

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

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

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

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

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

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

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

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

    if 1.3000000000000001e-9 < phi2

    1. Initial program 78.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 7: 83.3% accurate, 0.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.06 \cdot 10^{-13}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 2.9 \cdot 10^{-8}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_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.
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.06e-13)
   (*
    R
    (acos
     (fma
      (sin phi2)
      (sin phi1)
      (* (* (cos phi1) (cos phi2)) (cos (- lambda2 lambda1))))))
   (if (<= phi2 2.9e-8)
     (*
      R
      (acos
       (*
        (cos phi1)
        (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2))))))
     (*
      R
      (acos
       (fma
        (sin phi2)
        (sin phi1)
        (* (cos phi2) (* (cos phi1) (cos (- lambda1 lambda2))))))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= -1.06e-13) {
		tmp = R * acos(fma(sin(phi2), sin(phi1), ((cos(phi1) * cos(phi2)) * cos((lambda2 - lambda1)))));
	} else if (phi2 <= 2.9e-8) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(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])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= -1.06e-13)
		tmp = Float64(R * acos(fma(sin(phi2), sin(phi1), Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda2 - lambda1))))));
	elseif (phi2 <= 2.9e-8)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = Float64(R * acos(fma(sin(phi2), sin(phi1), 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.
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.06e-13], N[(R * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 2.9e-8], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + 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])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq -1.06 \cdot 10^{-13}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\\

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

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_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.06e-13

    1. Initial program 76.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(\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-lft-inN/A

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      4. associate-*r*N/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 \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      5. associate-*r*N/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 \left(\cos \lambda_1 \cdot \cos \lambda_2\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      6. distribute-lft-inN/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 \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      7. cos-diffN/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) \]
      8. 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) \]
      9. 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) \]
      10. 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. Applied egg-rr76.5%

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

    if -1.06e-13 < phi2 < 2.9000000000000002e-8

    1. Initial program 69.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--.f6469.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. Simplified69.7%

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

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

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

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

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

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

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

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

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

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

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

    if 2.9000000000000002e-8 < phi2

    1. Initial program 78.4%

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)\right)\right)\right), R\right) \]
      11. *-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 \left(\lambda_1 - \lambda_2\right), \cos \phi_1\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(\left(\lambda_1 - \lambda_2\right)\right), \cos \phi_1\right)\right)\right)\right), R\right) \]
      13. --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(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \cos \phi_1\right)\right)\right)\right), R\right) \]
      14. cos-lowering-cos.f6478.4%

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

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

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

Alternative 8: 83.3% accurate, 0.9× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_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.06e-13

    1. Initial program 76.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(\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-lft-inN/A

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

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

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

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

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

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

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

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

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

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

    if -1.06e-13 < phi2 < 1.0999999999999999e-8

    1. Initial program 69.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--.f6469.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. Simplified69.7%

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

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

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

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

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

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

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

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

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

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

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

    if 1.0999999999999999e-8 < phi2

    1. Initial program 78.4%

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{fma.f64}\left(\mathsf{sin.f64}\left(\phi_2\right), \mathsf{sin.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)\right)\right)\right), R\right) \]
      11. *-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 \left(\lambda_1 - \lambda_2\right), \cos \phi_1\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(\left(\lambda_1 - \lambda_2\right)\right), \cos \phi_1\right)\right)\right)\right), R\right) \]
      13. --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(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \cos \phi_1\right)\right)\right)\right), R\right) \]
      14. cos-lowering-cos.f6478.4%

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

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

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

Alternative 9: 83.3% accurate, 0.9× speedup?

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

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

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


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

    1. Initial program 76.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(\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-lft-inN/A

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

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

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

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

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

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

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

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

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

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

    if -1.06e-13 < phi2 < 2.39999999999999998e-8

    1. Initial program 69.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--.f6469.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. Simplified69.7%

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

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

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

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

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

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

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

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

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

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

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

    if 2.39999999999999998e-8 < phi2

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

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

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

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

Alternative 10: 83.3% accurate, 1.0× speedup?

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

\mathbf{elif}\;\phi_2 \leq 10^{-9}:\\
\;\;\;\;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 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\


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

    1. Initial program 76.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(\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-lft-inN/A

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

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

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

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

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

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

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

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

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

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

    if -1.06e-13 < phi2 < 1.00000000000000006e-9

    1. Initial program 69.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--.f6469.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. Simplified69.7%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_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. *-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_1, \cos \lambda_2\right)\right)\right)\right), R\right) \]
      10. 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_1\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f6488.5%

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

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

    if 1.00000000000000006e-9 < phi2

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

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

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

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

Alternative 11: 82.4% accurate, 1.0× speedup?

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

\mathbf{elif}\;\phi_2 \leq 4 \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\_1 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot t\_0\right)\right)\\


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

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

    if -1.4499999999999999e-115 < phi2 < 4.00000000000000015e-10

    1. Initial program 67.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--.f6467.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. Simplified67.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. *-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_1, \cos \lambda_2\right)\right)\right)\right), R\right) \]
      10. 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_1\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f6487.4%

        \[\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_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.4%

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

    if 4.00000000000000015e-10 < phi2

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

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

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

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

Alternative 12: 82.4% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\ \mathbf{if}\;\phi_2 \leq -1.45 \cdot 10^{-115}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 1.25 \cdot 10^{-8}:\\ \;\;\;\;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.
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 phi2) (* (cos phi1) (cos (- lambda1 lambda2)))))))))
   (if (<= phi2 -1.45e-115)
     t_0
     (if (<= phi2 1.25e-8)
       (*
        R
        (acos
         (*
          (cos phi1)
          (+
           (* (sin lambda1) (sin lambda2))
           (* (cos lambda1) (cos lambda2))))))
       t_0))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = R * acos(((sin(phi1) * sin(phi2)) + (cos(phi2) * (cos(phi1) * cos((lambda1 - lambda2))))));
	double tmp;
	if (phi2 <= -1.45e-115) {
		tmp = t_0;
	} else if (phi2 <= 1.25e-8) {
		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.
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(phi2) * (cos(phi1) * cos((lambda1 - lambda2))))))
    if (phi2 <= (-1.45d-115)) then
        tmp = t_0
    else if (phi2 <= 1.25d-8) 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;
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(phi2) * (Math.cos(phi1) * Math.cos((lambda1 - lambda2))))));
	double tmp;
	if (phi2 <= -1.45e-115) {
		tmp = t_0;
	} else if (phi2 <= 1.25e-8) {
		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])
[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(phi2) * (math.cos(phi1) * math.cos((lambda1 - lambda2))))))
	tmp = 0
	if phi2 <= -1.45e-115:
		tmp = t_0
	elif phi2 <= 1.25e-8:
		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])
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(cos(phi2) * Float64(cos(phi1) * cos(Float64(lambda1 - lambda2)))))))
	tmp = 0.0
	if (phi2 <= -1.45e-115)
		tmp = t_0;
	elseif (phi2 <= 1.25e-8)
		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])){:}
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(phi2) * (cos(phi1) * cos((lambda1 - lambda2))))));
	tmp = 0.0;
	if (phi2 <= -1.45e-115)
		tmp = t_0;
	elseif (phi2 <= 1.25e-8)
		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.
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[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -1.45e-115], t$95$0, If[LessEqual[phi2, 1.25e-8], 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])\\\\
[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 + \cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\\
\mathbf{if}\;\phi_2 \leq -1.45 \cdot 10^{-115}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\phi_2 \leq 1.25 \cdot 10^{-8}:\\
\;\;\;\;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 < -1.4499999999999999e-115 or 1.2499999999999999e-8 < 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. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

    if -1.4499999999999999e-115 < phi2 < 1.2499999999999999e-8

    1. Initial program 67.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--.f6467.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. Simplified67.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. *-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_1, \cos \lambda_2\right)\right)\right)\right), R\right) \]
      10. 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_1\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f6487.4%

        \[\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_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr87.4%

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

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

Alternative 13: 75.7% accurate, 1.0× speedup?

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

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

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


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

    1. Initial program 52.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_1 \cdot \cos \phi_2\right)\right)\right)\right), R\right) \]
      11. *-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), \left(\cos \phi_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. *-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, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      13. 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), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      14. 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{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Simplified52.6%

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

    if -3.1e-6 < lambda1 < 9.99999999999999955e-7

    1. Initial program 88.8%

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

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

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

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

    if 9.99999999999999955e-7 < lambda1

    1. Initial program 60.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. 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-lft-inN/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \phi_2 \cdot \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(\cos \phi_2, \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\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_2\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) \]
      9. 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{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      10. 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{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) \]
      11. *-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{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_1, \cos \lambda_2\right)\right)\right)\right), R\right) \]
      12. 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{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      13. cos-lowering-cos.f6451.3%

        \[\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{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Simplified51.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -3.1 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{elif}\;\lambda_1 \leq 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_2\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 14: 75.8% accurate, 1.0× speedup?

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

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

    1. Initial program 52.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_1 \cdot \cos \phi_2\right)\right)\right)\right), R\right) \]
      11. *-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), \left(\cos \phi_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. *-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, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      13. 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), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      14. 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{cos.f64}\left(\phi_1\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Simplified52.6%

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

    if -6.60000000000000034e-6 < lambda1 < 2.6999999999999999e-5

    1. Initial program 88.8%

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\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(\cos \phi_1, \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\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), \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      8. *-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, \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\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{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
      10. 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), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f6488.8%

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

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

    if 2.6999999999999999e-5 < lambda1

    1. Initial program 60.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. 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-lft-inN/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \phi_2 \cdot \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(\cos \phi_2, \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\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_2\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) \]
      9. 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{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      10. 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{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) \]
      11. *-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{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_1, \cos \lambda_2\right)\right)\right)\right), R\right) \]
      12. 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{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      13. cos-lowering-cos.f6451.3%

        \[\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{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Simplified51.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -6.6 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{elif}\;\lambda_1 \leq 2.7 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \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 15: 75.6% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\\ \mathbf{if}\;\phi_1 \leq -6 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\ \mathbf{elif}\;\phi_1 \leq 0.058:\\ \;\;\;\;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 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (+ (* (sin lambda1) (sin lambda2)) (* (cos lambda1) (cos lambda2)))))
   (if (<= phi1 -6e-11)
     (* R (acos (* (cos phi1) t_0)))
     (if (<= phi1 0.058)
       (* 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);
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 <= -6e-11) {
		tmp = R * acos((cos(phi1) * t_0));
	} else if (phi1 <= 0.058) {
		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.
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 <= (-6d-11)) then
        tmp = r * acos((cos(phi1) * t_0))
    else if (phi1 <= 0.058d0) 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;
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 <= -6e-11) {
		tmp = R * Math.acos((Math.cos(phi1) * t_0));
	} else if (phi1 <= 0.058) {
		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])
[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 <= -6e-11:
		tmp = R * math.acos((math.cos(phi1) * t_0))
	elif phi1 <= 0.058:
		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])
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 <= -6e-11)
		tmp = Float64(R * acos(Float64(cos(phi1) * t_0)));
	elseif (phi1 <= 0.058)
		tmp = Float64(R * acos(Float64(cos(phi2) * t_0)));
	else
		tmp = Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(cos(phi1) * Float64(cos(phi2) * cos(lambda1))))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = (sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2));
	tmp = 0.0;
	if (phi1 <= -6e-11)
		tmp = R * acos((cos(phi1) * t_0));
	elseif (phi1 <= 0.058)
		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.
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, -6e-11], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 0.058], 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[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\\
\mathbf{if}\;\phi_1 \leq -6 \cdot 10^{-11}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

\mathbf{elif}\;\phi_1 \leq 0.058:\\
\;\;\;\;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 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \lambda_1\right)\right)\\


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

    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--.f6456.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. Simplified56.5%

      \[\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. *-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_1, \cos \lambda_2\right)\right)\right)\right), R\right) \]
      10. 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_1\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f6470.6%

        \[\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_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr70.6%

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

    if -6e-11 < phi1 < 0.0580000000000000029

    1. Initial program 66.0%

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

        \[\leadsto \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-lft-inN/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \phi_2 \cdot \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(\cos \phi_2, \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\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_2\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) \]
      9. 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{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      10. 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{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) \]
      11. *-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{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_1, \cos \lambda_2\right)\right)\right)\right), R\right) \]
      12. 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{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      13. cos-lowering-cos.f6486.9%

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

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

    if 0.0580000000000000029 < phi1

    1. Initial program 82.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(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      3. +-lowering-+.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \left(\cos \lambda_1 \cdot \cos \phi_2\right)\right)\right)\right), R\right) \]
      11. *-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), \left(\cos \phi_2 \cdot \cos \lambda_1\right)\right)\right)\right), R\right) \]
      12. *-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, \cos \lambda_1\right)\right)\right)\right), R\right) \]
      13. 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), \cos \lambda_1\right)\right)\right)\right), R\right) \]
      14. cos-lowering-cos.f6457.8%

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

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

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

Alternative 16: 73.2% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\\ \mathbf{if}\;\phi_2 \leq 7 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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 (<= phi2 7e-5)
     (* R (acos (* (cos phi1) t_0)))
     (* R (acos (* (cos phi2) t_0))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2));
	double tmp;
	if (phi2 <= 7e-5) {
		tmp = R * acos((cos(phi1) * t_0));
	} else {
		tmp = R * acos((cos(phi2) * t_0));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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 (phi2 <= 7d-5) then
        tmp = r * acos((cos(phi1) * t_0))
    else
        tmp = r * acos((cos(phi2) * t_0))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
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 (phi2 <= 7e-5) {
		tmp = R * Math.acos((Math.cos(phi1) * t_0));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * t_0));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[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 phi2 <= 7e-5:
		tmp = R * math.acos((math.cos(phi1) * t_0))
	else:
		tmp = R * math.acos((math.cos(phi2) * t_0))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
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 (phi2 <= 7e-5)
		tmp = Float64(R * acos(Float64(cos(phi1) * t_0)));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * t_0)));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
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 (phi2 <= 7e-5)
		tmp = R * acos((cos(phi1) * t_0));
	else
		tmp = R * acos((cos(phi2) * t_0));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
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[phi2, 7e-5], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[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_2 \leq 7 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

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


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

    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. 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.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. Simplified54.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. *-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_1, \cos \lambda_2\right)\right)\right)\right), R\right) \]
      10. 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_1\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f6467.6%

        \[\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_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr67.6%

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

    if 6.9999999999999994e-5 < 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. 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-lft-inN/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\left(\cos \phi_2 \cdot \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(\cos \phi_2, \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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), \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_1 \cdot \cos \lambda_2\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(\left(\sin \lambda_1 \cdot \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\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_2\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) \]
      9. 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{sin.f64}\left(\lambda_1\right), \sin \lambda_2\right), \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right)\right)\right), R\right) \]
      10. 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{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) \]
      11. *-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{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\cos \lambda_1, \cos \lambda_2\right)\right)\right)\right), R\right) \]
      12. 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{sin.f64}\left(\lambda_1\right), \mathsf{sin.f64}\left(\lambda_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\lambda_1\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      13. cos-lowering-cos.f6452.9%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 7 \cdot 10^{-5}:\\ \;\;\;\;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 17: 67.9% accurate, 1.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 7 \cdot 10^{-5}:\\ \;\;\;\;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(\frac{\frac{\sin \phi_1 \cdot \sin \phi_2}{2}}{\frac{\phi_1}{\cos \left(\lambda_1 - \lambda_2\right) \cdot 2} \cdot \tan \phi_2}\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 7e-5)
   (*
    R
    (acos
     (*
      (cos phi1)
      (+ (* (sin lambda1) (sin lambda2)) (* (cos lambda1) (cos lambda2))))))
   (*
    R
    (acos
     (/
      (/ (* (sin phi1) (sin phi2)) 2.0)
      (* (/ phi1 (* (cos (- lambda1 lambda2)) 2.0)) (tan phi2)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 7e-5) {
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	} else {
		tmp = R * acos((((sin(phi1) * sin(phi2)) / 2.0) / ((phi1 / (cos((lambda1 - lambda2)) * 2.0)) * tan(phi2))));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (phi2 <= 7d-5) then
        tmp = r * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))))
    else
        tmp = r * acos((((sin(phi1) * sin(phi2)) / 2.0d0) / ((phi1 / (cos((lambda1 - lambda2)) * 2.0d0)) * tan(phi2))))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 7e-5) {
		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.sin(phi1) * Math.sin(phi2)) / 2.0) / ((phi1 / (Math.cos((lambda1 - lambda2)) * 2.0)) * Math.tan(phi2))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 7e-5:
		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.sin(phi1) * math.sin(phi2)) / 2.0) / ((phi1 / (math.cos((lambda1 - lambda2)) * 2.0)) * math.tan(phi2))))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 7e-5)
		tmp = Float64(R * acos(Float64(cos(phi1) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda1) * cos(lambda2))))));
	else
		tmp = Float64(R * acos(Float64(Float64(Float64(sin(phi1) * sin(phi2)) / 2.0) / Float64(Float64(phi1 / Float64(cos(Float64(lambda1 - lambda2)) * 2.0)) * tan(phi2)))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 7e-5)
		tmp = R * acos((cos(phi1) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda1) * cos(lambda2)))));
	else
		tmp = R * acos((((sin(phi1) * sin(phi2)) / 2.0) / ((phi1 / (cos((lambda1 - lambda2)) * 2.0)) * tan(phi2))));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 7e-5], 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[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] / N[(N[(phi1 / N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision] * N[Tan[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 7 \cdot 10^{-5}:\\
\;\;\;\;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(\frac{\frac{\sin \phi_1 \cdot \sin \phi_2}{2}}{\frac{\phi_1}{\cos \left(\lambda_1 - \lambda_2\right) \cdot 2} \cdot \tan \phi_2}\right)\\


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

    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. 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.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. Simplified54.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. *-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_1, \cos \lambda_2\right)\right)\right)\right), R\right) \]
      10. 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_1\right), \cos \lambda_2\right)\right)\right)\right), R\right) \]
      11. cos-lowering-cos.f6467.6%

        \[\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_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    7. Applied egg-rr67.6%

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

    if 6.9999999999999994e-5 < 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. Step-by-step derivation
      1. +-commutativeN/A

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(\left(\left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \frac{2}{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)} + 2 \cdot 1\right), \left(2 \cdot \frac{2}{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}\right)\right)\right), R\right) \]
    4. Applied egg-rr40.3%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\cos \left(\lambda_1 - \lambda_2\right), \left(\cos \phi_2 + \cos \left(\mathsf{neg}\left(\phi_2\right)\right)\right)\right), \left(\phi_1 \cdot \sin \phi_2\right)\right), \mathsf{*.f64}\left(2, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right)\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{*.f64}\left(\cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right), \left(\cos \phi_2 + \cos \left(\mathsf{neg}\left(\phi_2\right)\right)\right)\right), \left(\phi_1 \cdot \sin \phi_2\right)\right), \mathsf{*.f64}\left(2, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right)\right)\right)\right)\right), R\right) \]
      4. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right), \left(\cos \phi_2 + \cos \left(\mathsf{neg}\left(\phi_2\right)\right)\right)\right), \left(\phi_1 \cdot \sin \phi_2\right)\right), \mathsf{*.f64}\left(2, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\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{*.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right), \left(\cos \phi_2 + \cos \left(\mathsf{neg}\left(\phi_2\right)\right)\right)\right), \left(\phi_1 \cdot \sin \phi_2\right)\right), \mathsf{*.f64}\left(2, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right)\right)\right)\right)\right), R\right) \]
      6. neg-mul-1N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(2, \mathsf{cos.f64}\left(\phi_2\right)\right)\right), \mathsf{*.f64}\left(\phi_1, \mathsf{sin.f64}\left(\phi_2\right)\right)\right), \mathsf{*.f64}\left(2, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right)\right)\right)\right)\right), R\right) \]
    7. Simplified41.6%

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

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

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

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

Alternative 18: 57.9% accurate, 1.2× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 4.9 \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(\frac{\frac{\sin \phi_1 \cdot \sin \phi_2}{2}}{\frac{\phi_1}{\cos \left(\lambda_1 - \lambda_2\right) \cdot 2} \cdot \tan \phi_2}\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 4.9e-6)
   (* R (acos (* (cos phi1) (cos (- lambda2 lambda1)))))
   (*
    R
    (acos
     (/
      (/ (* (sin phi1) (sin phi2)) 2.0)
      (* (/ phi1 (* (cos (- lambda1 lambda2)) 2.0)) (tan phi2)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 4.9e-6) {
		tmp = R * acos((cos(phi1) * cos((lambda2 - lambda1))));
	} else {
		tmp = R * acos((((sin(phi1) * sin(phi2)) / 2.0) / ((phi1 / (cos((lambda1 - lambda2)) * 2.0)) * tan(phi2))));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (phi2 <= 4.9d-6) then
        tmp = r * acos((cos(phi1) * cos((lambda2 - lambda1))))
    else
        tmp = r * acos((((sin(phi1) * sin(phi2)) / 2.0d0) / ((phi1 / (cos((lambda1 - lambda2)) * 2.0d0)) * tan(phi2))))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 4.9e-6) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos((lambda2 - lambda1))));
	} else {
		tmp = R * Math.acos((((Math.sin(phi1) * Math.sin(phi2)) / 2.0) / ((phi1 / (Math.cos((lambda1 - lambda2)) * 2.0)) * Math.tan(phi2))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 4.9e-6:
		tmp = R * math.acos((math.cos(phi1) * math.cos((lambda2 - lambda1))))
	else:
		tmp = R * math.acos((((math.sin(phi1) * math.sin(phi2)) / 2.0) / ((phi1 / (math.cos((lambda1 - lambda2)) * 2.0)) * math.tan(phi2))))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 4.9e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(Float64(lambda2 - lambda1)))));
	else
		tmp = Float64(R * acos(Float64(Float64(Float64(sin(phi1) * sin(phi2)) / 2.0) / Float64(Float64(phi1 / Float64(cos(Float64(lambda1 - lambda2)) * 2.0)) * tan(phi2)))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 4.9e-6)
		tmp = R * acos((cos(phi1) * cos((lambda2 - lambda1))));
	else
		tmp = R * acos((((sin(phi1) * sin(phi2)) / 2.0) / ((phi1 / (cos((lambda1 - lambda2)) * 2.0)) * tan(phi2))));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 4.9e-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[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] / N[(N[(phi1 / N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * 2.0), $MachinePrecision]), $MachinePrecision] * N[Tan[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 4.9 \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(\frac{\frac{\sin \phi_1 \cdot \sin \phi_2}{2}}{\frac{\phi_1}{\cos \left(\lambda_1 - \lambda_2\right) \cdot 2} \cdot \tan \phi_2}\right)\\


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

    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. 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.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. Simplified54.2%

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

    if 4.89999999999999967e-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. Step-by-step derivation
      1. +-commutativeN/A

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(\left(\left(\left(\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \frac{2}{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)} + 2 \cdot 1\right), \left(2 \cdot \frac{2}{\cos \left(\phi_1 - \phi_2\right) - \cos \left(\phi_1 + \phi_2\right)}\right)\right)\right), R\right) \]
    4. Applied egg-rr40.3%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\cos \left(\lambda_1 - \lambda_2\right), \left(\cos \phi_2 + \cos \left(\mathsf{neg}\left(\phi_2\right)\right)\right)\right), \left(\phi_1 \cdot \sin \phi_2\right)\right), \mathsf{*.f64}\left(2, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right)\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{*.f64}\left(\cos \left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right), \left(\cos \phi_2 + \cos \left(\mathsf{neg}\left(\phi_2\right)\right)\right)\right), \left(\phi_1 \cdot \sin \phi_2\right)\right), \mathsf{*.f64}\left(2, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right)\right)\right)\right)\right), R\right) \]
      4. neg-mul-1N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right), \left(\cos \phi_2 + \cos \left(\mathsf{neg}\left(\phi_2\right)\right)\right)\right), \left(\phi_1 \cdot \sin \phi_2\right)\right), \mathsf{*.f64}\left(2, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\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{*.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right), \left(\cos \phi_2 + \cos \left(\mathsf{neg}\left(\phi_2\right)\right)\right)\right), \left(\phi_1 \cdot \sin \phi_2\right)\right), \mathsf{*.f64}\left(2, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right)\right)\right)\right)\right), R\right) \]
      6. neg-mul-1N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{/.f64}\left(\mathsf{/.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{*.f64}\left(2, \mathsf{cos.f64}\left(\phi_2\right)\right)\right), \mathsf{*.f64}\left(\phi_1, \mathsf{sin.f64}\left(\phi_2\right)\right)\right), \mathsf{*.f64}\left(2, \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right)\right)\right)\right)\right), R\right) \]
    7. Simplified41.6%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 4.9 \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(\frac{\frac{\sin \phi_1 \cdot \sin \phi_2}{2}}{\frac{\phi_1}{\cos \left(\lambda_1 - \lambda_2\right) \cdot 2} \cdot \tan \phi_2}\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 19: 28.9% accurate, 1.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;t\_0 \leq 0.93:\\ \;\;\;\;R \cdot \cos^{-1} t\_0\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (if (<= t_0 0.93)
     (* R (acos t_0))
     (* R (fabs (remainder (- lambda1 lambda2) (* PI 2.0)))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.93) {
		tmp = R * acos(t_0);
	} else {
		tmp = R * fabs(remainder((lambda1 - lambda2), (((double) M_PI) * 2.0)));
	}
	return tmp;
}
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.93) {
		tmp = R * Math.acos(t_0);
	} else {
		tmp = R * Math.abs(Math.IEEEremainder((lambda1 - lambda2), (Math.PI * 2.0)));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	tmp = 0
	if t_0 <= 0.93:
		tmp = R * math.acos(t_0)
	else:
		tmp = R * math.fabs(math.remainder((lambda1 - lambda2), (math.pi * 2.0)))
	return tmp
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[t$95$0, 0.93], N[(R * N[ArcCos[t$95$0], $MachinePrecision]), $MachinePrecision], N[(R * N[Abs[N[With[{TMP1 = N[(lambda1 - lambda2), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;t\_0 \leq 0.93:\\
\;\;\;\;R \cdot \cos^{-1} t\_0\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (cos.f64 (-.f64 lambda1 lambda2)) < 0.930000000000000049

    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--.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. Taylor expanded in phi1 around 0

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

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

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

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

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

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

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

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

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

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

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

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

    if 0.930000000000000049 < (cos.f64 (-.f64 lambda1 lambda2))

    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. 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--.f6433.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. Simplified33.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--.f649.0%

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\left(\left|\left(\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|\right), R\right) \]
      3. fabs-lowering-fabs.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{fabs.f64}\left(\left(\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right), R\right) \]
      4. remainder-lowering-remainder.f64N/A

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{fabs.f64}\left(\mathsf{remainder.f64}\left(\left(\lambda_1 - \lambda_2\right), \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right), R\right) \]
      6. --lowering--.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{fabs.f64}\left(\mathsf{remainder.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right), \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right), R\right) \]
      7. *-lowering-*.f64N/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{fabs.f64}\left(\mathsf{remainder.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right), \mathsf{*.f64}\left(2, \mathsf{PI}\left(\right)\right)\right)\right), R\right) \]
      8. PI-lowering-PI.f6422.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{fabs.f64}\left(\mathsf{remainder.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right), \mathsf{*.f64}\left(2, \mathsf{PI.f64}\left(\right)\right)\right)\right), R\right) \]
    10. Applied egg-rr22.0%

      \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \pi\right)\right)\right|} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification31.6%

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

Alternative 20: 57.8% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\ \mathbf{if}\;\phi_2 \leq 9.5 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda2 lambda1))))
   (if (<= phi2 9.5e-6)
     (* R (acos (* (cos phi1) t_0)))
     (* R (acos (* (cos phi2) t_0))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda2 - lambda1));
	double tmp;
	if (phi2 <= 9.5e-6) {
		tmp = R * acos((cos(phi1) * t_0));
	} else {
		tmp = R * acos((cos(phi2) * t_0));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos((lambda2 - lambda1))
    if (phi2 <= 9.5d-6) then
        tmp = r * acos((cos(phi1) * t_0))
    else
        tmp = r * acos((cos(phi2) * t_0))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda2 - lambda1));
	double tmp;
	if (phi2 <= 9.5e-6) {
		tmp = R * Math.acos((Math.cos(phi1) * t_0));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * t_0));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda2 - lambda1))
	tmp = 0
	if phi2 <= 9.5e-6:
		tmp = R * math.acos((math.cos(phi1) * t_0))
	else:
		tmp = R * math.acos((math.cos(phi2) * t_0))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda2 - lambda1))
	tmp = 0.0
	if (phi2 <= 9.5e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * t_0)));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * t_0)));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda2 - lambda1));
	tmp = 0.0;
	if (phi2 <= 9.5e-6)
		tmp = R * acos((cos(phi1) * t_0));
	else
		tmp = R * acos((cos(phi2) * t_0));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, 9.5e-6], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
\mathbf{if}\;\phi_2 \leq 9.5 \cdot 10^{-6}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

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


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

    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. 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.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. Simplified54.2%

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

    if 9.5000000000000005e-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 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--.f6441.5%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 9.5 \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 \left(\lambda_2 - \lambda_1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 21: 29.8% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;t\_0 \leq 0.99999:\\ \;\;\;\;R \cdot \cos^{-1} t\_0\\ \mathbf{else}:\\ \;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (if (<= t_0 0.99999) (* R (acos t_0)) (* (- lambda2 lambda1) R))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.99999) {
		tmp = R * acos(t_0);
	} else {
		tmp = (lambda2 - lambda1) * R;
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos((lambda1 - lambda2))
    if (t_0 <= 0.99999d0) then
        tmp = r * acos(t_0)
    else
        tmp = (lambda2 - lambda1) * r
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.99999) {
		tmp = R * Math.acos(t_0);
	} else {
		tmp = (lambda2 - lambda1) * R;
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	tmp = 0
	if t_0 <= 0.99999:
		tmp = R * math.acos(t_0)
	else:
		tmp = (lambda2 - lambda1) * R
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (t_0 <= 0.99999)
		tmp = Float64(R * acos(t_0));
	else
		tmp = Float64(Float64(lambda2 - lambda1) * R);
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda1 - lambda2));
	tmp = 0.0;
	if (t_0 <= 0.99999)
		tmp = R * acos(t_0);
	else
		tmp = (lambda2 - lambda1) * R;
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[t$95$0, 0.99999], N[(R * N[ArcCos[t$95$0], $MachinePrecision]), $MachinePrecision], N[(N[(lambda2 - lambda1), $MachinePrecision] * R), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;t\_0 \leq 0.99999:\\
\;\;\;\;R \cdot \cos^{-1} t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (cos.f64 (-.f64 lambda1 lambda2)) < 0.999990000000000046

    1. Initial program 73.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--.f6449.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. Simplified49.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 0.999990000000000046 < (cos.f64 (-.f64 lambda1 lambda2))

    1. Initial program 74.9%

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

      \[\leadsto \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--.f6433.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. Simplified33.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--.f645.0%

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
      9. sin-lowering-sin.f645.0%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
    10. Applied egg-rr5.0%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    11. Step-by-step derivation
      1. *-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) \]
      2. cos-diffN/A

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

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

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

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

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

Alternative 22: 42.5% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 1.45 \cdot 10^{-11}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda2 1.45e-11)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (* R (acos (* (cos phi1) (cos lambda2))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 1.45e-11) {
		tmp = R * acos((cos(phi1) * 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.
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.45d-11) then
        tmp = r * acos((cos(phi1) * 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;
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.45e-11) {
		tmp = R * Math.acos((Math.cos(phi1) * 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])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= 1.45e-11:
		tmp = R * math.acos((math.cos(phi1) * 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])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 1.45e-11)
		tmp = Float64(R * acos(Float64(cos(phi1) * 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])){:}
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.45e-11)
		tmp = R * acos((cos(phi1) * 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.
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.45e-11], N[(R * N[ArcCos[N[(N[Cos[phi1], $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])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 1.45 \cdot 10^{-11}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda2 < 1.45e-11

    1. Initial program 77.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Taylor expanded in 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.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. Simplified45.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(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. cos-negN/A

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right), R\right) \]
    8. Simplified35.1%

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

    if 1.45e-11 < lambda2

    1. Initial program 62.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--.f6446.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. Simplified46.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(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \color{blue}{\cos \lambda_2}\right)\right), R\right) \]
    7. Step-by-step derivation
      1. cos-lowering-cos.f6446.1%

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

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

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

Alternative 23: 37.2% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 1.85 \cdot 10^{-20}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda2 1.85e-20)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (* R (acos (cos (- lambda1 lambda2))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 1.85e-20) {
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	} else {
		tmp = R * acos(cos((lambda1 - lambda2)));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (lambda2 <= 1.85d-20) then
        tmp = r * acos((cos(phi1) * cos(lambda1)))
    else
        tmp = r * acos(cos((lambda1 - lambda2)))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 1.85e-20) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda1)));
	} else {
		tmp = R * Math.acos(Math.cos((lambda1 - lambda2)));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= 1.85e-20:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda1)))
	else:
		tmp = R * math.acos(math.cos((lambda1 - lambda2)))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 1.85e-20)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda1))));
	else
		tmp = Float64(R * acos(cos(Float64(lambda1 - lambda2))));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= 1.85e-20)
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	else
		tmp = R * acos(cos((lambda1 - lambda2)));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, 1.85e-20], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 1.85 \cdot 10^{-20}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda2 < 1.85e-20

    1. Initial program 76.9%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6446.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. Simplified46.0%

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right), R\right) \]
    8. Simplified35.2%

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

    if 1.85e-20 < lambda2

    1. Initial program 63.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 24: 42.6% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right) \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R (acos (* (cos phi1) (cos (- lambda2 lambda1))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * acos((cos(phi1) * cos((lambda2 - lambda1))));
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = r * acos((cos(phi1) * cos((lambda2 - lambda1))))
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * Math.acos((Math.cos(phi1) * Math.cos((lambda2 - lambda1))));
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	return R * math.acos((math.cos(phi1) * math.cos((lambda2 - lambda1))))
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(R * acos(Float64(cos(phi1) * cos(Float64(lambda2 - lambda1)))))
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = R * acos((cos(phi1) * cos((lambda2 - lambda1))));
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)
\end{array}
Derivation
  1. Initial program 73.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--.f6445.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. Simplified45.9%

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

    \[\leadsto R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right) \]
  7. Add Preprocessing

Alternative 25: 26.5% accurate, 2.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 2 \cdot 10^{-145}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{elif}\;\lambda_2 \leq 0.0005:\\ \;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda2 2e-145)
   (* R (acos (cos lambda1)))
   (if (<= lambda2 0.0005)
     (* (- lambda2 lambda1) R)
     (* R (acos (cos lambda2))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 2e-145) {
		tmp = R * acos(cos(lambda1));
	} else if (lambda2 <= 0.0005) {
		tmp = (lambda2 - lambda1) * R;
	} else {
		tmp = R * acos(cos(lambda2));
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (lambda2 <= 2d-145) then
        tmp = r * acos(cos(lambda1))
    else if (lambda2 <= 0.0005d0) then
        tmp = (lambda2 - lambda1) * r
    else
        tmp = r * acos(cos(lambda2))
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 2e-145) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else if (lambda2 <= 0.0005) {
		tmp = (lambda2 - lambda1) * R;
	} else {
		tmp = R * Math.acos(Math.cos(lambda2));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= 2e-145:
		tmp = R * math.acos(math.cos(lambda1))
	elif lambda2 <= 0.0005:
		tmp = (lambda2 - lambda1) * R
	else:
		tmp = R * math.acos(math.cos(lambda2))
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 2e-145)
		tmp = Float64(R * acos(cos(lambda1)));
	elseif (lambda2 <= 0.0005)
		tmp = Float64(Float64(lambda2 - lambda1) * R);
	else
		tmp = Float64(R * acos(cos(lambda2)));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= 2e-145)
		tmp = R * acos(cos(lambda1));
	elseif (lambda2 <= 0.0005)
		tmp = (lambda2 - lambda1) * R;
	else
		tmp = R * acos(cos(lambda2));
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, 2e-145], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda2, 0.0005], N[(N[(lambda2 - lambda1), $MachinePrecision] * R), $MachinePrecision], N[(R * N[ArcCos[N[Cos[lambda2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 2 \cdot 10^{-145}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{elif}\;\lambda_2 \leq 0.0005:\\
\;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\


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

    1. Initial program 76.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--.f6448.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. Simplified48.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--.f6429.2%

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
    9. 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.f6417.8%

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

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

    if 1.99999999999999983e-145 < lambda2 < 5.0000000000000001e-4

    1. Initial program 77.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--.f6430.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. Simplified30.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--.f6415.8%

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
      9. sin-lowering-sin.f6415.8%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
    10. Applied egg-rr15.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    11. Step-by-step derivation
      1. *-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) \]
      2. cos-diffN/A

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right), R\right) \]
    12. Applied egg-rr21.5%

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

    if 5.0000000000000001e-4 < lambda2

    1. Initial program 62.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--.f6446.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. Simplified46.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--.f6429.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. Simplified29.9%

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

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

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

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

Alternative 26: 20.6% accurate, 2.9× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -20:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;\left(\lambda_2 - \lambda_1\right) \cdot R\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= (- lambda1 lambda2) -20.0)
   (* R (acos (cos lambda1)))
   (* (- lambda2 lambda1) R)))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if ((lambda1 - lambda2) <= -20.0) {
		tmp = R * acos(cos(lambda1));
	} else {
		tmp = (lambda2 - lambda1) * R;
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if ((lambda1 - lambda2) <= (-20.0d0)) then
        tmp = r * acos(cos(lambda1))
    else
        tmp = (lambda2 - lambda1) * r
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if ((lambda1 - lambda2) <= -20.0) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else {
		tmp = (lambda2 - lambda1) * R;
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if (lambda1 - lambda2) <= -20.0:
		tmp = R * math.acos(math.cos(lambda1))
	else:
		tmp = (lambda2 - lambda1) * R
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (Float64(lambda1 - lambda2) <= -20.0)
		tmp = Float64(R * acos(cos(lambda1)));
	else
		tmp = Float64(Float64(lambda2 - lambda1) * R);
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if ((lambda1 - lambda2) <= -20.0)
		tmp = R * acos(cos(lambda1));
	else
		tmp = (lambda2 - lambda1) * R;
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[N[(lambda1 - lambda2), $MachinePrecision], -20.0], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[(lambda2 - lambda1), $MachinePrecision] * R), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 - \lambda_2 \leq -20:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (-.f64 lambda1 lambda2) < -20

    1. Initial program 73.3%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right), R\right) \]
      12. --lowering--.f6450.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. Simplified50.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 phi1 around 0

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -20 < (-.f64 lambda1 lambda2)

    1. Initial program 73.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--.f6443.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. Simplified43.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--.f6425.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. Simplified25.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(\mathsf{acos.f64}\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right), R\right) \]
      2. *-commutativeN/A

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
      9. sin-lowering-sin.f6429.8%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
    10. Applied egg-rr29.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    11. Step-by-step derivation
      1. *-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) \]
      2. cos-diffN/A

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

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

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

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

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

Alternative 27: 9.2% accurate, 122.6× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \left(\lambda_2 - \lambda_1\right) \cdot R \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* (- lambda2 lambda1) R))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return (lambda2 - lambda1) * R;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = (lambda2 - lambda1) * r
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return (lambda2 - lambda1) * R;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	return (lambda2 - lambda1) * R
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(Float64(lambda2 - lambda1) * R)
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = (lambda2 - lambda1) * R;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[(lambda2 - lambda1), $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\left(\lambda_2 - \lambda_1\right) \cdot R
\end{array}
Derivation
  1. Initial program 73.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--.f6445.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. Simplified45.9%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    10. --lowering--.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. Step-by-step derivation
    1. cos-diffN/A

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\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), R\right) \]
  10. Applied egg-rr33.4%

    \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
  11. Step-by-step derivation
    1. *-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) \]
    2. cos-diffN/A

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right), R\right) \]
  12. Applied egg-rr6.1%

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

Alternative 28: 3.7% accurate, 204.3× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\ [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \lambda_1 \cdot R \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2) :precision binary64 (* lambda1 R))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return lambda1 * R;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = lambda1 * r
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return lambda1 * R;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	return lambda1 * R
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(lambda1 * R)
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = lambda1 * R;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(lambda1 * R), $MachinePrecision]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\\\
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\lambda_1 \cdot R
\end{array}
Derivation
  1. Initial program 73.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--.f6445.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. Simplified45.9%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    10. --lowering--.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 lambda1 around inf

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

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

    Reproduce

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