Spherical law of cosines

Percentage Accurate: 74.3% → 96.5%
Time: 22.6s
Alternatives: 30
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (acos
   (+
    (* (sin phi1) (sin phi2))
    (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
  R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
}
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * r
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2))))) * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return math.acos(((math.sin(phi1) * math.sin(phi2)) + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2))))) * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 30 alternatives:

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

Initial Program: 74.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (acos
   (+
    (* (sin phi1) (sin phi2))
    (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
  R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
}
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * r
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2))))) * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return math.acos(((math.sin(phi1) * math.sin(phi2)) + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2))))) * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R
\end{array}

Alternative 1: 96.5% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \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.99995:\\ \;\;\;\;\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) \cdot R\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \end{array} \]
(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.99995)
     (*
      (acos
       (+
        t_0
        (*
         t_1
         (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2))))))
      R)
     (* R (fabs (remainder (- lambda1 lambda2) (* PI 2.0)))))))
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.99995) {
		tmp = acos((t_0 + (t_1 * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2)))))) * R;
	} else {
		tmp = R * fabs(remainder((lambda1 - lambda2), (((double) M_PI) * 2.0)));
	}
	return tmp;
}
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.99995], N[(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] * R), $MachinePrecision], N[(R * N[Abs[N[With[{TMP1 = N[(lambda1 - lambda2), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\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.99995:\\
\;\;\;\;\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) \cdot R\\

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


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

    1. Initial program 76.4%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6499.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr99.0%

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

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

    1. Initial program 14.9%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6414.9

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      2. acos-cosN/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      3. lower-fabs.f64N/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      4. lower-remainder.f64N/A

        \[\leadsto \left|\color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
      5. lift-PI.f64N/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right| \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      7. lower-*.f6455.9

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\pi \cdot 2\right)}\right)\right| \cdot R \]
    10. Applied egg-rr55.9%

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

    \[\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.99995:\\ \;\;\;\;\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) \cdot R\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 96.5% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ \mathbf{if}\;t\_0 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) \leq 0.99995:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right), \cos \phi_1, t\_0\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))))
   (if (<=
        (+ t_0 (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2))))
        0.99995)
     (*
      R
      (acos
       (fma
        (*
         (cos phi2)
         (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))
        (cos phi1)
        t_0)))
     (* R (fabs (remainder (- lambda1 lambda2) (* PI 2.0)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin(phi1) * sin(phi2);
	double tmp;
	if ((t_0 + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))) <= 0.99995) {
		tmp = R * acos(fma((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))), cos(phi1), t_0));
	} else {
		tmp = R * fabs(remainder((lambda1 - lambda2), (((double) M_PI) * 2.0)));
	}
	return tmp;
}
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[(t$95$0 + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], 0.99995], N[(R * N[ArcCos[N[(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] * N[Cos[phi1], $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[Abs[N[With[{TMP1 = N[(lambda1 - lambda2), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
\mathbf{if}\;t\_0 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) \leq 0.99995:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right), \cos \phi_1, t\_0\right)\right)\\

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


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

    1. Initial program 76.4%

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

        \[\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. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\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 \]
      4. lift-cos.f64N/A

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

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

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

        \[\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_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

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

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

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

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

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

    1. Initial program 14.9%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6414.9

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      2. acos-cosN/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      3. lower-fabs.f64N/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      4. lower-remainder.f64N/A

        \[\leadsto \left|\color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
      5. lift-PI.f64N/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right| \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      7. lower-*.f6455.9

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\pi \cdot 2\right)}\right)\right| \cdot R \]
    10. Applied egg-rr55.9%

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

    \[\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.99995:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right), \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 84.5% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := R \cdot \left(\pi \cdot 0.5\right)\\ t_1 := \sin \phi_1 \cdot \sin \phi_2\\ t_2 := \mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), t\_1\right)\\ \mathbf{if}\;\phi_2 \leq -0.0016:\\ \;\;\;\;t\_0 - R \cdot \mathsf{fma}\left(0.5 \cdot \sqrt{\pi}, \sqrt{\pi}, -\cos^{-1} t\_2\right)\\ \mathbf{elif}\;\phi_2 \leq 0.0046:\\ \;\;\;\;R \cdot \cos^{-1} \left(t\_1 + \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \left(\cos \phi_1 \cdot \mathsf{fma}\left(-0.5, \phi_2 \cdot \phi_2, 1\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} t\_2, R, t\_0\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* R (* PI 0.5)))
        (t_1 (* (sin phi1) (sin phi2)))
        (t_2 (fma (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2))) t_1)))
   (if (<= phi2 -0.0016)
     (- t_0 (* R (fma (* 0.5 (sqrt PI)) (sqrt PI) (- (acos t_2)))))
     (if (<= phi2 0.0046)
       (*
        R
        (acos
         (+
          t_1
          (*
           (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2)))
           (* (cos phi1) (fma -0.5 (* phi2 phi2) 1.0))))))
       (fma (- (asin t_2)) R t_0)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = R * (((double) M_PI) * 0.5);
	double t_1 = sin(phi1) * sin(phi2);
	double t_2 = fma(cos(phi1), (cos(phi2) * cos((lambda1 - lambda2))), t_1);
	double tmp;
	if (phi2 <= -0.0016) {
		tmp = t_0 - (R * fma((0.5 * sqrt(((double) M_PI))), sqrt(((double) M_PI)), -acos(t_2)));
	} else if (phi2 <= 0.0046) {
		tmp = R * acos((t_1 + (fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2))) * (cos(phi1) * fma(-0.5, (phi2 * phi2), 1.0)))));
	} else {
		tmp = fma(-asin(t_2), R, t_0);
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(R * Float64(pi * 0.5))
	t_1 = Float64(sin(phi1) * sin(phi2))
	t_2 = fma(cos(phi1), Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))), t_1)
	tmp = 0.0
	if (phi2 <= -0.0016)
		tmp = Float64(t_0 - Float64(R * fma(Float64(0.5 * sqrt(pi)), sqrt(pi), Float64(-acos(t_2)))));
	elseif (phi2 <= 0.0046)
		tmp = Float64(R * acos(Float64(t_1 + Float64(fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2))) * Float64(cos(phi1) * fma(-0.5, Float64(phi2 * phi2), 1.0))))));
	else
		tmp = fma(Float64(-asin(t_2)), R, t_0);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + t$95$1), $MachinePrecision]}, If[LessEqual[phi2, -0.0016], N[(t$95$0 - N[(R * N[(N[(0.5 * N[Sqrt[Pi], $MachinePrecision]), $MachinePrecision] * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[t$95$2], $MachinePrecision])), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 0.0046], N[(R * N[ArcCos[N[(t$95$1 + N[(N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[(-0.5 * N[(phi2 * phi2), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[ArcSin[t$95$2], $MachinePrecision]) * R + t$95$0), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := R \cdot \left(\pi \cdot 0.5\right)\\
t_1 := \sin \phi_1 \cdot \sin \phi_2\\
t_2 := \mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), t\_1\right)\\
\mathbf{if}\;\phi_2 \leq -0.0016:\\
\;\;\;\;t\_0 - R \cdot \mathsf{fma}\left(0.5 \cdot \sqrt{\pi}, \sqrt{\pi}, -\cos^{-1} t\_2\right)\\

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

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


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

    1. Initial program 77.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Applied egg-rr77.4%

      \[\leadsto \color{blue}{\left(\pi \cdot 0.5\right) \cdot R + \left(-\sin^{-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)\right) \cdot R} \]
    4. Step-by-step derivation
      1. lift-sin.f64N/A

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

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

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

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

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

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

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

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

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\sin^{-1} \color{blue}{\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)}\right)\right) \cdot R \]
      10. asin-acosN/A

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

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

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\frac{\color{blue}{\mathsf{PI}\left(\right)}}{2} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
      13. div-invN/A

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\color{blue}{\mathsf{PI}\left(\right) \cdot \frac{1}{2}} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
      14. metadata-evalN/A

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\mathsf{PI}\left(\right) \cdot \color{blue}{\frac{1}{2}} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
    5. Applied egg-rr77.1%

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

    if -0.00160000000000000008 < phi2 < 0.0045999999999999999

    1. Initial program 68.1%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6490.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr90.0%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \color{blue}{\left(1 + \frac{-1}{2} \cdot {\phi_2}^{2}\right)}\right) \cdot \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. +-commutativeN/A

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

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

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

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

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

    if 0.0045999999999999999 < phi2

    1. Initial program 78.6%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6499.2

        \[\leadsto \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 \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr99.2%

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

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

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

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

Alternative 4: 84.4% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\\ t_1 := R \cdot \left(\pi \cdot 0.5\right)\\ \mathbf{if}\;\phi_2 \leq -0.0014:\\ \;\;\;\;t\_1 - R \cdot \mathsf{fma}\left(0.5 \cdot \sqrt{\pi}, \sqrt{\pi}, -\cos^{-1} t\_0\right)\\ \mathbf{elif}\;\phi_2 \leq 0.0205:\\ \;\;\;\;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}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} t\_0, R, t\_1\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (fma
          (cos phi1)
          (* (cos phi2) (cos (- lambda1 lambda2)))
          (* (sin phi1) (sin phi2))))
        (t_1 (* R (* PI 0.5))))
   (if (<= phi2 -0.0014)
     (- t_1 (* R (fma (* 0.5 (sqrt PI)) (sqrt PI) (- (acos t_0)))))
     (if (<= phi2 0.0205)
       (*
        R
        (acos
         (+
          (*
           (* (cos phi1) (cos phi2))
           (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2))))
          (* (sin phi1) phi2))))
       (fma (- (asin t_0)) R t_1)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = fma(cos(phi1), (cos(phi2) * cos((lambda1 - lambda2))), (sin(phi1) * sin(phi2)));
	double t_1 = R * (((double) M_PI) * 0.5);
	double tmp;
	if (phi2 <= -0.0014) {
		tmp = t_1 - (R * fma((0.5 * sqrt(((double) M_PI))), sqrt(((double) M_PI)), -acos(t_0)));
	} else if (phi2 <= 0.0205) {
		tmp = R * acos((((cos(phi1) * cos(phi2)) * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2)))) + (sin(phi1) * phi2)));
	} else {
		tmp = fma(-asin(t_0), R, t_1);
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = fma(cos(phi1), Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))), Float64(sin(phi1) * sin(phi2)))
	t_1 = Float64(R * Float64(pi * 0.5))
	tmp = 0.0
	if (phi2 <= -0.0014)
		tmp = Float64(t_1 - Float64(R * fma(Float64(0.5 * sqrt(pi)), sqrt(pi), Float64(-acos(t_0)))));
	elseif (phi2 <= 0.0205)
		tmp = Float64(R * acos(Float64(Float64(Float64(cos(phi1) * cos(phi2)) * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2)))) + Float64(sin(phi1) * phi2))));
	else
		tmp = fma(Float64(-asin(t_0)), R, t_1);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -0.0014], N[(t$95$1 - N[(R * N[(N[(0.5 * N[Sqrt[Pi], $MachinePrecision]), $MachinePrecision] * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[t$95$0], $MachinePrecision])), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 0.0205], N[(R * N[ArcCos[N[(N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * 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[((-N[ArcSin[t$95$0], $MachinePrecision]) * R + t$95$1), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\\
t_1 := R \cdot \left(\pi \cdot 0.5\right)\\
\mathbf{if}\;\phi_2 \leq -0.0014:\\
\;\;\;\;t\_1 - R \cdot \mathsf{fma}\left(0.5 \cdot \sqrt{\pi}, \sqrt{\pi}, -\cos^{-1} t\_0\right)\\

\mathbf{elif}\;\phi_2 \leq 0.0205:\\
\;\;\;\;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}:\\
\;\;\;\;\mathsf{fma}\left(-\sin^{-1} t\_0, R, t\_1\right)\\


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

    1. Initial program 77.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Applied egg-rr77.4%

      \[\leadsto \color{blue}{\left(\pi \cdot 0.5\right) \cdot R + \left(-\sin^{-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)\right) \cdot R} \]
    4. Step-by-step derivation
      1. lift-sin.f64N/A

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

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

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

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

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

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

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

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

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\sin^{-1} \color{blue}{\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)}\right)\right) \cdot R \]
      10. asin-acosN/A

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

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

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\frac{\color{blue}{\mathsf{PI}\left(\right)}}{2} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
      13. div-invN/A

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\color{blue}{\mathsf{PI}\left(\right) \cdot \frac{1}{2}} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
      14. metadata-evalN/A

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\mathsf{PI}\left(\right) \cdot \color{blue}{\frac{1}{2}} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
    5. Applied egg-rr77.1%

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

    if -0.00139999999999999999 < phi2 < 0.0205000000000000009

    1. Initial program 68.1%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6490.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr90.0%

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

      \[\leadsto \cos^{-1} \left(\color{blue}{\phi_2 \cdot \sin \phi_1} + \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 \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\phi_2 \cdot \sin \phi_1} + \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. lower-sin.f6489.8

        \[\leadsto \cos^{-1} \left(\phi_2 \cdot \color{blue}{\sin \phi_1} + \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. Simplified89.8%

      \[\leadsto \cos^{-1} \left(\color{blue}{\phi_2 \cdot \sin \phi_1} + \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.0205000000000000009 < phi2

    1. Initial program 78.6%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6499.2

        \[\leadsto \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 \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr99.2%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -0.0014:\\ \;\;\;\;R \cdot \left(\pi \cdot 0.5\right) - R \cdot \mathsf{fma}\left(0.5 \cdot \sqrt{\pi}, \sqrt{\pi}, -\cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 0.0205:\\ \;\;\;\;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}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 84.3% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\\ t_1 := R \cdot \left(\pi \cdot 0.5\right)\\ \mathbf{if}\;\phi_2 \leq -0.00014:\\ \;\;\;\;t\_1 - R \cdot \mathsf{fma}\left(0.5 \cdot \sqrt{\pi}, \sqrt{\pi}, -\cos^{-1} t\_0\right)\\ \mathbf{elif}\;\phi_2 \leq 1.8 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right), \cos \phi_1, \sin \phi_1 \cdot \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} t\_0, R, t\_1\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (fma
          (cos phi1)
          (* (cos phi2) (cos (- lambda1 lambda2)))
          (* (sin phi1) (sin phi2))))
        (t_1 (* R (* PI 0.5))))
   (if (<= phi2 -0.00014)
     (- t_1 (* R (fma (* 0.5 (sqrt PI)) (sqrt PI) (- (acos t_0)))))
     (if (<= phi2 1.8e-5)
       (*
        R
        (acos
         (fma
          (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2)))
          (cos phi1)
          (* (sin phi1) phi2))))
       (fma (- (asin t_0)) R t_1)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = fma(cos(phi1), (cos(phi2) * cos((lambda1 - lambda2))), (sin(phi1) * sin(phi2)));
	double t_1 = R * (((double) M_PI) * 0.5);
	double tmp;
	if (phi2 <= -0.00014) {
		tmp = t_1 - (R * fma((0.5 * sqrt(((double) M_PI))), sqrt(((double) M_PI)), -acos(t_0)));
	} else if (phi2 <= 1.8e-5) {
		tmp = R * acos(fma(fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2))), cos(phi1), (sin(phi1) * phi2)));
	} else {
		tmp = fma(-asin(t_0), R, t_1);
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = fma(cos(phi1), Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))), Float64(sin(phi1) * sin(phi2)))
	t_1 = Float64(R * Float64(pi * 0.5))
	tmp = 0.0
	if (phi2 <= -0.00014)
		tmp = Float64(t_1 - Float64(R * fma(Float64(0.5 * sqrt(pi)), sqrt(pi), Float64(-acos(t_0)))));
	elseif (phi2 <= 1.8e-5)
		tmp = Float64(R * acos(fma(fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))), cos(phi1), Float64(sin(phi1) * phi2))));
	else
		tmp = fma(Float64(-asin(t_0)), R, t_1);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -0.00014], N[(t$95$1 - N[(R * N[(N[(0.5 * N[Sqrt[Pi], $MachinePrecision]), $MachinePrecision] * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[t$95$0], $MachinePrecision])), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 1.8e-5], N[(R * N[ArcCos[N[(N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * phi2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[ArcSin[t$95$0], $MachinePrecision]) * R + t$95$1), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\\
t_1 := R \cdot \left(\pi \cdot 0.5\right)\\
\mathbf{if}\;\phi_2 \leq -0.00014:\\
\;\;\;\;t\_1 - R \cdot \mathsf{fma}\left(0.5 \cdot \sqrt{\pi}, \sqrt{\pi}, -\cos^{-1} t\_0\right)\\

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

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


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

    1. Initial program 76.8%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Applied egg-rr76.8%

      \[\leadsto \color{blue}{\left(\pi \cdot 0.5\right) \cdot R + \left(-\sin^{-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)\right) \cdot R} \]
    4. Step-by-step derivation
      1. lift-sin.f64N/A

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

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

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

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

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

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

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

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

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\sin^{-1} \color{blue}{\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)}\right)\right) \cdot R \]
      10. asin-acosN/A

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

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

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\frac{\color{blue}{\mathsf{PI}\left(\right)}}{2} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
      13. div-invN/A

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\color{blue}{\mathsf{PI}\left(\right) \cdot \frac{1}{2}} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
      14. metadata-evalN/A

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\mathsf{PI}\left(\right) \cdot \color{blue}{\frac{1}{2}} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
    5. Applied egg-rr76.5%

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

    if -1.3999999999999999e-4 < phi2 < 1.80000000000000005e-5

    1. Initial program 68.2%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6489.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr89.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 \]
    5. Taylor expanded in phi2 around 0

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1} + \phi_2 \cdot \sin \phi_1\right) \cdot R \]
      3. lower-fma.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \lambda_2}, \sin \lambda_1 \cdot \sin \lambda_2\right), \cos \phi_1, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      7. lower-*.f64N/A

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right), \color{blue}{\cos \phi_1}, \phi_2 \cdot \sin \phi_1\right)\right) \cdot R \]
      11. lower-*.f64N/A

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

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

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

    if 1.80000000000000005e-5 < phi2

    1. Initial program 78.6%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6499.2

        \[\leadsto \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 \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr99.2%

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

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

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

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

Alternative 6: 84.1% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\\ t_1 := R \cdot \left(\pi \cdot 0.5\right)\\ \mathbf{if}\;\phi_2 \leq -0.0001:\\ \;\;\;\;t\_1 - R \cdot \mathsf{fma}\left(0.5 \cdot \sqrt{\pi}, \sqrt{\pi}, -\cos^{-1} t\_0\right)\\ \mathbf{elif}\;\phi_2 \leq 3.1 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-\sin^{-1} t\_0, R, t\_1\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (fma
          (cos phi1)
          (* (cos phi2) (cos (- lambda1 lambda2)))
          (* (sin phi1) (sin phi2))))
        (t_1 (* R (* PI 0.5))))
   (if (<= phi2 -0.0001)
     (- t_1 (* R (fma (* 0.5 (sqrt PI)) (sqrt PI) (- (acos t_0)))))
     (if (<= phi2 3.1e-6)
       (*
        R
        (acos
         (*
          (cos phi1)
          (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
       (fma (- (asin t_0)) R t_1)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = fma(cos(phi1), (cos(phi2) * cos((lambda1 - lambda2))), (sin(phi1) * sin(phi2)));
	double t_1 = R * (((double) M_PI) * 0.5);
	double tmp;
	if (phi2 <= -0.0001) {
		tmp = t_1 - (R * fma((0.5 * sqrt(((double) M_PI))), sqrt(((double) M_PI)), -acos(t_0)));
	} else if (phi2 <= 3.1e-6) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))));
	} else {
		tmp = fma(-asin(t_0), R, t_1);
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = fma(cos(phi1), Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))), Float64(sin(phi1) * sin(phi2)))
	t_1 = Float64(R * Float64(pi * 0.5))
	tmp = 0.0
	if (phi2 <= -0.0001)
		tmp = Float64(t_1 - Float64(R * fma(Float64(0.5 * sqrt(pi)), sqrt(pi), Float64(-acos(t_0)))));
	elseif (phi2 <= 3.1e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = fma(Float64(-asin(t_0)), R, t_1);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -0.0001], N[(t$95$1 - N[(R * N[(N[(0.5 * N[Sqrt[Pi], $MachinePrecision]), $MachinePrecision] * N[Sqrt[Pi], $MachinePrecision] + (-N[ArcCos[t$95$0], $MachinePrecision])), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 3.1e-6], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-N[ArcSin[t$95$0], $MachinePrecision]) * R + t$95$1), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\\
t_1 := R \cdot \left(\pi \cdot 0.5\right)\\
\mathbf{if}\;\phi_2 \leq -0.0001:\\
\;\;\;\;t\_1 - R \cdot \mathsf{fma}\left(0.5 \cdot \sqrt{\pi}, \sqrt{\pi}, -\cos^{-1} t\_0\right)\\

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

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


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

    1. Initial program 76.8%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Add Preprocessing
    3. Applied egg-rr76.8%

      \[\leadsto \color{blue}{\left(\pi \cdot 0.5\right) \cdot R + \left(-\sin^{-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)\right) \cdot R} \]
    4. Step-by-step derivation
      1. lift-sin.f64N/A

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

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

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

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

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

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

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

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

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\sin^{-1} \color{blue}{\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)}\right)\right) \cdot R \]
      10. asin-acosN/A

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

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

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\frac{\color{blue}{\mathsf{PI}\left(\right)}}{2} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
      13. div-invN/A

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\color{blue}{\mathsf{PI}\left(\right) \cdot \frac{1}{2}} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
      14. metadata-evalN/A

        \[\leadsto \left(\mathsf{PI}\left(\right) \cdot \frac{1}{2}\right) \cdot R + \left(\mathsf{neg}\left(\left(\mathsf{PI}\left(\right) \cdot \color{blue}{\frac{1}{2}} + \left(\mathsf{neg}\left(\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)\right)\right)\right)\right)\right) \cdot R \]
    5. Applied egg-rr76.5%

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

    if -1.00000000000000005e-4 < phi2 < 3.1e-6

    1. Initial program 68.2%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6489.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr89.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 \]
    5. Taylor expanded in phi2 around 0

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. lower-fma.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \lambda_2}, \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      6. lower-*.f64N/A

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

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

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

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

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

    if 3.1e-6 < phi2

    1. Initial program 78.6%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6499.2

        \[\leadsto \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 \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr99.2%

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

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

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

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

Alternative 7: 84.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\ \mathbf{if}\;\phi_2 \leq -0.0001:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 3.1 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (fma
          (-
           (asin
            (fma
             (cos phi1)
             (* (cos phi2) (cos (- lambda1 lambda2)))
             (* (sin phi1) (sin phi2)))))
          R
          (* R (* PI 0.5)))))
   (if (<= phi2 -0.0001)
     t_0
     (if (<= phi2 3.1e-6)
       (*
        R
        (acos
         (*
          (cos phi1)
          (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
       t_0))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = fma(-asin(fma(cos(phi1), (cos(phi2) * cos((lambda1 - lambda2))), (sin(phi1) * sin(phi2)))), R, (R * (((double) M_PI) * 0.5)));
	double tmp;
	if (phi2 <= -0.0001) {
		tmp = t_0;
	} else if (phi2 <= 3.1e-6) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))));
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = fma(Float64(-asin(fma(cos(phi1), Float64(cos(phi2) * cos(Float64(lambda1 - lambda2))), Float64(sin(phi1) * sin(phi2))))), R, Float64(R * Float64(pi * 0.5)))
	tmp = 0.0
	if (phi2 <= -0.0001)
		tmp = t_0;
	elseif (phi2 <= 3.1e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = t_0;
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[((-N[ArcSin[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]) * R + N[(R * N[(Pi * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -0.0001], t$95$0, If[LessEqual[phi2, 3.1e-6], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \mathsf{fma}\left(-\sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right), R, R \cdot \left(\pi \cdot 0.5\right)\right)\\
\mathbf{if}\;\phi_2 \leq -0.0001:\\
\;\;\;\;t\_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -1.00000000000000005e-4 or 3.1e-6 < phi2

    1. Initial program 77.7%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6498.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr98.9%

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

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

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

    if -1.00000000000000005e-4 < phi2 < 3.1e-6

    1. Initial program 68.2%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6489.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr89.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 \]
    5. Taylor expanded in phi2 around 0

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. lower-fma.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \lambda_2}, \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      6. lower-*.f64N/A

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

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

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

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

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

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

Alternative 8: 84.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq -0.0001:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 3.1 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\pi, 0.5, -\sin^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot t\_0\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi2) (cos (- lambda1 lambda2)))))
   (if (<= phi2 -0.0001)
     (* R (acos (fma t_0 (cos phi1) (* (sin phi1) (sin phi2)))))
     (if (<= phi2 3.1e-6)
       (*
        R
        (acos
         (*
          (cos phi1)
          (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
       (*
        R
        (fma
         PI
         0.5
         (- (asin (fma (sin phi1) (sin phi2) (* (cos phi1) t_0))))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi2) * cos((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -0.0001) {
		tmp = R * acos(fma(t_0, cos(phi1), (sin(phi1) * sin(phi2))));
	} else if (phi2 <= 3.1e-6) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))));
	} else {
		tmp = R * fma(((double) M_PI), 0.5, -asin(fma(sin(phi1), sin(phi2), (cos(phi1) * t_0))));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi2) * cos(Float64(lambda1 - lambda2)))
	tmp = 0.0
	if (phi2 <= -0.0001)
		tmp = Float64(R * acos(fma(t_0, cos(phi1), Float64(sin(phi1) * sin(phi2)))));
	elseif (phi2 <= 3.1e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = Float64(R * fma(pi, 0.5, Float64(-asin(fma(sin(phi1), sin(phi2), Float64(cos(phi1) * t_0))))));
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -0.0001], N[(R * N[ArcCos[N[(t$95$0 * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 3.1e-6], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[(Pi * 0.5 + (-N[ArcSin[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq -0.0001:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\

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

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


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

    1. Initial program 76.8%

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

        \[\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. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\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 \]
      4. lift-cos.f64N/A

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

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

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

        \[\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_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

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

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

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

    if -1.00000000000000005e-4 < phi2 < 3.1e-6

    1. Initial program 68.2%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6489.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr89.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 \]
    5. Taylor expanded in phi2 around 0

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. lower-fma.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \lambda_2}, \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      6. lower-*.f64N/A

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

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

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

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

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

    if 3.1e-6 < phi2

    1. Initial program 78.6%

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

        \[\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. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\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 \]
      4. lift-cos.f64N/A

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

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

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

        \[\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_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

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

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

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

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

      \[\leadsto \color{blue}{\mathsf{fma}\left(\pi, 0.5, -\sin^{-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)\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Final simplification83.6%

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

Alternative 9: 84.3% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
t_1 := \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq -0.0001:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_1, \cos \phi_1, t\_0\right)\right)\\

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

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


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

    1. Initial program 76.8%

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

        \[\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. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\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 \]
      4. lift-cos.f64N/A

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

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

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

        \[\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_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

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

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

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

    if -1.00000000000000005e-4 < phi2 < 3.1e-6

    1. Initial program 68.2%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6489.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr89.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 \]
    5. Taylor expanded in phi2 around 0

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. lower-fma.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \lambda_2}, \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      6. lower-*.f64N/A

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

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

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

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

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

    if 3.1e-6 < phi2

    1. Initial program 78.6%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6499.2

        \[\leadsto \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 \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr99.2%

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

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

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

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

Alternative 10: 84.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq -0.0001:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 3.1 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_1 \cdot t\_0\right)\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi2) (cos (- lambda1 lambda2)))))
   (if (<= phi2 -0.0001)
     (* R (acos (fma t_0 (cos phi1) (* (sin phi1) (sin phi2)))))
     (if (<= phi2 3.1e-6)
       (*
        R
        (acos
         (*
          (cos phi1)
          (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
       (* R (acos (fma (sin phi2) (sin phi1) (* (cos phi1) t_0))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi2) * cos((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -0.0001) {
		tmp = R * acos(fma(t_0, cos(phi1), (sin(phi1) * sin(phi2))));
	} else if (phi2 <= 3.1e-6) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))));
	} else {
		tmp = R * acos(fma(sin(phi2), sin(phi1), (cos(phi1) * t_0)));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi2) * cos(Float64(lambda1 - lambda2)))
	tmp = 0.0
	if (phi2 <= -0.0001)
		tmp = Float64(R * acos(fma(t_0, cos(phi1), Float64(sin(phi1) * sin(phi2)))));
	elseif (phi2 <= 3.1e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = Float64(R * acos(fma(sin(phi2), sin(phi1), Float64(cos(phi1) * t_0))));
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -0.0001], N[(R * N[ArcCos[N[(t$95$0 * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 3.1e-6], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq -0.0001:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\

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

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


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

    1. Initial program 76.8%

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

        \[\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. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\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 \]
      4. lift-cos.f64N/A

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

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

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

        \[\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_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

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

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

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

    if -1.00000000000000005e-4 < phi2 < 3.1e-6

    1. Initial program 68.2%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6489.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr89.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 \]
    5. Taylor expanded in phi2 around 0

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. lower-fma.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \lambda_2}, \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      6. lower-*.f64N/A

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

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

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

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

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

    if 3.1e-6 < phi2

    1. Initial program 78.6%

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

        \[\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. lift-sin.f64N/A

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right)\right) \cdot R \]
      14. lower-*.f64N/A

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

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

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

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

Alternative 11: 79.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq -0.000108:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 3.1 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 -0.000108)
   (*
    R
    (acos
     (fma (cos phi2) (* (cos phi1) (cos lambda2)) (* (sin phi1) (sin phi2)))))
   (if (<= phi2 3.1e-6)
     (*
      R
      (acos
       (*
        (cos phi1)
        (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
     (*
      R
      (acos
       (fma
        (sin phi2)
        (sin phi1)
        (* (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2))))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= -0.000108) {
		tmp = R * acos(fma(cos(phi2), (cos(phi1) * cos(lambda2)), (sin(phi1) * sin(phi2))));
	} else if (phi2 <= 3.1e-6) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))));
	} else {
		tmp = R * acos(fma(sin(phi2), sin(phi1), (cos(phi1) * (cos(phi2) * cos((lambda1 - lambda2))))));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= -0.000108)
		tmp = Float64(R * acos(fma(cos(phi2), Float64(cos(phi1) * cos(lambda2)), Float64(sin(phi1) * sin(phi2)))));
	elseif (phi2 <= 3.1e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = Float64(R * acos(fma(sin(phi2), sin(phi1), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda1 - lambda2)))))));
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, -0.000108], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 3.1e-6], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq -0.000108:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\

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

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


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

    1. Initial program 76.8%

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

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

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      4. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      5. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2}, \cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \color{blue}{\cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \color{blue}{\cos \phi_1} \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \color{blue}{\cos \lambda_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \color{blue}{\cos \lambda_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      10. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \color{blue}{\sin \phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      11. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \color{blue}{\sin \phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      12. lower-sin.f64N/A

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

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

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

    if -1.08e-4 < phi2 < 3.1e-6

    1. Initial program 68.2%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6489.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr89.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 \]
    5. Taylor expanded in phi2 around 0

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. lower-fma.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \lambda_2}, \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      6. lower-*.f64N/A

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

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

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

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

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

    if 3.1e-6 < phi2

    1. Initial program 78.6%

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

        \[\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. lift-sin.f64N/A

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\cos \left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_2 \cdot \sin \phi_1 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\right)\right) \cdot R \]
      12. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \color{blue}{\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)}\right)\right) \cdot R \]
      14. lower-*.f64N/A

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

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

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

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

Alternative 12: 74.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \lambda_1 \cdot \sin \lambda_2\\ \mathbf{if}\;\phi_2 \leq -0.000108:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 4.5 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, t\_0\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \left(t\_0 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin lambda1) (sin lambda2))))
   (if (<= phi2 -0.000108)
     (*
      R
      (acos
       (fma
        (cos phi2)
        (* (cos phi1) (cos lambda2))
        (* (sin phi1) (sin phi2)))))
     (if (<= phi2 4.5e-6)
       (* R (acos (* (cos phi1) (fma (cos lambda1) (cos lambda2) t_0))))
       (* R (acos (* (cos phi2) (+ t_0 (* (cos lambda2) (cos lambda1))))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin(lambda1) * sin(lambda2);
	double tmp;
	if (phi2 <= -0.000108) {
		tmp = R * acos(fma(cos(phi2), (cos(phi1) * cos(lambda2)), (sin(phi1) * sin(phi2))));
	} else if (phi2 <= 4.5e-6) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), t_0)));
	} else {
		tmp = R * acos((cos(phi2) * (t_0 + (cos(lambda2) * cos(lambda1)))));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(lambda1) * sin(lambda2))
	tmp = 0.0
	if (phi2 <= -0.000108)
		tmp = Float64(R * acos(fma(cos(phi2), Float64(cos(phi1) * cos(lambda2)), Float64(sin(phi1) * sin(phi2)))));
	elseif (phi2 <= 4.5e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), t_0))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * Float64(t_0 + Float64(cos(lambda2) * cos(lambda1))))));
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -0.000108], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 4.5e-6], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(t$95$0 + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \lambda_1 \cdot \sin \lambda_2\\
\mathbf{if}\;\phi_2 \leq -0.000108:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\

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

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


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

    1. Initial program 76.8%

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

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

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      4. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      5. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2}, \cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \color{blue}{\cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \color{blue}{\cos \phi_1} \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \color{blue}{\cos \lambda_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \color{blue}{\cos \lambda_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      10. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \color{blue}{\sin \phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      11. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \color{blue}{\sin \phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      12. lower-sin.f64N/A

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

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

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

    if -1.08e-4 < phi2 < 4.50000000000000011e-6

    1. Initial program 68.2%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6489.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr89.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 \]
    5. Taylor expanded in phi2 around 0

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. lower-fma.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \lambda_2}, \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      6. lower-*.f64N/A

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

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

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

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

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

    if 4.50000000000000011e-6 < phi2

    1. Initial program 78.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6449.9

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)}\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_2 \cdot \sin \lambda_1\right)\right) \cdot R \]
      4. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right)\right) \cdot R \]
      5. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right)\right) \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)}\right) \cdot R \]
      9. lower-+.f64N/A

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

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

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

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

