Spherical law of cosines

Percentage Accurate: 73.7% → 96.1%
Time: 34.1s
Alternatives: 27
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 27 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.7% 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.1% accurate, 0.3× speedup?

\[\begin{array}{l} [lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\ \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ \mathbf{if}\;t_0 + t_1 \cdot \cos \left(\lambda_1 - \lambda_2\right) \leq 0.9993:\\ \;\;\;\;\cos^{-1} \left(\left(e^{\mathsf{log1p}\left(t_0\right)} + -1\right) + t_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\lambda_2, R, -\lambda_1 \cdot R\right)\\ \end{array} \end{array} \]
NOTE: lambda1 and lambda2 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 (<= (+ t_0 (* t_1 (cos (- lambda1 lambda2)))) 0.9993)
     (*
      (acos
       (+
        (+ (exp (log1p t_0)) -1.0)
        (*
         t_1
         (fma
          (cos lambda2)
          (cos lambda1)
          (log (exp (* (sin lambda1) (sin lambda2))))))))
      R)
     (fma lambda2 R (- (* lambda1 R))))))
assert(lambda1 < lambda2);
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 ((t_0 + (t_1 * cos((lambda1 - lambda2)))) <= 0.9993) {
		tmp = acos(((exp(log1p(t_0)) + -1.0) + (t_1 * fma(cos(lambda2), cos(lambda1), log(exp((sin(lambda1) * sin(lambda2)))))))) * R;
	} else {
		tmp = fma(lambda2, R, -(lambda1 * R));
	}
	return tmp;
}
lambda1, lambda2 = sort([lambda1, lambda2])
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 (Float64(t_0 + Float64(t_1 * cos(Float64(lambda1 - lambda2)))) <= 0.9993)
		tmp = Float64(acos(Float64(Float64(exp(log1p(t_0)) + -1.0) + Float64(t_1 * fma(cos(lambda2), cos(lambda1), log(exp(Float64(sin(lambda1) * sin(lambda2)))))))) * R);
	else
		tmp = fma(lambda2, R, Float64(-Float64(lambda1 * R)));
	end
	return tmp
end
NOTE: lambda1 and lambda2 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[(t$95$0 + N[(t$95$1 * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.9993], N[(N[ArcCos[N[(N[(N[Exp[N[Log[1 + t$95$0], $MachinePrecision]], $MachinePrecision] + -1.0), $MachinePrecision] + N[(t$95$1 * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[Log[N[Exp[N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(lambda2 * R + (-N[(lambda1 * R), $MachinePrecision])), $MachinePrecision]]]]
\begin{array}{l}
[lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\
\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
\mathbf{if}\;t_0 + t_1 \cdot \cos \left(\lambda_1 - \lambda_2\right) \leq 0.9993:\\
\;\;\;\;\cos^{-1} \left(\left(e^{\mathsf{log1p}\left(t_0\right)} + -1\right) + t_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right) \cdot R\\

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


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

    1. Initial program 80.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. cos-diff99.1%

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

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

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

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\color{blue}{\cos \lambda_2}, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    5. Simplified99.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 \]
    6. Step-by-step derivation
      1. expm1-log1p-u99.1%

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

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

      \[\leadsto \cos^{-1} \left(\color{blue}{\left(e^{\mathsf{log1p}\left(\sin \phi_1 \cdot \sin \phi_2\right)} - 1\right)} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    8. Step-by-step derivation
      1. add-log-exp99.1%

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

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

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

    1. Initial program 10.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. fma-def10.2%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*10.2%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg10.2%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-110.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-110.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in10.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative10.2%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative10.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg10.2%

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

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

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

      \[\leadsto \color{blue}{\lambda_2 \cdot R + -1 \cdot \left(R \cdot \lambda_1\right)} \]
    9. Step-by-step derivation
      1. fma-def28.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\lambda_2, R, -1 \cdot \left(R \cdot \lambda_1\right)\right)} \]
      2. mul-1-neg28.0%

        \[\leadsto \mathsf{fma}\left(\lambda_2, R, \color{blue}{-R \cdot \lambda_1}\right) \]
      3. *-commutative28.0%

        \[\leadsto \mathsf{fma}\left(\lambda_2, R, -\color{blue}{\lambda_1 \cdot R}\right) \]
    10. Simplified28.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) \leq 0.9993:\\ \;\;\;\;\cos^{-1} \left(\left(e^{\mathsf{log1p}\left(\sin \phi_1 \cdot \sin \phi_2\right)} + -1\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\lambda_2, R, -\lambda_1 \cdot R\right)\\ \end{array} \]

Alternative 2: 96.1% accurate, 0.4× speedup?

\[\begin{array}{l} [lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\ \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ \mathbf{if}\;t_0 + t_1 \cdot \cos \left(\lambda_1 - \lambda_2\right) \leq 0.9993:\\ \;\;\;\;R \cdot \cos^{-1} \left(\left(e^{\mathsf{log1p}\left(t_0\right)} + -1\right) + t_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(\lambda_2, R, -\lambda_1 \cdot R\right)\\ \end{array} \end{array} \]
NOTE: lambda1 and lambda2 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 (<= (+ t_0 (* t_1 (cos (- lambda1 lambda2)))) 0.9993)
     (*
      R
      (acos
       (+
        (+ (exp (log1p t_0)) -1.0)
        (*
         t_1
         (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2)))))))
     (fma lambda2 R (- (* lambda1 R))))))
assert(lambda1 < lambda2);
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 ((t_0 + (t_1 * cos((lambda1 - lambda2)))) <= 0.9993) {
		tmp = R * acos(((exp(log1p(t_0)) + -1.0) + (t_1 * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2))))));
	} else {
		tmp = fma(lambda2, R, -(lambda1 * R));
	}
	return tmp;
}
lambda1, lambda2 = sort([lambda1, lambda2])
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 (Float64(t_0 + Float64(t_1 * cos(Float64(lambda1 - lambda2)))) <= 0.9993)
		tmp = Float64(R * acos(Float64(Float64(exp(log1p(t_0)) + -1.0) + Float64(t_1 * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2)))))));
	else
		tmp = fma(lambda2, R, Float64(-Float64(lambda1 * R)));
	end
	return tmp
