Equirectangular approximation to distance on a great circle

Percentage Accurate: 59.1% → 91.0%
Time: 6.3s
Alternatives: 9
Speedup: 13.9×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 9 alternatives:

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

Initial Program: 59.1% accurate, 1.0× speedup?

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

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

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

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


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

    1. Initial program 69.2%

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

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

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

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

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

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

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

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

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

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

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

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

    if 7.49999999999999972e-36 < phi2

    1. Initial program 50.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 85.1% accurate, 1.2× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 7.5 \cdot 10^{-36}:\\ \;\;\;\;\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right), \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \lambda_1, \phi_2\right) \cdot R\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 7.5e-36)
   (* (hypot (* (- lambda1 lambda2) (cos (* 0.5 phi1))) phi1) R)
   (* (hypot (* (cos (* 0.5 phi2)) lambda1) phi2) R)))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 7.5e-36) {
		tmp = hypot(((lambda1 - lambda2) * cos((0.5 * phi1))), phi1) * R;
	} else {
		tmp = hypot((cos((0.5 * phi2)) * lambda1), phi2) * R;
	}
	return tmp;
}
assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 7.5e-36) {
		tmp = Math.hypot(((lambda1 - lambda2) * Math.cos((0.5 * phi1))), phi1) * R;
	} else {
		tmp = Math.hypot((Math.cos((0.5 * phi2)) * lambda1), 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 <= 7.5e-36:
		tmp = math.hypot(((lambda1 - lambda2) * math.cos((0.5 * phi1))), phi1) * R
	else:
		tmp = math.hypot((math.cos((0.5 * phi2)) * lambda1), 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 <= 7.5e-36)
		tmp = Float64(hypot(Float64(Float64(lambda1 - lambda2) * cos(Float64(0.5 * phi1))), phi1) * R);
	else
		tmp = Float64(hypot(Float64(cos(Float64(0.5 * phi2)) * lambda1), 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 <= 7.5e-36)
		tmp = hypot(((lambda1 - lambda2) * cos((0.5 * phi1))), phi1) * R;
	else
		tmp = hypot((cos((0.5 * phi2)) * lambda1), 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, 7.5e-36], N[(N[Sqrt[N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2 + phi1 ^ 2], $MachinePrecision] * R), $MachinePrecision], N[(N[Sqrt[N[(N[Cos[N[(0.5 * phi2), $MachinePrecision]], $MachinePrecision] * lambda1), $MachinePrecision] ^ 2 + phi2 ^ 2], $MachinePrecision] * R), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 7.5 \cdot 10^{-36}:\\
\;\;\;\;\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right), \phi_1\right) \cdot R\\

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


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

    1. Initial program 69.2%

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

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

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

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

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

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

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

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

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

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

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

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

    if 7.49999999999999972e-36 < phi2

    1. Initial program 50.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      1. Initial program 57.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 -inf

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

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

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

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

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

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

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

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

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

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

      if -3.6000000000000001e42 < phi1

      1. Initial program 64.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 R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}}} \]
      4. Step-by-step derivation
        1. unpow2N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

      Alternative 4: 80.6% accurate, 2.3× speedup?

      \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 5.6 \cdot 10^{+26}:\\ \;\;\;\;\mathsf{hypot}\left(1 \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\ \end{array} \end{array} \]
      NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
      (FPCore (R lambda1 lambda2 phi1 phi2)
       :precision binary64
       (if (<= phi2 5.6e+26)
         (* (hypot (* 1.0 (- lambda1 lambda2)) phi1) R)
         (fma (- R) phi1 (* R phi2))))
      assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
      double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
      	double tmp;
      	if (phi2 <= 5.6e+26) {
      		tmp = hypot((1.0 * (lambda1 - lambda2)), phi1) * R;
      	} else {
      		tmp = fma(-R, phi1, (R * phi2));
      	}
      	return tmp;
      }
      
      R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
      function code(R, lambda1, lambda2, phi1, phi2)
      	tmp = 0.0
      	if (phi2 <= 5.6e+26)
      		tmp = Float64(hypot(Float64(1.0 * Float64(lambda1 - lambda2)), phi1) * R);
      	else
      		tmp = fma(Float64(-R), phi1, Float64(R * phi2));
      	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, 5.6e+26], N[(N[Sqrt[N[(1.0 * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] ^ 2 + phi1 ^ 2], $MachinePrecision] * R), $MachinePrecision], N[((-R) * phi1 + N[(R * phi2), $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 5.6 \cdot 10^{+26}:\\
      \;\;\;\;\mathsf{hypot}\left(1 \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \cdot R\\
      
      \mathbf{else}:\\
      \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\
      
      
      \end{array}
      \end{array}
      
      Derivation
      1. Split input into 2 regimes
      2. if phi2 < 5.59999999999999999e26

        1. Initial program 69.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          if 5.59999999999999999e26 < phi2

          1. Initial program 43.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 -inf

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          Alternative 5: 61.2% 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 -5 \cdot 10^{+207}:\\ \;\;\;\;\left(\left(1 - \frac{\lambda_2}{\lambda_1}\right) \cdot \left(-\lambda_1\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\ \end{array} \end{array} \]
          NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
          (FPCore (R lambda1 lambda2 phi1 phi2)
           :precision binary64
           (if (<= (- lambda1 lambda2) -5e+207)
             (* (* (- 1.0 (/ lambda2 lambda1)) (- lambda1)) R)
             (fma (- R) phi1 (* R phi2))))
          assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
          double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
          	double tmp;
          	if ((lambda1 - lambda2) <= -5e+207) {
          		tmp = ((1.0 - (lambda2 / lambda1)) * -lambda1) * R;
          	} else {
          		tmp = fma(-R, phi1, (R * phi2));
          	}
          	return tmp;
          }
          
          R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
          function code(R, lambda1, lambda2, phi1, phi2)
          	tmp = 0.0
          	if (Float64(lambda1 - lambda2) <= -5e+207)
          		tmp = Float64(Float64(Float64(1.0 - Float64(lambda2 / lambda1)) * Float64(-lambda1)) * R);
          	else
          		tmp = fma(Float64(-R), phi1, Float64(R * phi2));
          	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], -5e+207], N[(N[(N[(1.0 - N[(lambda2 / lambda1), $MachinePrecision]), $MachinePrecision] * (-lambda1)), $MachinePrecision] * R), $MachinePrecision], N[((-R) * phi1 + N[(R * phi2), $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 -5 \cdot 10^{+207}:\\
          \;\;\;\;\left(\left(1 - \frac{\lambda_2}{\lambda_1}\right) \cdot \left(-\lambda_1\right)\right) \cdot R\\
          
          \mathbf{else}:\\
          \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if (-.f64 lambda1 lambda2) < -4.9999999999999999e207

            1. Initial program 42.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 lambda1 around -inf

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                if -4.9999999999999999e207 < (-.f64 lambda1 lambda2)

                1. Initial program 66.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 -inf

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                Alternative 6: 51.1% accurate, 13.9× speedup?

                \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.7 \cdot 10^{-150}:\\ \;\;\;\;\left(-\phi_1\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 1.35 \cdot 10^{+30}:\\ \;\;\;\;\left(-\lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;R \cdot \phi_2\\ \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.7e-150)
                   (* (- phi1) R)
                   (if (<= phi2 1.35e+30) (* (- lambda1) R) (* R phi2))))
                assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                	double tmp;
                	if (phi2 <= 1.7e-150) {
                		tmp = -phi1 * R;
                	} else if (phi2 <= 1.35e+30) {
                		tmp = -lambda1 * R;
                	} else {
                		tmp = R * phi2;
                	}
                	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.7d-150) then
                        tmp = -phi1 * r
                    else if (phi2 <= 1.35d+30) then
                        tmp = -lambda1 * r
                    else
                        tmp = r * phi2
                    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.7e-150) {
                		tmp = -phi1 * R;
                	} else if (phi2 <= 1.35e+30) {
                		tmp = -lambda1 * R;
                	} else {
                		tmp = R * phi2;
                	}
                	return tmp;
                }
                
                [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                def code(R, lambda1, lambda2, phi1, phi2):
                	tmp = 0
                	if phi2 <= 1.7e-150:
                		tmp = -phi1 * R
                	elif phi2 <= 1.35e+30:
                		tmp = -lambda1 * R
                	else:
                		tmp = R * phi2
                	return tmp
                
                R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                function code(R, lambda1, lambda2, phi1, phi2)
                	tmp = 0.0
                	if (phi2 <= 1.7e-150)
                		tmp = Float64(Float64(-phi1) * R);
                	elseif (phi2 <= 1.35e+30)
                		tmp = Float64(Float64(-lambda1) * R);
                	else
                		tmp = Float64(R * phi2);
                	end
                	return tmp
                end
                
                R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
                function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
                	tmp = 0.0;
                	if (phi2 <= 1.7e-150)
                		tmp = -phi1 * R;
                	elseif (phi2 <= 1.35e+30)
                		tmp = -lambda1 * R;
                	else
                		tmp = R * phi2;
                	end
                	tmp_2 = tmp;
                end
                
                NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 1.7e-150], N[((-phi1) * R), $MachinePrecision], If[LessEqual[phi2, 1.35e+30], N[((-lambda1) * R), $MachinePrecision], N[(R * phi2), $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.7 \cdot 10^{-150}:\\
                \;\;\;\;\left(-\phi_1\right) \cdot R\\
                
                \mathbf{elif}\;\phi_2 \leq 1.35 \cdot 10^{+30}:\\
                \;\;\;\;\left(-\lambda_1\right) \cdot R\\
                
                \mathbf{else}:\\
                \;\;\;\;R \cdot \phi_2\\
                
                
                \end{array}
                \end{array}
                
                Derivation
                1. Split input into 3 regimes
                2. if phi2 < 1.7e-150

                  1. Initial program 69.2%

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

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

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

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

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

                  if 1.7e-150 < phi2 < 1.3499999999999999e30

                  1. Initial program 71.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 R \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}}} \]
                  4. Step-by-step derivation
                    1. unpow2N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                      if 1.3499999999999999e30 < phi2

                      1. Initial program 44.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}{R \cdot \phi_2} \]
                      4. Step-by-step derivation
                        1. lower-*.f6468.1

                          \[\leadsto \color{blue}{R \cdot \phi_2} \]
                      5. Applied rewrites68.1%

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

                      \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.7 \cdot 10^{-150}:\\ \;\;\;\;\left(-\phi_1\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 1.35 \cdot 10^{+30}:\\ \;\;\;\;\left(-\lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;R \cdot \phi_2\\ \end{array} \]
                    6. Add Preprocessing

                    Alternative 7: 59.7% accurate, 13.9× speedup?

                    \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1.4 \cdot 10^{+167}:\\ \;\;\;\;\left(-\lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\ \end{array} \end{array} \]
                    NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                    (FPCore (R lambda1 lambda2 phi1 phi2)
                     :precision binary64
                     (if (<= lambda1 -1.4e+167) (* (- lambda1) R) (fma (- R) phi1 (* R phi2))))
                    assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                    double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                    	double tmp;
                    	if (lambda1 <= -1.4e+167) {
                    		tmp = -lambda1 * R;
                    	} else {
                    		tmp = fma(-R, phi1, (R * phi2));
                    	}
                    	return tmp;
                    }
                    
                    R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                    function code(R, lambda1, lambda2, phi1, phi2)
                    	tmp = 0.0
                    	if (lambda1 <= -1.4e+167)
                    		tmp = Float64(Float64(-lambda1) * R);
                    	else
                    		tmp = fma(Float64(-R), phi1, Float64(R * phi2));
                    	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[lambda1, -1.4e+167], N[((-lambda1) * R), $MachinePrecision], N[((-R) * phi1 + N[(R * phi2), $MachinePrecision]), $MachinePrecision]]
                    
                    \begin{array}{l}
                    [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                    \\
                    \begin{array}{l}
                    \mathbf{if}\;\lambda_1 \leq -1.4 \cdot 10^{+167}:\\
                    \;\;\;\;\left(-\lambda_1\right) \cdot R\\
                    
                    \mathbf{else}:\\
                    \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\
                    
                    
                    \end{array}
                    \end{array}
                    
                    Derivation
                    1. Split input into 2 regimes
                    2. if lambda1 < -1.3999999999999999e167

                      1. Initial program 23.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                          if -1.3999999999999999e167 < lambda1

                          1. Initial program 67.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 -inf

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                          Alternative 8: 37.0% accurate, 19.9× speedup?

                          \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.35 \cdot 10^{+30}:\\ \;\;\;\;\left(-\lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;R \cdot \phi_2\\ \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.35e+30) (* (- lambda1) R) (* R phi2)))
                          assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                          double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                          	double tmp;
                          	if (phi2 <= 1.35e+30) {
                          		tmp = -lambda1 * R;
                          	} else {
                          		tmp = R * phi2;
                          	}
                          	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.35d+30) then
                                  tmp = -lambda1 * r
                              else
                                  tmp = r * phi2
                              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.35e+30) {
                          		tmp = -lambda1 * R;
                          	} else {
                          		tmp = R * phi2;
                          	}
                          	return tmp;
                          }
                          
                          [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                          def code(R, lambda1, lambda2, phi1, phi2):
                          	tmp = 0
                          	if phi2 <= 1.35e+30:
                          		tmp = -lambda1 * R
                          	else:
                          		tmp = R * phi2
                          	return tmp
                          
                          R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                          function code(R, lambda1, lambda2, phi1, phi2)
                          	tmp = 0.0
                          	if (phi2 <= 1.35e+30)
                          		tmp = Float64(Float64(-lambda1) * R);
                          	else
                          		tmp = Float64(R * phi2);
                          	end
                          	return tmp
                          end
                          
                          R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
                          function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
                          	tmp = 0.0;
                          	if (phi2 <= 1.35e+30)
                          		tmp = -lambda1 * R;
                          	else
                          		tmp = R * phi2;
                          	end
                          	tmp_2 = tmp;
                          end
                          
                          NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                          code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 1.35e+30], N[((-lambda1) * R), $MachinePrecision], N[(R * phi2), $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.35 \cdot 10^{+30}:\\
                          \;\;\;\;\left(-\lambda_1\right) \cdot R\\
                          
                          \mathbf{else}:\\
                          \;\;\;\;R \cdot \phi_2\\
                          
                          
                          \end{array}
                          \end{array}
                          
                          Derivation
                          1. Split input into 2 regimes
                          2. if phi2 < 1.3499999999999999e30

                            1. Initial program 69.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. Taylor expanded in phi1 around 0

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                                if 1.3499999999999999e30 < phi2

                                1. Initial program 44.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}{R \cdot \phi_2} \]
                                4. Step-by-step derivation
                                  1. lower-*.f6468.1

                                    \[\leadsto \color{blue}{R \cdot \phi_2} \]
                                5. Applied rewrites68.1%

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

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

                              Alternative 9: 30.8% accurate, 46.5× speedup?

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

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

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

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

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

                              Reproduce

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