Alternative 13: 74.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq -0.000108:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 4.5 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \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} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 -0.000108)
   (*
    R
    (acos
     (fma (cos phi2) (* (cos phi1) (cos lambda2)) (* (sin phi1) (sin phi2)))))
   (if (<= phi2 4.5e-6)
     (*
      R
      (acos
       (*
        (cos phi1)
        (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
     (*
      R
      (acos
       (*
        (cos phi2)
        (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1)))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= -0.000108) {
		tmp = R * acos(fma(cos(phi2), (cos(phi1) * cos(lambda2)), (sin(phi1) * sin(phi2))));
	} else if (phi2 <= 4.5e-6) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))));
	} else {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= -0.000108)
		tmp = Float64(R * acos(fma(cos(phi2), Float64(cos(phi1) * cos(lambda2)), Float64(sin(phi1) * sin(phi2)))));
	elseif (phi2 <= 4.5e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, -0.000108], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 4.5e-6], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[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}

\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq -0.000108:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\

\mathbf{elif}\;\phi_2 \leq 4.5 \cdot 10^{-6}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \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 3 regimes
  2. if phi2 < -1.08e-4

    1. Initial program 76.8%

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

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

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2 \cdot \left(\cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      4. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      5. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2}, \cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \color{blue}{\cos \phi_1 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \color{blue}{\cos \phi_1} \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \color{blue}{\cos \lambda_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \color{blue}{\cos \lambda_2}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      10. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \color{blue}{\sin \phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      11. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_2, \color{blue}{\sin \phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      12. lower-sin.f64N/A

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

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

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

    if -1.08e-4 < phi2 < 4.50000000000000011e-6

    1. Initial program 68.2%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6489.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr89.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 \]
    5. Taylor expanded in phi2 around 0

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. lower-fma.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \lambda_2}, \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      6. lower-*.f64N/A

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

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

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

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

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

    if 4.50000000000000011e-6 < phi2

    1. Initial program 78.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6449.9

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)}\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_2 \cdot \sin \lambda_1\right)\right) \cdot R \]
      4. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right)\right) \cdot R \]
      5. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right)\right) \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\color{blue}{\sin \lambda_1 \cdot \sin \lambda_2} + \cos \lambda_2 \cdot \cos \lambda_1\right)\right) \cdot R \]
      10. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\color{blue}{\sin \lambda_2 \cdot \sin \lambda_1} + \cos \lambda_2 \cdot \cos \lambda_1\right)\right) \cdot R \]
      11. lower-fma.f64N/A

        \[\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 \]
      12. lower-*.f6462.2

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

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

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

