Spherical law of cosines

Percentage Accurate: 73.6% → 96.1%
Time: 24.7s
Alternatives: 25
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 25 alternatives:

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

Initial Program: 73.6% accurate, 1.0× speedup?

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

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

Alternative 1: 96.1% accurate, 0.4× speedup?

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

\\
\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.9998:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left|\left(\left(\lambda_2 - \lambda_1\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.99980000000000002

    1. Initial program 76.3%

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f649.0

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified9.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. cos-lowering-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. --lowering--.f649.0

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

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

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

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

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

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

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

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

        \[\leadsto \left|\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
      8. cos-diffN/A

        \[\leadsto \left|\left(\cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
      9. acos-cos-sN/A

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

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

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

        \[\leadsto \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      13. PI-lowering-PI.f6473.0

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

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

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

Alternative 2: 96.1% 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.9998:\\ \;\;\;\;R \cdot \cos^{-1} \left(t\_0 + t\_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_2 - \lambda_1\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.9998)
     (*
      R
      (acos
       (+
        t_0
        (*
         t_1
         (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2)))))))
     (* R (fabs (remainder (- lambda2 lambda1) (* 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.9998) {
		tmp = R * acos((t_0 + (t_1 * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2))))));
	} else {
		tmp = R * fabs(remainder((lambda2 - lambda1), (((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.9998], N[(R * N[ArcCos[N[(t$95$0 + N[(t$95$1 * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[Abs[N[With[{TMP1 = N[(lambda2 - lambda1), $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.9998:\\
\;\;\;\;R \cdot \cos^{-1} \left(t\_0 + t\_1 \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left|\left(\left(\lambda_2 - \lambda_1\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.99980000000000002

    1. Initial program 76.3%

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f649.0

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified9.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. cos-lowering-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. --lowering--.f649.0

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

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

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

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

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

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

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

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

        \[\leadsto \left|\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
      8. cos-diffN/A

        \[\leadsto \left|\left(\cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
      9. acos-cos-sN/A

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

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

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

        \[\leadsto \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      13. PI-lowering-PI.f6473.0

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

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

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

Alternative 3: 96.1% 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.9998:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right), t\_0\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_2 - \lambda_1\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.9998)
     (*
      R
      (acos
       (fma
        (cos phi2)
        (*
         (cos phi1)
         (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))
        t_0)))
     (* R (fabs (remainder (- lambda2 lambda1) (* 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.9998) {
		tmp = R * acos(fma(cos(phi2), (cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))), t_0));
	} else {
		tmp = R * fabs(remainder((lambda2 - lambda1), (((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.9998], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * 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] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[Abs[N[With[{TMP1 = N[(lambda2 - lambda1), $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.9998:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right), t\_0\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left|\left(\left(\lambda_2 - \lambda_1\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.99980000000000002

    1. Initial program 76.3%

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f649.0

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified9.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. cos-lowering-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. --lowering--.f649.0

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

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

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

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

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

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

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

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

        \[\leadsto \left|\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
      8. cos-diffN/A

        \[\leadsto \left|\left(\cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
      9. acos-cos-sN/A

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

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

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

        \[\leadsto \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      13. PI-lowering-PI.f6473.0

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

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

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

Alternative 4: 83.0% accurate, 0.7× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -6.5e5 or 1.8999999999999999e-18 < phi1

    1. Initial program 75.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. +-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 \]
      2. accelerator-lowering-fma.f64N/A

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

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

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

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

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

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

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

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

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

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

    if -6.5e5 < phi1 < 1.8999999999999999e-18

    1. Initial program 72.8%

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

        \[\leadsto \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. accelerator-lowering-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. cos-lowering-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. cos-lowering-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. *-lowering-*.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. sin-lowering-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. sin-lowering-sin.f6493.5

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

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
      2. sin-lowering-sin.f6492.2

        \[\leadsto \cos^{-1} \left(\phi_1 \cdot \color{blue}{\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 \]
    7. Simplified92.2%

      \[\leadsto \cos^{-1} \left(\color{blue}{\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 \]
  3. Recombined 2 regimes into one program.
  4. Final simplification83.0%

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

Alternative 5: 83.1% accurate, 0.8× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -0.062 or 1.8999999999999999e-18 < phi1

    1. Initial program 76.3%

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

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

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

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

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

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

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

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

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

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

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

    if -0.062 < phi1 < 1.8999999999999999e-18

    1. Initial program 72.1%

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

        \[\leadsto \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. accelerator-lowering-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. cos-lowering-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. cos-lowering-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. *-lowering-*.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. sin-lowering-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. sin-lowering-sin.f6493.4

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_1 \cdot \sin \phi_2 + \cos \phi_2 \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_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right) + \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
      2. accelerator-lowering-fma.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 6: 82.8% accurate, 1.0× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -6.5e-25 or 1.8999999999999999e-18 < phi1

    1. Initial program 75.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. +-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 \]
      2. accelerator-lowering-fma.f64N/A

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

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

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

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

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

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

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

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

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

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

    if -6.5e-25 < phi1 < 1.8999999999999999e-18

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

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

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

      \[\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. *-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 \]
      3. +-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 \]
      4. *-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 \]
      5. accelerator-lowering-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 \]
      6. sin-lowering-sin.f64N/A

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

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

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

        \[\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 \]
      10. cos-lowering-cos.f6493.0

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

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

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

Alternative 7: 82.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := 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)\\ \mathbf{if}\;\phi_1 \leq -6.5 \cdot 10^{-25}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_1 \leq 1.9 \cdot 10^{-18}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (*
          R
          (acos
           (fma
            (sin phi2)
            (sin phi1)
            (* (cos phi1) (* (cos phi2) (cos (- lambda1 lambda2)))))))))
   (if (<= phi1 -6.5e-25)
     t_0
     (if (<= phi1 1.9e-18)
       (*
        R
        (acos
         (*
          (cos phi2)
          (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))
       t_0))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = R * acos(fma(sin(phi2), sin(phi1), (cos(phi1) * (cos(phi2) * cos((lambda1 - lambda2))))));
	double tmp;
	if (phi1 <= -6.5e-25) {
		tmp = t_0;
	} else if (phi1 <= 1.9e-18) {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(R * acos(fma(sin(phi2), sin(phi1), Float64(cos(phi1) * Float64(cos(phi2) * cos(Float64(lambda1 - lambda2)))))))
	tmp = 0.0
	if (phi1 <= -6.5e-25)
		tmp = t_0;
	elseif (phi1 <= 1.9e-18)
		tmp = Float64(R * acos(Float64(cos(phi2) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1))))));
	else
		tmp = t_0;
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = 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]}, If[LessEqual[phi1, -6.5e-25], t$95$0, If[LessEqual[phi1, 1.9e-18], 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], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := 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)\\
\mathbf{if}\;\phi_1 \leq -6.5 \cdot 10^{-25}:\\
\;\;\;\;t\_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -6.5e-25 or 1.8999999999999999e-18 < phi1

    1. Initial program 75.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. *-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 \]
      2. accelerator-lowering-fma.f64N/A

        \[\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 \]
      3. sin-lowering-sin.f64N/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\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 \]
      4. sin-lowering-sin.f64N/A

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right)\right)\right) \cdot R \]
    4. Applied egg-rr75.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 \]

    if -6.5e-25 < phi1 < 1.8999999999999999e-18

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

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

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

      \[\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. *-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 \]
      3. +-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 \]
      4. *-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 \]
      5. accelerator-lowering-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 \]
      6. sin-lowering-sin.f64N/A

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

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

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

        \[\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 \]
      10. cos-lowering-cos.f6493.0

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -6.5 \cdot 10^{-25}:\\ \;\;\;\;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)\\ \mathbf{elif}\;\phi_1 \leq 1.9 \cdot 10^{-18}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_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 8: 73.3% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ \mathbf{if}\;\lambda_1 \leq -3.9 \cdot 10^{-7}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_1, t\_0\right)\right)\\ \mathbf{elif}\;\lambda_1 \leq 7.5 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \phi_1 \cdot \cos \phi_2, t\_0\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
 (let* ((t_0 (* (sin phi1) (sin phi2))))
   (if (<= lambda1 -3.9e-7)
     (* R (acos (fma (cos phi2) (* (cos phi1) (cos lambda1)) t_0)))
     (if (<= lambda1 7.5e-5)
       (* R (acos (fma (cos lambda2) (* (cos phi1) (cos phi2)) t_0)))
       (*
        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 t_0 = sin(phi1) * sin(phi2);
	double tmp;
	if (lambda1 <= -3.9e-7) {
		tmp = R * acos(fma(cos(phi2), (cos(phi1) * cos(lambda1)), t_0));
	} else if (lambda1 <= 7.5e-5) {
		tmp = R * acos(fma(cos(lambda2), (cos(phi1) * cos(phi2)), t_0));
	} else {
		tmp = R * acos((cos(phi2) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))));
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(phi1) * sin(phi2))
	tmp = 0.0
	if (lambda1 <= -3.9e-7)
		tmp = Float64(R * acos(fma(cos(phi2), Float64(cos(phi1) * cos(lambda1)), t_0)));
	elseif (lambda1 <= 7.5e-5)
		tmp = Float64(R * acos(fma(cos(lambda2), Float64(cos(phi1) * cos(phi2)), t_0)));
	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_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[lambda1, -3.9e-7], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[lambda1, 7.5e-5], N[(R * N[ArcCos[N[(N[Cos[lambda2], $MachinePrecision] * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] + t$95$0), $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}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
\mathbf{if}\;\lambda_1 \leq -3.9 \cdot 10^{-7}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_1, t\_0\right)\right)\\

\mathbf{elif}\;\lambda_1 \leq 7.5 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \phi_1 \cdot \cos \phi_2, t\_0\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 lambda1 < -3.90000000000000025e-7

    1. Initial program 63.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 lambda2 around 0

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\color{blue}{\cos \phi_2}, \cos \lambda_1 \cdot \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, \color{blue}{\cos \phi_1 \cdot \cos \lambda_1}, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
      6. *-lowering-*.f64N/A

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

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

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

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

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

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

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

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

    if -3.90000000000000025e-7 < lambda1 < 7.49999999999999934e-5

    1. Initial program 93.9%

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

        \[\leadsto \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. accelerator-lowering-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. cos-lowering-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. cos-lowering-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. *-lowering-*.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. sin-lowering-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. sin-lowering-sin.f6493.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-rr93.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 lambda1 around 0

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

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

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

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

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

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

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

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

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

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

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

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

    if 7.49999999999999934e-5 < lambda1

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6432.4

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

      \[\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. *-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 \]
      3. +-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 \]
      4. *-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 \]
      5. accelerator-lowering-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 \]
      6. sin-lowering-sin.f64N/A

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

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

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

        \[\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 \]
      10. cos-lowering-cos.f6449.7

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -3.9 \cdot 10^{-7}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \cos \lambda_1, \sin \phi_1 \cdot \sin \phi_2\right)\right)\\ \mathbf{elif}\;\lambda_1 \leq 7.5 \cdot 10^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_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 9: 73.4% accurate, 1.0× speedup?

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

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

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

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


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

    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. accelerator-lowering-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. cos-lowering-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. cos-lowering-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. *-lowering-*.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. sin-lowering-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. sin-lowering-sin.f6499.3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -0.062 < phi1 < 1.05e-4

    1. Initial program 72.1%

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6470.4

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

      \[\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. *-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 \]
      3. +-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 \]
      4. *-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 \]
      5. accelerator-lowering-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 \]
      6. sin-lowering-sin.f64N/A

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

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

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

        \[\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 \]
      10. cos-lowering-cos.f6490.2

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \color{blue}{\cos \lambda_1}\right)\right) \cdot R \]
    7. Applied egg-rr90.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 2 regimes into one program.
  4. Final simplification72.6%

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

Alternative 10: 65.6% accurate, 1.0× speedup?

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

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

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


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

    1. Initial program 73.7%

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

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

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

      \[\leadsto \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. *-lowering-*.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. accelerator-lowering-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. cos-lowering-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. cos-lowering-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. *-lowering-*.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. sin-lowering-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. sin-lowering-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. cos-lowering-cos.f6451.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. Simplified51.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 -6.00000000000000015e-5 < phi1 < 0.900000000000000022

    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. *-lowering-*.f64N/A

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6471.3

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

      \[\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. *-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 \]
      3. +-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 \]
      4. *-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 \]
      5. accelerator-lowering-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 \]
      6. sin-lowering-sin.f64N/A

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

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

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

        \[\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 \]
      10. cos-lowering-cos.f6491.2

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \color{blue}{\cos \lambda_1}\right)\right) \cdot R \]
    7. Applied egg-rr91.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 \]

    if 0.900000000000000022 < phi1

    1. Initial program 77.5%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 + \lambda_2 \cdot \left(\lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1\right)\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. +-commutativeN/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(\lambda_2 \cdot \left(\lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1\right) + \cos \lambda_1\right)}\right) \cdot R \]
      2. accelerator-lowering-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(\lambda_2, \lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1, \cos \lambda_1\right)}\right) \cdot R \]
    5. Simplified51.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    7. 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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      4. accelerator-lowering-fma.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \mathsf{fma}\left(\lambda_2, \lambda_2 \cdot \frac{-1}{2}, 1\right), \color{blue}{\sin \phi_2} \cdot \sin \phi_1\right)\right) \cdot R \]
      17. sin-lowering-sin.f6437.4

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

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

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

Alternative 11: 62.9% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -0.072:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\

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

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


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

    1. Initial program 75.2%

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

      \[\leadsto \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. *-lowering-*.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. cos-lowering-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. --lowering--.f64N/A

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

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

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

    if -0.0719999999999999946 < phi1 < 0.021999999999999999

    1. Initial program 72.1%

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6470.4

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

      \[\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. *-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 \]
      3. +-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 \]
      4. *-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 \]
      5. accelerator-lowering-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 \]
      6. sin-lowering-sin.f64N/A

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

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

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

        \[\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 \]
      10. cos-lowering-cos.f6490.2

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \color{blue}{\cos \lambda_1}\right)\right) \cdot R \]
    7. Applied egg-rr90.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 \]

    if 0.021999999999999999 < phi1

    1. Initial program 77.5%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 + \lambda_2 \cdot \left(\lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1\right)\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. +-commutativeN/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(\lambda_2 \cdot \left(\lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1\right) + \cos \lambda_1\right)}\right) \cdot R \]
      2. accelerator-lowering-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(\lambda_2, \lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1, \cos \lambda_1\right)}\right) \cdot R \]
    5. Simplified51.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    7. 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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      4. accelerator-lowering-fma.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \mathsf{fma}\left(\lambda_2, \lambda_2 \cdot \frac{-1}{2}, 1\right), \color{blue}{\sin \phi_2} \cdot \sin \phi_1\right)\right) \cdot R \]
      17. sin-lowering-sin.f6437.4

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

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

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

