Equirectangular approximation to distance on a great circle

Percentage Accurate: 59.1% → 90.6%
Time: 14.9s
Alternatives: 13
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 13 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: 90.6% 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 3.3 \cdot 10^{-22}:\\ \;\;\;\;\mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right) \cdot R\\ \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 3.3e-22)
   (* (hypot phi1 (* (- lambda1 lambda2) (cos (* phi1 0.5)))) R)
   (* 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 <= 3.3e-22) {
		tmp = hypot(phi1, ((lambda1 - lambda2) * cos((phi1 * 0.5)))) * R;
	} 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 <= 3.3e-22) {
		tmp = Math.hypot(phi1, ((lambda1 - lambda2) * Math.cos((phi1 * 0.5)))) * R;
	} 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 <= 3.3e-22:
		tmp = math.hypot(phi1, ((lambda1 - lambda2) * math.cos((phi1 * 0.5)))) * R
	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 <= 3.3e-22)
		tmp = Float64(hypot(phi1, Float64(Float64(lambda1 - lambda2) * cos(Float64(phi1 * 0.5)))) * R);
	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 <= 3.3e-22)
		tmp = hypot(phi1, ((lambda1 - lambda2) * cos((phi1 * 0.5)))) * R;
	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, 3.3e-22], N[(N[Sqrt[phi1 ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(phi1 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision] * R), $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 3.3 \cdot 10^{-22}:\\
\;\;\;\;\mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right) \cdot R\\

\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 < 3.3000000000000001e-22

    1. Initial program 59.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 phi2 around 0

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

    if 3.3000000000000001e-22 < phi2

    1. Initial program 52.5%

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

\mathbf{elif}\;\phi_2 \leq 2.8 \cdot 10^{+130}:\\
\;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \left(\lambda_1 - \lambda_2\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right)\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_2 \cdot \cos \left(\phi_2 \cdot 0.5\right)\right)\\


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

    1. Initial program 59.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 phi2 around 0

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

    if 4.4000000000000002e-14 < phi2 < 2.7999999999999999e130

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

        \[\leadsto R \cdot \sqrt{\left(\color{blue}{\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. lift-+.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\color{blue}{\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)} \]
      3. lift-/.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \color{blue}{\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)} \]
      4. lift-cos.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\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)} \]
      5. lift-*.f64N/A

        \[\leadsto R \cdot \sqrt{\color{blue}{\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)} \]
      6. lift--.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\color{blue}{\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)} \]
      7. lift-+.f64N/A

        \[\leadsto 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{\color{blue}{\phi_1 + \phi_2}}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      8. lift-/.f64N/A

        \[\leadsto 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 \color{blue}{\left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lift-cos.f64N/A

        \[\leadsto 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 \color{blue}{\cos \left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      10. lift-*.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \color{blue}{\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)} \]
      11. lift--.f64N/A

        \[\leadsto 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) + \color{blue}{\left(\phi_1 - \phi_2\right)} \cdot \left(\phi_1 - \phi_2\right)} \]
      12. lift--.f64N/A

        \[\leadsto 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 \color{blue}{\left(\phi_1 - \phi_2\right)}} \]
    4. Applied egg-rr67.0%

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

    if 2.7999999999999999e130 < phi2

    1. Initial program 41.5%

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_2, \lambda_2 \cdot \cos \color{blue}{\left(0.5 \cdot \phi_2\right)}\right) \cdot R \]
    8. Simplified84.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 4.4 \cdot 10^{-14}:\\ \;\;\;\;\mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 2.8 \cdot 10^{+130}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \left(\lambda_1 - \lambda_2\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right)\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_2 \cdot \cos \left(\phi_2 \cdot 0.5\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 81.8% 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 -2.75 \cdot 10^{+45}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\ \mathbf{elif}\;\phi_1 \leq -5.2 \cdot 10^{-16}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \left(\lambda_1 - \lambda_2\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right)\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 (<= phi1 -2.75e+45)
   (* R (hypot phi1 (* lambda1 (cos (* phi1 0.5)))))
   (if (<= phi1 -5.2e-16)
     (*
      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 (phi1 <= -2.75e+45) {
		tmp = R * hypot(phi1, (lambda1 * cos((phi1 * 0.5))));
	} else if (phi1 <= -5.2e-16) {
		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 (phi1 <= -2.75e+45)
		tmp = Float64(R * hypot(phi1, Float64(lambda1 * cos(Float64(phi1 * 0.5)))));
	elseif (phi1 <= -5.2e-16)
		tmp = Float64(R * sqrt(fma(Float64(lambda1 - lambda2), Float64(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[phi1, -2.75e+45], N[(R * N[Sqrt[phi1 ^ 2 + N[(lambda1 * N[Cos[N[(phi1 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, -5.2e-16], N[(R * N[Sqrt[N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(0.5 + N[(0.5 * N[Cos[N[(phi2 + phi1), $MachinePrecision]], $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_1 \leq -2.75 \cdot 10^{+45}:\\
\;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\

\mathbf{elif}\;\phi_1 \leq -5.2 \cdot 10^{-16}:\\
\;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \left(\lambda_1 - \lambda_2\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right)\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 phi1 < -2.75e45

    1. Initial program 48.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 phi2 around 0

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_1, \lambda_1 \cdot \cos \color{blue}{\left(0.5 \cdot \phi_1\right)}\right) \cdot R \]
    8. Simplified59.8%

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

    if -2.75e45 < phi1 < -5.1999999999999997e-16

    1. Initial program 61.6%

      \[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 R \cdot \sqrt{\left(\color{blue}{\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. lift-+.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\color{blue}{\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)} \]
      3. lift-/.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \color{blue}{\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)} \]
      4. lift-cos.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\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)} \]
      5. lift-*.f64N/A

        \[\leadsto R \cdot \sqrt{\color{blue}{\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)} \]
      6. lift--.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\color{blue}{\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)} \]
      7. lift-+.f64N/A

        \[\leadsto 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{\color{blue}{\phi_1 + \phi_2}}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      8. lift-/.f64N/A

        \[\leadsto 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 \color{blue}{\left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lift-cos.f64N/A

        \[\leadsto 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 \color{blue}{\cos \left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      10. lift-*.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \color{blue}{\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)} \]
      11. lift--.f64N/A

        \[\leadsto 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) + \color{blue}{\left(\phi_1 - \phi_2\right)} \cdot \left(\phi_1 - \phi_2\right)} \]
      12. lift--.f64N/A

        \[\leadsto 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 \color{blue}{\left(\phi_1 - \phi_2\right)}} \]
    4. Applied egg-rr61.6%

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

    if -5.1999999999999997e-16 < phi1

    1. Initial program 60.5%

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_2, \color{blue}{\lambda_1 - \lambda_2}\right) \cdot R \]
    8. Simplified66.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.75 \cdot 10^{+45}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\ \mathbf{elif}\;\phi_1 \leq -5.2 \cdot 10^{-16}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \left(\lambda_1 - \lambda_2\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right)\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} \]
  5. Add Preprocessing

Alternative 4: 82.2% 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_1 \leq -1.75 \cdot 10^{+145}:\\ \;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq -5.2 \cdot 10^{-16}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \left(\lambda_1 - \lambda_2\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right)\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 (<= phi1 -1.75e+145)
   (* R (- phi2 phi1))
   (if (<= phi1 -5.2e-16)
     (*
      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 (phi1 <= -1.75e+145) {
		tmp = R * (phi2 - phi1);
	} else if (phi1 <= -5.2e-16) {
		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 (phi1 <= -1.75e+145)
		tmp = Float64(R * Float64(phi2 - phi1));
	elseif (phi1 <= -5.2e-16)
		tmp = Float64(R * sqrt(fma(Float64(lambda1 - lambda2), Float64(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[phi1, -1.75e+145], N[(R * N[(phi2 - phi1), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, -5.2e-16], N[(R * N[Sqrt[N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(0.5 + N[(0.5 * N[Cos[N[(phi2 + phi1), $MachinePrecision]], $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_1 \leq -1.75 \cdot 10^{+145}:\\
\;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\

\mathbf{elif}\;\phi_1 \leq -5.2 \cdot 10^{-16}:\\
\;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \left(\lambda_1 - \lambda_2\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right)\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 phi1 < -1.7500000000000001e145

    1. Initial program 39.4%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in 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-/.f6459.0

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

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

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

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

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

        \[\leadsto \color{blue}{R \cdot \phi_2 - R \cdot \phi_1} \]
      4. distribute-lft-out--N/A

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

        \[\leadsto \color{blue}{R \cdot \left(\phi_2 - \phi_1\right)} \]
      6. lower--.f6465.4

        \[\leadsto R \cdot \color{blue}{\left(\phi_2 - \phi_1\right)} \]
    8. Simplified65.4%

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

    if -1.7500000000000001e145 < phi1 < -5.1999999999999997e-16

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

        \[\leadsto R \cdot \sqrt{\left(\color{blue}{\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. lift-+.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\color{blue}{\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)} \]
      3. lift-/.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \color{blue}{\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)} \]
      4. lift-cos.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\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)} \]
      5. lift-*.f64N/A

        \[\leadsto R \cdot \sqrt{\color{blue}{\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)} \]
      6. lift--.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\color{blue}{\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)} \]
      7. lift-+.f64N/A

        \[\leadsto 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{\color{blue}{\phi_1 + \phi_2}}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      8. lift-/.f64N/A

        \[\leadsto 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 \color{blue}{\left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lift-cos.f64N/A

        \[\leadsto 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 \color{blue}{\cos \left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      10. lift-*.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \color{blue}{\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)} \]
      11. lift--.f64N/A

        \[\leadsto 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) + \color{blue}{\left(\phi_1 - \phi_2\right)} \cdot \left(\phi_1 - \phi_2\right)} \]
      12. lift--.f64N/A

        \[\leadsto 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 \color{blue}{\left(\phi_1 - \phi_2\right)}} \]
    4. Applied egg-rr61.1%

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

    if -5.1999999999999997e-16 < phi1

    1. Initial program 60.5%

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_2, \color{blue}{\lambda_1 - \lambda_2}\right) \cdot R \]
    8. Simplified66.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.75 \cdot 10^{+145}:\\ \;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq -5.2 \cdot 10^{-16}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \left(\lambda_1 - \lambda_2\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right)\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} \]
  5. Add Preprocessing

Alternative 5: 81.1% 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_1 \leq -1.75 \cdot 10^{+145}:\\ \;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq -10500000:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \left(\lambda_1 - \lambda_2\right) \cdot \mathsf{fma}\left(0.5, \cos \phi_1, 0.5\right), \phi_1 \cdot \phi_1\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 -1.75e+145)
   (* R (- phi2 phi1))
   (if (<= phi1 -10500000.0)
     (*
      R
      (sqrt
       (fma
        (- lambda1 lambda2)
        (* (- lambda1 lambda2) (fma 0.5 (cos phi1) 0.5))
        (* phi1 phi1))))
     (* 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 <= -1.75e+145) {
		tmp = R * (phi2 - phi1);
	} else if (phi1 <= -10500000.0) {
		tmp = R * sqrt(fma((lambda1 - lambda2), ((lambda1 - lambda2) * fma(0.5, cos(phi1), 0.5)), (phi1 * phi1)));
	} 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 (phi1 <= -1.75e+145)
		tmp = Float64(R * Float64(phi2 - phi1));
	elseif (phi1 <= -10500000.0)
		tmp = Float64(R * sqrt(fma(Float64(lambda1 - lambda2), Float64(Float64(lambda1 - lambda2) * fma(0.5, cos(phi1), 0.5)), Float64(phi1 * phi1))));
	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[phi1, -1.75e+145], N[(R * N[(phi2 - phi1), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, -10500000.0], N[(R * N[Sqrt[N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(0.5 * N[Cos[phi1], $MachinePrecision] + 0.5), $MachinePrecision]), $MachinePrecision] + N[(phi1 * phi1), $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_1 \leq -1.75 \cdot 10^{+145}:\\
\;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\

\mathbf{elif}\;\phi_1 \leq -10500000:\\
\;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \left(\lambda_1 - \lambda_2\right) \cdot \mathsf{fma}\left(0.5, \cos \phi_1, 0.5\right), \phi_1 \cdot \phi_1\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 phi1 < -1.7500000000000001e145

    1. Initial program 39.4%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in 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-/.f6459.0

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

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

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

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

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

        \[\leadsto \color{blue}{R \cdot \phi_2 - R \cdot \phi_1} \]
      4. distribute-lft-out--N/A

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

        \[\leadsto \color{blue}{R \cdot \left(\phi_2 - \phi_1\right)} \]
      6. lower--.f6465.4

        \[\leadsto R \cdot \color{blue}{\left(\phi_2 - \phi_1\right)} \]
    8. Simplified65.4%

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

    if -1.7500000000000001e145 < phi1 < -1.05e7

    1. Initial program 63.3%

      \[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. Applied egg-rr63.1%

      \[\leadsto R \cdot \color{blue}{\frac{1}{\sqrt{\frac{1}{\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)}}}} \]
    4. Taylor expanded in phi2 around 0

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \cos \phi_1, \frac{1}{2}\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \color{blue}{\phi_1 \cdot \phi_1}\right)} \]
      12. lower-*.f6450.9

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos \phi_1, 0.5\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \color{blue}{\phi_1 \cdot \phi_1}\right)} \]
    6. Simplified50.9%

      \[\leadsto \color{blue}{R \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos \phi_1, 0.5\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 \cdot \phi_1\right)}} \]
    7. Step-by-step derivation
      1. lift-cos.f64N/A

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos \phi_1, 0.5\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 \cdot \phi_1\right)} \cdot R} \]
    8. Applied egg-rr50.9%

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

    if -1.05e7 < phi1

    1. Initial program 60.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 \color{blue}{R \cdot \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 \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}} \cdot R} \]
      2. lower-*.f64N/A

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_2, \color{blue}{\lambda_1 - \lambda_2}\right) \cdot R \]
    8. Simplified67.0%

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

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

Alternative 6: 81.1% 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_1 \leq -1.75 \cdot 10^{+145}:\\ \;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq -10500000:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos \phi_1, 0.5\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 \cdot \phi_1\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 -1.75e+145)
   (* R (- phi2 phi1))
   (if (<= phi1 -10500000.0)
     (*
      R
      (sqrt
       (fma
        (fma 0.5 (cos phi1) 0.5)
        (* (- lambda1 lambda2) (- lambda1 lambda2))
        (* phi1 phi1))))
     (* 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 <= -1.75e+145) {
		tmp = R * (phi2 - phi1);
	} else if (phi1 <= -10500000.0) {
		tmp = R * sqrt(fma(fma(0.5, cos(phi1), 0.5), ((lambda1 - lambda2) * (lambda1 - lambda2)), (phi1 * phi1)));
	} 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 (phi1 <= -1.75e+145)
		tmp = Float64(R * Float64(phi2 - phi1));
	elseif (phi1 <= -10500000.0)
		tmp = Float64(R * sqrt(fma(fma(0.5, cos(phi1), 0.5), Float64(Float64(lambda1 - lambda2) * Float64(lambda1 - lambda2)), Float64(phi1 * phi1))));
	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[phi1, -1.75e+145], N[(R * N[(phi2 - phi1), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, -10500000.0], N[(R * N[Sqrt[N[(N[(0.5 * N[Cos[phi1], $MachinePrecision] + 0.5), $MachinePrecision] * N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] + N[(phi1 * phi1), $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_1 \leq -1.75 \cdot 10^{+145}:\\
\;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\

\mathbf{elif}\;\phi_1 \leq -10500000:\\
\;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos \phi_1, 0.5\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 \cdot \phi_1\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 phi1 < -1.7500000000000001e145

    1. Initial program 39.4%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in 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-/.f6459.0

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

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

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

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

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

        \[\leadsto \color{blue}{R \cdot \phi_2 - R \cdot \phi_1} \]
      4. distribute-lft-out--N/A

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

        \[\leadsto \color{blue}{R \cdot \left(\phi_2 - \phi_1\right)} \]
      6. lower--.f6465.4

        \[\leadsto R \cdot \color{blue}{\left(\phi_2 - \phi_1\right)} \]
    8. Simplified65.4%

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

    if -1.7500000000000001e145 < phi1 < -1.05e7

    1. Initial program 63.3%

      \[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. Applied egg-rr63.1%

      \[\leadsto R \cdot \color{blue}{\frac{1}{\sqrt{\frac{1}{\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)}}}} \]
    4. Taylor expanded in phi2 around 0

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(\frac{1}{2}, \cos \phi_1, \frac{1}{2}\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \color{blue}{\phi_1 \cdot \phi_1}\right)} \]
      12. lower-*.f6450.9

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\mathsf{fma}\left(0.5, \cos \phi_1, 0.5\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \color{blue}{\phi_1 \cdot \phi_1}\right)} \]
    6. Simplified50.9%

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

    if -1.05e7 < phi1

    1. Initial program 60.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 \color{blue}{R \cdot \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 \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}} \cdot R} \]
      2. lower-*.f64N/A

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_2, \color{blue}{\lambda_1 - \lambda_2}\right) \cdot R \]
    8. Simplified67.0%

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

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

Alternative 7: 77.9% accurate, 2.4× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.8 \cdot 10^{+32}:\\ \;\;\;\;\phi_2 \cdot \mathsf{fma}\left(R \cdot \left(-\phi_1\right), \frac{1}{\phi_2}, R\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 -2.8e+32)
   (* phi2 (fma (* R (- phi1)) (/ 1.0 phi2) R))
   (* 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 <= -2.8e+32) {
		tmp = phi2 * fma((R * -phi1), (1.0 / phi2), R);
	} 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 (phi1 <= -2.8e+32)
		tmp = Float64(phi2 * fma(Float64(R * Float64(-phi1)), Float64(1.0 / phi2), R));
	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[phi1, -2.8e+32], N[(phi2 * N[(N[(R * (-phi1)), $MachinePrecision] * N[(1.0 / phi2), $MachinePrecision] + R), $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 -2.8 \cdot 10^{+32}:\\
\;\;\;\;\phi_2 \cdot \mathsf{fma}\left(R \cdot \left(-\phi_1\right), \frac{1}{\phi_2}, R\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 < -2.8e32

    1. Initial program 51.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-/.f6454.0

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

      \[\leadsto \color{blue}{\phi_2 \cdot \left(R - \phi_1 \cdot \frac{R}{\phi_2}\right)} \]
    6. Step-by-step derivation
      1. lift-/.f64N/A

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \phi_2 \cdot \mathsf{fma}\left(\color{blue}{\left(\mathsf{neg}\left(\phi_1\right)\right)} \cdot R, \frac{1}{\phi_2}, R\right) \]
      13. lower-/.f6459.0

        \[\leadsto \phi_2 \cdot \mathsf{fma}\left(\left(-\phi_1\right) \cdot R, \color{blue}{\frac{1}{\phi_2}}, R\right) \]
    7. Applied egg-rr59.0%

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

    if -2.8e32 < phi1

    1. Initial program 60.0%

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

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_2, \color{blue}{\lambda_1 - \lambda_2}\right) \cdot R \]
    8. Simplified66.3%

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

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

Alternative 8: 62.4% 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 -2.5 \cdot 10^{+152}:\\ \;\;\;\;\left(-\lambda_1\right) \cdot \mathsf{fma}\left(R, \frac{\lambda_2}{-\lambda_1}, R\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) -2.5e+152)
   (* (- lambda1) (fma R (/ lambda2 (- lambda1)) R))
   (* 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) <= -2.5e+152) {
		tmp = -lambda1 * fma(R, (lambda2 / -lambda1), R);
	} 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) <= -2.5e+152)
		tmp = Float64(Float64(-lambda1) * fma(R, Float64(lambda2 / Float64(-lambda1)), R));
	else
		tmp = Float64(R * Float64(phi2 - phi1));
	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[N[(lambda1 - lambda2), $MachinePrecision], -2.5e+152], N[((-lambda1) * N[(R * N[(lambda2 / (-lambda1)), $MachinePrecision] + R), $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 -2.5 \cdot 10^{+152}:\\
\;\;\;\;\left(-\lambda_1\right) \cdot \mathsf{fma}\left(R, \frac{\lambda_2}{-\lambda_1}, R\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) < -2.5e152

    1. Initial program 44.3%

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_2, \color{blue}{\lambda_1 - \lambda_2}\right) \cdot R \]
    8. Simplified64.7%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \left(\mathsf{neg}\left(\lambda_1\right)\right) \cdot \color{blue}{\mathsf{fma}\left(R, -1 \cdot \frac{\lambda_2}{\lambda_1}, R\right)} \]
      11. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(\lambda_1\right)\right) \cdot \mathsf{fma}\left(R, \color{blue}{\mathsf{neg}\left(\frac{\lambda_2}{\lambda_1}\right)}, R\right) \]
      12. distribute-neg-frac2N/A

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

        \[\leadsto \left(\mathsf{neg}\left(\lambda_1\right)\right) \cdot \mathsf{fma}\left(R, \frac{\lambda_2}{\color{blue}{-1 \cdot \lambda_1}}, R\right) \]
      14. lower-/.f64N/A

        \[\leadsto \left(\mathsf{neg}\left(\lambda_1\right)\right) \cdot \mathsf{fma}\left(R, \color{blue}{\frac{\lambda_2}{-1 \cdot \lambda_1}}, R\right) \]
      15. mul-1-negN/A

        \[\leadsto \left(\mathsf{neg}\left(\lambda_1\right)\right) \cdot \mathsf{fma}\left(R, \frac{\lambda_2}{\color{blue}{\mathsf{neg}\left(\lambda_1\right)}}, R\right) \]
      16. lower-neg.f6456.3

        \[\leadsto \left(-\lambda_1\right) \cdot \mathsf{fma}\left(R, \frac{\lambda_2}{\color{blue}{-\lambda_1}}, R\right) \]
    11. Simplified56.3%

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

    if -2.5e152 < (-.f64 lambda1 lambda2)

    1. Initial program 62.4%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in 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-/.f6427.6

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

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

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

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

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

        \[\leadsto \color{blue}{R \cdot \phi_2 - R \cdot \phi_1} \]
      4. distribute-lft-out--N/A

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

        \[\leadsto \color{blue}{R \cdot \left(\phi_2 - \phi_1\right)} \]
      6. lower--.f6428.1

        \[\leadsto R \cdot \color{blue}{\left(\phi_2 - \phi_1\right)} \]
    8. Simplified28.1%

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

Alternative 9: 62.4% accurate, 8.2× 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 -4 \cdot 10^{+194}:\\ \;\;\;\;R \cdot \left(\lambda_2 \cdot \left(1 - \frac{\lambda_1}{\lambda_2}\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) -4e+194)
   (* R (* lambda2 (- 1.0 (/ lambda1 lambda2))))
   (* 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) <= -4e+194) {
		tmp = R * (lambda2 * (1.0 - (lambda1 / lambda2)));
	} 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) <= (-4d+194)) then
        tmp = r * (lambda2 * (1.0d0 - (lambda1 / lambda2)))
    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) <= -4e+194) {
		tmp = R * (lambda2 * (1.0 - (lambda1 / lambda2)));
	} 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) <= -4e+194:
		tmp = R * (lambda2 * (1.0 - (lambda1 / lambda2)))
	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) <= -4e+194)
		tmp = Float64(R * Float64(lambda2 * Float64(1.0 - Float64(lambda1 / lambda2))));
	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) <= -4e+194)
		tmp = R * (lambda2 * (1.0 - (lambda1 / lambda2)));
	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], -4e+194], N[(R * N[(lambda2 * N[(1.0 - N[(lambda1 / lambda2), $MachinePrecision]), $MachinePrecision]), $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 -4 \cdot 10^{+194}:\\
\;\;\;\;R \cdot \left(\lambda_2 \cdot \left(1 - \frac{\lambda_1}{\lambda_2}\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) < -3.99999999999999978e194

    1. Initial program 46.4%

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

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_2, \color{blue}{\lambda_1 - \lambda_2}\right) \cdot R \]
    8. Simplified66.9%

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

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

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

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

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

        \[\leadsto \left(\lambda_2 \cdot \color{blue}{\left(1 - \frac{\lambda_1}{\lambda_2}\right)}\right) \cdot R \]
      5. lower-/.f6460.3

        \[\leadsto \left(\lambda_2 \cdot \left(1 - \color{blue}{\frac{\lambda_1}{\lambda_2}}\right)\right) \cdot R \]
    11. Simplified60.3%

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

    if -3.99999999999999978e194 < (-.f64 lambda1 lambda2)

    1. Initial program 61.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 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-/.f6427.6

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

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

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

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

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

        \[\leadsto \color{blue}{R \cdot \phi_2 - R \cdot \phi_1} \]
      4. distribute-lft-out--N/A

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

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

        \[\leadsto R \cdot \color{blue}{\left(\phi_2 - \phi_1\right)} \]
    8. Simplified27.6%

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

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

Alternative 10: 47.6% accurate, 15.5× 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.6 \cdot 10^{-282}:\\ \;\;\;\;R \cdot \left(-\phi_1\right)\\ \mathbf{elif}\;\phi_2 \leq 1.18 \cdot 10^{-5}:\\ \;\;\;\;\lambda_2 \cdot R\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 -1.6e-282)
   (* R (- phi1))
   (if (<= phi2 1.18e-5) (* lambda2 R) (* phi2 R))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= -1.6e-282) {
		tmp = R * -phi1;
	} else if (phi2 <= 1.18e-5) {
		tmp = lambda2 * R;
	} else {
		tmp = phi2 * R;
	}
	return tmp;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (phi2 <= (-1.6d-282)) then
        tmp = r * -phi1
    else if (phi2 <= 1.18d-5) then
        tmp = lambda2 * r
    else
        tmp = phi2 * r
    end if
    code = tmp
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= -1.6e-282) {
		tmp = R * -phi1;
	} else if (phi2 <= 1.18e-5) {
		tmp = lambda2 * R;
	} else {
		tmp = phi2 * R;
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= -1.6e-282:
		tmp = R * -phi1
	elif phi2 <= 1.18e-5:
		tmp = lambda2 * R
	else:
		tmp = phi2 * R
	return tmp
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= -1.6e-282)
		tmp = Float64(R * Float64(-phi1));
	elseif (phi2 <= 1.18e-5)
		tmp = Float64(lambda2 * R);
	else
		tmp = Float64(phi2 * R);
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= -1.6e-282)
		tmp = R * -phi1;
	elseif (phi2 <= 1.18e-5)
		tmp = lambda2 * R;
	else
		tmp = phi2 * R;
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, -1.6e-282], N[(R * (-phi1)), $MachinePrecision], If[LessEqual[phi2, 1.18e-5], N[(lambda2 * R), $MachinePrecision], N[(phi2 * R), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq -1.6 \cdot 10^{-282}:\\
\;\;\;\;R \cdot \left(-\phi_1\right)\\

\mathbf{elif}\;\phi_2 \leq 1.18 \cdot 10^{-5}:\\
\;\;\;\;\lambda_2 \cdot R\\

\mathbf{else}:\\
\;\;\;\;\phi_2 \cdot R\\


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

    1. Initial program 53.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(R \cdot \phi_1\right)} \]
    4. Step-by-step derivation
      1. mul-1-negN/A

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

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

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

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

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

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

        \[\leadsto \phi_1 \cdot \color{blue}{\left(-R\right)} \]
    5. Simplified15.2%

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

    if -1.59999999999999991e-282 < phi2 < 1.18000000000000005e-5

    1. Initial program 71.5%

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

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{R \cdot \lambda_2} \]
    10. Step-by-step derivation
      1. lower-*.f6425.7

        \[\leadsto \color{blue}{R \cdot \lambda_2} \]
    11. Simplified25.7%

      \[\leadsto \color{blue}{R \cdot \lambda_2} \]

    if 1.18000000000000005e-5 < phi2

    1. Initial program 52.5%

      \[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-*.f6458.7

        \[\leadsto \color{blue}{\phi_2 \cdot R} \]
    5. Simplified58.7%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.6 \cdot 10^{-282}:\\ \;\;\;\;R \cdot \left(-\phi_1\right)\\ \mathbf{elif}\;\phi_2 \leq 1.18 \cdot 10^{-5}:\\ \;\;\;\;\lambda_2 \cdot R\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \]
  5. Add Preprocessing