end
NOTE: lambda1 and lambda2 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[(t$95$0 + N[(t$95$1 * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.9993], N[(R * N[ArcCos[N[(N[(N[Exp[N[Log[1 + t$95$0], $MachinePrecision]], $MachinePrecision] + -1.0), $MachinePrecision] + N[(t$95$1 * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(lambda2 * R + (-N[(lambda1 * R), $MachinePrecision])), $MachinePrecision]]]]
\begin{array}{l}
[lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\
\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
\mathbf{if}\;t_0 + t_1 \cdot \cos \left(\lambda_1 - \lambda_2\right) \leq 0.9993:\\
\;\;\;\;R \cdot \cos^{-1} \left(\left(e^{\mathsf{log1p}\left(t_0\right)} + -1\right) + t_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\

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


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

    1. Initial program 80.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. cos-diff99.1%

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

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

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

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\color{blue}{\cos \lambda_2}, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    5. Simplified99.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 \]
    6. Step-by-step derivation
      1. expm1-log1p-u99.1%

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

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

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

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

    1. Initial program 10.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. fma-def10.2%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*10.2%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg10.2%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-110.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-110.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in10.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative10.2%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative10.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg10.2%

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

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

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

      \[\leadsto \color{blue}{\lambda_2 \cdot R + -1 \cdot \left(R \cdot \lambda_1\right)} \]
    9. Step-by-step derivation
      1. fma-def28.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\lambda_2, R, -1 \cdot \left(R \cdot \lambda_1\right)\right)} \]
      2. mul-1-neg28.0%

        \[\leadsto \mathsf{fma}\left(\lambda_2, R, \color{blue}{-R \cdot \lambda_1}\right) \]
      3. *-commutative28.0%

        \[\leadsto \mathsf{fma}\left(\lambda_2, R, -\color{blue}{\lambda_1 \cdot R}\right) \]
    10. Simplified28.0%

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

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

Alternative 3: 96.2% accurate, 0.4× speedup?

\[\begin{array}{l} [lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\ \\ \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.038:\\ \;\;\;\;\mathsf{fma}\left(\lambda_2, R, -\lambda_1 \cdot R\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t_0 + t_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \end{array} \end{array} \]
NOTE: lambda1 and lambda2 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.038)
     (fma lambda2 R (- (* lambda1 R)))
     (*
      R
      (acos
       (+
        t_0
        (*
         t_1
         (fma
          (cos lambda2)
          (cos lambda1)
          (* (sin lambda1) (sin lambda2))))))))))
assert(lambda1 < lambda2);
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.038) {
		tmp = fma(lambda2, R, -(lambda1 * R));
	} else {
		tmp = R * acos((t_0 + (t_1 * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2))))));
	}
	return tmp;
}
lambda1, lambda2 = sort([lambda1, lambda2])
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.038)
		tmp = fma(lambda2, R, Float64(-Float64(lambda1 * R)));
	else
		tmp = Float64(R * acos(Float64(t_0 + Float64(t_1 * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2)))))));
	end
	return tmp
end
NOTE: lambda1 and lambda2 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.038], N[(lambda2 * R + (-N[(lambda1 * R), $MachinePrecision])), $MachinePrecision], N[(R * N[ArcCos[N[(t$95$0 + N[(t$95$1 * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\
\\
\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.038:\\
\;\;\;\;\mathsf{fma}\left(\lambda_2, R, -\lambda_1 \cdot R\right)\\

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


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

    1. Initial program 10.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. fma-def10.2%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*10.2%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg10.2%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-110.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-110.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in10.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative10.2%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative10.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg10.2%

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

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

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

      \[\leadsto \color{blue}{\lambda_2 \cdot R + -1 \cdot \left(R \cdot \lambda_1\right)} \]
    9. Step-by-step derivation
      1. fma-def28.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\lambda_2, R, -1 \cdot \left(R \cdot \lambda_1\right)\right)} \]
      2. mul-1-neg28.0%

        \[\leadsto \mathsf{fma}\left(\lambda_2, R, \color{blue}{-R \cdot \lambda_1}\right) \]
      3. *-commutative28.0%

        \[\leadsto \mathsf{fma}\left(\lambda_2, R, -\color{blue}{\lambda_1 \cdot R}\right) \]
    10. Simplified28.0%

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

    if 0.0379999999999999991 < (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 80.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. cos-diff99.1%

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

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

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

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

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

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

Alternative 4: 96.2% accurate, 0.4× speedup?

\[\begin{array}{l} [lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\ \\ \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.038:\\ \;\;\;\;\mathsf{fma}\left(\lambda_2, R, -\lambda_1 \cdot R\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t_0 + t_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \end{array} \]
NOTE: lambda1 and lambda2 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.038)
     (fma lambda2 R (- (* lambda1 R)))
     (*
      R
      (acos
       (+
        t_0
        (*
         t_1
         (+
          (* (sin lambda1) (sin lambda2))
          (* (cos lambda2) (cos lambda1))))))))))
assert(lambda1 < lambda2);
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.038) {
		tmp = fma(lambda2, R, -(lambda1 * R));
	} else {
		tmp = R * acos((t_0 + (t_1 * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1))))));
	}
	return tmp;
}
lambda1, lambda2 = sort([lambda1, lambda2])
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.038)
		tmp = fma(lambda2, R, Float64(-Float64(lambda1 * R)));
	else
		tmp = Float64(R * acos(Float64(t_0 + Float64(t_1 * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1)))))));
	end
	return tmp