Alternative 14: 69.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq -0.00315:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 4.5 \cdot 10^{-6}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \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} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 -0.00315)
   (* R (acos (fma (cos phi2) (cos phi1) (* (sin phi1) (sin phi2)))))
   (if (<= phi2 4.5e-6)
     (*
      R
      (acos
       (*
        (cos phi1)
        (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
     (*
      R
      (acos
       (*
        (cos phi2)
        (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1)))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= -0.00315) {
		tmp = R * acos(fma(cos(phi2), cos(phi1), (sin(phi1) * sin(phi2))));
	} else if (phi2 <= 4.5e-6) {
		tmp = R * acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))));
	} else {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= -0.00315)
		tmp = Float64(R * acos(fma(cos(phi2), cos(phi1), Float64(sin(phi1) * sin(phi2)))));
	elseif (phi2 <= 4.5e-6)
		tmp = Float64(R * acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, -0.00315], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 4.5e-6], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[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}

\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq -0.00315:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\

\mathbf{elif}\;\phi_2 \leq 4.5 \cdot 10^{-6}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \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 3 regimes
  2. if phi2 < -0.00315

    1. Initial program 77.4%

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2 \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      3. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \color{blue}{\cos \phi_1}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \color{blue}{\sin \phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \color{blue}{\sin \phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \color{blue}{\sin \phi_2} \cdot \sin \phi_1\right)\right) \cdot R \]
      8. lower-sin.f6446.4

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

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

    if -0.00315 < phi2 < 4.50000000000000011e-6

    1. Initial program 68.1%

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

        \[\leadsto \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. *-commutativeN/A

        \[\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 \lambda_2 \cdot \cos \lambda_1} + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\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 \]
      4. lower-cos.f64N/A

        \[\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. lower-cos.f64N/A

        \[\leadsto \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, \color{blue}{\cos \lambda_1}, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \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, \color{blue}{\sin \lambda_1} \cdot \sin \lambda_2\right)\right) \cdot R \]
      8. lower-sin.f6490.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(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \color{blue}{\sin \lambda_2}\right)\right) \cdot R \]
    4. Applied egg-rr90.0%

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. lower-fma.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \color{blue}{\cos \lambda_2}, \sin \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_1\right) \cdot R \]
      6. lower-*.f64N/A

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

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

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

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

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

    if 4.50000000000000011e-6 < phi2

    1. Initial program 78.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6449.9

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)}\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_2 \cdot \sin \lambda_1\right)\right) \cdot R \]
      4. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right)\right) \cdot R \]
      5. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right)\right) \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\color{blue}{\sin \lambda_1 \cdot \sin \lambda_2} + \cos \lambda_2 \cdot \cos \lambda_1\right)\right) \cdot R \]
      10. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\color{blue}{\sin \lambda_2 \cdot \sin \lambda_1} + \cos \lambda_2 \cdot \cos \lambda_1\right)\right) \cdot R \]
      11. lower-fma.f64N/A

        \[\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 \]
      12. lower-*.f6462.2

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

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

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

