Equirectangular approximation to distance on a great circle

Percentage Accurate: 59.5% → 91.1%
Time: 8.1s
Alternatives: 10
Speedup: 2.4×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 10 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.5% accurate, 1.0× speedup?

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

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

Alternative 1: 91.1% accurate, 1.2× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1 \cdot 10^{-13}:\\ \;\;\;\;\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 (<= phi1 -1e-13)
   (* (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 (phi1 <= -1e-13) {
		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 (phi1 <= -1e-13) {
		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 phi1 <= -1e-13:
		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 (phi1 <= -1e-13)
		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 (phi1 <= -1e-13)
		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[phi1, -1e-13], 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_1 \leq -1 \cdot 10^{-13}:\\
\;\;\;\;\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 phi1 < -1e-13

    1. Initial program 56.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{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_1}^{2}} \]
      2. 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(\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. cos-neg-revN/A

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

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

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

        \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(\color{blue}{\frac{-1}{2}} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \]
      11. 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) \]
      12. lower--.f6483.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 rewrites83.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)} \]

    if -1e-13 < phi1

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1 \cdot 10^{-13}:\\ \;\;\;\;\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: 86.3% accurate, 1.2× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.35 \cdot 10^{+31}:\\ \;\;\;\;\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 1.35e+31)
   (* (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 <= 1.35e+31) {
		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 <= 1.35e+31) {
		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 <= 1.35e+31:
		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 <= 1.35e+31)
		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 <= 1.35e+31)
		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, 1.35e+31], 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 1.35 \cdot 10^{+31}:\\