end
NOTE: lambda1 and lambda2 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.038], N[(lambda2 * R + (-N[(lambda1 * R), $MachinePrecision])), $MachinePrecision], N[(R * N[ArcCos[N[(t$95$0 + N[(t$95$1 * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\
\\
\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.038:\\
\;\;\;\;\mathsf{fma}\left(\lambda_2, R, -\lambda_1 \cdot R\right)\\

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


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

    1. Initial program 10.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. fma-def10.2%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*10.2%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg10.2%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-110.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-110.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in10.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative10.2%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative10.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg10.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg10.2%

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

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

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

      \[\leadsto \color{blue}{\lambda_2 \cdot R + -1 \cdot \left(R \cdot \lambda_1\right)} \]
    9. Step-by-step derivation
      1. fma-def28.0%

        \[\leadsto \color{blue}{\mathsf{fma}\left(\lambda_2, R, -1 \cdot \left(R \cdot \lambda_1\right)\right)} \]
      2. mul-1-neg28.0%

        \[\leadsto \mathsf{fma}\left(\lambda_2, R, \color{blue}{-R \cdot \lambda_1}\right) \]
      3. *-commutative28.0%

        \[\leadsto \mathsf{fma}\left(\lambda_2, R, -\color{blue}{\lambda_1 \cdot R}\right) \]
    10. Simplified28.0%

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

    if 0.0379999999999999991 < (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 80.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. cos-diff99.1%

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

        \[\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. Applied egg-rr99.1%

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

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

Alternative 5: 83.3% accurate, 0.7× speedup?

\[\begin{array}{l} [lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\ \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ \mathbf{if}\;\phi_2 \leq -0.00033:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 6.5 \cdot 10^{+36}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \phi_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t_0 + t_1 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\ \end{array} \end{array} \]
NOTE: lambda1 and lambda2 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 (<= phi2 -0.00033)
     (*
      R
      (-
       (/ PI 2.0)
       (asin (+ t_0 (* (cos phi1) (* (cos phi2) (cos (- lambda2 lambda1))))))))
     (if (<= phi2 6.5e+36)
       (*
        R
        (acos
         (+
          (*
           t_1
           (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2))))
          (* (sin phi1) phi2))))
       (*
        R
        (acos (+ t_0 (* t_1 (expm1 (log1p (cos (- lambda1 lambda2))))))))))))
assert(lambda1 < lambda2);
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 (phi2 <= -0.00033) {
		tmp = R * ((((double) M_PI) / 2.0) - asin((t_0 + (cos(phi1) * (cos(phi2) * cos((lambda2 - lambda1)))))));
	} else if (phi2 <= 6.5e+36) {
		tmp = R * acos(((t_1 * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2)))) + (sin(phi1) * phi2)));
	} else {
		tmp = R * acos((t_0 + (t_1 * expm1(log1p(cos((lambda1 - lambda2)))))));
	}
	return tmp;
}
lambda1, lambda2 = sort([lambda1, lambda2])
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 (phi2 <= -0.00033)
		tmp = Float64(R * Float64(Float64(pi / 2.0) - asin(Float64(t_0 + Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda2 - lambda1))))))));
	elseif (phi2 <= 6.5e+36)
		tmp = Float64(R * acos(Float64(Float64(t_1 * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2)))) + Float64(sin(phi1) * phi2))));
	else
		tmp = Float64(R * acos(Float64(t_0 + Float64(t_1 * expm1(log1p(cos(Float64(lambda1 - lambda2))))))));
	end
	return tmp
