Spherical law of cosines

Percentage Accurate: 73.0% → 96.3%
Time: 29.2s
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.0% 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.3% accurate, 0.4× speedup?

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

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


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

    1. Initial program 18.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. 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) \]
    3. Simplified18.6%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6418.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) \]
    7. Simplified18.6%

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

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

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

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

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

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

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

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

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

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

    1. Initial program 74.5%

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

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 96.4% accurate, 0.4× speedup?

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

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


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

    1. Initial program 18.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. 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) \]
    3. Simplified18.6%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6418.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) \]
    7. Simplified18.6%

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

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

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

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

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

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

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

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

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

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

    1. Initial program 74.5%

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right), R\right) \]
      2. cos-multN/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 \left(\lambda_1 - \lambda_2\right) \cdot \frac{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}{2}\right)\right)\right), R\right) \]
      3. clear-numN/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 \left(\lambda_1 - \lambda_2\right) \cdot \frac{1}{\frac{2}{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}}\right)\right)\right), R\right) \]
      4. un-div-invN/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(\frac{\cos \left(\lambda_1 - \lambda_2\right)}{\frac{2}{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\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(\cos \left(\lambda_1 - \lambda_2\right), \left(\frac{2}{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \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{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right), \left(\frac{2}{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \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{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \left(\frac{2}{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}\right)\right)\right)\right), R\right) \]
      8. clear-numN/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(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \left(\frac{1}{\frac{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}{2}}\right)\right)\right)\right), R\right) \]
      9. cos-multN/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(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \left(\frac{1}{\cos \phi_1 \cdot \cos \phi_2}\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{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(1, \left(\cos \phi_1 \cdot \cos \phi_2\right)\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(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(1, \left(\cos \phi_2 \cdot \cos \phi_1\right)\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(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\cos \phi_2, \cos \phi_1\right)\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(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \phi_1\right)\right)\right)\right)\right), R\right) \]
      14. cos-lowering-cos.f6474.5%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\frac{\cos \left(\lambda_1 - \lambda_2\right)}{\frac{1}{\cos \phi_2 \cdot \cos \phi_1}}}\right) \cdot R \]
    5. 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(\frac{\cos \left(\lambda_1 - \lambda_2\right)}{1} \cdot \left(\cos \phi_2 \cdot \cos \phi_1\right)\right)\right)\right), R\right) \]
      2. /-rgt-identityN/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 \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_2 \cdot \cos \phi_1\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), \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right), R\right) \]
      4. *-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 \left(\lambda_1 - \lambda_2\right)\right)\right)\right), R\right) \]
      5. 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), \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      6. distribute-lft-outN/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 \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) \]
      7. 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(\cos \phi_1 \cdot \left(\cos \phi_2 \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)\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(\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot \cos \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right), R\right) \]
      9. 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(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot \cos \phi_1 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right)\right), R\right) \]
      10. accelerator-lowering-fma.f64N/A

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

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

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

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

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

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

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

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

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

Alternative 3: 96.4% accurate, 0.4× speedup?

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

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


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

    1. Initial program 18.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. 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) \]
    3. Simplified18.6%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6418.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) \]
    7. Simplified18.6%

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

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

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

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

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

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

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

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

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

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

    1. Initial program 74.5%

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

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

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

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

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

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

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

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

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

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

Alternative 4: 96.4% accurate, 0.4× speedup?

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

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


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

    1. Initial program 18.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. 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) \]
    3. Simplified18.6%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6418.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) \]
    7. Simplified18.6%

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

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

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

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

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

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

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

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

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

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

    1. Initial program 74.5%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. 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.f6499.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-rr99.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 \]
  3. Recombined 2 regimes into one program.
  4. Final simplification95.5%

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

Alternative 5: 83.2% accurate, 0.8× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < -4.89999999999999967e-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. 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) \]
    3. Simplified69.5%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

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

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

    if -4.89999999999999967e-6 < phi2 < 6.99999999999999993e-4

    1. Initial program 68.9%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\phi_2 \cdot \sin \phi_1 + \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. Step-by-step derivation
      1. +-commutativeN/A

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

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

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

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

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

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

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

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

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

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

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

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

    if 6.99999999999999993e-4 < phi2

    1. Initial program 82.3%

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

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

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

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

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

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