Alternative 15: 60.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.000116:\\ \;\;\;\;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 \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -0.000116)
   (* R (acos (* (cos phi1) (cos (- lambda2 lambda1)))))
   (*
    R
    (acos
     (*
      (cos phi2)
      (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -0.000116) {
		tmp = R * acos((cos(phi1) * cos((lambda2 - lambda1))));
	} else {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -0.000116)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(Float64(lambda2 - lambda1)))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -0.000116], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(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}

\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -0.000116:\\
\;\;\;\;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 \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 < -1.16e-4

    1. Initial program 82.8%

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

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

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

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

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

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

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

    if -1.16e-4 < phi1

    1. Initial program 69.7%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)}\right) \cdot R \]
      2. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\color{blue}{\cos \lambda_2} \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \sin \lambda_1\right)\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \color{blue}{\cos \lambda_1} + \sin \lambda_2 \cdot \sin \lambda_1\right)\right) \cdot R \]
      4. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2} \cdot \sin \lambda_1\right)\right) \cdot R \]
      5. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \sin \lambda_2 \cdot \color{blue}{\sin \lambda_1}\right)\right) \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_1 \cdot \sin \lambda_2}\right)\right) \cdot R \]
      7. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)}\right) \cdot R \]
      9. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\color{blue}{\sin \lambda_1 \cdot \sin \lambda_2} + \cos \lambda_2 \cdot \cos \lambda_1\right)\right) \cdot R \]
      10. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \left(\color{blue}{\sin \lambda_2 \cdot \sin \lambda_1} + \cos \lambda_2 \cdot \cos \lambda_1\right)\right) \cdot R \]
      11. lower-fma.f64N/A

        \[\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 \]
      12. lower-*.f6468.8

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.000116:\\ \;\;\;\;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 \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 16: 56.9% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\ \mathbf{if}\;\phi_2 \leq -0.0245:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 0.0034:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\mathsf{fma}\left(\phi_2, \phi_2 \cdot -0.5, 1\right), \cos \phi_1 \cdot t\_0, \sin \phi_1 \cdot \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\pi, 0.5, -\sin^{-1} \left(\cos \phi_2 \cdot t\_0\right)\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda2 lambda1))))
   (if (<= phi2 -0.0245)
     (* R (acos (fma (cos phi2) (cos phi1) (* (sin phi1) (sin phi2)))))
     (if (<= phi2 0.0034)
       (*
        R
        (acos
         (fma
          (fma phi2 (* phi2 -0.5) 1.0)
          (* (cos phi1) t_0)
          (* (sin phi1) phi2))))
       (* R (fma PI 0.5 (- (asin (* (cos phi2) t_0)))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda2 - lambda1));
	double tmp;
	if (phi2 <= -0.0245) {
		tmp = R * acos(fma(cos(phi2), cos(phi1), (sin(phi1) * sin(phi2))));
	} else if (phi2 <= 0.0034) {
		tmp = R * acos(fma(fma(phi2, (phi2 * -0.5), 1.0), (cos(phi1) * t_0), (sin(phi1) * phi2)));
	} else {
		tmp = R * fma(((double) M_PI), 0.5, -asin((cos(phi2) * t_0)));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda2 - lambda1))
	tmp = 0.0
	if (phi2 <= -0.0245)
		tmp = Float64(R * acos(fma(cos(phi2), cos(phi1), Float64(sin(phi1) * sin(phi2)))));
	elseif (phi2 <= 0.0034)
		tmp = Float64(R * acos(fma(fma(phi2, Float64(phi2 * -0.5), 1.0), Float64(cos(phi1) * t_0), Float64(sin(phi1) * phi2))));
	else
		tmp = Float64(R * fma(pi, 0.5, Float64(-asin(Float64(cos(phi2) * t_0)))));
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda2 - lambda1), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, -0.0245], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 0.0034], N[(R * N[ArcCos[N[(N[(phi2 * N[(phi2 * -0.5), $MachinePrecision] + 1.0), $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * phi2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[(Pi * 0.5 + (-N[ArcSin[N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision])), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
\mathbf{if}\;\phi_2 \leq -0.0245:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\

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

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


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

    1. Initial program 77.4%

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2 \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
      3. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      4. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \color{blue}{\cos \phi_1}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      5. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \color{blue}{\sin \phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      6. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \color{blue}{\sin \phi_2 \cdot \sin \phi_1}\right)\right) \cdot R \]
      7. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1, \color{blue}{\sin \phi_2} \cdot \sin \phi_1\right)\right) \cdot R \]
      8. lower-sin.f6446.4

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

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

    if -0.024500000000000001 < phi2 < 0.00339999999999999981

    1. Initial program 68.1%

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

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

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

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

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

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

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

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

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

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

    if 0.00339999999999999981 < phi2

    1. Initial program 78.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6449.9

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_2 - \lambda_1\right)\right) \cdot R \]
      2. lift--.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      3. lift-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      4. lift-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)} \cdot R \]
      5. acos-asinN/A

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

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

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

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

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

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

        \[\leadsto \mathsf{fma}\left(\mathsf{PI}\left(\right), \frac{1}{2}, \color{blue}{\mathsf{neg}\left(\sin^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)}\right) \cdot R \]
      12. lower-asin.f6449.9

        \[\leadsto \mathsf{fma}\left(\pi, 0.5, -\color{blue}{\sin^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)}\right) \cdot R \]
    7. Applied egg-rr49.9%

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

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

