Spherical law of cosines

Percentage Accurate: 73.7% → 94.1%
Time: 28.5s
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.7% accurate, 1.0× speedup?

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

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

Alternative 1: 94.1% accurate, 0.5× speedup?

\[\begin{array}{l} \\ \log \left(e^{\cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1 \cdot \cos \phi_2, \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)}\right) \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (log
   (exp
    (acos
     (fma
      (* (cos phi1) (cos phi2))
      (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2)))
      (* (sin phi1) (sin phi2))))))
  R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return log(exp(acos(fma((cos(phi1) * cos(phi2)), fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2))), (sin(phi1) * sin(phi2)))))) * R;
}
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(log(exp(acos(fma(Float64(cos(phi1) * cos(phi2)), fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2))), Float64(sin(phi1) * sin(phi2)))))) * R)
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[Log[N[Exp[N[ArcCos[N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\log \left(e^{\cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1 \cdot \cos \phi_2, \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)}\right) \cdot R
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 2: 94.1% accurate, 0.5× speedup?

\[\begin{array}{l} \\ 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, e^{\mathsf{log1p}\left(\sin \lambda_1 \cdot \sin \lambda_2\right)} + -1\right)\right) \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R
  (acos
   (+
    (* (sin phi1) (sin phi2))
    (*
     (* (cos phi1) (cos phi2))
     (fma
      (cos lambda2)
      (cos lambda1)
      (+ (exp (log1p (* (sin lambda1) (sin lambda2)))) -1.0)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * fma(cos(lambda2), cos(lambda1), (exp(log1p((sin(lambda1) * sin(lambda2)))) + -1.0)))));
}
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * fma(cos(lambda2), cos(lambda1), Float64(exp(log1p(Float64(sin(lambda1) * sin(lambda2)))) + -1.0))))))
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Exp[N[Log[1 + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] + -1.0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
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, e^{\mathsf{log1p}\left(\sin \lambda_1 \cdot \sin \lambda_2\right)} + -1\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 3: 94.1% accurate, 0.6× speedup?

\[\begin{array}{l} \\ R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R
  (acos
   (+
    (* (sin phi1) (sin phi2))
    (*
     (* (cos phi1) (cos phi2))
     (fma (cos lambda2) (cos lambda1) (* (sin lambda1) (sin lambda2))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * fma(cos(lambda2), cos(lambda1), (sin(lambda1) * sin(lambda2))))));
}
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * fma(cos(lambda2), cos(lambda1), Float64(sin(lambda1) * sin(lambda2)))))))
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_2, \cos \lambda_1, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 4: 94.1% accurate, 0.7× speedup?

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

\\
R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 5: 83.6% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \cos \phi_2\\
t_1 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_2 := \sin \phi_1 \cdot \sin \phi_2\\
\mathbf{if}\;\phi_1 \leq -9 \cdot 10^{-10}:\\
\;\;\;\;R \cdot \cos^{-1} \left(t_2 + t_0 \cdot t_1\right)\\