Alternative 11: 60.4% 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 -9 \cdot 10^{+154}:\\ \;\;\;\;-\lambda_1 \cdot R\\ \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 -9e+154) (- (* lambda1 R)) (* 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 <= -9e+154) {
		tmp = -(lambda1 * R);
	} 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 <= (-9d+154)) then
        tmp = -(lambda1 * r)
    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 <= -9e+154) {
		tmp = -(lambda1 * R);
	} 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 <= -9e+154:
		tmp = -(lambda1 * R)
	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 <= -9e+154)
		tmp = Float64(-Float64(lambda1 * R));
	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 <= -9e+154)
		tmp = -(lambda1 * R);
	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, -9e+154], (-N[(lambda1 * R), $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 -9 \cdot 10^{+154}:\\
\;\;\;\;-\lambda_1 \cdot R\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\


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

    1. Initial program 56.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 \color{blue}{R \cdot \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 \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}} \cdot R} \]
      2. lower-*.f64N/A

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_2, \color{blue}{\lambda_1 - \lambda_2}\right) \cdot R \]
    8. Simplified73.0%

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

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

        \[\leadsto \color{blue}{\left(\mathsf{neg}\left(\lambda_1\right)\right)} \cdot R \]
      2. lower-neg.f6467.3

        \[\leadsto \color{blue}{\left(-\lambda_1\right)} \cdot R \]
    11. Simplified67.3%

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

    if -9.00000000000000018e154 < lambda1

    1. Initial program 58.3%

      \[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-/.f6427.1

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

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

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

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

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

        \[\leadsto \color{blue}{R \cdot \phi_2 - R \cdot \phi_1} \]
      4. distribute-lft-out--N/A

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

        \[\leadsto \color{blue}{R \cdot \left(\phi_2 - \phi_1\right)} \]
      6. lower--.f6426.2

        \[\leadsto R \cdot \color{blue}{\left(\phi_2 - \phi_1\right)} \]
    8. Simplified26.2%

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

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

Alternative 12: 37.1% accurate, 23.2× speedup?

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

\mathbf{else}:\\
\;\;\;\;\phi_2 \cdot R\\


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

    1. Initial program 59.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 \color{blue}{R \cdot \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 \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}} \cdot R} \]
      2. lower-*.f64N/A

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

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

        \[\leadsto \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}} \cdot R \]
      5. unpow2N/A

        \[\leadsto \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}} \cdot R \]
      6. unpow2N/A

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

        \[\leadsto \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)}} \cdot R \]
      8. lower-hypot.f64N/A

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

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

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_2, \color{blue}{\lambda_1 - \lambda_2}\right) \cdot R \]
    8. Simplified62.1%

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

      \[\leadsto \color{blue}{R \cdot \lambda_2} \]
    10. Step-by-step derivation
      1. lower-*.f6414.2

        \[\leadsto \color{blue}{R \cdot \lambda_2} \]
    11. Simplified14.2%

      \[\leadsto \color{blue}{R \cdot \lambda_2} \]

    if 1.18000000000000005e-5 < phi2

    1. Initial program 52.5%

      \[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-*.f6458.7

        \[\leadsto \color{blue}{\phi_2 \cdot R} \]
    5. Simplified58.7%

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

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

Alternative 13: 14.1% accurate, 46.5× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \lambda_2 \cdot R \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2) :precision binary64 (* lambda2 R))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return lambda2 * R;
}
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = lambda2 * r
end function
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return lambda2 * R;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	return lambda2 * R
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(lambda2 * R)
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = lambda2 * R;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(lambda2 * R), $MachinePrecision]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\lambda_2 \cdot R
\end{array}
Derivation
  1. Initial program 58.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 \color{blue}{R \cdot \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 \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}} \cdot R} \]
    2. lower-*.f64N/A

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

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

      \[\leadsto \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}} \cdot R \]
    5. unpow2N/A

      \[\leadsto \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}} \cdot R \]
    6. unpow2N/A

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

      \[\leadsto \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)}} \cdot R \]
    8. lower-hypot.f64N/A

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

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

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

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

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

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

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

      \[\leadsto \mathsf{hypot}\left(\phi_2, \color{blue}{\lambda_1 - \lambda_2}\right) \cdot R \]
  8. Simplified62.9%

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

    \[\leadsto \color{blue}{R \cdot \lambda_2} \]
  10. Step-by-step derivation
    1. lower-*.f6413.5

      \[\leadsto \color{blue}{R \cdot \lambda_2} \]
  11. Simplified13.5%

    \[\leadsto \color{blue}{R \cdot \lambda_2} \]
  12. Final simplification13.5%

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

Reproduce

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