Alternative 17: 37.3% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -1.5 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

\mathbf{elif}\;\phi_1 \leq 1.6 \cdot 10^{-308}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right)\\

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


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

    1. Initial program 81.8%

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

        \[\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. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\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 \]
      4. lift-cos.f64N/A

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

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

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

        \[\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_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \lambda_1}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \lambda_1}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lower-cos.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_1\right)} \cdot R \]
      3. lower-cos.f64N/A

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

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

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

    if -1.50000000000000004e-5 < phi1 < 1.6000000000000001e-308

    1. Initial program 70.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6470.6

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_1\right)} \cdot R \]
      3. lower-cos.f64N/A

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

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

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

    if 1.6000000000000001e-308 < phi1

    1. Initial program 69.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6444.9

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_2\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_2\right)} \cdot R \]
      3. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \lambda_2\right) \cdot R \]
      4. lower-cos.f6434.2

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

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

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

Alternative 18: 50.9% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
\mathbf{if}\;\phi_1 \leq -1.15 \cdot 10^{-11}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

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


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

    1. Initial program 80.9%

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)} \cdot \cos \phi_1\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right) \cdot \cos \phi_1\right) \cdot R \]
      8. cos-negN/A

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

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

        \[\leadsto \cos^{-1} \left(\cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right) \cdot \cos \phi_1\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot \cos \phi_1\right) \cdot R \]
      12. lower--.f64N/A

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

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

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

    if -1.15000000000000007e-11 < phi1

    1. Initial program 70.2%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6453.5

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

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

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