Alternative 12: 62.9% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -0.082:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\\

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

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


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

    1. Initial program 75.2%

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

      \[\leadsto \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. *-lowering-*.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. cos-lowering-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. --lowering--.f64N/A

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

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

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

    if -0.0820000000000000034 < phi1 < 160

    1. Initial program 72.1%

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6470.4

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

      \[\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. *-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 \]
      3. accelerator-lowering-fma.f64N/A

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

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

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

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

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

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

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

    if 160 < phi1

    1. Initial program 77.5%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 + \lambda_2 \cdot \left(\lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1\right)\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. +-commutativeN/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(\lambda_2 \cdot \left(\lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1\right) + \cos \lambda_1\right)}\right) \cdot R \]
      2. accelerator-lowering-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(\lambda_2, \lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1, \cos \lambda_1\right)}\right) \cdot R \]
    5. Simplified51.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    7. 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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      4. accelerator-lowering-fma.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \mathsf{fma}\left(\lambda_2, \lambda_2 \cdot \frac{-1}{2}, 1\right), \color{blue}{\sin \phi_2} \cdot \sin \phi_1\right)\right) \cdot R \]
      17. sin-lowering-sin.f6437.4

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

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

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

Alternative 13: 53.4% accurate, 1.2× speedup?

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

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

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

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


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

    1. Initial program 74.4%

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

      \[\leadsto \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. *-lowering-*.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. cos-lowering-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. --lowering--.f64N/A

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

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

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

    if -0.0379999999999999991 < phi1 < 0.299999999999999989

    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(\phi_1 \cdot \left(\sin \phi_2 + \frac{-1}{2} \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)\right) + \cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    4. Step-by-step derivation
      1. distribute-rgt-inN/A

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

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

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

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

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

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

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

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

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

    if 0.299999999999999989 < phi1

    1. Initial program 77.5%

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

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 + \lambda_2 \cdot \left(\lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1\right)\right)}\right) \cdot R \]
    4. Step-by-step derivation
      1. +-commutativeN/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(\lambda_2 \cdot \left(\lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1\right) + \cos \lambda_1\right)}\right) \cdot R \]
      2. accelerator-lowering-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(\lambda_2, \lambda_2 \cdot \left(\frac{-1}{2} \cdot \cos \lambda_1 + \frac{-1}{6} \cdot \left(\lambda_2 \cdot \sin \lambda_1\right)\right) - -1 \cdot \sin \lambda_1, \cos \lambda_1\right)}\right) \cdot R \]
    5. Simplified51.6%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    7. 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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\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 \left(1 + \frac{-1}{2} \cdot {\lambda_2}^{2}\right)\right)} + \sin \phi_1 \cdot \sin \phi_2\right) \cdot R \]
      4. accelerator-lowering-fma.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_2, \cos \phi_1 \cdot \mathsf{fma}\left(\lambda_2, \lambda_2 \cdot \frac{-1}{2}, 1\right), \color{blue}{\sin \phi_2} \cdot \sin \phi_1\right)\right) \cdot R \]
      17. sin-lowering-sin.f6437.4

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

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

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