end
NOTE: lambda1 and lambda2 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[phi2, -0.00033], N[(R * N[(N[(Pi / 2.0), $MachinePrecision] - N[ArcSin[N[(t$95$0 + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 6.5e+36], N[(R * N[ArcCos[N[(N[(t$95$1 * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * phi2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(t$95$0 + N[(t$95$1 * N[(Exp[N[Log[1 + N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]] - 1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
[lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\
\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
\mathbf{if}\;\phi_2 \leq -0.00033:\\
\;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(t_0 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\

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

\mathbf{else}:\\
\;\;\;\;R \cdot \cos^{-1} \left(t_0 + t_1 \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\


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

    1. Initial program 83.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. cos-diff98.9%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. cos-neg98.9%

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

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

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

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    6. Step-by-step derivation
      1. expm1-log1p-u98.9%

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

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

      \[\leadsto \cos^{-1} \left(\color{blue}{\left(e^{\mathsf{log1p}\left(\sin \phi_1 \cdot \sin \phi_2\right)} - 1\right)} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    8. Step-by-step derivation
      1. add-log-exp98.9%

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

      \[\leadsto \cos^{-1} \left(\left(e^{\mathsf{log1p}\left(\sin \phi_1 \cdot \sin \phi_2\right)} - 1\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \color{blue}{\log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)}\right)\right) \cdot R \]
    10. Step-by-step derivation
      1. acos-asin99.0%

        \[\leadsto \color{blue}{\left(\frac{\pi}{2} - \sin^{-1} \left(\left(e^{\mathsf{log1p}\left(\sin \phi_1 \cdot \sin \phi_2\right)} - 1\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right)\right)} \cdot R \]
      2. expm1-def99.0%

        \[\leadsto \left(\frac{\pi}{2} - \sin^{-1} \left(\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sin \phi_1 \cdot \sin \phi_2\right)\right)} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right)\right) \cdot R \]
      3. expm1-log1p-u99.0%

        \[\leadsto \left(\frac{\pi}{2} - \sin^{-1} \left(\color{blue}{\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, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right)\right) \cdot R \]
      4. associate-*l*99.0%

        \[\leadsto \left(\frac{\pi}{2} - \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right)}\right)\right) \cdot R \]
      5. fma-udef98.9%

        \[\leadsto \left(\frac{\pi}{2} - \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \color{blue}{\left(\cos \lambda_2 \cdot \cos \lambda_1 + \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)}\right)\right)\right) \cdot R \]
      6. add-log-exp98.9%

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

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

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

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

    if -3.3e-4 < phi2 < 6.4999999999999998e36

    1. Initial program 74.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. cos-diff90.4%

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

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

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

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\color{blue}{\cos \lambda_2}, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    5. Simplified90.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 \]
    6. Taylor expanded in phi2 around 0 89.7%

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

    if 6.4999999999999998e36 < phi2

    1. Initial program 75.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. expm1-log1p-u75.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{expm1}\left(\mathsf{log1p}\left(\cos \left(\lambda_1 - \lambda_2\right)\right)\right)}\right) \cdot R \]
    3. Applied egg-rr75.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{expm1}\left(\mathsf{log1p}\left(\cos \left(\lambda_1 - \lambda_2\right)\right)\right)}\right) \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification85.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -0.00033:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 6.5 \cdot 10^{+36}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right) + \sin \phi_1 \cdot \phi_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{expm1}\left(\mathsf{log1p}\left(\cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\ \end{array} \]

Alternative 6: 83.7% accurate, 0.7× speedup?

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

\mathbf{elif}\;\phi_1 \leq 0.115:\\
\;\;\;\;R \cdot \cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \left(1 + -0.5 \cdot \left(\phi_1 \cdot \phi_1\right)\right) \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)\\

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


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

    1. Initial program 85.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. add-cube-cbrt85.5%

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

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

      \[\leadsto \cos^{-1} \left(\color{blue}{{\left(\sqrt[3]{\sin \phi_1 \cdot \sin \phi_2}\right)}^{3}} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    4. Step-by-step derivation
      1. rem-cube-cbrt85.6%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\log \left(e^{\sin \phi_1 \cdot \sin \phi_2}\right)} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    5. Applied egg-rr85.6%

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

    if -3.6999999999999999e-4 < phi1 < 0.115000000000000005

    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. cos-diff89.5%

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

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

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

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\color{blue}{\cos \lambda_2}, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    5. Simplified89.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 \]
    6. Taylor expanded in phi1 around 0 88.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(-0.5 \cdot \left({\phi_1}^{2} \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right) + \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right) + \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. associate-+r+88.9%

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\phi_1 \cdot \sin \phi_2 + \left(-0.5 \cdot \left({\phi_1}^{2} \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right) + \cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right)} \cdot R \]
      3. associate-*r*88.9%

        \[\leadsto \cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \left(\color{blue}{\left(-0.5 \cdot {\phi_1}^{2}\right) \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)} + \cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)\right) \cdot R \]
      4. distribute-lft1-in88.9%

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

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

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

        \[\leadsto \cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \left(-0.5 \cdot \left(\phi_1 \cdot \phi_1\right) + 1\right) \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) \cdot R \]
    8. Simplified88.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_1 \cdot \sin \phi_2 + \left(-0.5 \cdot \left(\phi_1 \cdot \phi_1\right) + 1\right) \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 R \]

    if 0.115000000000000005 < phi1

    1. Initial program 79.1%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. fma-def79.1%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*79.1%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.00037:\\ \;\;\;\;R \cdot \cos^{-1} \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \log \left(e^{\sin \phi_1 \cdot \sin \phi_2}\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 0.115:\\ \;\;\;\;R \cdot \cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \left(1 + -0.5 \cdot \left(\phi_1 \cdot \phi_1\right)\right) \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)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\ \end{array} \]

Alternative 7: 82.2% accurate, 0.8× speedup?

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

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

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


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

    1. Initial program 86.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. add-cube-cbrt85.9%

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\sqrt[3]{\sin \phi_1 \cdot \sin \phi_2} \cdot \sqrt[3]{\sin \phi_1 \cdot \sin \phi_2}\right) \cdot \sqrt[3]{\sin \phi_1 \cdot \sin \phi_2}} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      2. pow385.9%

        \[\leadsto \cos^{-1} \left(\color{blue}{{\left(\sqrt[3]{\sin \phi_1 \cdot \sin \phi_2}\right)}^{3}} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    3. Applied egg-rr85.9%

      \[\leadsto \cos^{-1} \left(\color{blue}{{\left(\sqrt[3]{\sin \phi_1 \cdot \sin \phi_2}\right)}^{3}} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    4. Step-by-step derivation
      1. rem-cube-cbrt86.0%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\log \left(e^{\sin \phi_1 \cdot \sin \phi_2}\right)} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    5. Applied egg-rr86.0%

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

    if -7.19999999999999962e-8 < phi1 < 1.2e17

    1. Initial program 71.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. cos-diff89.6%

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. +-commutative85.0%

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

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

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

    if 1.2e17 < phi1

    1. Initial program 79.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. fma-def79.4%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*79.4%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -7.2 \cdot 10^{-8}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \log \left(e^{\sin \phi_1 \cdot \sin \phi_2}\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 1.2 \cdot 10^{+17}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\ \end{array} \]

Alternative 8: 82.2% accurate, 0.9× speedup?

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

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

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


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

    1. Initial program 86.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. cos-diff99.0%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. cos-neg99.0%

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

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

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

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    6. Step-by-step derivation
      1. expm1-log1p-u99.0%

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

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

      \[\leadsto \cos^{-1} \left(\color{blue}{\left(e^{\mathsf{log1p}\left(\sin \phi_1 \cdot \sin \phi_2\right)} - 1\right)} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    8. Step-by-step derivation
      1. add-log-exp99.0%

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

      \[\leadsto \cos^{-1} \left(\left(e^{\mathsf{log1p}\left(\sin \phi_1 \cdot \sin \phi_2\right)} - 1\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \color{blue}{\log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)}\right)\right) \cdot R \]
    10. Step-by-step derivation
      1. acos-asin99.0%

        \[\leadsto \color{blue}{\left(\frac{\pi}{2} - \sin^{-1} \left(\left(e^{\mathsf{log1p}\left(\sin \phi_1 \cdot \sin \phi_2\right)} - 1\right) + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right)\right)} \cdot R \]
      2. expm1-def98.9%

        \[\leadsto \left(\frac{\pi}{2} - \sin^{-1} \left(\color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\sin \phi_1 \cdot \sin \phi_2\right)\right)} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right)\right) \cdot R \]
      3. expm1-log1p-u98.9%

        \[\leadsto \left(\frac{\pi}{2} - \sin^{-1} \left(\color{blue}{\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, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right)\right) \cdot R \]
      4. associate-*l*98.9%

        \[\leadsto \left(\frac{\pi}{2} - \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)\right)}\right)\right) \cdot R \]
      5. fma-udef98.9%

        \[\leadsto \left(\frac{\pi}{2} - \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \color{blue}{\left(\cos \lambda_2 \cdot \cos \lambda_1 + \log \left(e^{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right)}\right)\right)\right) \cdot R \]
      6. add-log-exp98.9%

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

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

        \[\leadsto \left(\frac{\pi}{2} - \sin^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)}\right)\right)\right) \cdot R \]
    11. Applied egg-rr85.9%

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

    if -7.00000000000000048e-8 < phi1 < 1.2e17

    1. Initial program 71.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. cos-diff89.6%

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. +-commutative85.0%

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

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

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

    if 1.2e17 < phi1

    1. Initial program 79.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. fma-def79.4%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*79.4%

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

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

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

