Equirectangular approximation to distance on a great circle

Percentage Accurate: 59.7% → 91.5%
Time: 13.1s
Alternatives: 12
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 12 alternatives:

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.09999999999999992e-17 < phi2

    1. Initial program 53.9%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\phi_2, \cos \left(0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)} \]
    6. Step-by-step derivation
      1. lift-*.f64N/A

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

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

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

        \[\leadsto R \cdot \mathsf{hypot}\left(\phi_2, \cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \color{blue}{\frac{1}{\frac{\lambda_1 + \lambda_2}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}}\right) \]
      5. un-div-invN/A

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

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

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

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

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

        \[\leadsto R \cdot \mathsf{hypot}\left(\phi_2, \frac{\cos \left(0.5 \cdot \phi_2\right)}{\color{blue}{\frac{1}{\lambda_1 - \lambda_2}}}\right) \]
    7. Applied egg-rr84.2%

      \[\leadsto R \cdot \mathsf{hypot}\left(\phi_2, \color{blue}{\frac{\cos \left(0.5 \cdot \phi_2\right)}{\frac{1}{\lambda_1 - \lambda_2}}}\right) \]
    8. Step-by-step derivation
      1. lift-*.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \mathsf{hypot}\left(\phi_2, \frac{1}{\frac{1}{\color{blue}{\cos \left(0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right)}}}\right) \]
    9. Applied egg-rr84.2%

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

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