Alternative 6: 83.2% accurate, 0.8× speedup?

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

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

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


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

    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. 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) \]
    3. Simplified69.5%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

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

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

    if -4.69999999999999972e-5 < phi2 < 6.99999999999999993e-4

    1. Initial program 68.9%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\left(\phi_2 \cdot \sin \phi_1 + \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. Step-by-step derivation
      1. +-commutativeN/A

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

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

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

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

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

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

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

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

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

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

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

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

    if 6.99999999999999993e-4 < phi2

    1. Initial program 82.3%

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

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

Alternative 7: 83.0% accurate, 0.8× speedup?

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

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

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


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

    1. Initial program 70.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. 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) \]
    3. Simplified70.4%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

        \[\leadsto \mathsf{+.f64}\left(\left(\frac{\mathsf{PI}\left(\right)}{2} \cdot R\right), \color{blue}{\left(\left(\mathsf{neg}\left(\sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right) \cdot R\right)}\right) \]
    6. Applied egg-rr70.3%

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

    if -9.7999999999999998e-11 < phi2 < 6.99999999999999993e-4

    1. Initial program 68.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. 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) \]
    3. Simplified68.4%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 6.99999999999999993e-4 < phi2

    1. Initial program 82.3%

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

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

Alternative 8: 83.1% accurate, 0.9× speedup?

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

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

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


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

    1. Initial program 70.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. 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) \]
    3. Simplified70.0%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

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

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

    if -5.4999999999999996e-10 < phi2 < 6.99999999999999993e-4

    1. Initial program 68.7%

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

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 6.99999999999999993e-4 < phi2

    1. Initial program 82.3%

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

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

Alternative 9: 83.1% accurate, 1.0× speedup?

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

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

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


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

    1. Initial program 70.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. 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) \]
    3. Simplified70.0%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

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

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

    if -3.7999999999999998e-10 < phi2 < 6.99999999999999993e-4

    1. Initial program 68.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 6.99999999999999993e-4 < phi2

    1. Initial program 82.3%

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

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

Alternative 10: 83.1% accurate, 1.0× speedup?

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

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

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


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

    1. Initial program 70.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(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right)\right), R\right) \]
      2. cos-multN/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 \left(\lambda_1 - \lambda_2\right) \cdot \frac{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}{2}\right)\right)\right), R\right) \]
      3. clear-numN/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 \left(\lambda_1 - \lambda_2\right) \cdot \frac{1}{\frac{2}{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}}\right)\right)\right), R\right) \]
      4. un-div-invN/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(\frac{\cos \left(\lambda_1 - \lambda_2\right)}{\frac{2}{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\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(\cos \left(\lambda_1 - \lambda_2\right), \left(\frac{2}{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \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{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right), \left(\frac{2}{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \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{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \left(\frac{2}{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}\right)\right)\right)\right), R\right) \]
      8. clear-numN/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(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \left(\frac{1}{\frac{\cos \left(\phi_1 + \phi_2\right) + \cos \left(\phi_1 - \phi_2\right)}{2}}\right)\right)\right)\right), R\right) \]
      9. cos-multN/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(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \left(\frac{1}{\cos \phi_1 \cdot \cos \phi_2}\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{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(1, \left(\cos \phi_1 \cdot \cos \phi_2\right)\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(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(1, \left(\cos \phi_2 \cdot \cos \phi_1\right)\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(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\cos \phi_2, \cos \phi_1\right)\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(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \cos \phi_1\right)\right)\right)\right)\right), R\right) \]
      14. cos-lowering-cos.f6469.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{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(1, \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\phi_1\right)\right)\right)\right)\right)\right), R\right) \]
    4. Applied egg-rr69.9%

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{/.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(\mathsf{/.f64}\left(1, \mathsf{cos.f64}\left(\phi_2\right)\right), \cos \phi_1\right)\right)\right)\right), R\right) \]
      10. cos-lowering-cos.f6469.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{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right), \mathsf{/.f64}\left(\mathsf{/.f64}\left(1, \mathsf{cos.f64}\left(\phi_2\right)\right), \mathsf{cos.f64}\left(\phi_1\right)\right)\right)\right)\right), R\right) \]
    6. Applied egg-rr69.9%

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

    if -5.4999999999999996e-10 < phi2 < 6.99999999999999993e-4

    1. Initial program 68.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 6.99999999999999993e-4 < phi2

    1. Initial program 82.3%

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

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