Alternative 14: 49.9% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\ \mathbf{if}\;\phi_1 \leq -0.0033:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \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 -0.0033)
     (* R (acos (* (cos phi1) t_0)))
     (* R (acos (+ (* (sin phi1) (sin phi2)) (* (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 <= -0.0033) {
		tmp = R * acos((cos(phi1) * t_0));
	} else {
		tmp = R * acos(((sin(phi1) * sin(phi2)) + (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 <= (-0.0033d0)) then
        tmp = r * acos((cos(phi1) * t_0))
    else
        tmp = r * acos(((sin(phi1) * sin(phi2)) + (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 <= -0.0033) {
		tmp = R * Math.acos((Math.cos(phi1) * t_0));
	} else {
		tmp = R * Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + (Math.cos(phi2) * t_0)));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda2 - lambda1))
	tmp = 0
	if phi1 <= -0.0033:
		tmp = R * math.acos((math.cos(phi1) * t_0))
	else:
		tmp = R * math.acos(((math.sin(phi1) * math.sin(phi2)) + (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 <= -0.0033)
		tmp = Float64(R * acos(Float64(cos(phi1) * t_0)));
	else
		tmp = Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + 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 <= -0.0033)
		tmp = R * acos((cos(phi1) * t_0));
	else
		tmp = R * acos(((sin(phi1) * sin(phi2)) + (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, -0.0033], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[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_1 \leq -0.0033:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

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


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

    1. Initial program 74.4%

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

      \[\leadsto \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. *-lowering-*.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. cos-lowering-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. --lowering--.f64N/A

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

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

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

    if -0.0033 < phi1

    1. Initial program 74.5%

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

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

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

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

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \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(\sin \phi_1 \cdot \sin \phi_2 + \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(\sin \phi_1 \cdot \sin \phi_2 + \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(\sin \phi_1 \cdot \sin \phi_2 + \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(\sin \phi_1 \cdot \sin \phi_2 + \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(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_2 \cdot \color{blue}{\cos \left(\lambda_2 + -1 \cdot \lambda_1\right)}\right) \cdot R \]
      9. cos-lowering-cos.f64N/A

        \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \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(\sin \phi_1 \cdot \sin \phi_2 + \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(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
      12. --lowering--.f6450.9

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

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

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

Alternative 15: 49.8% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
\mathbf{if}\;\phi_1 \leq -6 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

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


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

    1. Initial program 73.7%

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

      \[\leadsto \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. *-lowering-*.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. cos-lowering-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. --lowering--.f64N/A

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

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

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

    if -6.00000000000000015e-5 < phi1

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6450.3

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

      \[\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. 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 \]
      2. 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 \]
      3. 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 \]
      4. 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 \]
      5. add-cube-cbrtN/A

        \[\leadsto \left(\color{blue}{\left(\left(\sqrt[3]{\mathsf{PI}\left(\right)} \cdot \sqrt[3]{\mathsf{PI}\left(\right)}\right) \cdot \sqrt[3]{\mathsf{PI}\left(\right)}\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 \]
      6. associate-*l*N/A

        \[\leadsto \left(\color{blue}{\left(\sqrt[3]{\mathsf{PI}\left(\right)} \cdot \sqrt[3]{\mathsf{PI}\left(\right)}\right) \cdot \left(\sqrt[3]{\mathsf{PI}\left(\right)} \cdot \frac{1}{2}\right)} + \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. accelerator-lowering-fma.f64N/A

        \[\leadsto \color{blue}{\mathsf{fma}\left(\sqrt[3]{\mathsf{PI}\left(\right)} \cdot \sqrt[3]{\mathsf{PI}\left(\right)}, \sqrt[3]{\mathsf{PI}\left(\right)} \cdot \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 \]
      8. pow2N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{{\left(\sqrt[3]{\mathsf{PI}\left(\right)}\right)}^{2}}, \sqrt[3]{\mathsf{PI}\left(\right)} \cdot \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 \]
      9. pow1/3N/A

        \[\leadsto \mathsf{fma}\left({\color{blue}{\left({\mathsf{PI}\left(\right)}^{\frac{1}{3}}\right)}}^{2}, \sqrt[3]{\mathsf{PI}\left(\right)} \cdot \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. pow-powN/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{{\mathsf{PI}\left(\right)}^{\left(\frac{1}{3} \cdot 2\right)}}, \sqrt[3]{\mathsf{PI}\left(\right)} \cdot \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. pow-lowering-pow.f64N/A

        \[\leadsto \mathsf{fma}\left(\color{blue}{{\mathsf{PI}\left(\right)}^{\left(\frac{1}{3} \cdot 2\right)}}, \sqrt[3]{\mathsf{PI}\left(\right)} \cdot \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 \]
      12. PI-lowering-PI.f64N/A

        \[\leadsto \mathsf{fma}\left({\color{blue}{\mathsf{PI}\left(\right)}}^{\left(\frac{1}{3} \cdot 2\right)}, \sqrt[3]{\mathsf{PI}\left(\right)} \cdot \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 \]
      13. metadata-evalN/A

        \[\leadsto \mathsf{fma}\left({\mathsf{PI}\left(\right)}^{\color{blue}{\frac{2}{3}}}, \sqrt[3]{\mathsf{PI}\left(\right)} \cdot \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 \]
      14. *-lowering-*.f64N/A

        \[\leadsto \mathsf{fma}\left({\mathsf{PI}\left(\right)}^{\frac{2}{3}}, \color{blue}{\sqrt[3]{\mathsf{PI}\left(\right)} \cdot \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 \]
      15. cbrt-lowering-cbrt.f64N/A

        \[\leadsto \mathsf{fma}\left({\mathsf{PI}\left(\right)}^{\frac{2}{3}}, \color{blue}{\sqrt[3]{\mathsf{PI}\left(\right)}} \cdot \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 \]
      16. PI-lowering-PI.f64N/A

        \[\leadsto \mathsf{fma}\left({\mathsf{PI}\left(\right)}^{\frac{2}{3}}, \sqrt[3]{\color{blue}{\mathsf{PI}\left(\right)}} \cdot \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 \]
      17. neg-lowering-neg.f64N/A

        \[\leadsto \mathsf{fma}\left({\mathsf{PI}\left(\right)}^{\frac{2}{3}}, \sqrt[3]{\mathsf{PI}\left(\right)} \cdot \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 \]
      18. asin-lowering-asin.f64N/A

        \[\leadsto \mathsf{fma}\left({\mathsf{PI}\left(\right)}^{\frac{2}{3}}, \sqrt[3]{\mathsf{PI}\left(\right)} \cdot \frac{1}{2}, \mathsf{neg}\left(\color{blue}{\sin^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)}\right)\right) \cdot R \]
    7. Applied egg-rr49.8%

      \[\leadsto \color{blue}{\mathsf{fma}\left({\pi}^{0.6666666666666666}, \sqrt[3]{\pi} \cdot 0.5, -\sin^{-1} \left(\cos \phi_2 \cdot \cos \left(\lambda_2 - \lambda_1\right)\right)\right)} \cdot R \]
    8. Applied egg-rr50.5%

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

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

Alternative 16: 50.2% 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^{-5}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_2 \cdot t\_0\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda2 lambda1))))
   (if (<= phi1 -1.15e-5)
     (* 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-5) {
		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-5)) 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-5) {
		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-5:
		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-5)
		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-5)
		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-5], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_2 - \lambda_1\right)\\
\mathbf{if}\;\phi_1 \leq -1.15 \cdot 10^{-5}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot t\_0\right)\\

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


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

    1. Initial program 73.7%

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

      \[\leadsto \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. *-lowering-*.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. cos-lowering-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. --lowering--.f64N/A

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

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

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

    if -1.15e-5 < phi1

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6450.3

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

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

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

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.005:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\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.005)
   (* R (acos (* (cos phi1) (cos lambda2))))
   (* R (acos (* (cos phi2) (cos (- lambda2 lambda1)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -0.005) {
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	} 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.005d0)) then
        tmp = r * acos((cos(phi1) * cos(lambda2)))
    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.005) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda2)));
	} 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.005:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda2)))
	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.005)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda2))));
	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.005)
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	else
		tmp = R * acos((cos(phi2) * cos((lambda2 - lambda1))));
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -0.005], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda2], $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.005:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\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 < -0.0050000000000000001

    1. Initial program 74.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_2 \cdot \left(\sin \phi_1 \cdot \left(1 + {\phi_2}^{2} \cdot \left(\frac{1}{120} \cdot {\phi_2}^{2} - \frac{1}{6}\right)\right)\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. cos-negN/A

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_2, \sin \phi_1 \cdot \mathsf{fma}\left(\color{blue}{\phi_2 \cdot \phi_2}, \frac{1}{120} \cdot {\phi_2}^{2} - \frac{1}{6}, 1\right), \cos \lambda_2 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right) \cdot R \]
      11. sub-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_2, \sin \phi_1 \cdot \mathsf{fma}\left(\phi_2 \cdot \phi_2, \color{blue}{\frac{1}{120} \cdot {\phi_2}^{2} + \left(\mathsf{neg}\left(\frac{1}{6}\right)\right)}, 1\right), \cos \lambda_2 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right) \cdot R \]
      12. metadata-evalN/A

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_2} \cdot \cos \phi_1\right) \cdot R \]
      3. cos-lowering-cos.f6436.8

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

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

    if -0.0050000000000000001 < phi1

    1. Initial program 74.5%

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6450.1

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified50.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 simplification46.4%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.005:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\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 18: 28.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;t\_0 \leq 0.9998:\\ \;\;\;\;R \cdot \cos^{-1} t\_0\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_2 - \lambda_1\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 (cos (- lambda1 lambda2))))
   (if (<= t_0 0.9998)
     (* R (acos t_0))
     (* R (fabs (remainder (- lambda2 lambda1) (* PI 2.0)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.9998) {
		tmp = R * acos(t_0);
	} else {
		tmp = R * fabs(remainder((lambda2 - lambda1), (((double) M_PI) * 2.0)));
	}
	return tmp;
}
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.9998) {
		tmp = R * Math.acos(t_0);
	} else {
		tmp = R * Math.abs(Math.IEEEremainder((lambda2 - lambda1), (Math.PI * 2.0)));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	tmp = 0
	if t_0 <= 0.9998:
		tmp = R * math.acos(t_0)
	else:
		tmp = R * math.fabs(math.remainder((lambda2 - lambda1), (math.pi * 2.0)))
	return tmp
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[t$95$0, 0.9998], N[(R * N[ArcCos[t$95$0], $MachinePrecision]), $MachinePrecision], N[(R * N[Abs[N[With[{TMP1 = N[(lambda2 - lambda1), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;t\_0 \leq 0.9998:\\
\;\;\;\;R \cdot \cos^{-1} t\_0\\

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


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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6443.7

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified43.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. cos-lowering-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. --lowering--.f6430.3

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

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

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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6434.3

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified34.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. cos-lowering-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. --lowering--.f645.2

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

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

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

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

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

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

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

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

        \[\leadsto \left|\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
      8. cos-diffN/A

        \[\leadsto \left|\left(\cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
      9. acos-cos-sN/A

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

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

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

        \[\leadsto \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      13. PI-lowering-PI.f6414.7

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

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

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

Alternative 19: 36.5% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.072:\\ \;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\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 -0.072)
   (* R (acos (* (cos phi1) (cos lambda2))))
   (* R (acos (* (cos phi2) (cos lambda1))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -0.072) {
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	} 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 <= (-0.072d0)) then
        tmp = r * acos((cos(phi1) * cos(lambda2)))
    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 <= -0.072) {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda2)));
	} else {
		tmp = R * Math.acos((Math.cos(phi2) * Math.cos(lambda1)));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi1 <= -0.072:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda2)))
	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 <= -0.072)
		tmp = Float64(R * acos(Float64(cos(phi1) * cos(lambda2))));
	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 <= -0.072)
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	else
		tmp = R * acos((cos(phi2) * cos(lambda1)));
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -0.072], N[(R * N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[Cos[lambda2], $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 -0.072:\\
\;\;\;\;R \cdot \cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\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 < -0.0719999999999999946

    1. Initial program 75.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_2 \cdot \left(\sin \phi_1 \cdot \left(1 + {\phi_2}^{2} \cdot \left(\frac{1}{120} \cdot {\phi_2}^{2} - \frac{1}{6}\right)\right)\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. cos-negN/A

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_2, \sin \phi_1 \cdot \mathsf{fma}\left(\color{blue}{\phi_2 \cdot \phi_2}, \frac{1}{120} \cdot {\phi_2}^{2} - \frac{1}{6}, 1\right), \cos \lambda_2 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right) \cdot R \]
      11. sub-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_2, \sin \phi_1 \cdot \mathsf{fma}\left(\phi_2 \cdot \phi_2, \color{blue}{\frac{1}{120} \cdot {\phi_2}^{2} + \left(\mathsf{neg}\left(\frac{1}{6}\right)\right)}, 1\right), \cos \lambda_2 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right) \cdot R \]
      12. metadata-evalN/A

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

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

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

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

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

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

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

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

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

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

    if -0.0719999999999999946 < phi1

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.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. 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. *-lowering-*.f64N/A

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

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

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

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

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

Alternative 20: 34.4% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -0.00075:\\ \;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\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 (<= lambda1 -0.00075)
   (* R (acos (cos (- lambda1 lambda2))))
   (* R (acos (* (cos phi1) (cos lambda2))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -0.00075) {
		tmp = R * acos(cos((lambda1 - lambda2)));
	} 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 (lambda1 <= (-0.00075d0)) then
        tmp = r * acos(cos((lambda1 - lambda2)))
    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 (lambda1 <= -0.00075) {
		tmp = R * Math.acos(Math.cos((lambda1 - lambda2)));
	} else {
		tmp = R * Math.acos((Math.cos(phi1) * Math.cos(lambda2)));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -0.00075:
		tmp = R * math.acos(math.cos((lambda1 - lambda2)))
	else:
		tmp = R * math.acos((math.cos(phi1) * math.cos(lambda2)))
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -0.00075)
		tmp = Float64(R * acos(cos(Float64(lambda1 - lambda2))));
	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 (lambda1 <= -0.00075)
		tmp = R * acos(cos((lambda1 - lambda2)));
	else
		tmp = R * acos((cos(phi1) * cos(lambda2)));
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -0.00075], N[(R * N[ArcCos[N[Cos[N[(lambda1 - lambda2), $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_1 \leq -0.00075:\\
\;\;\;\;R \cdot \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right)\\

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


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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6442.3

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified42.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. cos-lowering-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. --lowering--.f6426.9

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

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

    if -7.5000000000000002e-4 < lambda1

    1. Initial program 78.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\phi_2 \cdot \left(\sin \phi_1 \cdot \left(1 + {\phi_2}^{2} \cdot \left(\frac{1}{120} \cdot {\phi_2}^{2} - \frac{1}{6}\right)\right)\right) + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right)\right)} \cdot R \]
    7. Step-by-step derivation
      1. cos-negN/A

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_2, \sin \phi_1 \cdot \mathsf{fma}\left(\color{blue}{\phi_2 \cdot \phi_2}, \frac{1}{120} \cdot {\phi_2}^{2} - \frac{1}{6}, 1\right), \cos \lambda_2 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right) \cdot R \]
      11. sub-negN/A

        \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\phi_2, \sin \phi_1 \cdot \mathsf{fma}\left(\phi_2 \cdot \phi_2, \color{blue}{\frac{1}{120} \cdot {\phi_2}^{2} + \left(\mathsf{neg}\left(\frac{1}{6}\right)\right)}, 1\right), \cos \lambda_2 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right)\right)\right) \cdot R \]
      12. metadata-evalN/A

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

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

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

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

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

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

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

        \[\leadsto \cos^{-1} \left(\color{blue}{\cos \lambda_2} \cdot \cos \phi_1\right) \cdot R \]
      3. cos-lowering-cos.f6437.0

        \[\leadsto \cos^{-1} \left(\cos \lambda_2 \cdot \color{blue}{\cos \phi_1}\right) \cdot R \]
    11. Simplified37.0%

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

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