Alternative 9: 82.2% accurate, 0.9× speedup?

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

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

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


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

    1. Initial program 86.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 \]

    if -7.00000000000000048e-8 < phi1 < 1.2e17

    1. Initial program 71.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. cos-diff89.6%

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. +-commutative85.0%

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

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

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

    if 1.2e17 < phi1

    1. Initial program 79.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. fma-def79.4%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*79.4%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -7 \cdot 10^{-8}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\ \mathbf{elif}\;\phi_1 \leq 1.2 \cdot 10^{+17}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\ \end{array} \]

Alternative 10: 82.2% accurate, 0.9× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -7.00000000000000048e-8 or 1.2e17 < phi1

    1. Initial program 83.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 \]

    if -7.00000000000000048e-8 < phi1 < 1.2e17

    1. Initial program 71.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. cos-diff89.6%

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. fma-def85.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -7 \cdot 10^{-8} \lor \neg \left(\phi_1 \leq 1.2 \cdot 10^{+17}\right):\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \end{array} \]

Alternative 11: 82.2% accurate, 0.9× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -7.00000000000000048e-8 or 1.2e17 < phi1

    1. Initial program 83.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 \]

    if -7.00000000000000048e-8 < phi1 < 1.2e17

    1. Initial program 71.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. cos-diff89.6%

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. +-commutative85.0%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -7 \cdot 10^{-8} \lor \neg \left(\phi_1 \leq 1.2 \cdot 10^{+17}\right):\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \]

Alternative 12: 73.6% accurate, 1.0× speedup?

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

\mathbf{elif}\;\phi_2 \leq 3.7 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t_1\right)\\

\mathbf{elif}\;\phi_2 \leq 9.2 \cdot 10^{+228}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t_1\right)\\