Alternative 19: 48.5% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -0.00095:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

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


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

    1. Initial program 82.8%

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

        \[\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. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\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 \]
      4. lift-cos.f64N/A

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

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

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

        \[\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_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \lambda_1}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \lambda_1}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lower-cos.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_1\right)} \cdot R \]
      3. lower-cos.f64N/A

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

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

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

    if -9.49999999999999998e-4 < phi1

    1. Initial program 69.7%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

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

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

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

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

Alternative 20: 37.3% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -1.5 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

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


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

    1. Initial program 81.8%

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

        \[\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. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\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 \]
      4. lift-cos.f64N/A

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

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

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

        \[\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_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \lambda_1}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \lambda_1}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lower-cos.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_1\right)} \cdot R \]
      3. lower-cos.f64N/A

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

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

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

    if -1.50000000000000004e-5 < phi1

    1. Initial program 69.9%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \lambda_1}\right) \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \lambda_1\right)} \cdot R \]
      3. lower-cos.f64N/A

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

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

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

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

Alternative 21: 37.0% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 3.8 \cdot 10^{-7}:\\ \;\;\;\;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} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda2 3.8e-7)
   (* R (acos (* (cos phi1) (cos lambda1))))
   (* R (acos (* (cos phi1) (cos lambda2))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 3.8e-7) {
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	} else {
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	}
	return tmp;
}
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 <= 3.8d-7) then
        tmp = r * acos((cos(phi1) * cos(lambda1)))
    else
        tmp = r * acos((cos(phi1) * cos(lambda2)))
    end if
    code = tmp
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 3.8e-7) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda1)));
	} else {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda2)));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= 3.8e-7:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda1)))
	else:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda2)))
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 3.8e-7)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda1))));
	else
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda2))));
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= 3.8e-7)
		tmp = R * acos((cos(phi1) * cos(lambda1)));
	else
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, 3.8e-7], 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}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 3.8 \cdot 10^{-7}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

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


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

    1. Initial program 78.4%

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

        \[\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. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\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 \]
      4. lift-cos.f64N/A

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

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

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

        \[\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_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \lambda_1}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \lambda_1}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lower-cos.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_1\right)} \cdot R \]
      3. lower-cos.f64N/A

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

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

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

    if 3.80000000000000015e-7 < lambda2

    1. Initial program 59.5%

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

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \lambda_2 + \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_1 \cdot \color{blue}{\left(\lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2\right)}\right) \cdot R \]
      4. lower-fma.f64N/A

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

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_2\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_2\right)} \cdot R \]
      3. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_1} \cdot \cos \lambda_2\right) \cdot R \]
      4. lower-cos.f6441.8

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

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

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

Alternative 22: 34.5% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 3.2 \cdot 10^{-6}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right)\\

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


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

    1. Initial program 78.4%

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

        \[\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. lift-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \color{blue}{\sin \phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. lift-*.f64N/A

        \[\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 \]
      4. lift-cos.f64N/A

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

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

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

        \[\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_1 - \lambda_2\right)}\right) \cdot R \]
      8. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right)} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      9. lift-*.f64N/A

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      11. lift-*.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      12. lift-*.f64N/A

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot \cos \phi_1} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      15. lower-fma.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \lambda_1}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2 \cdot \cos \lambda_1}, \cos \phi_1, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      3. lower-cos.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_1\right)} \cdot R \]
      2. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \lambda_1\right)} \cdot R \]
      3. lower-cos.f64N/A

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

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

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

    if 3.1999999999999999e-6 < lambda2

    1. Initial program 59.5%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

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

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

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

Alternative 23: 21.1% accurate, 2.5× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -50:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_1 \cdot \lambda_1, 0.25, -0.5\right), \lambda_1\right), \mathsf{fma}\left(-0.5, \lambda_1 \cdot \lambda_1, 1\right)\right)\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= (- lambda1 lambda2) -50.0)
   (* R (acos (cos (- lambda1 lambda2))))
   (*
    R
    (acos
     (*
      (cos phi2)
      (fma
       lambda2
       (fma lambda2 (fma (* lambda1 lambda1) 0.25 -0.5) lambda1)
       (fma -0.5 (* lambda1 lambda1) 1.0)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if ((lambda1 - lambda2) <= -50.0) {
		tmp = R * acos(cos((lambda1 - lambda2)));
	} else {
		tmp = R * acos((cos(phi2) * fma(lambda2, fma(lambda2, fma((lambda1 * lambda1), 0.25, -0.5), lambda1), fma(-0.5, (lambda1 * lambda1), 1.0))));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (Float64(lambda1 - lambda2) <= -50.0)
		tmp = Float64(R * acos(cos(Float64(lambda1 - lambda2))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(lambda2, fma(lambda2, fma(Float64(lambda1 * lambda1), 0.25, -0.5), lambda1), fma(-0.5, Float64(lambda1 * lambda1), 1.0)))));
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[N[(lambda1 - lambda2), $MachinePrecision], -50.0], N[(R * N[ArcCos[N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(lambda2 * N[(lambda2 * N[(N[(lambda1 * lambda1), $MachinePrecision] * 0.25 + -0.5), $MachinePrecision] + lambda1), $MachinePrecision] + N[(-0.5 * N[(lambda1 * lambda1), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 - \lambda_2 \leq -50:\\
\;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\

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


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

    1. Initial program 73.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6451.8

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

    if -50 < (-.f64 lambda1 lambda2)

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6438.9

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\lambda_1 \cdot \left(\frac{-1}{2} \cdot \left(\lambda_1 \cdot \cos \lambda_2\right) - -1 \cdot \sin \lambda_2\right) + \cos \lambda_2\right)}\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\lambda_1, \frac{-1}{2} \cdot \left(\lambda_1 \cdot \cos \lambda_2\right) - -1 \cdot \sin \lambda_2, \cos \lambda_2\right)}\right) \cdot R \]
      3. cancel-sign-sub-invN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \color{blue}{\frac{-1}{2} \cdot \left(\lambda_1 \cdot \cos \lambda_2\right) + \left(\mathsf{neg}\left(-1\right)\right) \cdot \sin \lambda_2}, \cos \lambda_2\right)\right) \cdot R \]
      4. metadata-evalN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \frac{-1}{2} \cdot \left(\lambda_1 \cdot \cos \lambda_2\right) + \color{blue}{1} \cdot \sin \lambda_2, \cos \lambda_2\right)\right) \cdot R \]
      5. *-lft-identityN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \frac{-1}{2} \cdot \left(\lambda_1 \cdot \cos \lambda_2\right) + \color{blue}{\sin \lambda_2}, \cos \lambda_2\right)\right) \cdot R \]
      6. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \color{blue}{\mathsf{fma}\left(\frac{-1}{2}, \lambda_1 \cdot \cos \lambda_2, \sin \lambda_2\right)}, \cos \lambda_2\right)\right) \cdot R \]
      7. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \mathsf{fma}\left(\frac{-1}{2}, \color{blue}{\lambda_1 \cdot \cos \lambda_2}, \sin \lambda_2\right), \cos \lambda_2\right)\right) \cdot R \]
      8. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \mathsf{fma}\left(\frac{-1}{2}, \lambda_1 \cdot \color{blue}{\cos \lambda_2}, \sin \lambda_2\right), \cos \lambda_2\right)\right) \cdot R \]
      9. lower-sin.f64N/A

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

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

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

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(1 + \left(\frac{-1}{2} \cdot {\lambda_1}^{2} + \lambda_2 \cdot \left(\lambda_1 + \lambda_2 \cdot \left(\frac{1}{4} \cdot {\lambda_1}^{2} - \frac{1}{2}\right)\right)\right)\right)}\right) \cdot R \]
    10. Step-by-step derivation
      1. associate-+r+N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\left(1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right) + \lambda_2 \cdot \left(\lambda_1 + \lambda_2 \cdot \left(\frac{1}{4} \cdot {\lambda_1}^{2} - \frac{1}{2}\right)\right)\right)}\right) \cdot R \]
      2. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\lambda_2 \cdot \left(\lambda_1 + \lambda_2 \cdot \left(\frac{1}{4} \cdot {\lambda_1}^{2} - \frac{1}{2}\right)\right) + \left(1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)\right)}\right) \cdot R \]
      3. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\lambda_2, \lambda_1 + \lambda_2 \cdot \left(\frac{1}{4} \cdot {\lambda_1}^{2} - \frac{1}{2}\right), 1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)}\right) \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \color{blue}{\lambda_2 \cdot \left(\frac{1}{4} \cdot {\lambda_1}^{2} - \frac{1}{2}\right) + \lambda_1}, 1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)\right) \cdot R \]
      5. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \color{blue}{\mathsf{fma}\left(\lambda_2, \frac{1}{4} \cdot {\lambda_1}^{2} - \frac{1}{2}, \lambda_1\right)}, 1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)\right) \cdot R \]
      6. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, \color{blue}{\frac{1}{4} \cdot {\lambda_1}^{2} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right)}, \lambda_1\right), 1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)\right) \cdot R \]
      7. *-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, \color{blue}{{\lambda_1}^{2} \cdot \frac{1}{4}} + \left(\mathsf{neg}\left(\frac{1}{2}\right)\right), \lambda_1\right), 1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)\right) \cdot R \]
      8. metadata-evalN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, {\lambda_1}^{2} \cdot \frac{1}{4} + \color{blue}{\frac{-1}{2}}, \lambda_1\right), 1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)\right) \cdot R \]
      9. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, \color{blue}{\mathsf{fma}\left({\lambda_1}^{2}, \frac{1}{4}, \frac{-1}{2}\right)}, \lambda_1\right), 1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)\right) \cdot R \]
      10. unpow2N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\color{blue}{\lambda_1 \cdot \lambda_1}, \frac{1}{4}, \frac{-1}{2}\right), \lambda_1\right), 1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)\right) \cdot R \]
      11. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\color{blue}{\lambda_1 \cdot \lambda_1}, \frac{1}{4}, \frac{-1}{2}\right), \lambda_1\right), 1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)\right) \cdot R \]
      12. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_1 \cdot \lambda_1, \frac{1}{4}, \frac{-1}{2}\right), \lambda_1\right), \color{blue}{\frac{-1}{2} \cdot {\lambda_1}^{2} + 1}\right)\right) \cdot R \]
      13. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_1 \cdot \lambda_1, \frac{1}{4}, \frac{-1}{2}\right), \lambda_1\right), \color{blue}{\mathsf{fma}\left(\frac{-1}{2}, {\lambda_1}^{2}, 1\right)}\right)\right) \cdot R \]
      14. unpow2N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_1 \cdot \lambda_1, \frac{1}{4}, \frac{-1}{2}\right), \lambda_1\right), \mathsf{fma}\left(\frac{-1}{2}, \color{blue}{\lambda_1 \cdot \lambda_1}, 1\right)\right)\right) \cdot R \]
      15. lower-*.f6418.1

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_2, \mathsf{fma}\left(\lambda_1 \cdot \lambda_1, 0.25, -0.5\right), \lambda_1\right), \mathsf{fma}\left(-0.5, \color{blue}{\lambda_1 \cdot \lambda_1}, 1\right)\right)\right) \cdot R \]
    11. Simplified18.1%

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

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