Alternative 21: 20.8% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 1.25 \cdot 10^{-43}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_2\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda2 1.25e-43)
   (* R (acos (cos lambda1)))
   (* R (acos (cos lambda2)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 1.25e-43) {
		tmp = R * acos(cos(lambda1));
	} 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 (lambda2 <= 1.25d-43) then
        tmp = r * acos(cos(lambda1))
    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 (lambda2 <= 1.25e-43) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else {
		tmp = R * Math.acos(Math.cos(lambda2));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= 1.25e-43:
		tmp = R * math.acos(math.cos(lambda1))
	else:
		tmp = R * math.acos(math.cos(lambda2))
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 1.25e-43)
		tmp = Float64(R * acos(cos(lambda1)));
	else
		tmp = Float64(R * acos(cos(lambda2)));
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= 1.25e-43)
		tmp = R * acos(cos(lambda1));
	else
		tmp = R * acos(cos(lambda2));
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, 1.25e-43], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[Cos[lambda2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 1.25 \cdot 10^{-43}:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

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


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

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6444.4

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

      \[\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. cos-lowering-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. --lowering--.f6425.0

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

      \[\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. cos-lowering-cos.f6416.5

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    11. Simplified16.5%

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

    if 1.25000000000000005e-43 < lambda2

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.f6434.0

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified34.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. cos-lowering-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. --lowering--.f6422.4

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

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
    11. Simplified21.7%

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

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

Alternative 22: 21.8% accurate, 3.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1.05:\\ \;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -1.05)
   (* R (acos (cos lambda1)))
   (* R (fabs (remainder (- lambda2 lambda1) (* PI 2.0))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1.05) {
		tmp = R * acos(cos(lambda1));
	} else {
		tmp = R * fabs(remainder((lambda2 - lambda1), (((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 <= -1.05) {
		tmp = R * Math.acos(Math.cos(lambda1));
	} else {
		tmp = R * Math.abs(Math.IEEEremainder((lambda2 - lambda1), (Math.PI * 2.0)));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -1.05:
		tmp = R * math.acos(math.cos(lambda1))
	else:
		tmp = R * math.fabs(math.remainder((lambda2 - lambda1), (math.pi * 2.0)))
	return tmp
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -1.05], N[(R * N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[Abs[N[With[{TMP1 = N[(lambda2 - lambda1), $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 -1.05:\\
\;\;\;\;R \cdot \cos^{-1} \cos \lambda_1\\

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


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

    1. Initial program 63.0%

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

        \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
      2. cos-lowering-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. cos-lowering-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. --lowering--.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. cos-lowering-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. --lowering--.f6427.2

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

      \[\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. cos-lowering-cos.f6427.3

        \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_1} \cdot R \]
    11. Simplified27.3%

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

    if -1.05000000000000004 < lambda1

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

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

        \[\leadsto \cos^{-1} \left(\cos \phi_2 \cdot \cos \color{blue}{\left(\lambda_2 - \lambda_1\right)}\right) \cdot R \]
    5. Simplified40.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. cos-lowering-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. --lowering--.f6423.3

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

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

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

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

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

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

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

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

        \[\leadsto \left|\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
      8. cos-diffN/A

        \[\leadsto \left|\left(\cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
      9. acos-cos-sN/A

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

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

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

        \[\leadsto \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
      13. PI-lowering-PI.f6418.2

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

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

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

Alternative 23: 19.5% accurate, 5.4× speedup?

\[\begin{array}{l} \\ R \cdot \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right| \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R (fabs (remainder (- lambda2 lambda1) (* PI 2.0)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * fabs(remainder((lambda2 - lambda1), (((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((lambda2 - lambda1), (Math.PI * 2.0)));
}
def code(R, lambda1, lambda2, phi1, phi2):
	return R * math.fabs(math.remainder((lambda2 - lambda1), (math.pi * 2.0)))
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * N[Abs[N[With[{TMP1 = N[(lambda2 - lambda1), $MachinePrecision], TMP2 = N[(Pi * 2.0), $MachinePrecision]}, TMP1 - Round[TMP1 / TMP2] * TMP2], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
R \cdot \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|
\end{array}
Derivation
  1. Initial program 74.5%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    2. cos-lowering-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. cos-lowering-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. --lowering--.f6441.4

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

    \[\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. cos-lowering-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. --lowering--.f6424.2

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

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

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

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

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

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

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

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

      \[\leadsto \left|\left(\cos^{-1} \left(\cos \lambda_2 \cdot \cos \lambda_1 + \color{blue}{\sin \lambda_2 \cdot \sin \lambda_1}\right) \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
    8. cos-diffN/A

      \[\leadsto \left|\left(\cos^{-1} \color{blue}{\cos \left(\lambda_2 - \lambda_1\right)} \mathsf{rem} \left(2 \cdot \mathsf{PI}\left(\right)\right)\right)\right| \cdot R \]
    9. acos-cos-sN/A

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

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

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

      \[\leadsto \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \color{blue}{\left(\mathsf{PI}\left(\right) \cdot 2\right)}\right)\right| \cdot R \]
    13. PI-lowering-PI.f6418.5

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

    \[\leadsto \color{blue}{\left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right|} \cdot R \]
  11. Final simplification18.5%

    \[\leadsto R \cdot \left|\left(\left(\lambda_2 - \lambda_1\right) \mathsf{rem} \left(\pi \cdot 2\right)\right)\right| \]
  12. Add Preprocessing

Alternative 24: 5.4% accurate, 69.7× speedup?

\[\begin{array}{l} \\ R \cdot \left(\lambda_2 - \lambda_1\right) \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R (- lambda2 lambda1)))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * (lambda2 - lambda1);
}
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = r * (lambda2 - lambda1)
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * (lambda2 - lambda1);
}
def code(R, lambda1, lambda2, phi1, phi2):
	return R * (lambda2 - lambda1)
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(R * Float64(lambda2 - lambda1))
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = R * (lambda2 - lambda1);
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * N[(lambda2 - lambda1), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
R \cdot \left(\lambda_2 - \lambda_1\right)
\end{array}
Derivation
  1. Initial program 74.5%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    2. cos-lowering-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. cos-lowering-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. --lowering--.f6441.4

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

    \[\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. cos-lowering-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. --lowering--.f6424.2

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
    7. --lowering--.f644.6

      \[\leadsto \color{blue}{\left(\lambda_2 - \lambda_1\right)} \cdot R \]
  10. Applied egg-rr4.6%

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

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

Alternative 25: 5.4% 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 74.5%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    2. cos-lowering-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. cos-lowering-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. --lowering--.f6441.4

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

    \[\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. cos-lowering-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. --lowering--.f6424.2

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

    \[\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. *-lowering-*.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 2024198 
(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))