\mathbf{else}:\\
\;\;\;\;t_0\\


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

    1. Initial program 81.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. Taylor expanded in lambda2 around 0 62.9%

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

    if -1.3499999999999999e-5 < phi2 < 3.69999999999999981e-5

    1. Initial program 75.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. cos-diff90.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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Applied egg-rr90.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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. cos-neg90.2%

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

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\color{blue}{\cos \lambda_2}, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    5. Simplified90.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 \]
    6. Taylor expanded in phi2 around 0 90.0%

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

    if 3.69999999999999981e-5 < phi2 < 9.20000000000000052e228

    1. Initial program 71.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. cos-diff98.8%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. cos-neg98.8%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.35 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1\right)\\ \mathbf{elif}\;\phi_2 \leq 3.7 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 9.2 \cdot 10^{+228}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1\right)\\ \end{array} \]

Alternative 13: 74.4% accurate, 1.0× speedup?

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

\mathbf{elif}\;\phi_2 \leq 4.6 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t_2\right)\\

\mathbf{elif}\;\phi_2 \leq 1.55 \cdot 10^{+231}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t_2\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if phi2 < -5.5000000000000003e-7

    1. Initial program 83.2%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(-\lambda_2\right)}\right) \cdot R \]
    3. Step-by-step derivation
      1. cos-neg53.9%

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

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

    if -5.5000000000000003e-7 < phi2 < 4.6e-5

    1. Initial program 75.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. cos-diff90.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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Applied egg-rr90.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(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. cos-neg90.2%

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

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\color{blue}{\cos \lambda_2}, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    5. Simplified90.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 \]
    6. Taylor expanded in phi2 around 0 90.0%

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

    if 4.6e-5 < phi2 < 1.54999999999999995e231

    1. Initial program 71.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. cos-diff98.8%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. cos-neg98.8%

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

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

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

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

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

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

    if 1.54999999999999995e231 < phi2

    1. Initial program 77.0%

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

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

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

Alternative 14: 82.2% accurate, 1.0× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -7.00000000000000048e-8 or 1.2e17 < phi1

    1. Initial program 83.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 \]

    if -7.00000000000000048e-8 < phi1 < 1.2e17

    1. Initial program 71.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. cos-diff89.6%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -7 \cdot 10^{-8} \lor \neg \left(\phi_1 \leq 1.2 \cdot 10^{+17}\right):\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \]

Alternative 15: 61.4% accurate, 1.0× speedup?

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

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


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

    1. Initial program 78.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. cos-diff93.0%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. cos-neg93.0%

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

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

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

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

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

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

    if 0.0014599999999999999 < phi2

    1. Initial program 73.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. fma-def73.4%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*73.4%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg46.2%

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

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

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_2\right) \cdot R \]
      4. neg-mul-146.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_2\right) \cdot R \]
      5. remove-double-neg46.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_2\right) \cdot R \]
      6. mul-1-neg46.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      7. distribute-neg-in46.2%

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_2\right) \cdot R \]
      10. +-commutative46.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_2\right) \cdot R \]
      11. mul-1-neg46.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_2\right) \cdot R \]
      12. unsub-neg46.2%

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

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

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

Alternative 16: 63.7% accurate, 1.0× speedup?

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

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


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

    1. Initial program 78.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. cos-diff93.0%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. cos-neg93.0%

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

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

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

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

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

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

    if 4.3999999999999999e-5 < phi2

    1. Initial program 73.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. cos-diff99.1%

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

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

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

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\color{blue}{\cos \lambda_2}, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    5. Simplified99.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 \]
    6. Taylor expanded in phi1 around 0 55.8%

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

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

Alternative 17: 51.5% accurate, 1.5× speedup?

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

    1. Initial program 85.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. fma-def85.2%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*85.2%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg61.9%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-161.9%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-161.9%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg61.9%

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

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in61.9%

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg61.9%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg61.9%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)} \cdot R \]
    7. Step-by-step derivation
      1. acos-asin61.9%

        \[\leadsto \color{blue}{\left(\frac{\pi}{2} - \sin^{-1} \left(\cos \left(\lambda_2 - \lambda_1\right) \cdot \cos \phi_1\right)\right)} \cdot R \]
      2. *-commutative61.9%

        \[\leadsto \left(\frac{\pi}{2} - \sin^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)}\right) \cdot R \]
    8. Applied egg-rr61.9%

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

    if -0.00220000000000000013 < phi1

    1. Initial program 74.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. fma-def74.3%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*74.3%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg54.3%

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

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

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_2\right) \cdot R \]
      4. neg-mul-154.3%

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

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

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      7. distribute-neg-in54.3%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.0022:\\ \;\;\;\;R \cdot \left(\frac{\pi}{2} - \sin^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\ \end{array} \]

Alternative 18: 51.5% accurate, 2.0× speedup?

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

    1. Initial program 85.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. fma-def85.2%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*85.2%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg61.9%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-161.9%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-161.9%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg61.9%

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

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in61.9%

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg61.9%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg61.9%

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

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

    if -0.00220000000000000013 < phi1

    1. Initial program 74.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. fma-def74.3%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*74.3%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg54.3%

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

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

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_2\right) \cdot R \]
      4. neg-mul-154.3%

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

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

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_2\right) \cdot R \]
      7. distribute-neg-in54.3%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.0022:\\ \;\;\;\;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} \]