Alternative 11: 83.1% accurate, 1.0× speedup?

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

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

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


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

    1. Initial program 70.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. 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) \]
    3. Simplified70.4%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing

    if -2.54999999999999992e-11 < phi2 < 6.99999999999999993e-4

    1. Initial program 68.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 6.99999999999999993e-4 < phi2

    1. Initial program 82.3%

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

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

Alternative 12: 83.1% accurate, 1.0× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -1.06e-10 or 6.99999999999999993e-4 < phi2

    1. Initial program 75.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. 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) \]
    3. Simplified75.3%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing

    if -1.06e-10 < phi2 < 6.99999999999999993e-4

    1. Initial program 68.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

\mathbf{elif}\;\phi_2 \leq 8.6 \cdot 10^{-11}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

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


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

    1. Initial program 70.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. 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) \]
    3. Simplified70.0%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. Taylor expanded in lambda2 around 0

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \color{blue}{\cos \lambda_1}\right)\right)\right)\right), R\right) \]
    6. Step-by-step derivation
      1. cos-lowering-cos.f6453.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_2\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\lambda_1\right)\right)\right)\right)\right), R\right) \]
    7. Simplified53.8%

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

    if -5.79999999999999962e-10 < phi2 < 8.60000000000000003e-11

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 8.60000000000000003e-11 < phi2

    1. Initial program 81.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 14: 73.4% accurate, 1.0× speedup?

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

\mathbf{elif}\;\phi_1 \leq 3.2 \cdot 10^{-7}:\\
\;\;\;\;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_2\right)\right)\\


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

    1. Initial program 77.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -6.80000000000000012e-6 < phi1 < 3.2000000000000001e-7

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 3.2000000000000001e-7 < phi1

    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. 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) \]
    3. Simplified73.0%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    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 \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.f6454.7%

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

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

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

Alternative 15: 63.3% accurate, 1.0× speedup?

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

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


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

    1. Initial program 77.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -7.80000000000000049e-7 < phi1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 16: 60.0% accurate, 1.0× speedup?

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

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


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

    1. Initial program 69.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 0.0013500000000000001 < phi2

    1. Initial program 82.3%

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_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)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_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)\right), R\right) \]
      6. distribute-neg-inN/A

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_2\right), \mathsf{cos.f64}\left(\left(\lambda_2 - \lambda_1\right)\right)\right)\right)\right), R\right) \]
      12. --lowering--.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_2\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right)\right), R\right) \]
    5. Simplified52.6%

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

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

Alternative 17: 50.0% accurate, 1.2× speedup?

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

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


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

    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(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right)\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -4.69999999999999986e-4 < phi1

    1. Initial program 70.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(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right)\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_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)\right), R\right) \]
      5. mul-1-negN/A

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_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)\right), R\right) \]
      6. distribute-neg-inN/A

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

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

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

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

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

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

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

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

Alternative 18: 49.5% accurate, 1.2× speedup?

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

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


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

    1. Initial program 69.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(\mathsf{+.f64}\left(\mathsf{*.f64}\left(\mathsf{sin.f64}\left(\phi_1\right), \mathsf{sin.f64}\left(\phi_2\right)\right), \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right)\right), R\right) \]
    4. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 8.60000000000000003e-11 < phi2

    1. Initial program 81.7%

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

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6453.6%

        \[\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) \]
    7. Simplified53.6%

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

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

