Equirectangular approximation to distance on a great circle

Percentage Accurate: 59.7% → 91.6%
Time: 12.1s
Alternatives: 13
Speedup: 5.9×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 13 alternatives:

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

Initial Program: 59.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.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 2: 85.9% 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 8.2 \cdot 10^{+14}:\\ \;\;\;\;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{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 8.2e+14)
   (* R (hypot phi1 (* (- lambda1 lambda2) (cos (* phi1 0.5)))))
   (* 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 <= 8.2e+14) {
		tmp = R * hypot(phi1, ((lambda1 - lambda2) * cos((phi1 * 0.5))));
	} 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 <= 8.2e+14)
		tmp = Float64(R * hypot(phi1, Float64(Float64(lambda1 - lambda2) * cos(Float64(phi1 * 0.5)))));
	else
		tmp = Float64(R * fma(phi2, Float64(phi1 / Float64(-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, 8.2e+14], 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[(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 8.2 \cdot 10^{+14}:\\
\;\;\;\;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{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 < 8.2e14

    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)} \]

    if 8.2e14 < 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 simplification77.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 8.2 \cdot 10^{+14}:\\ \;\;\;\;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{fma}\left(\phi_2, \frac{\phi_1}{-\phi_2}, \phi_2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 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 8.2 \cdot 10^{+14}:\\ \;\;\;\;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 8.2e+14)
   (* 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 <= 8.2e+14) {
		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 <= 8.2e+14)
		tmp = Float64(R * hypot(phi1, Float64(lambda1 - lambda2)));
	else
		tmp = Float64(R * fma(phi2, Float64(phi1 / Float64(-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, 8.2e+14], 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 8.2 \cdot 10^{+14}:\\
\;\;\;\;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 < 8.2e14

    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 8.2e14 < 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. Add Preprocessing

Alternative 4: 61.6% accurate, 5.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.35 \cdot 10^{+117}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq -1.86 \cdot 10^{-51}:\\ \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\ \mathbf{elif}\;\phi_1 \leq -5.1 \cdot 10^{-142}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(-0.5, \frac{\phi_2 \cdot \phi_2}{\lambda_1}, \lambda_2 - \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \phi_2 \cdot \phi_2\right)}\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -1.35e+117)
   (* R (fma phi1 (/ phi2 phi1) (- phi1)))
   (if (<= phi1 -1.86e-51)
     (* phi2 (- R (/ (* R phi1) phi2)))
     (if (<= phi1 -5.1e-142)
       (* R (fma -0.5 (/ (* phi2 phi2) lambda1) (- lambda2 lambda1)))
       (*
        R
        (sqrt (fma (- lambda1 lambda2) (- lambda1 lambda2) (* 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 (phi1 <= -1.35e+117) {
		tmp = R * fma(phi1, (phi2 / phi1), -phi1);
	} else if (phi1 <= -1.86e-51) {
		tmp = phi2 * (R - ((R * phi1) / phi2));
	} else if (phi1 <= -5.1e-142) {
		tmp = R * fma(-0.5, ((phi2 * phi2) / lambda1), (lambda2 - lambda1));
	} else {
		tmp = R * sqrt(fma((lambda1 - lambda2), (lambda1 - lambda2), (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 (phi1 <= -1.35e+117)
		tmp = Float64(R * fma(phi1, Float64(phi2 / phi1), Float64(-phi1)));
	elseif (phi1 <= -1.86e-51)
		tmp = Float64(phi2 * Float64(R - Float64(Float64(R * phi1) / phi2)));
	elseif (phi1 <= -5.1e-142)
		tmp = Float64(R * fma(-0.5, Float64(Float64(phi2 * phi2) / lambda1), Float64(lambda2 - lambda1)));
	else
		tmp = Float64(R * sqrt(fma(Float64(lambda1 - lambda2), Float64(lambda1 - lambda2), Float64(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[phi1, -1.35e+117], N[(R * N[(phi1 * N[(phi2 / phi1), $MachinePrecision] + (-phi1)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, -1.86e-51], N[(phi2 * N[(R - N[(N[(R * phi1), $MachinePrecision] / phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, -5.1e-142], N[(R * N[(-0.5 * N[(N[(phi2 * phi2), $MachinePrecision] / lambda1), $MachinePrecision] + N[(lambda2 - lambda1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(R * N[Sqrt[N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision] + N[(phi2 * phi2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
\mathbf{if}\;\phi_1 \leq -1.35 \cdot 10^{+117}:\\
\;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\

\mathbf{elif}\;\phi_1 \leq -1.86 \cdot 10^{-51}:\\
\;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\

\mathbf{elif}\;\phi_1 \leq -5.1 \cdot 10^{-142}:\\
\;\;\;\;R \cdot \mathsf{fma}\left(-0.5, \frac{\phi_2 \cdot \phi_2}{\lambda_1}, \lambda_2 - \lambda_1\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \phi_2 \cdot \phi_2\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if phi1 < -1.3500000000000001e117

    1. Initial program 46.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}{\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.f6486.1

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

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

    if -1.3500000000000001e117 < phi1 < -1.8600000000000001e-51

    1. Initial program 55.6%

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lower-*.f6455.8

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified55.8%

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

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

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

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

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

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

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

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

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

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

    if -1.8600000000000001e-51 < phi1 < -5.1000000000000001e-142

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lower-*.f6472.8

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified72.8%

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
      8. lower-*.f6472.8

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \left(\left(\mathsf{neg}\left(\lambda_1 \cdot \color{blue}{\left(\mathsf{neg}\left(\frac{\lambda_2 + \frac{-1}{2} \cdot \frac{{\phi_2}^{2}}{\lambda_1}}{\lambda_1}\right)\right)}\right)\right) + \left(-1 \cdot \lambda_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(\lambda_1 \cdot \frac{\lambda_2 + \frac{-1}{2} \cdot \frac{{\phi_2}^{2}}{\lambda_1}}{\lambda_1}\right)\right)}\right)\right) + \left(-1 \cdot \lambda_1\right) \cdot 1\right) \]
      8. remove-double-negN/A

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

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

        \[\leadsto R \cdot \color{blue}{\mathsf{fma}\left(\lambda_1, \frac{\lambda_2 + \frac{-1}{2} \cdot \frac{{\phi_2}^{2}}{\lambda_1}}{\lambda_1}, -1 \cdot \lambda_1\right)} \]
    11. Simplified58.8%

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

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

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

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

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

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

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

        \[\leadsto R \cdot \mathsf{fma}\left(\frac{-1}{2}, \frac{\color{blue}{\phi_2 \cdot \phi_2}}{\lambda_1}, \lambda_2 - \lambda_1\right) \]
      7. lower--.f6458.8

        \[\leadsto R \cdot \mathsf{fma}\left(-0.5, \frac{\phi_2 \cdot \phi_2}{\lambda_1}, \color{blue}{\lambda_2 - \lambda_1}\right) \]
    14. Simplified58.8%

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

    if -5.1000000000000001e-142 < phi1

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lower-*.f6464.4

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
      8. lower-*.f6455.1

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.35 \cdot 10^{+117}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq -1.86 \cdot 10^{-51}:\\ \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\ \mathbf{elif}\;\phi_1 \leq -5.1 \cdot 10^{-142}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(-0.5, \frac{\phi_2 \cdot \phi_2}{\lambda_1}, \lambda_2 - \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \phi_2 \cdot \phi_2\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 56.6% accurate, 5.7× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.35 \cdot 10^{+117}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq -1.86 \cdot 10^{-51}:\\ \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\ \mathbf{elif}\;\phi_1 \leq 2.2 \cdot 10^{-187}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(-0.5, \frac{\phi_2 \cdot \phi_2}{\lambda_1}, \lambda_2 - \lambda_1\right)\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \end{array} \]
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi1 -1.35e+117)
   (* R (fma phi1 (/ phi2 phi1) (- phi1)))
   (if (<= phi1 -1.86e-51)
     (* phi2 (- R (/ (* R phi1) phi2)))
     (if (<= phi1 2.2e-187)
       (* R (fma -0.5 (/ (* phi2 phi2) lambda1) (- lambda2 lambda1)))
       (* phi2 R)))))
assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -1.35e+117) {
		tmp = R * fma(phi1, (phi2 / phi1), -phi1);
	} else if (phi1 <= -1.86e-51) {
		tmp = phi2 * (R - ((R * phi1) / phi2));
	} else if (phi1 <= 2.2e-187) {
		tmp = R * fma(-0.5, ((phi2 * phi2) / lambda1), (lambda2 - lambda1));
	} else {
		tmp = phi2 * R;
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -1.35e+117)
		tmp = Float64(R * fma(phi1, Float64(phi2 / phi1), Float64(-phi1)));
	elseif (phi1 <= -1.86e-51)
		tmp = Float64(phi2 * Float64(R - Float64(Float64(R * phi1) / phi2)));
	elseif (phi1 <= 2.2e-187)
		tmp = Float64(R * fma(-0.5, Float64(Float64(phi2 * phi2) / lambda1), Float64(lambda2 - lambda1)));
	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.35e+117], N[(R * N[(phi1 * N[(phi2 / phi1), $MachinePrecision] + (-phi1)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, -1.86e-51], N[(phi2 * N[(R - N[(N[(R * phi1), $MachinePrecision] / phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 2.2e-187], N[(R * N[(-0.5 * N[(N[(phi2 * phi2), $MachinePrecision] / lambda1), $MachinePrecision] + N[(lambda2 - lambda1), $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.35 \cdot 10^{+117}:\\
\;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\

\mathbf{elif}\;\phi_1 \leq -1.86 \cdot 10^{-51}:\\
\;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if phi1 < -1.3500000000000001e117

    1. Initial program 46.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}{\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.f6486.1

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

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

    if -1.3500000000000001e117 < phi1 < -1.8600000000000001e-51

    1. Initial program 55.6%

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lower-*.f6455.8

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified55.8%

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

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

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

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

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

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

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

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

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

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

    if -1.8600000000000001e-51 < phi1 < 2.20000000000000008e-187

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lower-*.f6469.7

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified69.7%

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
      8. lower-*.f6469.7

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \left(\left(\mathsf{neg}\left(\lambda_1 \cdot \color{blue}{\left(\mathsf{neg}\left(\frac{\lambda_2 + \frac{-1}{2} \cdot \frac{{\phi_2}^{2}}{\lambda_1}}{\lambda_1}\right)\right)}\right)\right) + \left(-1 \cdot \lambda_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(\lambda_1 \cdot \frac{\lambda_2 + \frac{-1}{2} \cdot \frac{{\phi_2}^{2}}{\lambda_1}}{\lambda_1}\right)\right)}\right)\right) + \left(-1 \cdot \lambda_1\right) \cdot 1\right) \]
      8. remove-double-negN/A

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

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

        \[\leadsto R \cdot \color{blue}{\mathsf{fma}\left(\lambda_1, \frac{\lambda_2 + \frac{-1}{2} \cdot \frac{{\phi_2}^{2}}{\lambda_1}}{\lambda_1}, -1 \cdot \lambda_1\right)} \]
    11. Simplified36.6%

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

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

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

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

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

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

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

        \[\leadsto R \cdot \mathsf{fma}\left(\frac{-1}{2}, \frac{\color{blue}{\phi_2 \cdot \phi_2}}{\lambda_1}, \lambda_2 - \lambda_1\right) \]
      7. lower--.f6436.8

        \[\leadsto R \cdot \mathsf{fma}\left(-0.5, \frac{\phi_2 \cdot \phi_2}{\lambda_1}, \color{blue}{\lambda_2 - \lambda_1}\right) \]
    14. Simplified36.8%

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

    if 2.20000000000000008e-187 < phi1

    1. Initial program 63.7%

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lower-*.f6461.3

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified61.3%

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

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

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

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

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

Alternative 6: 66.8% accurate, 5.9× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -4.9999999999999996e124

    1. Initial program 45.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}{\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.f6485.9

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

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

    if -4.9999999999999996e124 < phi1

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified63.9%

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

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

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

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

        \[\leadsto R \cdot \sqrt{\color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot \left(\lambda_1 - \lambda_2\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      4. lower--.f6463.2

        \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    8. Simplified63.2%

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

Alternative 7: 55.7% accurate, 6.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.35 \cdot 10^{+117}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq -1.86 \cdot 10^{-51}:\\ \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\ \mathbf{elif}\;\phi_1 \leq 4.6 \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.35e+117)
   (* R (fma phi1 (/ phi2 phi1) (- phi1)))
   (if (<= phi1 -1.86e-51)
     (* phi2 (- R (/ (* R phi1) phi2)))
     (if (<= phi1 4.6e-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.35e+117) {
		tmp = R * fma(phi1, (phi2 / phi1), -phi1);
	} else if (phi1 <= -1.86e-51) {
		tmp = phi2 * (R - ((R * phi1) / phi2));
	} else if (phi1 <= 4.6e-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.35e+117)
		tmp = Float64(R * fma(phi1, Float64(phi2 / phi1), Float64(-phi1)));
	elseif (phi1 <= -1.86e-51)
		tmp = Float64(phi2 * Float64(R - Float64(Float64(R * phi1) / phi2)));
	elseif (phi1 <= 4.6e-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.35e+117], N[(R * N[(phi1 * N[(phi2 / phi1), $MachinePrecision] + (-phi1)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, -1.86e-51], N[(phi2 * N[(R - N[(N[(R * phi1), $MachinePrecision] / phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 4.6e-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.35 \cdot 10^{+117}:\\
\;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\

\mathbf{elif}\;\phi_1 \leq -1.86 \cdot 10^{-51}:\\
\;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\

\mathbf{elif}\;\phi_1 \leq 4.6 \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 4 regimes
  2. if phi1 < -1.3500000000000001e117

    1. Initial program 46.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}{\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.f6486.1

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

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

    if -1.3500000000000001e117 < phi1 < -1.8600000000000001e-51

    1. Initial program 55.6%

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lower-*.f6455.8

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified55.8%

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

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

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

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

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

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

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

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

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

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

    if -1.8600000000000001e-51 < phi1 < 4.60000000000000032e-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 \sqrt{\color{blue}{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
      8. lower-*.f6470.1

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

      \[\leadsto R \cdot \color{blue}{\sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \phi_2 \cdot \phi_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 4.60000000000000032e-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 phi2 around 0

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified60.9%

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.35 \cdot 10^{+117}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq -1.86 \cdot 10^{-51}:\\ \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\ \mathbf{elif}\;\phi_1 \leq 4.6 \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, 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 -2.05 \cdot 10^{-51}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq 4.6 \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 -2.05e-51)
   (* R (fma phi1 (/ phi2 phi1) (- phi1)))
   (if (<= phi1 4.6e-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 <= -2.05e-51) {
		tmp = R * fma(phi1, (phi2 / phi1), -phi1);
	} else if (phi1 <= 4.6e-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 <= -2.05e-51)
		tmp = Float64(R * fma(phi1, Float64(phi2 / phi1), Float64(-phi1)));
	elseif (phi1 <= 4.6e-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, -2.05e-51], N[(R * N[(phi1 * N[(phi2 / phi1), $MachinePrecision] + (-phi1)), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi1, 4.6e-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 -2.05 \cdot 10^{-51}:\\
\;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\

\mathbf{elif}\;\phi_1 \leq 4.6 \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 < -2.04999999999999987e-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 -2.04999999999999987e-51 < phi1 < 4.60000000000000032e-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 \sqrt{\color{blue}{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
      8. lower-*.f6470.1

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

      \[\leadsto R \cdot \color{blue}{\sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \phi_2 \cdot \phi_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 4.60000000000000032e-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 phi2 around 0

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified60.9%

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

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

        \[\leadsto \color{blue}{R \cdot \phi_2} \]
    8. 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 -2.05 \cdot 10^{-51}:\\ \;\;\;\;R \cdot \mathsf{fma}\left(\phi_1, \frac{\phi_2}{\phi_1}, -\phi_1\right)\\ \mathbf{elif}\;\phi_1 \leq 4.6 \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 9: 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 9.4 \cdot 10^{+190}:\\ \;\;\;\;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 9.4e+190)
   (* 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 <= 9.4e+190) {
		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 <= 9.4e+190)
		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, 9.4e+190], 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 9.4 \cdot 10^{+190}:\\
\;\;\;\;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 < 9.3999999999999996e190

    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 9.3999999999999996e190 < 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 \sqrt{\color{blue}{{\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    4. Step-by-step derivation
      1. *-commutativeN/A

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lower-*.f6454.8

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified54.8%

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
      8. lower-*.f6454.8

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

      \[\leadsto R \cdot \color{blue}{\sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \phi_2 \cdot \phi_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 9.4 \cdot 10^{+190}:\\ \;\;\;\;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 10: 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 9.2 \cdot 10^{-189}:\\ \;\;\;\;-R \cdot \phi_1\\ \mathbf{elif}\;\phi_2 \leq 4.9 \cdot 10^{-116}:\\ \;\;\;\;-R \cdot \lambda_1\\ \mathbf{elif}\;\phi_2 \leq 3.3 \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 9.2e-189)
   (- (* R phi1))
   (if (<= phi2 4.9e-116)
     (- (* R lambda1))
     (if (<= phi2 3.3e+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 <= 9.2e-189) {
		tmp = -(R * phi1);
	} else if (phi2 <= 4.9e-116) {
		tmp = -(R * lambda1);
	} else if (phi2 <= 3.3e+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 <= 9.2d-189) then
        tmp = -(r * phi1)
    else if (phi2 <= 4.9d-116) then
        tmp = -(r * lambda1)
    else if (phi2 <= 3.3d+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 <= 9.2e-189) {
		tmp = -(R * phi1);
	} else if (phi2 <= 4.9e-116) {
		tmp = -(R * lambda1);
	} else if (phi2 <= 3.3e+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 <= 9.2e-189:
		tmp = -(R * phi1)
	elif phi2 <= 4.9e-116:
		tmp = -(R * lambda1)
	elif phi2 <= 3.3e+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 <= 9.2e-189)
		tmp = Float64(-Float64(R * phi1));
	elseif (phi2 <= 4.9e-116)
		tmp = Float64(-Float64(R * lambda1));
	elseif (phi2 <= 3.3e+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 <= 9.2e-189)
		tmp = -(R * phi1);
	elseif (phi2 <= 4.9e-116)
		tmp = -(R * lambda1);
	elseif (phi2 <= 3.3e+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, 9.2e-189], (-N[(R * phi1), $MachinePrecision]), If[LessEqual[phi2, 4.9e-116], (-N[(R * lambda1), $MachinePrecision]), If[LessEqual[phi2, 3.3e+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 9.2 \cdot 10^{-189}:\\
\;\;\;\;-R \cdot \phi_1\\

\mathbf{elif}\;\phi_2 \leq 4.9 \cdot 10^{-116}:\\
\;\;\;\;-R \cdot \lambda_1\\

\mathbf{elif}\;\phi_2 \leq 3.3 \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 < 9.1999999999999993e-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 9.1999999999999993e-189 < phi2 < 4.89999999999999977e-116

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified73.2%

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
      8. lower-*.f6453.1

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

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

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

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

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

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

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

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

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

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

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

    if 4.89999999999999977e-116 < phi2 < 3.3e14

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lower-*.f6460.8

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified60.8%

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
      8. lower-*.f6445.5

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
    8. Simplified45.5%

      \[\leadsto R \cdot \color{blue}{\sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \phi_2 \cdot \phi_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 3.3e14 < 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 0

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified50.2%

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

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

        \[\leadsto \color{blue}{R \cdot \phi_2} \]
    8. 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 9.2 \cdot 10^{-189}:\\ \;\;\;\;-R \cdot \phi_1\\ \mathbf{elif}\;\phi_2 \leq 4.9 \cdot 10^{-116}:\\ \;\;\;\;-R \cdot \lambda_1\\ \mathbf{elif}\;\phi_2 \leq 3.3 \cdot 10^{+14}:\\ \;\;\;\;R \cdot \lambda_2\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \]
  5. Add Preprocessing

Alternative 11: 50.8% 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 6 \cdot 10^{-130}:\\ \;\;\;\;-R \cdot \phi_1\\ \mathbf{elif}\;\phi_2 \leq 3.3 \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 6e-130)
   (- (* R phi1))
   (if (<= phi2 3.3e+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 <= 6e-130) {
		tmp = -(R * phi1);
	} else if (phi2 <= 3.3e+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 <= 6d-130) then
        tmp = -(r * phi1)
    else if (phi2 <= 3.3d+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 <= 6e-130) {
		tmp = -(R * phi1);
	} else if (phi2 <= 3.3e+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 <= 6e-130:
		tmp = -(R * phi1)
	elif phi2 <= 3.3e+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 <= 6e-130)
		tmp = Float64(-Float64(R * phi1));
	elseif (phi2 <= 3.3e+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 <= 6e-130)
		tmp = -(R * phi1);
	elseif (phi2 <= 3.3e+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, 6e-130], (-N[(R * phi1), $MachinePrecision]), If[LessEqual[phi2, 3.3e+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 \cdot 10^{-130}:\\
\;\;\;\;-R \cdot \phi_1\\

\mathbf{elif}\;\phi_2 \leq 3.3 \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 < 5.99999999999999972e-130

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

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

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

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

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

    if 5.99999999999999972e-130 < phi2 < 3.3e14

    1. Initial program 68.1%

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\color{blue}{\cos \left(\frac{1}{2} \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      9. lower-*.f6461.8

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified61.8%

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
      8. lower-*.f6448.4

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
    8. Simplified48.4%

      \[\leadsto R \cdot \color{blue}{\sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \phi_2 \cdot \phi_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-*.f6418.2

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

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

    if 3.3e14 < 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 0

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified50.2%

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

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

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

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

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

Alternative 12: 38.3% 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 3.3 \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 3.3e+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 <= 3.3e+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 <= 3.3d+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 <= 3.3e+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 <= 3.3e+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 <= 3.3e+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 <= 3.3e+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, 3.3e+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 3.3 \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 < 3.3e14

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified63.2%

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \color{blue}{\phi_2 \cdot \phi_2}\right)} \]
      8. lower-*.f6448.8

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

      \[\leadsto R \cdot \color{blue}{\sqrt{\mathsf{fma}\left(\lambda_1 - \lambda_2, \lambda_1 - \lambda_2, \phi_2 \cdot \phi_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 3.3e14 < 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 0

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    5. Simplified50.2%

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

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

        \[\leadsto \color{blue}{R \cdot \phi_2} \]
    8. 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 3.3 \cdot 10^{+14}:\\ \;\;\;\;R \cdot \lambda_2\\ \mathbf{else}:\\ \;\;\;\;\phi_2 \cdot R\\ \end{array} \]
  5. Add Preprocessing

Alternative 13: 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 phi2 around 0

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

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

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

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

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

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

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

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

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

      \[\leadsto R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot {\cos \color{blue}{\left(0.5 \cdot \phi_1\right)}}^{2} + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
  5. Simplified59.9%

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

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

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

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

    \[\leadsto \phi_2 \cdot R \]
  10. 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))))))