Alternative 19: 37.9% accurate, 2.0× speedup?

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

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


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

    1. Initial program 81.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. fma-def81.0%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*81.0%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg50.1%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-150.1%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-150.1%

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

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

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in50.1%

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg50.1%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg50.1%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(-\lambda_1\right)\right)} \cdot R \]
    8. Step-by-step derivation
      1. cos-neg44.4%

        \[\leadsto \cos^{-1} \left(\cos \phi_1 \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
      2. *-commutative44.4%

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

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

    if 0.00115 < lambda2

    1. Initial program 63.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. fma-def63.9%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*63.9%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg47.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(-\lambda_2\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. +-commutative47.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-147.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-147.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg47.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg47.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in47.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative47.4%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative47.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg47.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg47.4%

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

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

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

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

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

Alternative 20: 43.0% accurate, 2.0× speedup?

\[\begin{array}{l} [lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -8.8 \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: lambda1 and lambda2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -8.8e-5)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (* R (acos (* (cos phi1) (cos lambda2))))))
assert(lambda1 < lambda2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -8.8e-5) {
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	} else {
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	}
	return tmp;
}
NOTE: lambda1 and lambda2 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 <= (-8.8d-5)) then
        tmp = r * acos((cos(phi1) * cos(lambda1)))
    else
        tmp = r * acos((cos(phi1) * cos(lambda2)))
    end if
    code = tmp
end function
assert lambda1 < lambda2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -8.8e-5) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda1)));
	} else {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda2)));
	}
	return tmp;
}
[lambda1, lambda2] = sort([lambda1, lambda2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -8.8e-5:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda1)))
	else:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda2)))
	return tmp
lambda1, lambda2 = sort([lambda1, lambda2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -8.8e-5)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda1))));
	else
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda2))));
	end
	return tmp
end
lambda1, lambda2 = num2cell(sort([lambda1, lambda2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda1 <= -8.8e-5)
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	else
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	end
	tmp_2 = tmp;
end
NOTE: lambda1 and lambda2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -8.8e-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}
[lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -8.8 \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 < -8.7999999999999998e-5

    1. Initial program 66.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. Step-by-step derivation
      1. fma-def66.8%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*66.8%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg43.1%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-143.1%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-143.1%

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

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

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in43.1%

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg43.1%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg43.1%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(-\lambda_1\right)\right)} \cdot R \]
    8. Step-by-step derivation
      1. cos-neg43.4%

        \[\leadsto \cos^{-1} \left(\cos \phi_1 \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
      2. *-commutative43.4%

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

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

    if -8.7999999999999998e-5 < lambda1

    1. Initial program 80.1%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. fma-def80.1%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*80.1%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(-\lambda_2\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. +-commutative51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-151.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-151.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative51.4%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg51.4%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -8.8 \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} \]

Alternative 21: 43.1% accurate, 2.0× speedup?

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

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Step-by-step derivation
    1. fma-def77.1%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
    2. associate-*l*77.1%

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

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

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
  5. Step-by-step derivation
    1. sub-neg49.5%

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

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    3. neg-mul-149.5%

      \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
    4. neg-mul-149.5%

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

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

      \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    7. distribute-neg-in49.5%

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

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

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

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

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

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

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

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

Alternative 22: 20.1% accurate, 3.0× speedup?

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

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


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

    1. Initial program 66.5%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. fma-def66.5%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*66.5%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg43.2%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-143.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-143.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg43.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg43.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in43.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative43.2%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative43.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg43.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg43.2%

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(-\lambda_1\right)} \cdot R \]
    9. Step-by-step derivation
      1. cos-neg30.2%

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    10. Simplified30.2%

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

    if -1.74999999999999992e-7 < lambda1

    1. Initial program 80.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. fma-def80.2%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*80.2%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(-\lambda_2\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. +-commutative51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-151.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-151.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative51.4%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg51.4%

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

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

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

      \[\leadsto \color{blue}{\left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot R \]
    9. Step-by-step derivation
      1. neg-mul-14.9%

        \[\leadsto \left(\color{blue}{\left(-\lambda_1\right)} + \lambda_2\right) \cdot R \]
      2. +-commutative4.9%

        \[\leadsto \color{blue}{\left(\lambda_2 + \left(-\lambda_1\right)\right)} \cdot R \]
      3. sub-neg4.9%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1.75 \cdot 10^{-7}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\lambda_2 - \lambda_1\right)\\ \end{array} \]

Alternative 23: 26.5% accurate, 3.0× speedup?

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

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


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

    1. Initial program 66.5%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. fma-def66.5%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*66.5%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg43.2%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-143.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-143.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg43.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg43.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in43.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative43.2%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative43.2%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg43.2%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg43.2%

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(-\lambda_1\right)} \cdot R \]
    9. Step-by-step derivation
      1. cos-neg30.2%

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    10. Simplified30.2%

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

    if -3.1999999999999999e-6 < lambda1

    1. Initial program 80.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. fma-def80.2%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*80.2%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(-\lambda_2\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. +-commutative51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-151.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-151.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative51.4%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative51.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg51.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg51.4%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -3.2 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \]

Alternative 24: 26.6% accurate, 3.0× speedup?

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

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Step-by-step derivation
    1. fma-def77.1%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
    2. associate-*l*77.1%

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

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

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
  5. Step-by-step derivation
    1. sub-neg49.5%

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

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    3. neg-mul-149.5%

      \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
    4. neg-mul-149.5%

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

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

      \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    7. distribute-neg-in49.5%

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

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

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

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

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

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

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

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

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

Alternative 25: 8.8% accurate, 101.1× speedup?

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