Alternative 19: 50.3% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\ \mathbf{if}\;\phi_1 \leq -3.1 \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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda2 lambda1))))
   (if (<= phi1 -3.1e-6)
     (* R (acos (* (cos phi1) t_0)))
     (* R (acos (* (cos phi2) t_0))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda2 - lambda1));
	double tmp;
	if (phi1 <= -3.1e-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.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos((lambda2 - lambda1))
    if (phi1 <= (-3.1d-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;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda2 - lambda1));
	double tmp;
	if (phi1 <= -3.1e-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])
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda2 - lambda1))
	tmp = 0
	if phi1 <= -3.1e-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])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda2 - lambda1))
	tmp = 0.0
	if (phi1 <= -3.1e-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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda2 - lambda1));
	tmp = 0.0;
	if (phi1 <= -3.1e-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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi1, -3.1e-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])\\
\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
\mathbf{if}\;\phi_1 \leq -3.1 \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 phi1 < -3.1e-6

    1. Initial program 77.2%

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

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6452.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) \]
    7. Simplified52.0%

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

    if -3.1e-6 < phi1

    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. 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) \]
    3. Simplified69.9%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6447.1%

        \[\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) \]
    7. Simplified47.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -3.1 \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 20: 47.1% accurate, 2.0× speedup?

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

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


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

    1. Initial program 69.3%

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

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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.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) \]
    7. Simplified51.0%

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

    if 72000 < phi2

    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. 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) \]
    3. Simplified82.0%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6454.7%

        \[\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) \]
    7. Simplified54.7%

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

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

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

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

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

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

Alternative 21: 42.4% accurate, 2.0× speedup?

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

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


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

    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. 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) \]
    3. Simplified76.4%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6439.3%

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

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

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

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

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

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

    if 2e-16 < lambda2

    1. Initial program 57.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. 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) \]
    3. Simplified57.3%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6439.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) \]
    7. Simplified39.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    8. 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) \]
    9. Step-by-step derivation
      1. cos-lowering-cos.f6437.7%

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\lambda_2\right)\right)\right), R\right) \]
    10. Simplified37.7%

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

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

Alternative 22: 42.0% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -2.5 \cdot 10^{-5}:\\ \;\;\;\;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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -2.5e-5)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (* R (acos (* (cos phi1) (cos lambda2))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -2.5e-5) {
		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.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (lambda1 <= (-2.5d-5)) 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;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -2.5e-5) {
		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])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -2.5e-5:
		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])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -2.5e-5)
		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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda1 <= -2.5e-5)
		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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -2.5e-5], 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])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -2.5 \cdot 10^{-5}:\\
\;\;\;\;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 lambda1 < -2.50000000000000012e-5

    1. Initial program 56.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. 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) \]
    3. Simplified56.7%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\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) \]
    10. Simplified36.9%

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

    if -2.50000000000000012e-5 < lambda1

    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. 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) \]
    3. Simplified76.4%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    7. Simplified47.1%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    8. 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) \]
    9. Step-by-step derivation
      1. cos-lowering-cos.f6437.5%

        \[\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) \]
    10. Simplified37.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -2.5 \cdot 10^{-5}:\\ \;\;\;\;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: 31.9% accurate, 2.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -9.4 \cdot 10^{-5}:\\ \;\;\;\;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.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -9.4e-5)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (* R (acos (cos (- lambda1 lambda2))))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -9.4e-5) {
		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.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (phi1 <= (-9.4d-5)) 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;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -9.4e-5) {
		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])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi1 <= -9.4e-5:
		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])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -9.4e-5)
		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])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi1 <= -9.4e-5)
		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.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -9.4e-5], 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])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -9.4 \cdot 10^{-5}:\\
\;\;\;\;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 phi1 < -9.39999999999999945e-5

    1. Initial program 77.2%

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

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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--.f6452.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) \]
    7. Simplified52.0%

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

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

    if -9.39999999999999945e-5 < phi1

    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. 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) \]
    3. Simplified69.9%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -9.4 \cdot 10^{-5}:\\ \;\;\;\;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: 29.3% accurate, 2.9× speedup?

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

