Equirectangular approximation to distance on a great circle

Percentage Accurate: 60.4% → 91.3%
Time: 9.7s
Alternatives: 9
Speedup: 2.4×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\ R \cdot \sqrt{t\_0 \cdot t\_0 + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0)))))
   (* R (sqrt (+ (* t_0 t_0) (* (- phi1 phi2) (- phi1 phi2)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	return R * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
}
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
    t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0d0))
    code = r * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))))
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (lambda1 - lambda2) * Math.cos(((phi1 + phi2) / 2.0));
	return R * Math.sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = (lambda1 - lambda2) * math.cos(((phi1 + phi2) / 2.0))
	return R * math.sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))))
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(Float64(lambda1 - lambda2) * cos(Float64(Float64(phi1 + phi2) / 2.0)))
	return Float64(R * sqrt(Float64(Float64(t_0 * t_0) + Float64(Float64(phi1 - phi2) * Float64(phi1 - phi2)))))
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	tmp = R * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(N[(phi1 + phi2), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(R * N[Sqrt[N[(N[(t$95$0 * t$95$0), $MachinePrecision] + N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\
R \cdot \sqrt{t\_0 \cdot t\_0 + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)}
\end{array}
\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 9 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: 60.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\ R \cdot \sqrt{t\_0 \cdot t\_0 + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0)))))
   (* R (sqrt (+ (* t_0 t_0) (* (- phi1 phi2) (- phi1 phi2)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	return R * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
}
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
    t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0d0))
    code = r * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))))
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (lambda1 - lambda2) * Math.cos(((phi1 + phi2) / 2.0));
	return R * Math.sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = (lambda1 - lambda2) * math.cos(((phi1 + phi2) / 2.0))
	return R * math.sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))))
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(Float64(lambda1 - lambda2) * cos(Float64(Float64(phi1 + phi2) / 2.0)))
	return Float64(R * sqrt(Float64(Float64(t_0 * t_0) + Float64(Float64(phi1 - phi2) * Float64(phi1 - phi2)))))
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	tmp = R * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(N[(phi1 + phi2), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(R * N[Sqrt[N[(N[(t$95$0 * t$95$0), $MachinePrecision] + N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\
R \cdot \sqrt{t\_0 \cdot t\_0 + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)}
\end{array}
\end{array}

Alternative 1: 91.3% accurate, 1.2× speedup?

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

\mathbf{else}:\\
\;\;\;\;\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \cdot R\\


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

    1. Initial program 57.2%

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

      \[\leadsto R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_1}^{2}}} \]
    4. Step-by-step derivation
      1. unpow2N/A

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

        \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_1\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_1}^{2}} \]
      3. unswap-sqrN/A

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

        \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) + \color{blue}{\phi_1 \cdot \phi_1}} \]
      5. lower-hypot.f64N/A

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right)} \]
      6. *-commutativeN/A

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

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

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

        \[\leadsto R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}, \phi_1\right) \]
      10. lower-*.f6480.3

        \[\leadsto R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \color{blue}{\left(0.5 \cdot \phi_1\right)}, \phi_1\right) \]
    5. Applied rewrites80.3%

      \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right), \phi_1\right)} \]

    if -5.2e9 < phi1

    1. Initial program 54.4%

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

      \[\leadsto R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}}} \]
    4. Step-by-step derivation
      1. unpow2N/A

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

        \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_2}^{2}} \]
      3. unswap-sqrN/A

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

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

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right)} \]
      6. lower-*.f64N/A

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

        \[\leadsto R \cdot \mathsf{hypot}\left(\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \]
      8. lower-*.f64N/A

        \[\leadsto R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \]
      9. lower--.f6479.3

        \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}, \phi_2\right) \]
    5. Applied rewrites79.3%

      \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification79.5%

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

Alternative 2: 87.4% accurate, 1.2× speedup?

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