\mathbf{elif}\;\phi_1 \leq 10^{-36}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \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 \log \left(e^{\cos^{-1} \left(\mathsf{fma}\left(t_1, t_0, t_2\right)\right)}\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 6: 83.6% accurate, 0.7× speedup?

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

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

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 7: 83.9% accurate, 0.7× speedup?

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

\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \cos \phi_2\\
t_1 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_2 := \sin \phi_1 \cdot \sin \phi_2\\
\mathbf{if}\;\phi_1 \leq -9 \cdot 10^{-10}:\\
\;\;\;\;R \cdot \cos^{-1} \left(t_2 + t_0 \cdot t_1\right)\\

\mathbf{elif}\;\phi_1 \leq 4.8 \cdot 10^{-17}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \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 \left(\pi \cdot 0.5 - \sin^{-1} \left(\mathsf{fma}\left(t_1, t_0, t_2\right)\right)\right)\\


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 8: 83.6% accurate, 0.8× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -9 \cdot 10^{-10} \lor \neg \left(\phi_1 \leq 5 \cdot 10^{-39}\right):\\
\;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 9: 83.6% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -9 \cdot 10^{-10} \lor \neg \left(\phi_1 \leq 5.3 \cdot 10^{-39}\right):\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \cos \phi_2 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (or (<= phi1 -9e-10) (not (<= phi1 5.3e-39)))
   (*
    R
    (acos
     (+
      (* (sin phi1) (sin phi2))
      (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2))))))
   (*
    R
    (acos
     (+
      (* phi1 (sin phi2))
      (*
       (cos phi2)
       (+
        (* (sin lambda1) (sin lambda2))
        (* (cos lambda2) (cos lambda1)))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if ((phi1 <= -9e-10) || !(phi1 <= 5.3e-39)) {
		tmp = R * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	} else {
		tmp = R * acos(((phi1 * sin(phi2)) + (cos(phi2) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * 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 <= (-9d-10)) .or. (.not. (phi1 <= 5.3d-39))) then
        tmp = r * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))))
    else
        tmp = r * acos(((phi1 * sin(phi2)) + (cos(phi2) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * 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 <= -9e-10) || !(phi1 <= 5.3e-39)) {
		tmp = R * Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2)))));
	} else {
		tmp = R * Math.acos(((phi1 * Math.sin(phi2)) + (Math.cos(phi2) * ((Math.sin(lambda1) * Math.sin(lambda2)) + (Math.cos(lambda2) * Math.cos(lambda1))))));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if (phi1 <= -9e-10) or not (phi1 <= 5.3e-39):
		tmp = R * math.acos(((math.sin(phi1) * math.sin(phi2)) + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2)))))
	else:
		tmp = R * math.acos(((phi1 * math.sin(phi2)) + (math.cos(phi2) * ((math.sin(lambda1) * math.sin(lambda2)) + (math.cos(lambda2) * math.cos(lambda1))))))
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if ((phi1 <= -9e-10) || !(phi1 <= 5.3e-39))
		tmp = Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))));
	else
		tmp = Float64(R * acos(Float64(Float64(phi1 * sin(phi2)) + Float64(cos(phi2) * Float64(Float64(sin(lambda1) * sin(lambda2)) + Float64(cos(lambda2) * cos(lambda1)))))));
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if ((phi1 <= -9e-10) || ~((phi1 <= 5.3e-39)))
		tmp = R * acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	else
		tmp = R * acos(((phi1 * sin(phi2)) + (cos(phi2) * ((sin(lambda1) * sin(lambda2)) + (cos(lambda2) * cos(lambda1))))));
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[Or[LessEqual[phi1, -9e-10], N[Not[LessEqual[phi1, 5.3e-39]], $MachinePrecision]], N[(R * N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[(phi1 * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi2], $MachinePrecision] * N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -9 \cdot 10^{-10} \lor \neg \left(\phi_1 \leq 5.3 \cdot 10^{-39}\right):\\
\;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 10: 68.5% accurate, 0.9× speedup?

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

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

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 11: 68.5% accurate, 1.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq -6.8 \cdot 10^{+22}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\phi_1 \cdot \phi_2 + \cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2 + \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 12: 62.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_1 \cdot \cos \phi_2\\ \mathbf{if}\;\phi_1 \leq -2.8 \cdot 10^{+36} \lor \neg \left(\phi_1 \leq 1.75 \cdot 10^{-35}\right):\\ \;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t_0 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + t_0 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi1) (cos phi2))))
   (if (or (<= phi1 -2.8e+36) (not (<= phi1 1.75e-35)))
     (* R (acos (+ (* (sin phi1) (sin phi2)) (* t_0 (cos lambda1)))))
     (* R (acos (+ (* phi1 (sin phi2)) (* t_0 (cos (- lambda1 lambda2)))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi1) * cos(phi2);
	double tmp;
	if ((phi1 <= -2.8e+36) || !(phi1 <= 1.75e-35)) {
		tmp = R * acos(((sin(phi1) * sin(phi2)) + (t_0 * cos(lambda1))));
	} else {
		tmp = R * acos(((phi1 * sin(phi2)) + (t_0 * cos((lambda1 - lambda2)))));
	}
	return tmp;
}
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos(phi1) * cos(phi2)
    if ((phi1 <= (-2.8d+36)) .or. (.not. (phi1 <= 1.75d-35))) then
        tmp = r * acos(((sin(phi1) * sin(phi2)) + (t_0 * cos(lambda1))))
    else
        tmp = r * acos(((phi1 * sin(phi2)) + (t_0 * cos((lambda1 - lambda2)))))
    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(phi1) * Math.cos(phi2);
	double tmp;
	if ((phi1 <= -2.8e+36) || !(phi1 <= 1.75e-35)) {
		tmp = R * Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + (t_0 * Math.cos(lambda1))));
	} else {
		tmp = R * Math.acos(((phi1 * Math.sin(phi2)) + (t_0 * Math.cos((lambda1 - lambda2)))));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos(phi1) * math.cos(phi2)
	tmp = 0
	if (phi1 <= -2.8e+36) or not (phi1 <= 1.75e-35):
		tmp = R * math.acos(((math.sin(phi1) * math.sin(phi2)) + (t_0 * math.cos(lambda1))))
	else:
		tmp = R * math.acos(((phi1 * math.sin(phi2)) + (t_0 * math.cos((lambda1 - lambda2)))))
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if ((phi1 <= -2.8e+36) || !(phi1 <= 1.75e-35))
		tmp = Float64(R * acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(t_0 * cos(lambda1)))));
	else
		tmp = Float64(R * acos(Float64(Float64(phi1 * sin(phi2)) + Float64(t_0 * cos(Float64(lambda1 - lambda2))))));
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(phi1) * cos(phi2);
	tmp = 0.0;
	if ((phi1 <= -2.8e+36) || ~((phi1 <= 1.75e-35)))
		tmp = R * acos(((sin(phi1) * sin(phi2)) + (t_0 * cos(lambda1))));
	else
		tmp = R * acos(((phi1 * sin(phi2)) + (t_0 * cos((lambda1 - lambda2)))));
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, If[Or[LessEqual[phi1, -2.8e+36], N[Not[LessEqual[phi1, 1.75e-35]], $MachinePrecision]], N[(R * N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(t$95$0 * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[(phi1 * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(t$95$0 * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \cos \phi_2\\
\mathbf{if}\;\phi_1 \leq -2.8 \cdot 10^{+36} \lor \neg \left(\phi_1 \leq 1.75 \cdot 10^{-35}\right):\\
\;\;\;\;R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t_0 \cdot \cos \lambda_1\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 13: 64.0% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_1 \cdot \cos \phi_2\\ t_1 := \sin \phi_1 \cdot \sin \phi_2\\ \mathbf{if}\;\lambda_1 \leq -7.6 \cdot 10^{-7}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t_1 + t_0 \cdot \cos \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(t_1 + t_0 \cdot \cos \lambda_2\right)\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi1) (cos phi2))) (t_1 (* (sin phi1) (sin phi2))))
   (if (<= lambda1 -7.6e-7)
     (* R (acos (+ t_1 (* t_0 (cos lambda1)))))
     (* R (acos (+ t_1 (* t_0 (cos lambda2))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi1) * cos(phi2);
	double t_1 = sin(phi1) * sin(phi2);
	double tmp;
	if (lambda1 <= -7.6e-7) {
		tmp = R * acos((t_1 + (t_0 * cos(lambda1))));
	} else {
		tmp = R * acos((t_1 + (t_0 * 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) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = cos(phi1) * cos(phi2)
    t_1 = sin(phi1) * sin(phi2)
    if (lambda1 <= (-7.6d-7)) then
        tmp = r * acos((t_1 + (t_0 * cos(lambda1))))
    else
        tmp = r * acos((t_1 + (t_0 * cos(lambda2))))
    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(phi1) * Math.cos(phi2);
	double t_1 = Math.sin(phi1) * Math.sin(phi2);
	double tmp;
	if (lambda1 <= -7.6e-7) {
		tmp = R * Math.acos((t_1 + (t_0 * Math.cos(lambda1))));
	} else {
		tmp = R * Math.acos((t_1 + (t_0 * Math.cos(lambda2))));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos(phi1) * math.cos(phi2)
	t_1 = math.sin(phi1) * math.sin(phi2)
	tmp = 0
	if lambda1 <= -7.6e-7:
		tmp = R * math.acos((t_1 + (t_0 * math.cos(lambda1))))
	else:
		tmp = R * math.acos((t_1 + (t_0 * math.cos(lambda2))))
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi1) * cos(phi2))
	t_1 = Float64(sin(phi1) * sin(phi2))
	tmp = 0.0
	if (lambda1 <= -7.6e-7)
		tmp = Float64(R * acos(Float64(t_1 + Float64(t_0 * cos(lambda1)))));
	else
		tmp = Float64(R * acos(Float64(t_1 + Float64(t_0 * cos(lambda2)))));
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(phi1) * cos(phi2);
	t_1 = sin(phi1) * sin(phi2);
	tmp = 0.0;
	if (lambda1 <= -7.6e-7)
		tmp = R * acos((t_1 + (t_0 * cos(lambda1))));
	else
		tmp = R * acos((t_1 + (t_0 * cos(lambda2))));
	end
	tmp_2 = 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[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[lambda1, -7.6e-7], N[(R * N[ArcCos[N[(t$95$1 + N[(t$95$0 * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(t$95$1 + N[(t$95$0 * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \cos \phi_2\\
t_1 := \sin \phi_1 \cdot \sin \phi_2\\
\mathbf{if}\;\lambda_1 \leq -7.6 \cdot 10^{-7}:\\
\;\;\;\;R \cdot \cos^{-1} \left(t_1 + t_0 \cdot \cos \lambda_1\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 14: 73.7% accurate, 1.0× speedup?

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

\\
R \cdot \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 15: 47.0% accurate, 1.0× speedup?

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

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 16: 47.8% accurate, 1.2× speedup?

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

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 17: 39.6% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_1 \leq -18:\\ \;\;\;\;R \cdot \cos^{-1} \left(\phi_1 \cdot \phi_2 + \cos \phi_1 \cdot \log \left(e^{t_0}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \cos^{-1} \left(\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 (- lambda1 lambda2))))
   (if (<= phi1 -18.0)
     (* R (acos (+ (* phi1 phi2) (* (cos phi1) (log (exp t_0))))))
     (* R (acos (+ (* phi1 (sin phi2)) (* (cos phi2) t_0)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (phi1 <= -18.0) {
		tmp = R * acos(((phi1 * phi2) + (cos(phi1) * log(exp(t_0)))));
	} else {
		tmp = R * acos(((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((lambda1 - lambda2))
    if (phi1 <= (-18.0d0)) then
        tmp = r * acos(((phi1 * phi2) + (cos(phi1) * log(exp(t_0)))))
    else
        tmp = r * acos(((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((lambda1 - lambda2));
	double tmp;
	if (phi1 <= -18.0) {
		tmp = R * Math.acos(((phi1 * phi2) + (Math.cos(phi1) * Math.log(Math.exp(t_0)))));
	} else {
		tmp = R * Math.acos(((phi1 * Math.sin(phi2)) + (Math.cos(phi2) * t_0)));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	tmp = 0
	if phi1 <= -18.0:
		tmp = R * math.acos(((phi1 * phi2) + (math.cos(phi1) * math.log(math.exp(t_0)))))
	else:
		tmp = R * math.acos(((phi1 * math.sin(phi2)) + (math.cos(phi2) * t_0)))
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi1 <= -18.0)
		tmp = Float64(R * acos(Float64(Float64(phi1 * phi2) + Float64(cos(phi1) * log(exp(t_0))))));
	else
		tmp = Float64(R * acos(Float64(Float64(phi1 * sin(phi2)) + Float64(cos(phi2) * t_0))));
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda1 - lambda2));
	tmp = 0.0;
	if (phi1 <= -18.0)
		tmp = R * acos(((phi1 * phi2) + (cos(phi1) * log(exp(t_0)))));
	else
		tmp = R * acos(((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[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi1, -18.0], N[(R * N[ArcCos[N[(N[(phi1 * phi2), $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[Log[N[Exp[t$95$0], $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[ArcCos[N[(N[(phi1 * 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_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_1 \leq -18:\\
\;\;\;\;R \cdot \cos^{-1} \left(\phi_1 \cdot \phi_2 + \cos \phi_1 \cdot \log \left(e^{t_0}\right)\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 18: 43.2% accurate, 1.2× speedup?

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

\\
R \cdot \cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 19: 33.7% accurate, 1.5× speedup?

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

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 20: 22.9% accurate, 1.5× speedup?

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

\\
\begin{array}{l}
t_0 := \phi_1 \cdot \sin \phi_2\\
\mathbf{if}\;\lambda_2 \leq 1.25 \cdot 10^{-7}:\\
\;\;\;\;R \cdot \cos^{-1} \left(t_0 + \cos \phi_1 \cdot \cos \lambda_1\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 21: 27.5% accurate, 1.5× speedup?

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

\\
R \cdot \cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 22: 20.8% accurate, 2.0× speedup?

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

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

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 23: 21.3% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -1.1 \cdot 10^{-6}:\\
\;\;\;\;R \cdot \cos^{-1} \left(\phi_1 \cdot \phi_2 + \cos \phi_1 \cdot \cos \lambda_1\right)\\

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


\end{array}
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 24: 25.3% accurate, 2.0× speedup?

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

\\
R \cdot \cos^{-1} \left(\phi_1 \cdot \phi_2 + \cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Alternative 25: 18.5% accurate, 2.9× speedup?

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

\\
R \cdot \cos^{-1} \left(\phi_1 \cdot \phi_2 + \cos \left(\lambda_2 - \lambda_1\right)\right)
\end{array}
Derivation
    &prev;&pcontext;&pcontext2;&ctx;
  1. Add Preprocessing

Reproduce

?
herbie shell --seed 2024003 
(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))