\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) < -50

    1. Initial program 70.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. 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) \]
    3. Simplified70.4%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. 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.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) \]
    7. Simplified45.6%

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

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

    if -50 < (-.f64 lambda1 lambda2)

    1. Initial program 72.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. 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) \]
    3. Simplified72.3%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    8. 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) \]
    9. Step-by-step derivation
      1. sub-negN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -50:\\ \;\;\;\;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 25: 26.2% accurate, 2.9× speedup?

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

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


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

    1. Initial program 56.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. 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) \]
    3. Simplified56.7%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    8. 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) \]
    9. Step-by-step derivation
      1. sub-negN/A

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\cos \lambda_1}\right), R\right) \]
    12. Step-by-step derivation
      1. cos-lowering-cos.f6424.2%

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

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

    if -1.2e-5 < lambda1

    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. 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) \]
    3. Simplified76.4%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    7. Simplified47.1%

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\lambda_2\right)\right), R\right) \]
    13. Simplified20.7%

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

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

Alternative 26: 20.1% accurate, 2.9× speedup?

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

    1. Initial program 56.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. 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) \]
    3. Simplified56.7%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
    8. 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) \]
    9. Step-by-step derivation
      1. sub-negN/A

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\color{blue}{\cos \lambda_1}\right), R\right) \]
    12. Step-by-step derivation
      1. cos-lowering-cos.f6424.2%

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

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

    if -1.3000000000000001e-8 < lambda1

    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. 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) \]
    3. Simplified76.4%

      \[\leadsto \color{blue}{\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) \cdot R} \]
    4. Add Preprocessing
    5. 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) \]
    6. Step-by-step derivation
      1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{*.f64}\left(\mathsf{cos.f64}\left(\phi_1\right), \mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_2, \lambda_1\right)\right)\right)\right), R\right) \]
    7. Simplified47.1%

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1.3 \cdot 10^{-8}:\\ \;\;\;\;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.1% accurate, 122.6× speedup?

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

    \[\leadsto \color{blue}{\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) \cdot R} \]
  4. Add Preprocessing
  5. 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) \]
  6. Step-by-step derivation
    1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
  8. 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) \]
  9. Step-by-step derivation
    1. sub-negN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 28: 3.6% accurate, 204.3× speedup?

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

    \[\leadsto \color{blue}{\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) \cdot R} \]
  4. Add Preprocessing
  5. 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) \]
  6. Step-by-step derivation
    1. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
  8. 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) \]
  9. Step-by-step derivation
    1. sub-negN/A

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

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

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

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right)\right), R\right) \]
    5. neg-mul-1N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\mathsf{neg}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right)\right), R\right) \]
    6. cos-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)\right), R\right) \]
    7. cos-lowering-cos.f64N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + -1 \cdot \lambda_2\right)\right)\right), R\right) \]
    8. neg-mul-1N/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)\right), R\right) \]
    9. sub-negN/A

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\left(\lambda_1 - \lambda_2\right)\right)\right), R\right) \]
    10. --lowering--.f6426.4%

      \[\leadsto \mathsf{*.f64}\left(\mathsf{acos.f64}\left(\mathsf{cos.f64}\left(\mathsf{\_.f64}\left(\lambda_1, \lambda_2\right)\right)\right), R\right) \]
  10. Simplified26.4%

    \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)} \cdot R \]
  11. Taylor expanded in lambda1 around inf

    \[\leadsto \color{blue}{R \cdot \lambda_1} \]
  12. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \lambda_1 \cdot \color{blue}{R} \]
    2. *-lowering-*.f645.9%

      \[\leadsto \mathsf{*.f64}\left(\lambda_1, \color{blue}{R}\right) \]
  13. Simplified5.9%

    \[\leadsto \color{blue}{\lambda_1 \cdot R} \]
  14. Add Preprocessing

Reproduce

?
herbie shell --seed 2024191 
(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))