Equirectangular approximation to distance on a great circle

Percentage Accurate: 59.1% → 91.1%
Time: 13.0s
Alternatives: 12
Speedup: 18.6×

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 12 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: 59.1% 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.1% 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_2 \leq 2.05 \cdot 10^{-17}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_2 \cdot 0.5\right)\right)\\ \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 2.05e-17)
   (* R (hypot phi1 (* (- lambda1 lambda2) (cos (* phi1 0.5)))))
   (* R (hypot phi2 (* (- lambda1 lambda2) (cos (* phi2 0.5)))))))
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 <= 2.05e-17) {
		tmp = R * hypot(phi1, ((lambda1 - lambda2) * cos((phi1 * 0.5))));
	} else {
		tmp = R * hypot(phi2, ((lambda1 - lambda2) * cos((phi2 * 0.5))));
	}
	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 (phi2 <= 2.05e-17) {
		tmp = R * Math.hypot(phi1, ((lambda1 - lambda2) * Math.cos((phi1 * 0.5))));
	} else {
		tmp = R * Math.hypot(phi2, ((lambda1 - lambda2) * Math.cos((phi2 * 0.5))));
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 2.05e-17:
		tmp = R * math.hypot(phi1, ((lambda1 - lambda2) * math.cos((phi1 * 0.5))))
	else:
		tmp = R * math.hypot(phi2, ((lambda1 - lambda2) * math.cos((phi2 * 0.5))))
	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 <= 2.05e-17)
		tmp = Float64(R * hypot(phi1, Float64(Float64(lambda1 - lambda2) * cos(Float64(phi1 * 0.5)))));
	else
		tmp = Float64(R * hypot(phi2, Float64(Float64(lambda1 - lambda2) * cos(Float64(phi2 * 0.5)))));
	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 <= 2.05e-17)
		tmp = R * hypot(phi1, ((lambda1 - lambda2) * cos((phi1 * 0.5))));
	else
		tmp = R * hypot(phi2, ((lambda1 - lambda2) * cos((phi2 * 0.5))));
	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, 2.05e-17], N[(R * N[Sqrt[phi1 ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(phi1 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], N[(R * N[Sqrt[phi2 ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(phi2 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 2.05 \cdot 10^{-17}:\\
\;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\

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


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

    1. Initial program 58.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 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. +-commutativeN/A

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

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

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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}} \]
      4. unpow2N/A

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
      5. unswap-sqrN/A

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
      6. lower-hypot.f64N/A

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

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

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

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

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

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

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

    if 2.05e-17 < phi2

    1. Initial program 58.8%

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

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

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

        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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}} \]
      4. unpow2N/A

        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
      5. unswap-sqrN/A

        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
      6. lower-hypot.f64N/A

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2.05 \cdot 10^{-17}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_2 \cdot 0.5\right)\right)\\ \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_2 \leq 2 \cdot 10^{-17}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 4.2 \cdot 10^{+93}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), 0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right), \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 \cdot \cos \left(\phi_2 \cdot 0.5\right)\right)\\ \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 2e-17)
   (* R (hypot phi1 (* (- lambda1 lambda2) (cos (* phi1 0.5)))))
   (if (<= phi2 4.2e+93)
     (*
      R
      (sqrt
       (fma
        (* (- lambda1 lambda2) (- lambda1 lambda2))
        (+ 0.5 (* 0.5 (cos (+ phi2 phi1))))
        (* (- phi1 phi2) (- phi1 phi2)))))
     (* R (hypot phi2 (* lambda1 (cos (* phi2 0.5))))))))
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 <= 2e-17) {
		tmp = R * hypot(phi1, ((lambda1 - lambda2) * cos((phi1 * 0.5))));
	} else if (phi2 <= 4.2e+93) {
		tmp = R * sqrt(fma(((lambda1 - lambda2) * (lambda1 - lambda2)), (0.5 + (0.5 * cos((phi2 + phi1)))), ((phi1 - phi2) * (phi1 - phi2))));
	} else {
		tmp = R * hypot(phi2, (lambda1 * cos((phi2 * 0.5))));
	}
	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 <= 2e-17)
		tmp = Float64(R * hypot(phi1, Float64(Float64(lambda1 - lambda2) * cos(Float64(phi1 * 0.5)))));
	elseif (phi2 <= 4.2e+93)
		tmp = Float64(R * sqrt(fma(Float64(Float64(lambda1 - lambda2) * Float64(lambda1 - lambda2)), Float64(0.5 + Float64(0.5 * cos(Float64(phi2 + phi1)))), Float64(Float64(phi1 - phi2) * Float64(phi1 - phi2)))));
	else
		tmp = Float64(R * hypot(phi2, Float64(lambda1 * cos(Float64(phi2 * 0.5)))));
	end
	return 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, 2e-17], N[(R * N[Sqrt[phi1 ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(phi1 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 4.2e+93], N[(R * N[Sqrt[N[(N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] * N[(0.5 + N[(0.5 * N[Cos[N[(phi2 + phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[Sqrt[phi2 ^ 2 + N[(lambda1 * N[Cos[N[(phi2 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 2 \cdot 10^{-17}:\\
\;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\

\mathbf{elif}\;\phi_2 \leq 4.2 \cdot 10^{+93}:\\
\;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), 0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right), \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)\right)}\\

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


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

    1. Initial program 58.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 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. +-commutativeN/A

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

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

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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}} \]
      4. unpow2N/A

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
      5. unswap-sqrN/A

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
      6. lower-hypot.f64N/A

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

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

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

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

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

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

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

    if 2.00000000000000014e-17 < phi2 < 4.1999999999999996e93

    1. Initial program 70.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. Step-by-step derivation
      1. lift-*.f64N/A

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

        \[\leadsto \color{blue}{\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)} \cdot R} \]
      3. lower-*.f6470.4

        \[\leadsto \color{blue}{\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)} \cdot R} \]
    4. Applied rewrites70.4%

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

    if 4.1999999999999996e93 < phi2

    1. Initial program 52.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 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. +-commutativeN/A

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

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

        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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}} \]
      4. unpow2N/A

        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
      5. unswap-sqrN/A

        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
      6. lower-hypot.f64N/A

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

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

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

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

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

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

      \[\leadsto R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 \cdot \color{blue}{\cos \left(\frac{1}{2} \cdot \phi_2\right)}\right) \]
    7. Step-by-step derivation
      1. Applied rewrites89.2%

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2 \cdot 10^{-17}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 4.2 \cdot 10^{+93}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), 0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right), \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 \cdot \cos \left(\phi_2 \cdot 0.5\right)\right)\\ \end{array} \]
    10. Add Preprocessing

    Alternative 3: 80.3% 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 -4.7 \cdot 10^{+85}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_2 \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 - \lambda_2\right)\\ \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.7e+85)
       (* R (hypot phi1 (* lambda2 (cos (* phi1 0.5)))))
       (* R (hypot phi2 (- lambda1 lambda2)))))
    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.7e+85) {
    		tmp = R * hypot(phi1, (lambda2 * cos((phi1 * 0.5))));
    	} else {
    		tmp = R * hypot(phi2, (lambda1 - lambda2));
    	}
    	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.7e+85) {
    		tmp = R * Math.hypot(phi1, (lambda2 * Math.cos((phi1 * 0.5))));
    	} else {
    		tmp = R * Math.hypot(phi2, (lambda1 - lambda2));
    	}
    	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.7e+85:
    		tmp = R * math.hypot(phi1, (lambda2 * math.cos((phi1 * 0.5))))
    	else:
    		tmp = R * math.hypot(phi2, (lambda1 - lambda2))
    	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.7e+85)
    		tmp = Float64(R * hypot(phi1, Float64(lambda2 * cos(Float64(phi1 * 0.5)))));
    	else
    		tmp = Float64(R * hypot(phi2, Float64(lambda1 - lambda2)));
    	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.7e+85)
    		tmp = R * hypot(phi1, (lambda2 * cos((phi1 * 0.5))));
    	else
    		tmp = R * hypot(phi2, (lambda1 - lambda2));
    	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.7e+85], N[(R * N[Sqrt[phi1 ^ 2 + N[(lambda2 * N[Cos[N[(phi1 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], N[(R * N[Sqrt[phi2 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $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.7 \cdot 10^{+85}:\\
    \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_2 \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 - \lambda_2\right)\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if phi1 < -4.7000000000000002e85

      1. Initial program 49.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 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. +-commutativeN/A

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

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

          \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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}} \]
        4. unpow2N/A

          \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
        5. unswap-sqrN/A

          \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
        6. lower-hypot.f64N/A

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

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

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

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

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

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

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\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 rewrites79.0%

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

        if -4.7000000000000002e85 < phi1

        1. Initial program 61.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 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. +-commutativeN/A

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

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

            \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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}} \]
          4. unpow2N/A

            \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
          5. unswap-sqrN/A

            \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
          6. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

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

        Alternative 4: 81.8% accurate, 1.7× speedup?

        \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2 \cdot 10^{-17}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\ \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{+93}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), 0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right), \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 - \lambda_2\right)\\ \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 2e-17)
           (* R (hypot phi1 (- lambda1 lambda2)))
           (if (<= phi2 8e+93)
             (*
              R
              (sqrt
               (fma
                (* (- lambda1 lambda2) (- lambda1 lambda2))
                (+ 0.5 (* 0.5 (cos (+ phi2 phi1))))
                (* (- phi1 phi2) (- phi1 phi2)))))
             (* R (hypot phi2 (- lambda1 lambda2))))))
        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 <= 2e-17) {
        		tmp = R * hypot(phi1, (lambda1 - lambda2));
        	} else if (phi2 <= 8e+93) {
        		tmp = R * sqrt(fma(((lambda1 - lambda2) * (lambda1 - lambda2)), (0.5 + (0.5 * cos((phi2 + phi1)))), ((phi1 - phi2) * (phi1 - phi2))));
        	} else {
        		tmp = R * hypot(phi2, (lambda1 - lambda2));
        	}
        	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 <= 2e-17)
        		tmp = Float64(R * hypot(phi1, Float64(lambda1 - lambda2)));
        	elseif (phi2 <= 8e+93)
        		tmp = Float64(R * sqrt(fma(Float64(Float64(lambda1 - lambda2) * Float64(lambda1 - lambda2)), Float64(0.5 + Float64(0.5 * cos(Float64(phi2 + phi1)))), Float64(Float64(phi1 - phi2) * Float64(phi1 - phi2)))));
        	else
        		tmp = Float64(R * hypot(phi2, Float64(lambda1 - lambda2)));
        	end
        	return 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, 2e-17], N[(R * N[Sqrt[phi1 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 8e+93], N[(R * N[Sqrt[N[(N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] * N[(0.5 + N[(0.5 * N[Cos[N[(phi2 + phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[Sqrt[phi2 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]]
        
        \begin{array}{l}
        [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
        \\
        \begin{array}{l}
        \mathbf{if}\;\phi_2 \leq 2 \cdot 10^{-17}:\\
        \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\
        
        \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{+93}:\\
        \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), 0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right), \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)\right)}\\
        
        \mathbf{else}:\\
        \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 - \lambda_2\right)\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 3 regimes
        2. if phi2 < 2.00000000000000014e-17

          1. Initial program 58.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 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. +-commutativeN/A

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

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

              \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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}} \]
            4. unpow2N/A

              \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
            5. unswap-sqrN/A

              \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
            6. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

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

            if 2.00000000000000014e-17 < phi2 < 8.00000000000000035e93

            1. Initial program 70.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. Step-by-step derivation
              1. lift-*.f64N/A

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

                \[\leadsto \color{blue}{\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)} \cdot R} \]
              3. lower-*.f6470.4

                \[\leadsto \color{blue}{\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)} \cdot R} \]
            4. Applied rewrites70.4%

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

            if 8.00000000000000035e93 < phi2

            1. Initial program 52.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 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. +-commutativeN/A

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

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

                \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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}} \]
              4. unpow2N/A

                \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
              5. unswap-sqrN/A

                \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
              6. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

              \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2 \cdot 10^{-17}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\ \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{+93}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), 0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right), \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 - \lambda_2\right)\\ \end{array} \]
            10. Add Preprocessing

            Alternative 5: 80.8% accurate, 1.8× speedup?

            \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2.05 \cdot 10^{-17}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\ \mathbf{elif}\;\phi_2 \leq 6.3 \cdot 10^{+93}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos \phi_2, 0.5\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2 \cdot \phi_2\right)}\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 - \lambda_2\right)\\ \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 2.05e-17)
               (* R (hypot phi1 (- lambda1 lambda2)))
               (if (<= phi2 6.3e+93)
                 (*
                  R
                  (sqrt
                   (fma
                    (fma 0.5 (cos phi2) 0.5)
                    (* (- lambda1 lambda2) (- lambda1 lambda2))
                    (* phi2 phi2))))
                 (* R (hypot phi2 (- lambda1 lambda2))))))
            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 <= 2.05e-17) {
            		tmp = R * hypot(phi1, (lambda1 - lambda2));
            	} else if (phi2 <= 6.3e+93) {
            		tmp = R * sqrt(fma(fma(0.5, cos(phi2), 0.5), ((lambda1 - lambda2) * (lambda1 - lambda2)), (phi2 * phi2)));
            	} else {
            		tmp = R * hypot(phi2, (lambda1 - lambda2));
            	}
            	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 <= 2.05e-17)
            		tmp = Float64(R * hypot(phi1, Float64(lambda1 - lambda2)));
            	elseif (phi2 <= 6.3e+93)
            		tmp = Float64(R * sqrt(fma(fma(0.5, cos(phi2), 0.5), Float64(Float64(lambda1 - lambda2) * Float64(lambda1 - lambda2)), Float64(phi2 * phi2))));
            	else
            		tmp = Float64(R * hypot(phi2, Float64(lambda1 - lambda2)));
            	end
            	return 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, 2.05e-17], N[(R * N[Sqrt[phi1 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 6.3e+93], N[(R * N[Sqrt[N[(N[(0.5 * N[Cos[phi2], $MachinePrecision] + 0.5), $MachinePrecision] * N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] + N[(phi2 * phi2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[Sqrt[phi2 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]]
            
            \begin{array}{l}
            [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
            \\
            \begin{array}{l}
            \mathbf{if}\;\phi_2 \leq 2.05 \cdot 10^{-17}:\\
            \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\
            
            \mathbf{elif}\;\phi_2 \leq 6.3 \cdot 10^{+93}:\\
            \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos \phi_2, 0.5\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2 \cdot \phi_2\right)}\\
            
            \mathbf{else}:\\
            \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 - \lambda_2\right)\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 3 regimes
            2. if phi2 < 2.05e-17

              1. Initial program 58.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 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. +-commutativeN/A

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

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

                  \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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}} \]
                4. unpow2N/A

                  \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
                5. unswap-sqrN/A

                  \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
                6. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

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

                if 2.05e-17 < phi2 < 6.29999999999999987e93

                1. Initial program 70.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. Step-by-step derivation
                  1. lift-*.f64N/A

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

                    \[\leadsto \color{blue}{\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)} \cdot R} \]
                  3. lower-*.f6470.4

                    \[\leadsto \color{blue}{\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)} \cdot R} \]
                4. Applied rewrites70.4%

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

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

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

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

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

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

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

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

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

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

                    \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \cos \phi_2, \frac{1}{2}\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \color{blue}{\phi_2 \cdot \phi_2}\right)} \cdot R \]
                  10. lower-*.f6464.0

                    \[\leadsto \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos \phi_2, 0.5\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \color{blue}{\phi_2 \cdot \phi_2}\right)} \cdot R \]
                7. Applied rewrites64.0%

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

                if 6.29999999999999987e93 < phi2

                1. Initial program 52.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 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. +-commutativeN/A

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

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

                    \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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}} \]
                  4. unpow2N/A

                    \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                  5. unswap-sqrN/A

                    \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                  6. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

                  \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2.05 \cdot 10^{-17}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\ \mathbf{elif}\;\phi_2 \leq 6.3 \cdot 10^{+93}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos \phi_2, 0.5\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2 \cdot \phi_2\right)}\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 - \lambda_2\right)\\ \end{array} \]
                10. Add Preprocessing

                Alternative 6: 80.5% 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_2 \leq 2.05 \cdot 10^{-17}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 - \lambda_2\right)\\ \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 2.05e-17)
                   (* R (hypot phi1 (- lambda1 lambda2)))
                   (* R (hypot phi2 (- lambda1 lambda2)))))
                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 <= 2.05e-17) {
                		tmp = R * hypot(phi1, (lambda1 - lambda2));
                	} else {
                		tmp = R * hypot(phi2, (lambda1 - lambda2));
                	}
                	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 (phi2 <= 2.05e-17) {
                		tmp = R * Math.hypot(phi1, (lambda1 - lambda2));
                	} else {
                		tmp = R * Math.hypot(phi2, (lambda1 - lambda2));
                	}
                	return tmp;
                }
                
                [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                def code(R, lambda1, lambda2, phi1, phi2):
                	tmp = 0
                	if phi2 <= 2.05e-17:
                		tmp = R * math.hypot(phi1, (lambda1 - lambda2))
                	else:
                		tmp = R * math.hypot(phi2, (lambda1 - lambda2))
                	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 <= 2.05e-17)
                		tmp = Float64(R * hypot(phi1, Float64(lambda1 - lambda2)));
                	else
                		tmp = Float64(R * hypot(phi2, Float64(lambda1 - lambda2)));
                	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 <= 2.05e-17)
                		tmp = R * hypot(phi1, (lambda1 - lambda2));
                	else
                		tmp = R * hypot(phi2, (lambda1 - lambda2));
                	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, 2.05e-17], N[(R * N[Sqrt[phi1 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], N[(R * N[Sqrt[phi2 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]
                
                \begin{array}{l}
                [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                \\
                \begin{array}{l}
                \mathbf{if}\;\phi_2 \leq 2.05 \cdot 10^{-17}:\\
                \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\
                
                \mathbf{else}:\\
                \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \lambda_1 - \lambda_2\right)\\
                
                
                \end{array}
                \end{array}
                
                Derivation
                1. Split input into 2 regimes
                2. if phi2 < 2.05e-17

                  1. Initial program 58.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 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. +-commutativeN/A

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

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

                      \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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}} \]
                    4. unpow2N/A

                      \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
                    5. unswap-sqrN/A

                      \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
                    6. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

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

                    if 2.05e-17 < phi2

                    1. Initial program 58.8%

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

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

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

                        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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}} \]
                      4. unpow2N/A

                        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                      5. unswap-sqrN/A

                        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                      6. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

                    Alternative 7: 79.2% 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_2 \leq 2.05 \cdot 10^{+86}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot \left(R - \phi_1 \cdot \frac{R}{\phi_2}\right)\\ \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 2.05e+86)
                       (* R (hypot phi1 (- lambda1 lambda2)))
                       (* phi2 (- R (* phi1 (/ R phi2))))))
                    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 <= 2.05e+86) {
                    		tmp = R * hypot(phi1, (lambda1 - lambda2));
                    	} else {
                    		tmp = phi2 * (R - (phi1 * (R / phi2)));
                    	}
                    	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 (phi2 <= 2.05e+86) {
                    		tmp = R * Math.hypot(phi1, (lambda1 - lambda2));
                    	} else {
                    		tmp = phi2 * (R - (phi1 * (R / phi2)));
                    	}
                    	return tmp;
                    }
                    
                    [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                    def code(R, lambda1, lambda2, phi1, phi2):
                    	tmp = 0
                    	if phi2 <= 2.05e+86:
                    		tmp = R * math.hypot(phi1, (lambda1 - lambda2))
                    	else:
                    		tmp = phi2 * (R - (phi1 * (R / phi2)))
                    	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 <= 2.05e+86)
                    		tmp = Float64(R * hypot(phi1, Float64(lambda1 - lambda2)));
                    	else
                    		tmp = Float64(phi2 * Float64(R - Float64(phi1 * Float64(R / phi2))));
                    	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 <= 2.05e+86)
                    		tmp = R * hypot(phi1, (lambda1 - lambda2));
                    	else
                    		tmp = phi2 * (R - (phi1 * (R / phi2)));
                    	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, 2.05e+86], N[(R * N[Sqrt[phi1 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], N[(phi2 * N[(R - N[(phi1 * N[(R / phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
                    
                    \begin{array}{l}
                    [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                    \\
                    \begin{array}{l}
                    \mathbf{if}\;\phi_2 \leq 2.05 \cdot 10^{+86}:\\
                    \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\
                    
                    \mathbf{else}:\\
                    \;\;\;\;\phi_2 \cdot \left(R - \phi_1 \cdot \frac{R}{\phi_2}\right)\\
                    
                    
                    \end{array}
                    \end{array}
                    
                    Derivation
                    1. Split input into 2 regimes
                    2. if phi2 < 2.05e86

                      1. Initial program 59.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 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. +-commutativeN/A

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

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

                          \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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}} \]
                        4. unpow2N/A

                          \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
                        5. unswap-sqrN/A

                          \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \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)}} \]
                        6. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

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

                        if 2.05e86 < phi2

                        1. Initial program 54.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}{\phi_2 \cdot \left(R + -1 \cdot \frac{R \cdot \phi_1}{\phi_2}\right)} \]
                        4. Step-by-step derivation
                          1. lower-*.f64N/A

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

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

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

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

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

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

                            \[\leadsto \phi_2 \cdot \left(R - \color{blue}{\phi_1 \cdot \frac{R}{\phi_2}}\right) \]
                          8. lower-/.f6480.5

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

                          \[\leadsto \color{blue}{\phi_2 \cdot \left(R - \phi_1 \cdot \frac{R}{\phi_2}\right)} \]
                      8. Recombined 2 regimes into one program.
                      9. Add Preprocessing

                      Alternative 8: 62.5% accurate, 7.7× speedup?

                      \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -1 \cdot 10^{+129}:\\ \;\;\;\;R \cdot \left(\left(1 - \frac{\lambda_2}{\lambda_1}\right) \cdot \left(-\lambda_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\ \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 (<= (- lambda1 lambda2) -1e+129)
                         (* R (* (- 1.0 (/ lambda2 lambda1)) (- lambda1)))
                         (* R (- phi2 phi1))))
                      assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                      double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                      	double tmp;
                      	if ((lambda1 - lambda2) <= -1e+129) {
                      		tmp = R * ((1.0 - (lambda2 / lambda1)) * -lambda1);
                      	} else {
                      		tmp = R * (phi2 - phi1);
                      	}
                      	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 ((lambda1 - lambda2) <= (-1d+129)) then
                              tmp = r * ((1.0d0 - (lambda2 / lambda1)) * -lambda1)
                          else
                              tmp = r * (phi2 - phi1)
                          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 ((lambda1 - lambda2) <= -1e+129) {
                      		tmp = R * ((1.0 - (lambda2 / lambda1)) * -lambda1);
                      	} else {
                      		tmp = R * (phi2 - phi1);
                      	}
                      	return tmp;
                      }
                      
                      [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                      def code(R, lambda1, lambda2, phi1, phi2):
                      	tmp = 0
                      	if (lambda1 - lambda2) <= -1e+129:
                      		tmp = R * ((1.0 - (lambda2 / lambda1)) * -lambda1)
                      	else:
                      		tmp = R * (phi2 - phi1)
                      	return tmp
                      
                      R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                      function code(R, lambda1, lambda2, phi1, phi2)
                      	tmp = 0.0
                      	if (Float64(lambda1 - lambda2) <= -1e+129)
                      		tmp = Float64(R * Float64(Float64(1.0 - Float64(lambda2 / lambda1)) * Float64(-lambda1)));
                      	else
                      		tmp = Float64(R * Float64(phi2 - phi1));
                      	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 ((lambda1 - lambda2) <= -1e+129)
                      		tmp = R * ((1.0 - (lambda2 / lambda1)) * -lambda1);
                      	else
                      		tmp = R * (phi2 - phi1);
                      	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[N[(lambda1 - lambda2), $MachinePrecision], -1e+129], N[(R * N[(N[(1.0 - N[(lambda2 / lambda1), $MachinePrecision]), $MachinePrecision] * (-lambda1)), $MachinePrecision]), $MachinePrecision], N[(R * N[(phi2 - phi1), $MachinePrecision]), $MachinePrecision]]
                      
                      \begin{array}{l}
                      [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                      \\
                      \begin{array}{l}
                      \mathbf{if}\;\lambda_1 - \lambda_2 \leq -1 \cdot 10^{+129}:\\
                      \;\;\;\;R \cdot \left(\left(1 - \frac{\lambda_2}{\lambda_1}\right) \cdot \left(-\lambda_1\right)\right)\\
                      
                      \mathbf{else}:\\
                      \;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\
                      
                      
                      \end{array}
                      \end{array}
                      
                      Derivation
                      1. Split input into 2 regimes
                      2. if (-.f64 lambda1 lambda2) < -1e129

                        1. Initial program 50.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. +-commutativeN/A

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

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

                            \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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}} \]
                          4. unpow2N/A

                            \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                          5. unswap-sqrN/A

                            \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                          6. lower-hypot.f64N/A

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

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

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

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

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

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

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

                            \[\leadsto R \cdot \left(-\lambda_1 \cdot \mathsf{fma}\left(-\lambda_2, \frac{\cos \left(0.5 \cdot \phi_2\right)}{\lambda_1}, \cos \left(0.5 \cdot \phi_2\right)\right)\right) \]
                          2. Taylor expanded in phi2 around 0

                            \[\leadsto R \cdot \left(\mathsf{neg}\left(\lambda_1 \cdot \left(1 + -1 \cdot \frac{\lambda_2}{\lambda_1}\right)\right)\right) \]
                          3. Step-by-step derivation
                            1. Applied rewrites59.2%

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

                            if -1e129 < (-.f64 lambda1 lambda2)

                            1. Initial program 61.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 -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. mul-1-negN/A

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

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

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

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

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

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

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

                                \[\leadsto \mathsf{neg}\left(\phi_1 \cdot \left(R - \frac{\color{blue}{\phi_2 \cdot R}}{\phi_1}\right)\right) \]
                              9. lower-*.f6434.3

                                \[\leadsto -\phi_1 \cdot \left(R - \frac{\color{blue}{\phi_2 \cdot R}}{\phi_1}\right) \]
                            5. Applied rewrites34.3%

                              \[\leadsto \color{blue}{-\phi_1 \cdot \left(R - \frac{\phi_2 \cdot R}{\phi_1}\right)} \]
                            6. Taylor expanded in phi1 around 0

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

                                \[\leadsto R \cdot \color{blue}{\left(\phi_2 - \phi_1\right)} \]
                            8. Recombined 2 regimes into one program.
                            9. Final simplification42.8%

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

                            Alternative 9: 51.0% accurate, 13.9× speedup?

                            \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.02 \cdot 10^{-138}:\\ \;\;\;\;R \cdot \left(-\phi_1\right)\\ \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{+20}:\\ \;\;\;\;R \cdot \left(-\lambda_1\right)\\ \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 1.02e-138)
                               (* R (- phi1))
                               (if (<= phi2 8e+20) (* R (- lambda1)) (* 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 <= 1.02e-138) {
                            		tmp = R * -phi1;
                            	} else if (phi2 <= 8e+20) {
                            		tmp = R * -lambda1;
                            	} 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 <= 1.02d-138) then
                                    tmp = r * -phi1
                                else if (phi2 <= 8d+20) then
                                    tmp = r * -lambda1
                                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 <= 1.02e-138) {
                            		tmp = R * -phi1;
                            	} else if (phi2 <= 8e+20) {
                            		tmp = R * -lambda1;
                            	} 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 <= 1.02e-138:
                            		tmp = R * -phi1
                            	elif phi2 <= 8e+20:
                            		tmp = R * -lambda1
                            	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 <= 1.02e-138)
                            		tmp = Float64(R * Float64(-phi1));
                            	elseif (phi2 <= 8e+20)
                            		tmp = Float64(R * Float64(-lambda1));
                            	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 <= 1.02e-138)
                            		tmp = R * -phi1;
                            	elseif (phi2 <= 8e+20)
                            		tmp = R * -lambda1;
                            	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, 1.02e-138], N[(R * (-phi1)), $MachinePrecision], If[LessEqual[phi2, 8e+20], N[(R * (-lambda1)), $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 1.02 \cdot 10^{-138}:\\
                            \;\;\;\;R \cdot \left(-\phi_1\right)\\
                            
                            \mathbf{elif}\;\phi_2 \leq 8 \cdot 10^{+20}:\\
                            \;\;\;\;R \cdot \left(-\lambda_1\right)\\
                            
                            \mathbf{else}:\\
                            \;\;\;\;\phi_2 \cdot R\\
                            
                            
                            \end{array}
                            \end{array}
                            
                            Derivation
                            1. Split input into 3 regimes
                            2. if phi2 < 1.02000000000000007e-138

                              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 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.f6422.0

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

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

                              if 1.02000000000000007e-138 < phi2 < 8e20

                              1. Initial program 66.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 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. +-commutativeN/A

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

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

                                  \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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}} \]
                                4. unpow2N/A

                                  \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                                5. unswap-sqrN/A

                                  \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                                6. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

                                  \[\leadsto R \cdot \left(-1 \cdot \lambda_1\right) \]
                                3. Step-by-step derivation
                                  1. Applied rewrites15.4%

                                    \[\leadsto R \cdot \left(-\lambda_1\right) \]

                                  if 8e20 < phi2

                                  1. Initial program 58.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. *-commutativeN/A

                                      \[\leadsto \color{blue}{\phi_2 \cdot R} \]
                                    2. lower-*.f6467.6

                                      \[\leadsto \color{blue}{\phi_2 \cdot R} \]
                                  5. Applied rewrites67.6%

                                    \[\leadsto \color{blue}{\phi_2 \cdot R} \]
                                4. Recombined 3 regimes into one program.
                                5. Add Preprocessing

                                Alternative 10: 60.6% 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_1 \leq -1.08 \cdot 10^{+103}:\\ \;\;\;\;R \cdot \left(-\lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\ \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 (<= lambda1 -1.08e+103) (* R (- lambda1)) (* R (- phi2 phi1))))
                                assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                                double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                                	double tmp;
                                	if (lambda1 <= -1.08e+103) {
                                		tmp = R * -lambda1;
                                	} else {
                                		tmp = R * (phi2 - phi1);
                                	}
                                	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 (lambda1 <= (-1.08d+103)) then
                                        tmp = r * -lambda1
                                    else
                                        tmp = r * (phi2 - phi1)
                                    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 (lambda1 <= -1.08e+103) {
                                		tmp = R * -lambda1;
                                	} else {
                                		tmp = R * (phi2 - phi1);
                                	}
                                	return tmp;
                                }
                                
                                [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                                def code(R, lambda1, lambda2, phi1, phi2):
                                	tmp = 0
                                	if lambda1 <= -1.08e+103:
                                		tmp = R * -lambda1
                                	else:
                                		tmp = R * (phi2 - phi1)
                                	return tmp
                                
                                R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                                function code(R, lambda1, lambda2, phi1, phi2)
                                	tmp = 0.0
                                	if (lambda1 <= -1.08e+103)
                                		tmp = Float64(R * Float64(-lambda1));
                                	else
                                		tmp = Float64(R * Float64(phi2 - phi1));
                                	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 (lambda1 <= -1.08e+103)
                                		tmp = R * -lambda1;
                                	else
                                		tmp = R * (phi2 - phi1);
                                	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[lambda1, -1.08e+103], N[(R * (-lambda1)), $MachinePrecision], N[(R * N[(phi2 - phi1), $MachinePrecision]), $MachinePrecision]]
                                
                                \begin{array}{l}
                                [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                                \\
                                \begin{array}{l}
                                \mathbf{if}\;\lambda_1 \leq -1.08 \cdot 10^{+103}:\\
                                \;\;\;\;R \cdot \left(-\lambda_1\right)\\
                                
                                \mathbf{else}:\\
                                \;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\
                                
                                
                                \end{array}
                                \end{array}
                                
                                Derivation
                                1. Split input into 2 regimes
                                2. if lambda1 < -1.08e103

                                  1. Initial program 52.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 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. +-commutativeN/A

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

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

                                      \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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}} \]
                                    4. unpow2N/A

                                      \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                                    5. unswap-sqrN/A

                                      \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                                    6. lower-hypot.f64N/A

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

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

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

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

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

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

                                    \[\leadsto R \cdot \left(-1 \cdot \color{blue}{\left(\lambda_1 \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right)}\right) \]
                                  7. Step-by-step derivation
                                    1. Applied rewrites56.2%

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

                                      \[\leadsto R \cdot \left(-1 \cdot \lambda_1\right) \]
                                    3. Step-by-step derivation
                                      1. Applied rewrites60.3%

                                        \[\leadsto R \cdot \left(-\lambda_1\right) \]

                                      if -1.08e103 < lambda1

                                      1. Initial program 60.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 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. mul-1-negN/A

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

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

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

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

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

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

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

                                          \[\leadsto \mathsf{neg}\left(\phi_1 \cdot \left(R - \frac{\color{blue}{\phi_2 \cdot R}}{\phi_1}\right)\right) \]
                                        9. lower-*.f6433.7

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

                                        \[\leadsto \color{blue}{-\phi_1 \cdot \left(R - \frac{\phi_2 \cdot R}{\phi_1}\right)} \]
                                      6. Taylor expanded in phi1 around 0

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

                                          \[\leadsto R \cdot \color{blue}{\left(\phi_2 - \phi_1\right)} \]
                                      8. Recombined 2 regimes into one program.
                                      9. Add Preprocessing

                                      Alternative 11: 38.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_2 \leq 8 \cdot 10^{+20}:\\ \;\;\;\;R \cdot \left(-\lambda_1\right)\\ \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 8e+20) (* R (- lambda1)) (* 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 <= 8e+20) {
                                      		tmp = R * -lambda1;
                                      	} 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 <= 8d+20) then
                                              tmp = r * -lambda1
                                          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 <= 8e+20) {
                                      		tmp = R * -lambda1;
                                      	} 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 <= 8e+20:
                                      		tmp = R * -lambda1
                                      	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 <= 8e+20)
                                      		tmp = Float64(R * Float64(-lambda1));
                                      	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 <= 8e+20)
                                      		tmp = R * -lambda1;
                                      	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, 8e+20], N[(R * (-lambda1)), $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 8 \cdot 10^{+20}:\\
                                      \;\;\;\;R \cdot \left(-\lambda_1\right)\\
                                      
                                      \mathbf{else}:\\
                                      \;\;\;\;\phi_2 \cdot R\\
                                      
                                      
                                      \end{array}
                                      \end{array}
                                      
                                      Derivation
                                      1. Split input into 2 regimes
                                      2. if phi2 < 8e20

                                        1. Initial program 58.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 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. +-commutativeN/A

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

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

                                            \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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}} \]
                                          4. unpow2N/A

                                            \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                                          5. unswap-sqrN/A

                                            \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \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)}} \]
                                          6. lower-hypot.f64N/A

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

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

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

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

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

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

                                          \[\leadsto R \cdot \left(-1 \cdot \color{blue}{\left(\lambda_1 \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right)}\right) \]
                                        7. Step-by-step derivation
                                          1. Applied rewrites15.8%

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

                                            \[\leadsto R \cdot \left(-1 \cdot \lambda_1\right) \]
                                          3. Step-by-step derivation
                                            1. Applied rewrites16.0%

                                              \[\leadsto R \cdot \left(-\lambda_1\right) \]

                                            if 8e20 < phi2

                                            1. Initial program 58.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. *-commutativeN/A

                                                \[\leadsto \color{blue}{\phi_2 \cdot R} \]
                                              2. lower-*.f6467.6

                                                \[\leadsto \color{blue}{\phi_2 \cdot R} \]
                                            5. Applied rewrites67.6%

                                              \[\leadsto \color{blue}{\phi_2 \cdot R} \]
                                          4. Recombined 2 regimes into one program.
                                          5. Add Preprocessing

                                          Alternative 12: 32.0% accurate, 46.5× speedup?

                                          \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \phi_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 (* phi2 R))
                                          assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                                          double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                                          	return phi2 * 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 = phi2 * 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 phi2 * R;
                                          }
                                          
                                          [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                                          def code(R, lambda1, lambda2, phi1, phi2):
                                          	return phi2 * R
                                          
                                          R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                                          function code(R, lambda1, lambda2, phi1, phi2)
                                          	return Float64(phi2 * R)
                                          end
                                          
                                          R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
                                          function tmp = code(R, lambda1, lambda2, phi1, phi2)
                                          	tmp = phi2 * 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[(phi2 * R), $MachinePrecision]
                                          
                                          \begin{array}{l}
                                          [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                                          \\
                                          \phi_2 \cdot R
                                          \end{array}
                                          
                                          Derivation
                                          1. Initial program 58.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 phi2 around inf

                                            \[\leadsto \color{blue}{R \cdot \phi_2} \]
                                          4. Step-by-step derivation
                                            1. *-commutativeN/A

                                              \[\leadsto \color{blue}{\phi_2 \cdot R} \]
                                            2. lower-*.f6416.8

                                              \[\leadsto \color{blue}{\phi_2 \cdot R} \]
                                          5. Applied rewrites16.8%

                                            \[\leadsto \color{blue}{\phi_2 \cdot R} \]
                                          6. Add Preprocessing

                                          Reproduce

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