Alternative 2: 91.6% accurate, 1.2× speedup?

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.09999999999999992e-17 < phi2

    1. Initial program 53.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 3: 88.0% accurate, 1.2× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 0.65:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\ \mathbf{elif}\;\phi_2 \leq 2.2 \cdot 10^{+150}:\\ \;\;\;\;R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \mathsf{fma}\left(\lambda_1 - \lambda_2, 0.5, \cos \left(\phi_2 + \phi_1\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot 0.5\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)}\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_2, -\frac{\phi_1}{\phi_2}, \phi_2\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 0.65)
   (* R (hypot phi1 (* (- lambda1 lambda2) (cos (* phi1 0.5)))))
   (if (<= phi2 2.2e+150)
     (*
      R
      (sqrt
       (+
        (*
         (- lambda1 lambda2)
         (fma
          (- lambda1 lambda2)
          0.5
          (* (cos (+ phi2 phi1)) (* (- lambda1 lambda2) 0.5))))
        (* (- phi1 phi2) (- phi1 phi2)))))
     (* R (fma phi2 (- (/ phi1 phi2)) 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 <= 0.65) {
		tmp = R * hypot(phi1, ((lambda1 - lambda2) * cos((phi1 * 0.5))));
	} else if (phi2 <= 2.2e+150) {
		tmp = R * sqrt((((lambda1 - lambda2) * fma((lambda1 - lambda2), 0.5, (cos((phi2 + phi1)) * ((lambda1 - lambda2) * 0.5)))) + ((phi1 - phi2) * (phi1 - phi2))));
	} else {
		tmp = R * fma(phi2, -(phi1 / phi2), 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 <= 0.65)
		tmp = Float64(R * hypot(phi1, Float64(Float64(lambda1 - lambda2) * cos(Float64(phi1 * 0.5)))));
	elseif (phi2 <= 2.2e+150)
		tmp = Float64(R * sqrt(Float64(Float64(Float64(lambda1 - lambda2) * fma(Float64(lambda1 - lambda2), 0.5, Float64(cos(Float64(phi2 + phi1)) * Float64(Float64(lambda1 - lambda2) * 0.5)))) + Float64(Float64(phi1 - phi2) * Float64(phi1 - phi2)))));
	else
		tmp = Float64(R * fma(phi2, Float64(-Float64(phi1 / phi2)), 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, 0.65], N[(R * N[Sqrt[phi1 ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(phi1 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 2.2e+150], N[(R * N[Sqrt[N[(N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(N[(lambda1 - lambda2), $MachinePrecision] * 0.5 + N[(N[Cos[N[(phi2 + phi1), $MachinePrecision]], $MachinePrecision] * N[(N[(lambda1 - lambda2), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[(phi2 * (-N[(phi1 / phi2), $MachinePrecision]) + phi2), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 0.65:\\
\;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_1 \cdot 0.5\right)\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < 0.650000000000000022

    1. Initial program 64.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 0

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 0.650000000000000022 < phi2 < 2.19999999999999999e150

    1. Initial program 58.8%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift--.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \left(\frac{\phi_1 + \phi_2}{2}\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right)\right)} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    4. Applied egg-rr58.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.19999999999999999e150 < phi2

    1. Initial program 48.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 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 \left(\phi_2 \cdot \color{blue}{\left(-1 \cdot \frac{\phi_1}{\phi_2} + 1\right)}\right) \]
      2. distribute-lft-inN/A

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

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

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

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

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

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

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

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

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

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

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

Alternative 4: 82.4% accurate, 1.6× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 0.65:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\ \mathbf{elif}\;\phi_2 \leq 2.2 \cdot 10^{+150}:\\ \;\;\;\;R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \mathsf{fma}\left(\lambda_1 - \lambda_2, 0.5, \cos \left(\phi_2 + \phi_1\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot 0.5\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)}\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_2, -\frac{\phi_1}{\phi_2}, \phi_2\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 0.65)
   (* R (hypot phi1 (- lambda1 lambda2)))
   (if (<= phi2 2.2e+150)
     (*
      R
      (sqrt
       (+
        (*
         (- lambda1 lambda2)
         (fma
          (- lambda1 lambda2)
          0.5
          (* (cos (+ phi2 phi1)) (* (- lambda1 lambda2) 0.5))))
        (* (- phi1 phi2) (- phi1 phi2)))))
     (* R (fma phi2 (- (/ phi1 phi2)) 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 <= 0.65) {
		tmp = R * hypot(phi1, (lambda1 - lambda2));
	} else if (phi2 <= 2.2e+150) {
		tmp = R * sqrt((((lambda1 - lambda2) * fma((lambda1 - lambda2), 0.5, (cos((phi2 + phi1)) * ((lambda1 - lambda2) * 0.5)))) + ((phi1 - phi2) * (phi1 - phi2))));
	} else {
		tmp = R * fma(phi2, -(phi1 / phi2), 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 <= 0.65)
		tmp = Float64(R * hypot(phi1, Float64(lambda1 - lambda2)));
	elseif (phi2 <= 2.2e+150)
		tmp = Float64(R * sqrt(Float64(Float64(Float64(lambda1 - lambda2) * fma(Float64(lambda1 - lambda2), 0.5, Float64(cos(Float64(phi2 + phi1)) * Float64(Float64(lambda1 - lambda2) * 0.5)))) + Float64(Float64(phi1 - phi2) * Float64(phi1 - phi2)))));
	else
		tmp = Float64(R * fma(phi2, Float64(-Float64(phi1 / phi2)), 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, 0.65], N[(R * N[Sqrt[phi1 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 2.2e+150], N[(R * N[Sqrt[N[(N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(N[(lambda1 - lambda2), $MachinePrecision] * 0.5 + N[(N[Cos[N[(phi2 + phi1), $MachinePrecision]], $MachinePrecision] * N[(N[(lambda1 - lambda2), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[(phi2 * (-N[(phi1 / phi2), $MachinePrecision]) + phi2), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 0.65:\\
\;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < 0.650000000000000022

    1. Initial program 64.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 0

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 0.650000000000000022 < phi2 < 2.19999999999999999e150

    1. Initial program 58.8%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift--.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \left(\frac{\phi_1 + \phi_2}{2}\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right)\right)} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    4. Applied egg-rr58.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.19999999999999999e150 < phi2

    1. Initial program 48.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 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 \left(\phi_2 \cdot \color{blue}{\left(-1 \cdot \frac{\phi_1}{\phi_2} + 1\right)}\right) \]
      2. distribute-lft-inN/A

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 82.4% accurate, 1.7× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 0.65:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\ \mathbf{elif}\;\phi_2 \leq 2.2 \cdot 10^{+150}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\phi_1 - \phi_2, \phi_1 - \phi_2, \left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(\phi_2 + \phi_1\right)\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_2, -\frac{\phi_1}{\phi_2}, \phi_2\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 0.65)
   (* R (hypot phi1 (- lambda1 lambda2)))
   (if (<= phi2 2.2e+150)
     (*
      R
      (sqrt
       (fma
        (- phi1 phi2)
        (- phi1 phi2)
        (*
         (* (- lambda1 lambda2) (- lambda1 lambda2))
         (+ 0.5 (* 0.5 (cos (+ phi2 phi1))))))))
     (* R (fma phi2 (- (/ phi1 phi2)) 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 <= 0.65) {
		tmp = R * hypot(phi1, (lambda1 - lambda2));
	} else if (phi2 <= 2.2e+150) {
		tmp = R * sqrt(fma((phi1 - phi2), (phi1 - phi2), (((lambda1 - lambda2) * (lambda1 - lambda2)) * (0.5 + (0.5 * cos((phi2 + phi1)))))));
	} else {
		tmp = R * fma(phi2, -(phi1 / phi2), 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 <= 0.65)
		tmp = Float64(R * hypot(phi1, Float64(lambda1 - lambda2)));
	elseif (phi2 <= 2.2e+150)
		tmp = Float64(R * sqrt(fma(Float64(phi1 - phi2), Float64(phi1 - phi2), Float64(Float64(Float64(lambda1 - lambda2) * Float64(lambda1 - lambda2)) * Float64(0.5 + Float64(0.5 * cos(Float64(phi2 + phi1))))))));
	else
		tmp = Float64(R * fma(phi2, Float64(-Float64(phi1 / phi2)), 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, 0.65], N[(R * N[Sqrt[phi1 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 2.2e+150], N[(R * N[Sqrt[N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision] + N[(N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] * N[(0.5 + N[(0.5 * N[Cos[N[(phi2 + phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(R * N[(phi2 * (-N[(phi1 / phi2), $MachinePrecision]) + phi2), $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 0.65:\\
\;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi2 < 0.650000000000000022

    1. Initial program 64.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 0

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 0.650000000000000022 < phi2 < 2.19999999999999999e150

    1. Initial program 58.8%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. lift--.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \color{blue}{\left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)}} \]
    4. Applied egg-rr58.9%

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

    if 2.19999999999999999e150 < phi2

    1. Initial program 48.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 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 \left(\phi_2 \cdot \color{blue}{\left(-1 \cdot \frac{\phi_1}{\phi_2} + 1\right)}\right) \]
      2. distribute-lft-inN/A

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

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

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

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

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

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

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

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

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

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

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

Alternative 6: 80.3% 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.95 \cdot 10^{+15}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_2, -\frac{\phi_1}{\phi_2}, \phi_2\right)\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 1.95e+15)
   (* R (hypot phi1 (- lambda1 lambda2)))
   (* R (fma phi2 (- (/ phi1 phi2)) 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.95e+15) {
		tmp = R * hypot(phi1, (lambda1 - lambda2));
	} else {
		tmp = R * fma(phi2, -(phi1 / phi2), 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.95e+15)
		tmp = Float64(R * hypot(phi1, Float64(lambda1 - lambda2)));
	else
		tmp = Float64(R * fma(phi2, Float64(-Float64(phi1 / phi2)), 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.95e+15], N[(R * N[Sqrt[phi1 ^ 2 + N[(lambda1 - lambda2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], N[(R * N[(phi2 * (-N[(phi1 / phi2), $MachinePrecision]) + phi2), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 1.95 \cdot 10^{+15}:\\
\;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \lambda_1 - \lambda_2\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < 1.95e15

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.95e15 < phi2

    1. Initial program 52.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 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 \left(\phi_2 \cdot \color{blue}{\left(-1 \cdot \frac{\phi_1}{\phi_2} + 1\right)}\right) \]
      2. distribute-lft-inN/A

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

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

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

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

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

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

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

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

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

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

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

Alternative 7: 55.1% accurate, 7.5× 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.7 \cdot 10^{-51}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq 6.5 \cdot 10^{-183}:\\ \;\;\;\;\lambda_2 \cdot \left(R - \frac{R \cdot \lambda_1}{\lambda_2}\right)\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -1.7e-51)
   (* R (fma phi1 (/ phi2 phi1) (- phi1)))
   (if (<= phi1 6.5e-183)
     (* lambda2 (- R (/ (* R 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.7e-51) {
		tmp = R * fma(phi1, (phi2 / phi1), -phi1);
	} else if (phi1 <= 6.5e-183) {
		tmp = lambda2 * (R - ((R * lambda1) / lambda2));
	} 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 <= -1.7e-51)
		tmp = Float64(R * fma(phi1, Float64(phi2 / phi1), Float64(-phi1)));
	elseif (phi1 <= 6.5e-183)
		tmp = Float64(lambda2 * Float64(R - Float64(Float64(R * lambda1) / lambda2)));
	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, -1.7e-51], N[(R * N[(phi1 * N[(phi2 / phi1), $MachinePrecision] + (-phi1)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 6.5e-183], N[(lambda2 * N[(R - N[(N[(R * lambda1), $MachinePrecision] / lambda2), $MachinePrecision]), $MachinePrecision]), $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 -1.7 \cdot 10^{-51}:\\
\;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\

\mathbf{elif}\;\phi_1 \leq 6.5 \cdot 10^{-183}:\\
\;\;\;\;\lambda_2 \cdot \left(R - \frac{R \cdot \lambda_1}{\lambda_2}\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if phi1 < -1.70000000000000001e-51

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -1.70000000000000001e-51 < phi1 < 6.50000000000000014e-183

    1. Initial program 72.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \lambda_2 \cdot \left(R - \color{blue}{\frac{R \cdot \lambda_1}{\lambda_2}}\right) \]
      6. *-commutativeN/A

        \[\leadsto \lambda_2 \cdot \left(R - \frac{\color{blue}{\lambda_1 \cdot R}}{\lambda_2}\right) \]
      7. lower-*.f6426.5

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

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

    if 6.50000000000000014e-183 < phi1

    1. Initial program 63.3%

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

      \[\leadsto R \cdot \color{blue}{\left(-1 \cdot \left(\phi_1 \cdot \left(1 + -1 \cdot \frac{\phi_2 + \frac{-1}{2} \cdot \frac{{\cos \left(\frac{1}{2} \cdot \left(\phi_2 - -1 \cdot \phi_1\right)\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}}{\phi_1}}{\phi_1}\right)\right)\right)} \]
    4. Simplified10.3%

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

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

        \[\leadsto \color{blue}{R \cdot \phi_2} \]
    7. Simplified19.2%

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

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

Alternative 8: 55.1% accurate, 9.0× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 1.05 \cdot 10^{+191}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \lambda_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 (<= lambda2 1.05e+191)
   (* R (fma phi1 (/ phi2 phi1) (- phi1)))
   (* R lambda2)))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 1.05e+191) {
		tmp = R * fma(phi1, (phi2 / phi1), -phi1);
	} else {
		tmp = R * lambda2;
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 1.05e+191)
		tmp = Float64(R * fma(phi1, Float64(phi2 / phi1), Float64(-phi1)));
	else
		tmp = Float64(R * lambda2);
	end
	return tmp
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, 1.05e+191], N[(R * N[(phi1 * N[(phi2 / phi1), $MachinePrecision] + (-phi1)), $MachinePrecision]), $MachinePrecision], N[(R * lambda2), $MachinePrecision]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 1.05 \cdot 10^{+191}:\\
\;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda2 < 1.05e191

    1. Initial program 62.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.05e191 < lambda2

    1. Initial program 54.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
    11. Simplified65.8%

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

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

Alternative 9: 50.1% accurate, 11.6× speedup?

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

\mathbf{elif}\;\phi_2 \leq 1.25 \cdot 10^{-115}:\\
\;\;\;\;-R \cdot \lambda_1\\

\mathbf{elif}\;\phi_2 \leq 4.9 \cdot 10^{+14}:\\
\;\;\;\;R \cdot \lambda_2\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if phi2 < 6.9000000000000003e-189

    1. Initial program 63.2%

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

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

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

    if 6.9000000000000003e-189 < phi2 < 1.2500000000000001e-115

    1. Initial program 73.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.2500000000000001e-115 < phi2 < 4.9e14

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
      2. lower-*.f6420.3

        \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
    11. Simplified20.3%

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

    if 4.9e14 < phi2

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 6.9 \cdot 10^{-189}:\\ \;\;\;\;-R \cdot \phi_1\\ \mathbf{elif}\;\phi_2 \leq 1.25 \cdot 10^{-115}:\\ \;\;\;\;-R \cdot \lambda_1\\ \mathbf{elif}\;\phi_2 \leq 4.9 \cdot 10^{+14}:\\ \;\;\;\;R \cdot \lambda_2\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \]
  5. Add Preprocessing

Alternative 10: 38.2% accurate, 15.5× speedup?

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

\mathbf{elif}\;\phi_2 \leq 4.9 \cdot 10^{+14}:\\
\;\;\;\;R \cdot \lambda_2\\

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


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

    1. Initial program 64.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.2500000000000001e-115 < phi2 < 4.9e14

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
      2. lower-*.f6420.3

        \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
    11. Simplified20.3%

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

    if 4.9e14 < phi2

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.25 \cdot 10^{-115}:\\ \;\;\;\;-R \cdot \lambda_1\\ \mathbf{elif}\;\phi_2 \leq 4.9 \cdot 10^{+14}:\\ \;\;\;\;R \cdot \lambda_2\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \]
  5. Add Preprocessing

Alternative 11: 38.2% accurate, 23.2× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < 4.9e14

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\lambda_2 \cdot R} \]
      2. lower-*.f6413.7

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

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

    if 4.9e14 < phi2

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

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

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

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

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

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

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

Alternative 12: 32.0% accurate, 46.5× speedup?

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

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

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

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

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

      \[\leadsto \color{blue}{R \cdot \phi_2} \]
  7. Simplified19.6%

    \[\leadsto \color{blue}{R \cdot \phi_2} \]
  8. Final simplification19.6%

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

Reproduce

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