\mathbf{else}:\\
\;\;\;\;\lambda_2 \cdot R\\


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

    1. Initial program 72.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. fma-def72.6%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*72.6%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg44.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(-\lambda_2\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      2. +-commutative44.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-144.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-144.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg44.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \color{blue}{\left(-\left(-\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      6. mul-1-neg44.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in44.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(-\left(\lambda_2 + -1 \cdot \lambda_1\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      8. +-commutative44.4%

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot \cos \phi_1\right) \cdot R \]
      10. +-commutative44.4%

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg44.4%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg44.4%

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

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

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

      \[\leadsto \color{blue}{\left(-1 \cdot \lambda_1\right)} \cdot R \]
    9. Step-by-step derivation
      1. neg-mul-17.8%

        \[\leadsto \color{blue}{\left(-\lambda_1\right)} \cdot R \]
    10. Simplified7.8%

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

    if -7.99999999999999983e-113 < lambda1

    1. Initial program 79.1%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Step-by-step derivation
      1. fma-def79.2%

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      2. associate-*l*79.2%

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    5. Step-by-step derivation
      1. sub-neg51.9%

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      3. neg-mul-151.9%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      4. neg-mul-151.9%

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(-\lambda_2\right)} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
      5. remove-double-neg51.9%

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

        \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      7. distribute-neg-in51.9%

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      11. mul-1-neg51.9%

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(-\lambda_1\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      12. unsub-neg51.9%

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

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

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

      \[\leadsto \color{blue}{\lambda_2} \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Final simplification6.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -8 \cdot 10^{-113}:\\ \;\;\;\;-\lambda_1 \cdot R\\ \mathbf{else}:\\ \;\;\;\;\lambda_2 \cdot R\\ \end{array} \]

Alternative 26: 9.0% accurate, 122.6× speedup?

\[\begin{array}{l} [lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\ \\ R \cdot \left(\lambda_2 - \lambda_1\right) \end{array} \]
NOTE: lambda1 and lambda2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R (- lambda2 lambda1)))
assert(lambda1 < lambda2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * (lambda2 - lambda1);
}
NOTE: lambda1 and lambda2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = r * (lambda2 - lambda1)
end function
assert lambda1 < lambda2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * (lambda2 - lambda1);
}
[lambda1, lambda2] = sort([lambda1, lambda2])
def code(R, lambda1, lambda2, phi1, phi2):
	return R * (lambda2 - lambda1)
lambda1, lambda2 = sort([lambda1, lambda2])
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(R * Float64(lambda2 - lambda1))
end
lambda1, lambda2 = num2cell(sort([lambda1, lambda2])){:}
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = R * (lambda2 - lambda1);
end
NOTE: lambda1 and lambda2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * N[(lambda2 - lambda1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\
\\
R \cdot \left(\lambda_2 - \lambda_1\right)
\end{array}
Derivation
  1. Initial program 77.1%

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Step-by-step derivation
    1. fma-def77.1%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
    2. associate-*l*77.1%

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

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

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
  5. Step-by-step derivation
    1. sub-neg49.5%

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

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    3. neg-mul-149.5%

      \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
    4. neg-mul-149.5%

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

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

      \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    7. distribute-neg-in49.5%

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\left(-1 \cdot \lambda_1 + \lambda_2\right)} \cdot R \]
  9. Step-by-step derivation
    1. neg-mul-14.8%

      \[\leadsto \left(\color{blue}{\left(-\lambda_1\right)} + \lambda_2\right) \cdot R \]
    2. +-commutative4.8%

      \[\leadsto \color{blue}{\left(\lambda_2 + \left(-\lambda_1\right)\right)} \cdot R \]
    3. sub-neg4.8%

      \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
  10. Simplified4.8%

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

    \[\leadsto R \cdot \left(\lambda_2 - \lambda_1\right) \]

Alternative 27: 6.8% accurate, 204.3× speedup?

\[\begin{array}{l} [lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\ \\ \lambda_2 \cdot R \end{array} \]
NOTE: lambda1 and lambda2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2) :precision binary64 (* lambda2 R))
assert(lambda1 < lambda2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return lambda2 * R;
}
NOTE: lambda1 and lambda2 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 * r
end function
assert lambda1 < lambda2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return lambda2 * R;
}
[lambda1, lambda2] = sort([lambda1, lambda2])
def code(R, lambda1, lambda2, phi1, phi2):
	return lambda2 * R
lambda1, lambda2 = sort([lambda1, lambda2])
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(lambda2 * R)
end
lambda1, lambda2 = num2cell(sort([lambda1, lambda2])){:}
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = lambda2 * R;
end
NOTE: lambda1 and lambda2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(lambda2 * R), $MachinePrecision]
\begin{array}{l}
[lambda1, lambda2] = \mathsf{sort}([lambda1, lambda2])\\
\\
\lambda_2 \cdot R
\end{array}
Derivation
  1. Initial program 77.1%

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Step-by-step derivation
    1. fma-def77.1%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
    2. associate-*l*77.1%

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

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

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
  5. Step-by-step derivation
    1. sub-neg49.5%

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

      \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\left(-\lambda_2\right) + \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
    3. neg-mul-149.5%

      \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{-1 \cdot \lambda_2} + \lambda_1\right) \cdot \cos \phi_1\right) \cdot R \]
    4. neg-mul-149.5%

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

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

      \[\leadsto \cos^{-1} \left(\cos \left(\left(-\lambda_2\right) + \left(-\color{blue}{-1 \cdot \lambda_1}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
    7. distribute-neg-in49.5%

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\lambda_2} \cdot R \]
  9. Final simplification5.0%

    \[\leadsto \lambda_2 \cdot R \]

Reproduce

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