\;\;\;\;\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 < 1.34999999999999993e31

    1. Initial program 59.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 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{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_1}^{2}} \]
      2. 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(\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. cos-neg-revN/A

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

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

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

        \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(\color{blue}{\frac{-1}{2}} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \]
      11. 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) \]
      12. lower--.f6481.1

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

      \[\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 1.34999999999999993e31 < phi2

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.35 \cdot 10^{+31}:\\ \;\;\;\;\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: 81.1% accurate, 1.3× speedup?

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

      1. Initial program 58.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 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{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_1}^{2}} \]
        2. 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(\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. cos-neg-revN/A

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

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

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

          \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(\color{blue}{\frac{-1}{2}} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \]
        11. 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) \]
        12. lower--.f6485.8

          \[\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 rewrites85.8%

        \[\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 lambda1 around inf

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

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

        if -1.6e17 < phi1

        1. Initial program 58.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

        Alternative 4: 80.2% accurate, 2.4× speedup?

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

          1. Initial program 55.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 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{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_1}^{2}} \]
            2. 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(\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. cos-neg-revN/A

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

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

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

              \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(\color{blue}{\frac{-1}{2}} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \]
            11. 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) \]
            12. lower--.f6486.7

              \[\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 rewrites86.7%

            \[\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(\lambda_1 - \lambda_2, \phi_1\right) \]
          7. Step-by-step derivation
            1. Applied rewrites80.6%

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

            if -2.49999999999999993e58 < phi1

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

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

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

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

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

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

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

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

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

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

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

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

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

            Alternative 5: 80.0% accurate, 2.4× speedup?

            \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.16 \cdot 10^{+38}:\\ \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(R, \frac{-\phi_1}{\phi_2}, R\right) \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.16e+38)
               (* (hypot (- lambda1 lambda2) phi1) R)
               (* (fma R (/ (- phi1) phi2) 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.16e+38) {
            		tmp = hypot((lambda1 - lambda2), phi1) * R;
            	} else {
            		tmp = fma(R, (-phi1 / phi2), 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.16e+38)
            		tmp = Float64(hypot(Float64(lambda1 - lambda2), phi1) * R);
            	else
            		tmp = Float64(fma(R, Float64(Float64(-phi1) / phi2), 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, 1.16e+38], N[(N[Sqrt[N[(lambda1 - lambda2), $MachinePrecision] ^ 2 + phi1 ^ 2], $MachinePrecision] * R), $MachinePrecision], N[(N[(R * N[((-phi1) / phi2), $MachinePrecision] + R), $MachinePrecision] * 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.16 \cdot 10^{+38}:\\
            \;\;\;\;\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right) \cdot R\\
            
            \mathbf{else}:\\
            \;\;\;\;\mathsf{fma}\left(R, \frac{-\phi_1}{\phi_2}, R\right) \cdot \phi_2\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 2 regimes
            2. if phi2 < 1.15999999999999991e38

              1. Initial program 59.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{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} + {\phi_1}^{2}} \]
                2. 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(\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. cos-neg-revN/A

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

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

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

                  \[\leadsto R \cdot \mathsf{hypot}\left(\cos \left(\color{blue}{\frac{-1}{2}} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \]
                11. 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) \]
                12. lower--.f6480.8

                  \[\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 rewrites80.8%

                \[\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(\lambda_1 - \lambda_2, \phi_1\right) \]
              7. Step-by-step derivation
                1. Applied rewrites74.7%

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

                if 1.15999999999999991e38 < phi2

                1. Initial program 52.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

              Alternative 6: 49.2% accurate, 8.2× speedup?

              \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.05 \cdot 10^{+25}:\\ \;\;\;\;\left(-\phi_1\right) \cdot R\\ \mathbf{elif}\;\phi_1 \leq -6.4 \cdot 10^{-268}:\\ \;\;\;\;\left(\mathsf{fma}\left(-0.25 \cdot \phi_1, \phi_2, 1\right) \cdot \lambda_2\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \end{array} \]
              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
              (FPCore (R lambda1 lambda2 phi1 phi2)
               :precision binary64
               (if (<= phi1 -2.05e+25)
                 (* (- phi1) R)
                 (if (<= phi1 -6.4e-268)
                   (* (* (fma (* -0.25 phi1) phi2 1.0) lambda2) R)
                   (* phi2 R))))
              assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
              double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
              	double tmp;
              	if (phi1 <= -2.05e+25) {
              		tmp = -phi1 * R;
              	} else if (phi1 <= -6.4e-268) {
              		tmp = (fma((-0.25 * phi1), phi2, 1.0) * lambda2) * R;
              	} else {
              		tmp = phi2 * R;
              	}
              	return tmp;
              }
              
              R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
              function code(R, lambda1, lambda2, phi1, phi2)
              	tmp = 0.0
              	if (phi1 <= -2.05e+25)
              		tmp = Float64(Float64(-phi1) * R);
              	elseif (phi1 <= -6.4e-268)
              		tmp = Float64(Float64(fma(Float64(-0.25 * phi1), phi2, 1.0) * lambda2) * R);
              	else
              		tmp = Float64(phi2 * R);
              	end
              	return tmp
              end
              
              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
              code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi1, -2.05e+25], N[((-phi1) * R), $MachinePrecision], If[LessEqual[phi1, -6.4e-268], N[(N[(N[(N[(-0.25 * phi1), $MachinePrecision] * phi2 + 1.0), $MachinePrecision] * lambda2), $MachinePrecision] * R), $MachinePrecision], N[(phi2 * R), $MachinePrecision]]]
              
              \begin{array}{l}
              [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
              \\
              \begin{array}{l}
              \mathbf{if}\;\phi_1 \leq -2.05 \cdot 10^{+25}:\\
              \;\;\;\;\left(-\phi_1\right) \cdot R\\
              
              \mathbf{elif}\;\phi_1 \leq -6.4 \cdot 10^{-268}:\\
              \;\;\;\;\left(\mathsf{fma}\left(-0.25 \cdot \phi_1, \phi_2, 1\right) \cdot \lambda_2\right) \cdot R\\
              
              \mathbf{else}:\\
              \;\;\;\;\phi_2 \cdot R\\
              
              
              \end{array}
              \end{array}
              
              Derivation
              1. Split input into 3 regimes
              2. if phi1 < -2.04999999999999983e25

                1. Initial program 56.8%

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

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

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

                if -2.04999999999999983e25 < phi1 < -6.3999999999999997e-268

                1. Initial program 60.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                    \[\leadsto R \cdot \left(\left(1 + \frac{-1}{4} \cdot \left(\phi_1 \cdot \phi_2\right)\right) \cdot \lambda_2\right) \]
                  3. Step-by-step derivation
                    1. Applied rewrites22.2%

                      \[\leadsto R \cdot \left(\mathsf{fma}\left(-0.25 \cdot \phi_1, \phi_2, 1\right) \cdot \lambda_2\right) \]

                    if -6.3999999999999997e-268 < phi1

                    1. Initial program 58.5%

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

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

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

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

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

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

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

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

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

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

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

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

                        \[\leadsto \color{blue}{R \cdot \phi_2} \]
                    8. Applied rewrites17.1%

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

                    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -2.05 \cdot 10^{+25}:\\ \;\;\;\;\left(-\phi_1\right) \cdot R\\ \mathbf{elif}\;\phi_1 \leq -6.4 \cdot 10^{-268}:\\ \;\;\;\;\left(\mathsf{fma}\left(-0.25 \cdot \phi_1, \phi_2, 1\right) \cdot \lambda_2\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \]
                  6. Add Preprocessing

                  Alternative 7: 58.6% accurate, 9.0× speedup?

                  \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2.3 \cdot 10^{+24}:\\ \;\;\;\;\left(\frac{\phi_2}{\phi_1} \cdot R - R\right) \cdot \phi_1\\ \mathbf{else}:\\ \;\;\;\;\left(\left(1 - \frac{\phi_1}{\phi_2}\right) \cdot \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 2.3e+24)
                     (* (- (* (/ phi2 phi1) R) R) phi1)
                     (* (* (- 1.0 (/ phi1 phi2)) 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 <= 2.3e+24) {
                  		tmp = (((phi2 / phi1) * R) - R) * phi1;
                  	} else {
                  		tmp = ((1.0 - (phi1 / phi2)) * phi2) * R;
                  	}
                  	return tmp;
                  }
                  
                  NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                  real(8) function code(r, lambda1, lambda2, phi1, phi2)
                      real(8), intent (in) :: r
                      real(8), intent (in) :: lambda1
                      real(8), intent (in) :: lambda2
                      real(8), intent (in) :: phi1
                      real(8), intent (in) :: phi2
                      real(8) :: tmp
                      if (phi2 <= 2.3d+24) then
                          tmp = (((phi2 / phi1) * r) - r) * phi1
                      else
                          tmp = ((1.0d0 - (phi1 / phi2)) * phi2) * r
                      end if
                      code = tmp
                  end function
                  
                  assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
                  public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                  	double tmp;
                  	if (phi2 <= 2.3e+24) {
                  		tmp = (((phi2 / phi1) * R) - R) * phi1;
                  	} else {
                  		tmp = ((1.0 - (phi1 / phi2)) * 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 <= 2.3e+24:
                  		tmp = (((phi2 / phi1) * R) - R) * phi1
                  	else:
                  		tmp = ((1.0 - (phi1 / phi2)) * 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 <= 2.3e+24)
                  		tmp = Float64(Float64(Float64(Float64(phi2 / phi1) * R) - R) * phi1);
                  	else
                  		tmp = Float64(Float64(Float64(1.0 - Float64(phi1 / phi2)) * 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 <= 2.3e+24)
                  		tmp = (((phi2 / phi1) * R) - R) * phi1;
                  	else
                  		tmp = ((1.0 - (phi1 / phi2)) * 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, 2.3e+24], N[(N[(N[(N[(phi2 / phi1), $MachinePrecision] * R), $MachinePrecision] - R), $MachinePrecision] * phi1), $MachinePrecision], N[(N[(N[(1.0 - N[(phi1 / phi2), $MachinePrecision]), $MachinePrecision] * phi2), $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 2.3 \cdot 10^{+24}:\\
                  \;\;\;\;\left(\frac{\phi_2}{\phi_1} \cdot R - R\right) \cdot \phi_1\\
                  
                  \mathbf{else}:\\
                  \;\;\;\;\left(\left(1 - \frac{\phi_1}{\phi_2}\right) \cdot \phi_2\right) \cdot R\\
                  
                  
                  \end{array}
                  \end{array}
                  
                  Derivation
                  1. Split input into 2 regimes
                  2. if phi2 < 2.2999999999999999e24

                    1. Initial program 59.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                      \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(-0.5 \cdot \phi_2\right), \phi_2\right)} \]
                    6. 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)} \]
                    7. Step-by-step derivation
                      1. mul-1-negN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                    if 2.2999999999999999e24 < phi2

                    1. Initial program 55.5%

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

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

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

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

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

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

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

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

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

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

                  Alternative 8: 57.2% accurate, 9.0× speedup?

                  \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -6.4 \cdot 10^{+174}:\\ \;\;\;\;\left(-\phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(R, \frac{-\phi_1}{\phi_2}, R\right) \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 (<= phi1 -6.4e+174) (* (- phi1) R) (* (fma R (/ (- phi1) phi2) 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 (phi1 <= -6.4e+174) {
                  		tmp = -phi1 * R;
                  	} else {
                  		tmp = fma(R, (-phi1 / phi2), 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 (phi1 <= -6.4e+174)
                  		tmp = Float64(Float64(-phi1) * R);
                  	else
                  		tmp = Float64(fma(R, Float64(Float64(-phi1) / phi2), 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[phi1, -6.4e+174], N[((-phi1) * R), $MachinePrecision], N[(N[(R * N[((-phi1) / phi2), $MachinePrecision] + R), $MachinePrecision] * phi2), $MachinePrecision]]
                  
                  \begin{array}{l}
                  [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                  \\
                  \begin{array}{l}
                  \mathbf{if}\;\phi_1 \leq -6.4 \cdot 10^{+174}:\\
                  \;\;\;\;\left(-\phi_1\right) \cdot R\\
                  
                  \mathbf{else}:\\
                  \;\;\;\;\mathsf{fma}\left(R, \frac{-\phi_1}{\phi_2}, R\right) \cdot \phi_2\\
                  
                  
                  \end{array}
                  \end{array}
                  
                  Derivation
                  1. Split input into 2 regimes
                  2. if phi1 < -6.4000000000000001e174

                    1. Initial program 39.4%

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

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

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

                    if -6.4000000000000001e174 < phi1

                    1. Initial program 60.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                  Alternative 9: 52.1% accurate, 19.9× speedup?

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

                    1. Initial program 56.0%

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

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

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

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

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

                    if -2.90000000000000007e38 < phi1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                  Alternative 10: 31.3% accurate, 46.5× speedup?

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

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

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

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

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

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

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

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

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

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

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

                    \[\leadsto \color{blue}{R \cdot \phi_2} \]
                  7. Step-by-step derivation
                    1. lower-*.f6415.0

                      \[\leadsto \color{blue}{R \cdot \phi_2} \]
                  8. Applied rewrites15.0%

                    \[\leadsto \color{blue}{R \cdot \phi_2} \]
                  9. Final simplification15.0%

                    \[\leadsto \phi_2 \cdot R \]
                  10. Add Preprocessing

                  Reproduce

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