Alternative 24: 22.5% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -2 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(-0.5, \lambda_1 \cdot \lambda_1, 1\right)\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= (- lambda1 lambda2) -2e-5)
   (* R (acos (cos (- lambda1 lambda2))))
   (* R (acos (* (cos phi2) (fma -0.5 (* lambda1 lambda1) 1.0))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if ((lambda1 - lambda2) <= -2e-5) {
		tmp = R * acos(cos((lambda1 - lambda2)));
	} else {
		tmp = R * acos((cos(phi2) * fma(-0.5, (lambda1 * lambda1), 1.0)));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (Float64(lambda1 - lambda2) <= -2e-5)
		tmp = Float64(R * acos(cos(Float64(lambda1 - lambda2))));
	else
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(-0.5, Float64(lambda1 * lambda1), 1.0))));
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[N[(lambda1 - lambda2), $MachinePrecision], -2e-5], N[(R * N[ArcCos[N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(-0.5 * N[(lambda1 * lambda1), $MachinePrecision] + 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 - \lambda_2 \leq -2 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\

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


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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6451.8

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

    if -2.00000000000000016e-5 < (-.f64 lambda1 lambda2)

    1. Initial program 72.7%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6438.8

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

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\lambda_1 \cdot \left(\frac{-1}{2} \cdot \left(\lambda_1 \cdot \cos \lambda_2\right) - -1 \cdot \sin \lambda_2\right) + \cos \lambda_2\right)}\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\lambda_1, \frac{-1}{2} \cdot \left(\lambda_1 \cdot \cos \lambda_2\right) - -1 \cdot \sin \lambda_2, \cos \lambda_2\right)}\right) \cdot R \]
      3. cancel-sign-sub-invN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \color{blue}{\frac{-1}{2} \cdot \left(\lambda_1 \cdot \cos \lambda_2\right) + \left(\mathsf{neg}\left(-1\right)\right) \cdot \sin \lambda_2}, \cos \lambda_2\right)\right) \cdot R \]
      4. metadata-evalN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \frac{-1}{2} \cdot \left(\lambda_1 \cdot \cos \lambda_2\right) + \color{blue}{1} \cdot \sin \lambda_2, \cos \lambda_2\right)\right) \cdot R \]
      5. *-lft-identityN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \frac{-1}{2} \cdot \left(\lambda_1 \cdot \cos \lambda_2\right) + \color{blue}{\sin \lambda_2}, \cos \lambda_2\right)\right) \cdot R \]
      6. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \color{blue}{\mathsf{fma}\left(\frac{-1}{2}, \lambda_1 \cdot \cos \lambda_2, \sin \lambda_2\right)}, \cos \lambda_2\right)\right) \cdot R \]
      7. lower-*.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \mathsf{fma}\left(\frac{-1}{2}, \color{blue}{\lambda_1 \cdot \cos \lambda_2}, \sin \lambda_2\right), \cos \lambda_2\right)\right) \cdot R \]
      8. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \mathsf{fma}\left(\frac{-1}{2}, \lambda_1 \cdot \color{blue}{\cos \lambda_2}, \sin \lambda_2\right), \cos \lambda_2\right)\right) \cdot R \]
      9. lower-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\lambda_1, \mathsf{fma}\left(\frac{-1}{2}, \lambda_1 \cdot \cos \lambda_2, \color{blue}{\sin \lambda_2}\right), \cos \lambda_2\right)\right) \cdot R \]
      10. lower-cos.f6428.0

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

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

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(1 + \frac{-1}{2} \cdot {\lambda_1}^{2}\right)}\right) \cdot R \]
    10. Step-by-step derivation
      1. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\left(\frac{-1}{2} \cdot {\lambda_1}^{2} + 1\right)}\right) \cdot R \]
      2. lower-fma.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\mathsf{fma}\left(\frac{-1}{2}, {\lambda_1}^{2}, 1\right)}\right) \cdot R \]
      3. unpow2N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\frac{-1}{2}, \color{blue}{\lambda_1 \cdot \lambda_1}, 1\right)\right) \cdot R \]
      4. lower-*.f6419.4

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(-0.5, \color{blue}{\lambda_1 \cdot \lambda_1}, 1\right)\right) \cdot R \]
    11. Simplified19.4%

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

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

Alternative 25: 21.3% accurate, 2.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1.05 \cdot 10^{-7}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{elif}\;\lambda_1 \leq 4.2 \cdot 10^{-295}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -1.05e-7)
   (* R (acos (cos lambda1)))
   (if (<= lambda1 4.2e-295)
     (* R (acos (cos phi1)))
     (* R (acos (cos lambda2))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1.05e-7) {
		tmp = R * acos(cos(lambda1));
	} else if (lambda1 <= 4.2e-295) {
		tmp = R * acos(cos(phi1));
	} else {
		tmp = R * acos(cos(lambda2));
	}
	return tmp;
}
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.05d-7)) then
        tmp = r * acos(cos(lambda1))
    else if (lambda1 <= 4.2d-295) then
        tmp = r * acos(cos(phi1))
    else
        tmp = r * acos(cos(lambda2))
    end if
    code = tmp
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1.05e-7) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else if (lambda1 <= 4.2e-295) {
		tmp = R * Math.acos(Math.cos(phi1));
	} else {
		tmp = R * Math.acos(Math.cos(lambda2));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -1.05e-7:
		tmp = R * math.acos(math.cos(lambda1))
	elif lambda1 <= 4.2e-295:
		tmp = R * math.acos(math.cos(phi1))
	else:
		tmp = R * math.acos(math.cos(lambda2))
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -1.05e-7)
		tmp = Float64(R * acos(cos(lambda1)));
	elseif (lambda1 <= 4.2e-295)
		tmp = Float64(R * acos(cos(phi1)));
	else
		tmp = Float64(R * acos(cos(lambda2)));
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda1 <= -1.05e-7)
		tmp = R * acos(cos(lambda1));
	elseif (lambda1 <= 4.2e-295)
		tmp = R * acos(cos(phi1));
	else
		tmp = R * acos(cos(lambda2));
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -1.05e-7], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda1, 4.2e-295], N[(R * N[ArcCos[N[Cos[phi1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[Cos[lambda2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -1.05 \cdot 10^{-7}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{elif}\;\lambda_1 \leq 4.2 \cdot 10^{-295}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\

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


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

    1. Initial program 58.3%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

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

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6430.8

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

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

    if -1.05e-7 < lambda1 < 4.19999999999999986e-295

    1. Initial program 90.3%

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

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \lambda_2 + \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_1 \cdot \color{blue}{\left(\lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2\right)}\right) \cdot R \]
      4. lower-fma.f64N/A

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6426.4

        \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]
    11. Simplified26.4%

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

    if 4.19999999999999986e-295 < lambda1

    1. Initial program 72.9%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6442.6

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)} \cdot R \]
    10. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
      2. lower-cos.f6418.4

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
    11. Simplified18.4%

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

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

Alternative 26: 21.2% accurate, 2.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -0.78:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{elif}\;\lambda_1 \leq -8.5 \cdot 10^{-199}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -0.78)
   (* R (acos (cos lambda1)))
   (if (<= lambda1 -8.5e-199)
     (* R (fabs (remainder (- lambda1 lambda2) (* PI 2.0))))
     (* R (acos (cos lambda2))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -0.78) {
		tmp = R * acos(cos(lambda1));
	} else if (lambda1 <= -8.5e-199) {
		tmp = R * fabs(remainder((lambda1 - lambda2), (((double) M_PI) * 2.0)));
	} else {
		tmp = R * acos(cos(lambda2));
	}
	return tmp;
}
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -0.78) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else if (lambda1 <= -8.5e-199) {
		tmp = R * Math.abs(Math.IEEEremainder((lambda1 - lambda2), (Math.PI * 2.0)));
	} else {
		tmp = R * Math.acos(Math.cos(lambda2));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -0.78:
		tmp = R * math.acos(math.cos(lambda1))
	elif lambda1 <= -8.5e-199:
		tmp = R * math.fabs(math.remainder((lambda1 - lambda2), (math.pi * 2.0)))
	else:
		tmp = R * math.acos(math.cos(lambda2))
	return tmp
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -0.78], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda1, -8.5e-199], N[(R * N[Abs[N[With[{TMP1 = N[(lambda1 - lambda2), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[Cos[lambda2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -0.78:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

\mathbf{elif}\;\lambda_1 \leq -8.5 \cdot 10^{-199}:\\
\;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\

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


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

    1. Initial program 58.2%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6443.0

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6431.0

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    11. Simplified31.0%

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

    if -0.78000000000000003 < lambda1 < -8.4999999999999994e-199

    1. Initial program 90.8%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6448.8

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      2. acos-cosN/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      3. lower-fabs.f64N/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      4. lower-remainder.f64N/A

        \[\leadsto \left|\color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
      5. lift-PI.f64N/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right| \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      7. lower-*.f6418.5

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\pi \cdot 2\right)}\right)\right| \cdot R \]
    10. Applied egg-rr18.5%

      \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|} \cdot R \]

    if -8.4999999999999994e-199 < lambda1

    1. Initial program 75.9%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6443.6

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)} \cdot R \]
    10. Step-by-step derivation
      1. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
      2. lower-cos.f6420.6

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
    11. Simplified20.6%

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

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

Alternative 27: 29.9% accurate, 2.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -0.0135:\\
\;\;\;\;R \cdot \cos^{-1} \cos \phi_1\\

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


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

    1. Initial program 82.8%

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

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \lambda_2 + \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_1 \cdot \color{blue}{\left(\lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2\right)}\right) \cdot R \]
      4. lower-fma.f64N/A

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6430.0

        \[\leadsto \cos^{-1} \color{blue}{\cos \phi_1} \cdot R \]
    11. Simplified30.0%

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

    if -0.0134999999999999998 < phi1

    1. Initial program 69.7%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

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

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

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