\mathbf{else}:\\
\;\;\;\;\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \cdot R\\


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

    1. Initial program 57.2%

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

      \[\leadsto R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_1}^{2}}} \]
    4. Step-by-step derivation
      1. unpow2N/A

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

        \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_1\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_1}^{2}} \]
      3. unswap-sqrN/A

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

        \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) + \color{blue}{\phi_1 \cdot \phi_1}} \]
      5. lower-hypot.f64N/A

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right)} \]
      6. *-commutativeN/A

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

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

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

        \[\leadsto R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}, \phi_1\right) \]
      10. lower-*.f6480.3

        \[\leadsto R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \color{blue}{\left(0.5 \cdot \phi_1\right)}, \phi_1\right) \]
    5. Applied rewrites80.3%

      \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right), \phi_1\right)} \]
    6. Taylor expanded in lambda1 around 0

      \[\leadsto R \cdot \sqrt{{\lambda_2}^{2} \cdot {\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} + {\phi_1}^{2}} \]
    7. Step-by-step derivation
      1. Applied rewrites70.8%

        \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \lambda_2, \color{blue}{\phi_1}\right) \]

      if -3.5e10 < phi1

      1. Initial program 54.4%

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

        \[\leadsto R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}}} \]
      4. Step-by-step derivation
        1. unpow2N/A

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

          \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_2}^{2}} \]
        3. unswap-sqrN/A

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

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

          \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right)} \]
        6. lower-*.f64N/A

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

          \[\leadsto R \cdot \mathsf{hypot}\left(\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \]
        8. lower-*.f64N/A

          \[\leadsto R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \]
        9. lower--.f6479.3

          \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}, \phi_2\right) \]
      5. Applied rewrites79.3%

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right)} \]
    8. Recombined 2 regimes into one program.
    9. Final simplification77.5%

      \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -35000000000:\\ \;\;\;\;\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \lambda_2, \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \cdot R\\ \end{array} \]
    10. Add Preprocessing

    Alternative 3: 81.9% accurate, 1.3× speedup?

    \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.21:\\ \;\;\;\;\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \lambda_2, \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \cdot R\\ \end{array} \end{array} \]
    NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
    (FPCore (R lambda1 lambda2 phi1 phi2)
     :precision binary64
     (if (<= phi1 -0.21)
       (* (hypot (* (cos (* 0.5 phi1)) lambda2) phi1) R)
       (* (hypot (- lambda1 lambda2) phi2) R)))
    assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
    double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
    	double tmp;
    	if (phi1 <= -0.21) {
    		tmp = hypot((cos((0.5 * phi1)) * lambda2), phi1) * R;
    	} else {
    		tmp = hypot((lambda1 - lambda2), phi2) * R;
    	}
    	return tmp;
    }
    
    assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
    public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
    	double tmp;
    	if (phi1 <= -0.21) {
    		tmp = Math.hypot((Math.cos((0.5 * phi1)) * lambda2), phi1) * R;
    	} else {
    		tmp = Math.hypot((lambda1 - lambda2), phi2) * R;
    	}
    	return tmp;
    }
    
    [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
    def code(R, lambda1, lambda2, phi1, phi2):
    	tmp = 0
    	if phi1 <= -0.21:
    		tmp = math.hypot((math.cos((0.5 * phi1)) * lambda2), phi1) * R
    	else:
    		tmp = math.hypot((lambda1 - lambda2), phi2) * R
    	return tmp
    
    R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
    function code(R, lambda1, lambda2, phi1, phi2)
    	tmp = 0.0
    	if (phi1 <= -0.21)
    		tmp = Float64(hypot(Float64(cos(Float64(0.5 * phi1)) * lambda2), phi1) * R);
    	else
    		tmp = Float64(hypot(Float64(lambda1 - lambda2), phi2) * R);
    	end
    	return tmp
    end
    
    R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
    function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
    	tmp = 0.0;
    	if (phi1 <= -0.21)
    		tmp = hypot((cos((0.5 * phi1)) * lambda2), phi1) * R;
    	else
    		tmp = hypot((lambda1 - lambda2), phi2) * R;
    	end
    	tmp_2 = tmp;
    end
    
    NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
    code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -0.21], N[(N[Sqrt[N[(N[Cos[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision] * lambda2), $MachinePrecision] ^ 2 + phi1 ^ 2], $MachinePrecision] * R), $MachinePrecision], N[(N[Sqrt[N[(lambda1 - lambda2), $MachinePrecision] ^ 2 + phi2 ^ 2], $MachinePrecision] * R), $MachinePrecision]]
    
    \begin{array}{l}
    [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
    \\
    \begin{array}{l}
    \mathbf{if}\;\phi_1 \leq -0.21:\\
    \;\;\;\;\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \lambda_2, \phi_1\right) \cdot R\\
    
    \mathbf{else}:\\
    \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \cdot R\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if phi1 < -0.209999999999999992

      1. Initial program 57.0%

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

        \[\leadsto R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_1}^{2}}} \]
      4. Step-by-step derivation
        1. unpow2N/A

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

          \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_1\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_1}^{2}} \]
        3. unswap-sqrN/A

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

          \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) + \color{blue}{\phi_1 \cdot \phi_1}} \]
        5. lower-hypot.f64N/A

          \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right)} \]
        6. *-commutativeN/A

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

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

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

          \[\leadsto R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}, \phi_1\right) \]
        10. lower-*.f6479.7

          \[\leadsto R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \color{blue}{\left(0.5 \cdot \phi_1\right)}, \phi_1\right) \]
      5. Applied rewrites79.7%

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right), \phi_1\right)} \]
      6. Taylor expanded in lambda1 around 0

        \[\leadsto R \cdot \sqrt{{\lambda_2}^{2} \cdot {\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} + {\phi_1}^{2}} \]
      7. Step-by-step derivation
        1. Applied rewrites70.6%

          \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \lambda_2, \color{blue}{\phi_1}\right) \]

        if -0.209999999999999992 < phi1

        1. Initial program 54.4%

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

          \[\leadsto R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}}} \]
        4. Step-by-step derivation
          1. unpow2N/A

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

            \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_2}^{2}} \]
          3. unswap-sqrN/A

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

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

            \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right)} \]
          6. lower-*.f64N/A

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

            \[\leadsto R \cdot \mathsf{hypot}\left(\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \]
          8. lower-*.f64N/A

            \[\leadsto R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \]
          9. lower--.f6479.4

            \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}, \phi_2\right) \]
        5. Applied rewrites79.4%

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

          \[\leadsto R \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \]
        7. Step-by-step derivation
          1. Applied rewrites70.1%

            \[\leadsto R \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \]
        8. Recombined 2 regimes into one program.
        9. Final simplification70.2%

          \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.21:\\ \;\;\;\;\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \lambda_2, \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \cdot R\\ \end{array} \]
        10. Add Preprocessing

        Alternative 4: 80.8% accurate, 2.4× speedup?

        \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -220000000000:\\ \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \cdot R\\ \end{array} \end{array} \]
        NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
        (FPCore (R lambda1 lambda2 phi1 phi2)
         :precision binary64
         (if (<= phi1 -220000000000.0)
           (* (hypot (- lambda1 lambda2) phi1) R)
           (* (hypot (- lambda1 lambda2) phi2) R)))
        assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
        double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
        	double tmp;
        	if (phi1 <= -220000000000.0) {
        		tmp = hypot((lambda1 - lambda2), phi1) * R;
        	} else {
        		tmp = hypot((lambda1 - lambda2), phi2) * R;
        	}
        	return tmp;
        }
        
        assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
        public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
        	double tmp;
        	if (phi1 <= -220000000000.0) {
        		tmp = Math.hypot((lambda1 - lambda2), phi1) * R;
        	} else {
        		tmp = Math.hypot((lambda1 - lambda2), phi2) * R;
        	}
        	return tmp;
        }
        
        [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
        def code(R, lambda1, lambda2, phi1, phi2):
        	tmp = 0
        	if phi1 <= -220000000000.0:
        		tmp = math.hypot((lambda1 - lambda2), phi1) * R
        	else:
        		tmp = math.hypot((lambda1 - lambda2), phi2) * R
        	return tmp
        
        R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
        function code(R, lambda1, lambda2, phi1, phi2)
        	tmp = 0.0
        	if (phi1 <= -220000000000.0)
        		tmp = Float64(hypot(Float64(lambda1 - lambda2), phi1) * R);
        	else
        		tmp = Float64(hypot(Float64(lambda1 - lambda2), phi2) * R);
        	end
        	return tmp
        end
        
        R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
        function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
        	tmp = 0.0;
        	if (phi1 <= -220000000000.0)
        		tmp = hypot((lambda1 - lambda2), phi1) * R;
        	else
        		tmp = hypot((lambda1 - lambda2), phi2) * R;
        	end
        	tmp_2 = tmp;
        end
        
        NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
        code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -220000000000.0], N[(N[Sqrt[N[(lambda1 - lambda2), $MachinePrecision] ^ 2 + phi1 ^ 2], $MachinePrecision] * R), $MachinePrecision], N[(N[Sqrt[N[(lambda1 - lambda2), $MachinePrecision] ^ 2 + phi2 ^ 2], $MachinePrecision] * R), $MachinePrecision]]
        
        \begin{array}{l}
        [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
        \\
        \begin{array}{l}
        \mathbf{if}\;\phi_1 \leq -220000000000:\\
        \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right) \cdot R\\
        
        \mathbf{else}:\\
        \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \cdot R\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if phi1 < -2.2e11

          1. Initial program 57.2%

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

            \[\leadsto R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_1}^{2}}} \]
          4. Step-by-step derivation
            1. unpow2N/A

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

              \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_1\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_1}^{2}} \]
            3. unswap-sqrN/A

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

              \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) + \color{blue}{\phi_1 \cdot \phi_1}} \]
            5. lower-hypot.f64N/A

              \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right)} \]
            6. *-commutativeN/A

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

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

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

              \[\leadsto R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}, \phi_1\right) \]
            10. lower-*.f6480.3

              \[\leadsto R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \color{blue}{\left(0.5 \cdot \phi_1\right)}, \phi_1\right) \]
          5. Applied rewrites80.3%

            \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right), \phi_1\right)} \]
          6. Taylor expanded in phi1 around 0

            \[\leadsto R \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right) \]
          7. Step-by-step derivation
            1. Applied rewrites72.0%

              \[\leadsto R \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right) \]

            if -2.2e11 < phi1

            1. Initial program 54.4%

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

              \[\leadsto R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}}} \]
            4. Step-by-step derivation
              1. unpow2N/A

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

                \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_2}^{2}} \]
              3. unswap-sqrN/A

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

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

                \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right)} \]
              6. lower-*.f64N/A

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

                \[\leadsto R \cdot \mathsf{hypot}\left(\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \]
              8. lower-*.f64N/A

                \[\leadsto R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \]
              9. lower--.f6479.3

                \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}, \phi_2\right) \]
            5. Applied rewrites79.3%

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

              \[\leadsto R \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \]
            7. Step-by-step derivation
              1. Applied rewrites70.0%

                \[\leadsto R \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \]
            8. Recombined 2 regimes into one program.
            9. Final simplification70.5%

              \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -220000000000:\\ \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \cdot R\\ \end{array} \]
            10. Add Preprocessing

            Alternative 5: 80.4% accurate, 2.4× speedup?

            \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -4.8 \cdot 10^{+49}:\\ \;\;\;\;\left(\phi_2 - \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \cdot R\\ \end{array} \end{array} \]
            NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
            (FPCore (R lambda1 lambda2 phi1 phi2)
             :precision binary64
             (if (<= phi1 -4.8e+49)
               (* (- phi2 phi1) R)
               (* (hypot (- lambda1 lambda2) phi2) R)))
            assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
            double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
            	double tmp;
            	if (phi1 <= -4.8e+49) {
            		tmp = (phi2 - phi1) * R;
            	} else {
            		tmp = hypot((lambda1 - lambda2), phi2) * R;
            	}
            	return tmp;
            }
            
            assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
            public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
            	double tmp;
            	if (phi1 <= -4.8e+49) {
            		tmp = (phi2 - phi1) * R;
            	} else {
            		tmp = Math.hypot((lambda1 - lambda2), phi2) * R;
            	}
            	return tmp;
            }
            
            [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
            def code(R, lambda1, lambda2, phi1, phi2):
            	tmp = 0
            	if phi1 <= -4.8e+49:
            		tmp = (phi2 - phi1) * R
            	else:
            		tmp = math.hypot((lambda1 - lambda2), phi2) * R
            	return tmp
            
            R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
            function code(R, lambda1, lambda2, phi1, phi2)
            	tmp = 0.0
            	if (phi1 <= -4.8e+49)
            		tmp = Float64(Float64(phi2 - phi1) * R);
            	else
            		tmp = Float64(hypot(Float64(lambda1 - lambda2), phi2) * R);
            	end
            	return tmp
            end
            
            R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
            function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
            	tmp = 0.0;
            	if (phi1 <= -4.8e+49)
            		tmp = (phi2 - phi1) * R;
            	else
            		tmp = hypot((lambda1 - lambda2), phi2) * R;
            	end
            	tmp_2 = tmp;
            end
            
            NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
            code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -4.8e+49], N[(N[(phi2 - phi1), $MachinePrecision] * R), $MachinePrecision], N[(N[Sqrt[N[(lambda1 - lambda2), $MachinePrecision] ^ 2 + phi2 ^ 2], $MachinePrecision] * R), $MachinePrecision]]
            
            \begin{array}{l}
            [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
            \\
            \begin{array}{l}
            \mathbf{if}\;\phi_1 \leq -4.8 \cdot 10^{+49}:\\
            \;\;\;\;\left(\phi_2 - \phi_1\right) \cdot R\\
            
            \mathbf{else}:\\
            \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \cdot R\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if phi1 < -4.8e49

              1. Initial program 51.9%

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

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

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

                  \[\leadsto \color{blue}{\left(-1 \cdot \phi_1\right) \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right)} \]
                3. mul-1-negN/A

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

                  \[\leadsto \color{blue}{\left(-\phi_1\right)} \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right) \]
                5. +-commutativeN/A

                  \[\leadsto \left(-\phi_1\right) \cdot \color{blue}{\left(-1 \cdot \frac{R \cdot \phi_2}{\phi_1} + R\right)} \]
                6. mul-1-negN/A

                  \[\leadsto \left(-\phi_1\right) \cdot \left(\color{blue}{\left(\mathsf{neg}\left(\frac{R \cdot \phi_2}{\phi_1}\right)\right)} + R\right) \]
                7. associate-/l*N/A

                  \[\leadsto \left(-\phi_1\right) \cdot \left(\left(\mathsf{neg}\left(\color{blue}{R \cdot \frac{\phi_2}{\phi_1}}\right)\right) + R\right) \]
                8. distribute-rgt-neg-inN/A

                  \[\leadsto \left(-\phi_1\right) \cdot \left(\color{blue}{R \cdot \left(\mathsf{neg}\left(\frac{\phi_2}{\phi_1}\right)\right)} + R\right) \]
                9. mul-1-negN/A

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

                  \[\leadsto \left(-\phi_1\right) \cdot \color{blue}{\mathsf{fma}\left(R, -1 \cdot \frac{\phi_2}{\phi_1}, R\right)} \]
                11. associate-*r/N/A

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

                  \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(R, \color{blue}{\frac{-1 \cdot \phi_2}{\phi_1}}, R\right) \]
                13. mul-1-negN/A

                  \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(R, \frac{\color{blue}{\mathsf{neg}\left(\phi_2\right)}}{\phi_1}, R\right) \]
                14. lower-neg.f6467.2

                  \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(R, \frac{\color{blue}{-\phi_2}}{\phi_1}, R\right) \]
              5. Applied rewrites67.2%

                \[\leadsto \color{blue}{\left(-\phi_1\right) \cdot \mathsf{fma}\left(R, \frac{-\phi_2}{\phi_1}, R\right)} \]
              6. Step-by-step derivation
                1. Applied rewrites67.2%

                  \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2}{-1}, \color{blue}{\frac{R}{\phi_1}}, R\right) \]
                2. Taylor expanded in phi2 around 0

                  \[\leadsto -1 \cdot \left(R \cdot \phi_1\right) + \color{blue}{R \cdot \phi_2} \]
                3. Step-by-step derivation
                  1. Applied rewrites67.2%

                    \[\leadsto \mathsf{fma}\left(-\phi_1, \color{blue}{R}, \phi_2 \cdot R\right) \]
                  2. Taylor expanded in phi2 around 0

                    \[\leadsto -1 \cdot \left(R \cdot \phi_1\right) + \color{blue}{R \cdot \phi_2} \]
                  3. Step-by-step derivation
                    1. Applied rewrites67.2%

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

                    if -4.8e49 < phi1

                    1. Initial program 55.7%

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

                      \[\leadsto R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}}} \]
                    4. Step-by-step derivation
                      1. unpow2N/A

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

                        \[\leadsto R \cdot \sqrt{\left(\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_2}^{2}} \]
                      3. unswap-sqrN/A

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

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

                        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right)} \]
                      6. lower-*.f64N/A

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

                        \[\leadsto R \cdot \mathsf{hypot}\left(\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \]
                      8. lower-*.f64N/A

                        \[\leadsto R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right) \]
                      9. lower--.f6479.0

                        \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}, \phi_2\right) \]
                    5. Applied rewrites79.0%

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

                      \[\leadsto R \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \]
                    7. Step-by-step derivation
                      1. Applied rewrites70.0%

                        \[\leadsto R \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \]
                    8. Recombined 2 regimes into one program.
                    9. Final simplification69.5%

                      \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -4.8 \cdot 10^{+49}:\\ \;\;\;\;\left(\phi_2 - \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_2\right) \cdot R\\ \end{array} \]
                    10. Add Preprocessing

                    Alternative 6: 60.2% accurate, 18.6× speedup?

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

                      1. Initial program 55.1%

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

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

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

                          \[\leadsto \color{blue}{\left(-1 \cdot \phi_1\right) \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right)} \]
                        3. mul-1-negN/A

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

                          \[\leadsto \color{blue}{\left(-\phi_1\right)} \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right) \]
                        5. +-commutativeN/A

                          \[\leadsto \left(-\phi_1\right) \cdot \color{blue}{\left(-1 \cdot \frac{R \cdot \phi_2}{\phi_1} + R\right)} \]
                        6. mul-1-negN/A

                          \[\leadsto \left(-\phi_1\right) \cdot \left(\color{blue}{\left(\mathsf{neg}\left(\frac{R \cdot \phi_2}{\phi_1}\right)\right)} + R\right) \]
                        7. associate-/l*N/A

                          \[\leadsto \left(-\phi_1\right) \cdot \left(\left(\mathsf{neg}\left(\color{blue}{R \cdot \frac{\phi_2}{\phi_1}}\right)\right) + R\right) \]
                        8. distribute-rgt-neg-inN/A

                          \[\leadsto \left(-\phi_1\right) \cdot \left(\color{blue}{R \cdot \left(\mathsf{neg}\left(\frac{\phi_2}{\phi_1}\right)\right)} + R\right) \]
                        9. mul-1-negN/A

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

                          \[\leadsto \left(-\phi_1\right) \cdot \color{blue}{\mathsf{fma}\left(R, -1 \cdot \frac{\phi_2}{\phi_1}, R\right)} \]
                        11. associate-*r/N/A

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

                          \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(R, \color{blue}{\frac{-1 \cdot \phi_2}{\phi_1}}, R\right) \]
                        13. mul-1-negN/A

                          \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(R, \frac{\color{blue}{\mathsf{neg}\left(\phi_2\right)}}{\phi_1}, R\right) \]
                        14. lower-neg.f6429.2

                          \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(R, \frac{\color{blue}{-\phi_2}}{\phi_1}, R\right) \]
                      5. Applied rewrites29.2%

                        \[\leadsto \color{blue}{\left(-\phi_1\right) \cdot \mathsf{fma}\left(R, \frac{-\phi_2}{\phi_1}, R\right)} \]
                      6. Step-by-step derivation
                        1. Applied rewrites30.8%

                          \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2}{-1}, \color{blue}{\frac{R}{\phi_1}}, R\right) \]
                        2. Taylor expanded in phi2 around 0

                          \[\leadsto -1 \cdot \left(R \cdot \phi_1\right) + \color{blue}{R \cdot \phi_2} \]
                        3. Step-by-step derivation
                          1. Applied rewrites31.7%

                            \[\leadsto \mathsf{fma}\left(-\phi_1, \color{blue}{R}, \phi_2 \cdot R\right) \]
                          2. Taylor expanded in phi2 around 0

                            \[\leadsto -1 \cdot \left(R \cdot \phi_1\right) + \color{blue}{R \cdot \phi_2} \]
                          3. Step-by-step derivation
                            1. Applied rewrites31.7%

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

                            if 6.1999999999999997e227 < lambda2

                            1. Initial program 52.4%

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

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

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

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

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

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

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

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

                                \[\leadsto \left(R \cdot \lambda_2\right) \cdot \cos \left(\color{blue}{\left(\phi_2 + \phi_1\right)} \cdot \frac{1}{2}\right) \]
                              8. lower-+.f6442.4

                                \[\leadsto \left(R \cdot \lambda_2\right) \cdot \cos \left(\color{blue}{\left(\phi_2 + \phi_1\right)} \cdot 0.5\right) \]
                            5. Applied rewrites42.4%

                              \[\leadsto \color{blue}{\left(R \cdot \lambda_2\right) \cdot \cos \left(\left(\phi_2 + \phi_1\right) \cdot 0.5\right)} \]
                            6. Taylor expanded in phi2 around 0

                              \[\leadsto \frac{-1}{2} \cdot \left(R \cdot \left(\lambda_2 \cdot \left(\phi_2 \cdot \sin \left(\frac{1}{2} \cdot \phi_1\right)\right)\right)\right) + \color{blue}{R \cdot \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \phi_1\right)\right)} \]
                            7. Step-by-step derivation
                              1. Applied rewrites34.5%

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

                                \[\leadsto R \cdot \lambda_2 \]
                              3. Step-by-step derivation
                                1. Applied rewrites72.3%

                                  \[\leadsto \lambda_2 \cdot R \]
                              4. Recombined 2 regimes into one program.
                              5. Add Preprocessing

                              Alternative 7: 53.1% accurate, 19.9× speedup?

                              \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -85000000:\\ \;\;\;\;\left(-\phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \end{array} \]
                              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                              (FPCore (R lambda1 lambda2 phi1 phi2)
                               :precision binary64
                               (if (<= phi1 -85000000.0) (* (- phi1) R) (* phi2 R)))
                              assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                              double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                              	double tmp;
                              	if (phi1 <= -85000000.0) {
                              		tmp = -phi1 * R;
                              	} else {
                              		tmp = phi2 * R;
                              	}
                              	return tmp;
                              }
                              
                              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                              real(8) function code(r, lambda1, lambda2, phi1, phi2)
                                  real(8), intent (in) :: r
                                  real(8), intent (in) :: lambda1
                                  real(8), intent (in) :: lambda2
                                  real(8), intent (in) :: phi1
                                  real(8), intent (in) :: phi2
                                  real(8) :: tmp
                                  if (phi1 <= (-85000000.0d0)) then
                                      tmp = -phi1 * r
                                  else
                                      tmp = phi2 * r
                                  end if
                                  code = tmp
                              end function
                              
                              assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
                              public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                              	double tmp;
                              	if (phi1 <= -85000000.0) {
                              		tmp = -phi1 * R;
                              	} else {
                              		tmp = phi2 * R;
                              	}
                              	return tmp;
                              }
                              
                              [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                              def code(R, lambda1, lambda2, phi1, phi2):
                              	tmp = 0
                              	if phi1 <= -85000000.0:
                              		tmp = -phi1 * R
                              	else:
                              		tmp = phi2 * R
                              	return tmp
                              
                              R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                              function code(R, lambda1, lambda2, phi1, phi2)
                              	tmp = 0.0
                              	if (phi1 <= -85000000.0)
                              		tmp = Float64(Float64(-phi1) * R);
                              	else
                              		tmp = Float64(phi2 * R);
                              	end
                              	return tmp
                              end
                              
                              R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
                              function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
                              	tmp = 0.0;
                              	if (phi1 <= -85000000.0)
                              		tmp = -phi1 * R;
                              	else
                              		tmp = phi2 * R;
                              	end
                              	tmp_2 = tmp;
                              end
                              
                              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                              code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -85000000.0], N[((-phi1) * R), $MachinePrecision], N[(phi2 * R), $MachinePrecision]]
                              
                              \begin{array}{l}
                              [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                              \\
                              \begin{array}{l}
                              \mathbf{if}\;\phi_1 \leq -85000000:\\
                              \;\;\;\;\left(-\phi_1\right) \cdot R\\
                              
                              \mathbf{else}:\\
                              \;\;\;\;\phi_2 \cdot R\\
                              
                              
                              \end{array}
                              \end{array}
                              
                              Derivation
                              1. Split input into 2 regimes
                              2. if phi1 < -8.5e7

                                1. Initial program 58.0%

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

                                  \[\leadsto R \cdot \color{blue}{\left(-1 \cdot \phi_1\right)} \]
                                4. Step-by-step derivation
                                  1. mul-1-negN/A

                                    \[\leadsto R \cdot \color{blue}{\left(\mathsf{neg}\left(\phi_1\right)\right)} \]
                                  2. lower-neg.f6462.7

                                    \[\leadsto R \cdot \color{blue}{\left(-\phi_1\right)} \]
                                5. Applied rewrites62.7%

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

                                if -8.5e7 < phi1

                                1. Initial program 54.2%

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

                                  \[\leadsto \color{blue}{R \cdot \phi_2} \]
                                4. Step-by-step derivation
                                  1. lower-*.f6421.2

                                    \[\leadsto \color{blue}{R \cdot \phi_2} \]
                                5. Applied rewrites21.2%

                                  \[\leadsto \color{blue}{R \cdot \phi_2} \]
                              3. Recombined 2 regimes into one program.
                              4. Final simplification30.3%

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

                              Alternative 8: 37.3% accurate, 23.2× speedup?

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

                                1. Initial program 57.7%

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

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

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

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

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

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

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

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

                                    \[\leadsto \left(R \cdot \lambda_2\right) \cdot \cos \left(\color{blue}{\left(\phi_2 + \phi_1\right)} \cdot \frac{1}{2}\right) \]
                                  8. lower-+.f6413.9

                                    \[\leadsto \left(R \cdot \lambda_2\right) \cdot \cos \left(\color{blue}{\left(\phi_2 + \phi_1\right)} \cdot 0.5\right) \]
                                5. Applied rewrites13.9%

                                  \[\leadsto \color{blue}{\left(R \cdot \lambda_2\right) \cdot \cos \left(\left(\phi_2 + \phi_1\right) \cdot 0.5\right)} \]
                                6. Taylor expanded in phi2 around 0

                                  \[\leadsto \frac{-1}{2} \cdot \left(R \cdot \left(\lambda_2 \cdot \left(\phi_2 \cdot \sin \left(\frac{1}{2} \cdot \phi_1\right)\right)\right)\right) + \color{blue}{R \cdot \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \phi_1\right)\right)} \]
                                7. Step-by-step derivation
                                  1. Applied rewrites14.4%

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

                                    \[\leadsto R \cdot \lambda_2 \]
                                  3. Step-by-step derivation
                                    1. Applied rewrites15.8%

                                      \[\leadsto \lambda_2 \cdot R \]

                                    if 7.0000000000000006e-30 < phi2

                                    1. Initial program 48.0%

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

                                      \[\leadsto \color{blue}{R \cdot \phi_2} \]
                                    4. Step-by-step derivation
                                      1. lower-*.f6449.2

                                        \[\leadsto \color{blue}{R \cdot \phi_2} \]
                                    5. Applied rewrites49.2%

                                      \[\leadsto \color{blue}{R \cdot \phi_2} \]
                                  4. Recombined 2 regimes into one program.
                                  5. Final simplification25.1%

                                    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 7 \cdot 10^{-30}:\\ \;\;\;\;\lambda_2 \cdot R\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \]
                                  6. Add Preprocessing

                                  Alternative 9: 13.9% accurate, 46.5× speedup?

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

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

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

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

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

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

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

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

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

                                      \[\leadsto \left(R \cdot \lambda_2\right) \cdot \cos \left(\color{blue}{\left(\phi_2 + \phi_1\right)} \cdot \frac{1}{2}\right) \]
                                    8. lower-+.f6414.6

                                      \[\leadsto \left(R \cdot \lambda_2\right) \cdot \cos \left(\color{blue}{\left(\phi_2 + \phi_1\right)} \cdot 0.5\right) \]
                                  5. Applied rewrites14.6%

                                    \[\leadsto \color{blue}{\left(R \cdot \lambda_2\right) \cdot \cos \left(\left(\phi_2 + \phi_1\right) \cdot 0.5\right)} \]
                                  6. Taylor expanded in phi2 around 0

                                    \[\leadsto \frac{-1}{2} \cdot \left(R \cdot \left(\lambda_2 \cdot \left(\phi_2 \cdot \sin \left(\frac{1}{2} \cdot \phi_1\right)\right)\right)\right) + \color{blue}{R \cdot \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \phi_1\right)\right)} \]
                                  7. Step-by-step derivation
                                    1. Applied rewrites13.4%

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

                                      \[\leadsto R \cdot \lambda_2 \]
                                    3. Step-by-step derivation
                                      1. Applied rewrites13.8%

                                        \[\leadsto \lambda_2 \cdot R \]
                                      2. Add Preprocessing

                                      Reproduce

                                      ?
                                      herbie shell --seed 2024276 
                                      (FPCore (R lambda1 lambda2 phi1 phi2)
                                        :name "Equirectangular approximation to distance on a great circle"
                                        :precision binary64
                                        (* R (sqrt (+ (* (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0))) (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0)))) (* (- phi1 phi2) (- phi1 phi2))))))