Alternative 28: 21.9% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -0.78:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -0.78)
   (* R (acos (cos lambda1)))
   (* R (fabs (remainder (- lambda1 lambda2) (* PI 2.0))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -0.78) {
		tmp = R * acos(cos(lambda1));
	} else {
		tmp = R * fabs(remainder((lambda1 - lambda2), (((double) M_PI) * 2.0)));
	}
	return tmp;
}
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -0.78) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else {
		tmp = R * Math.abs(Math.IEEEremainder((lambda1 - lambda2), (Math.PI * 2.0)));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -0.78:
		tmp = R * math.acos(math.cos(lambda1))
	else:
		tmp = R * math.fabs(math.remainder((lambda1 - lambda2), (math.pi * 2.0)))
	return tmp
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -0.78], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[Abs[N[With[{TMP1 = N[(lambda1 - lambda2), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -0.78:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

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


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

    1. Initial program 58.2%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6443.0

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    10. Step-by-step derivation
      1. lower-cos.f6431.0

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    11. Simplified31.0%

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

    if -0.78000000000000003 < 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. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      3. sub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
      4. remove-double-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
      5. mul-1-negN/A

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
      7. +-commutativeN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
      8. cos-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      10. mul-1-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
      11. unsub-negN/A

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. lower--.f6444.7

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
      2. remove-double-negN/A

        \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
      3. distribute-neg-inN/A

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
      4. +-commutativeN/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
      5. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
      6. cos-negN/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      7. lower-cos.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
      8. neg-mul-1N/A

        \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
      9. sub-negN/A

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

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

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

        \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
      2. acos-cosN/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      3. lower-fabs.f64N/A

        \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
      4. lower-remainder.f64N/A

        \[\leadsto \left|\color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
      5. lift-PI.f64N/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right| \cdot R \]
      6. *-commutativeN/A

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      7. lower-*.f6418.2

        \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\pi \cdot 2\right)}\right)\right| \cdot R \]
    10. Applied egg-rr18.2%

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

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

Alternative 29: 19.5% accurate, 5.4× speedup?

\[\begin{array}{l} \\ R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right| \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R (fabs (remainder (- lambda1 lambda2) (* PI 2.0)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * fabs(remainder((lambda1 - lambda2), (((double) M_PI) * 2.0)));
}
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * Math.abs(Math.IEEEremainder((lambda1 - lambda2), (Math.PI * 2.0)));
}
def code(R, lambda1, lambda2, phi1, phi2):
	return R * math.fabs(math.remainder((lambda1 - lambda2), (math.pi * 2.0)))
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * N[Abs[N[With[{TMP1 = N[(lambda1 - lambda2), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|
\end{array}
Derivation
  1. Initial program 73.0%

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

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
  4. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    2. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    3. sub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
    4. remove-double-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
    5. mul-1-negN/A

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

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
    7. +-commutativeN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
    8. cos-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
    9. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
    10. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
    11. unsub-negN/A

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

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

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

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

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
    2. remove-double-negN/A

      \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
    3. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
    4. +-commutativeN/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
    5. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
    6. cos-negN/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    7. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    8. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
    9. sub-negN/A

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

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

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

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot R \]
    2. acos-cosN/A

      \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
    3. lower-fabs.f64N/A

      \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right|} \cdot R \]
    4. lower-remainder.f64N/A

      \[\leadsto \left|\color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)}\right| \cdot R \]
    5. lift-PI.f64N/A

      \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(2 \cdot \color{blue}{\mathsf{PI}\left(\right)}\right)\right)\right| \cdot R \]
    6. *-commutativeN/A

      \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
    7. lower-*.f6419.2

      \[\leadsto \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \color{blue}{\left(\pi \cdot 2\right)}\right)\right| \cdot R \]
  10. Applied egg-rr19.2%

    \[\leadsto \color{blue}{\left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|} \cdot R \]
  11. Final simplification19.2%

    \[\leadsto R \cdot \left|\left(\left(\lambda_1 - \lambda_2\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right| \]
  12. Add Preprocessing

Alternative 30: 5.3% accurate, 104.5× speedup?

\[\begin{array}{l} \\ \lambda_1 \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2) :precision binary64 (* lambda1 R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return lambda1 * 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 = lambda1 * r
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return lambda1 * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return lambda1 * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(lambda1 * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = lambda1 * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(lambda1 * R), $MachinePrecision]
\begin{array}{l}

\\
\lambda_1 \cdot R
\end{array}
Derivation
  1. Initial program 73.0%

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

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
  4. Step-by-step derivation
    1. lower-*.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    2. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\color{blue}{\cos \phi_2} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    3. sub-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right) \cdot R \]
    4. remove-double-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_1\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right) \cdot R \]
    5. mul-1-negN/A

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

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\mathsf{neg}\left(\left(-1 \cdot \lambda_1 + \lambda_2\right)\right)\right)}\right) \cdot R \]
    7. +-commutativeN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_2 + -1 \cdot \lambda_1\right)}\right)\right)\right) \cdot R \]
    8. cos-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
    9. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
    10. mul-1-negN/A

      \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 + \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)}\right)\right) \cdot R \]
    11. unsub-negN/A

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

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

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

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

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\lambda_2 + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right)} \cdot R \]
    2. remove-double-negN/A

      \[\leadsto \cos^{-1} \cos \left(\color{blue}{\left(\mathsf{neg}\left(\left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} + \left(\mathsf{neg}\left(\lambda_1\right)\right)\right) \cdot R \]
    3. distribute-neg-inN/A

      \[\leadsto \cos^{-1} \cos \color{blue}{\left(\mathsf{neg}\left(\left(\left(\mathsf{neg}\left(\lambda_2\right)\right) + \lambda_1\right)\right)\right)} \cdot R \]
    4. +-commutativeN/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\color{blue}{\left(\lambda_1 + \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)}\right)\right) \cdot R \]
    5. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\mathsf{neg}\left(\left(\lambda_1 + \color{blue}{-1 \cdot \lambda_2}\right)\right)\right) \cdot R \]
    6. cos-negN/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    7. lower-cos.f64N/A

      \[\leadsto \cos^{-1} \color{blue}{\cos \left(\lambda_1 + -1 \cdot \lambda_2\right)} \cdot R \]
    8. neg-mul-1N/A

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 + \color{blue}{\left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
    9. sub-negN/A

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

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

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

    \[\leadsto \color{blue}{R \cdot \lambda_1} \]
  10. Step-by-step derivation
    1. *-commutativeN/A

      \[\leadsto \color{blue}{\lambda_1 \cdot R} \]
    2. lower-*.f644.9

      \[\leadsto \color{blue}{\lambda_1 \cdot R} \]
  11. Simplified4.9%

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

Reproduce

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