Equirectangular approximation to distance on a great circle

Percentage Accurate: 59.1% → 91.0%
Time: 9.8s
Alternatives: 11
Speedup: 8.5×

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))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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 11 alternatives:

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

Initial Program: 59.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\ R \cdot \sqrt{t\_0 \cdot t\_0 + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0)))))
   (* R (sqrt (+ (* t_0 t_0) (* (- phi1 phi2) (- phi1 phi2)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	return R * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0d0))
    code = r * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))))
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (lambda1 - lambda2) * Math.cos(((phi1 + phi2) / 2.0));
	return R * Math.sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = (lambda1 - lambda2) * math.cos(((phi1 + phi2) / 2.0))
	return R * math.sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))))
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(Float64(lambda1 - lambda2) * cos(Float64(Float64(phi1 + phi2) / 2.0)))
	return Float64(R * sqrt(Float64(Float64(t_0 * t_0) + Float64(Float64(phi1 - phi2) * Float64(phi1 - phi2)))))
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	tmp = R * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(N[(phi1 + phi2), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(R * N[Sqrt[N[(N[(t$95$0 * t$95$0), $MachinePrecision] + N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

Alternative 1: 91.0% accurate, 1.2× speedup?

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

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


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

    1. Initial program 60.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 7.5000000000000004e-13 < phi2

    1. Initial program 52.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\cos \left(-0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2\right)} \]
  3. Recombined 2 regimes into one program.
  4. 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 2.9 \cdot 10^{+17}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\cos \left(-0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \lambda_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 (<= phi2 2.9e+17)
   (* R (hypot (* (cos (* -0.5 phi1)) (- lambda1 lambda2)) phi1))
   (* R (hypot (* (cos (* 0.5 phi2)) lambda1) 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 <= 2.9e+17) {
		tmp = R * hypot((cos((-0.5 * phi1)) * (lambda1 - lambda2)), phi1);
	} else {
		tmp = R * hypot((cos((0.5 * phi2)) * lambda1), phi2);
	}
	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.9e+17) {
		tmp = R * Math.hypot((Math.cos((-0.5 * phi1)) * (lambda1 - lambda2)), phi1);
	} else {
		tmp = R * Math.hypot((Math.cos((0.5 * phi2)) * lambda1), phi2);
	}
	return tmp;
}
[R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 2.9e+17:
		tmp = R * math.hypot((math.cos((-0.5 * phi1)) * (lambda1 - lambda2)), phi1)
	else:
		tmp = R * math.hypot((math.cos((0.5 * phi2)) * lambda1), 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 <= 2.9e+17)
		tmp = Float64(R * hypot(Float64(cos(Float64(-0.5 * phi1)) * Float64(lambda1 - lambda2)), phi1));
	else
		tmp = Float64(R * hypot(Float64(cos(Float64(0.5 * phi2)) * lambda1), phi2));
	end
	return tmp
end
R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 2.9e+17)
		tmp = R * hypot((cos((-0.5 * phi1)) * (lambda1 - lambda2)), phi1);
	else
		tmp = R * hypot((cos((0.5 * phi2)) * lambda1), phi2);
	end
	tmp_2 = tmp;
end
NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 2.9e+17], N[(R * N[Sqrt[N[(N[Cos[N[(-0.5 * phi1), $MachinePrecision]], $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] ^ 2 + phi1 ^ 2], $MachinePrecision]), $MachinePrecision], N[(R * N[Sqrt[N[(N[Cos[N[(0.5 * phi2), $MachinePrecision]], $MachinePrecision] * lambda1), $MachinePrecision] ^ 2 + phi2 ^ 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.9 \cdot 10^{+17}:\\
\;\;\;\;R \cdot \mathsf{hypot}\left(\cos \left(-0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right)\\

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


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

    1. Initial program 61.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.9e17 < phi2

    1. Initial program 48.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    Alternative 3: 80.6% accurate, 1.3× speedup?

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

      1. Initial program 61.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        if 2.9e17 < phi2

        1. Initial program 48.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        Alternative 4: 79.9% accurate, 2.3× speedup?

        \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := 1 \cdot \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq 1.35 \cdot 10^{+16}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(t\_0, \phi_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(t\_0, \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
         (let* ((t_0 (* 1.0 (- lambda1 lambda2))))
           (if (<= phi2 1.35e+16) (* R (hypot t_0 phi1)) (* R (hypot t_0 phi2)))))
        assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
        double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
        	double t_0 = 1.0 * (lambda1 - lambda2);
        	double tmp;
        	if (phi2 <= 1.35e+16) {
        		tmp = R * hypot(t_0, phi1);
        	} else {
        		tmp = R * hypot(t_0, phi2);
        	}
        	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 t_0 = 1.0 * (lambda1 - lambda2);
        	double tmp;
        	if (phi2 <= 1.35e+16) {
        		tmp = R * Math.hypot(t_0, phi1);
        	} else {
        		tmp = R * Math.hypot(t_0, phi2);
        	}
        	return tmp;
        }
        
        [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
        def code(R, lambda1, lambda2, phi1, phi2):
        	t_0 = 1.0 * (lambda1 - lambda2)
        	tmp = 0
        	if phi2 <= 1.35e+16:
        		tmp = R * math.hypot(t_0, phi1)
        	else:
        		tmp = R * math.hypot(t_0, phi2)
        	return tmp
        
        R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
        function code(R, lambda1, lambda2, phi1, phi2)
        	t_0 = Float64(1.0 * Float64(lambda1 - lambda2))
        	tmp = 0.0
        	if (phi2 <= 1.35e+16)
        		tmp = Float64(R * hypot(t_0, phi1));
        	else
        		tmp = Float64(R * hypot(t_0, phi2));
        	end
        	return tmp
        end
        
        R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
        function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
        	t_0 = 1.0 * (lambda1 - lambda2);
        	tmp = 0.0;
        	if (phi2 <= 1.35e+16)
        		tmp = R * hypot(t_0, phi1);
        	else
        		tmp = R * hypot(t_0, phi2);
        	end
        	tmp_2 = tmp;
        end
        
        NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
        code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(1.0 * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, 1.35e+16], N[(R * N[Sqrt[t$95$0 ^ 2 + phi1 ^ 2], $MachinePrecision]), $MachinePrecision], N[(R * N[Sqrt[t$95$0 ^ 2 + phi2 ^ 2], $MachinePrecision]), $MachinePrecision]]]
        
        \begin{array}{l}
        [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
        \\
        \begin{array}{l}
        t_0 := 1 \cdot \left(\lambda_1 - \lambda_2\right)\\
        \mathbf{if}\;\phi_2 \leq 1.35 \cdot 10^{+16}:\\
        \;\;\;\;R \cdot \mathsf{hypot}\left(t\_0, \phi_1\right)\\
        
        \mathbf{else}:\\
        \;\;\;\;R \cdot \mathsf{hypot}\left(t\_0, \phi_2\right)\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if phi2 < 1.35e16

          1. Initial program 61.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            if 1.35e16 < phi2

            1. Initial program 48.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            Alternative 5: 79.4% accurate, 2.3× speedup?

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

              1. Initial program 61.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                if 1.8e17 < phi2

                1. Initial program 48.8%

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

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

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

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

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

                    \[\leadsto \color{blue}{\left(-\phi_1\right)} \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right) \]
                  5. fp-cancel-sign-sub-invN/A

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

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

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

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

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

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

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

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

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

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

                Alternative 6: 62.6% accurate, 7.2× speedup?

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

                  1. Initial program 47.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                      if -5.00000000000000004e154 < (-.f64 lambda1 lambda2)

                      1. Initial program 61.5%

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

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

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

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

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

                          \[\leadsto \color{blue}{\left(-\phi_1\right)} \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right) \]
                        5. fp-cancel-sign-sub-invN/A

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

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

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

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

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

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

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

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

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

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

                      Alternative 7: 60.1% accurate, 8.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 3.15 \cdot 10^{+122}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \left(R - \phi_2 \cdot \frac{R}{\phi_1}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-R\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 (<= phi2 3.15e+122)
                         (* (- phi1) (- R (* phi2 (/ R phi1))))
                         (* (- R) (- 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 (phi2 <= 3.15e+122) {
                      		tmp = -phi1 * (R - (phi2 * (R / phi1)));
                      	} else {
                      		tmp = -R * (phi1 - phi2);
                      	}
                      	return tmp;
                      }
                      
                      NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                      module fmin_fmax_functions
                          implicit none
                          private
                          public fmax
                          public fmin
                      
                          interface fmax
                              module procedure fmax88
                              module procedure fmax44
                              module procedure fmax84
                              module procedure fmax48
                          end interface
                          interface fmin
                              module procedure fmin88
                              module procedure fmin44
                              module procedure fmin84
                              module procedure fmin48
                          end interface
                      contains
                          real(8) function fmax88(x, y) result (res)
                              real(8), intent (in) :: x
                              real(8), intent (in) :: y
                              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                          end function
                          real(4) function fmax44(x, y) result (res)
                              real(4), intent (in) :: x
                              real(4), intent (in) :: y
                              res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                          end function
                          real(8) function fmax84(x, y) result(res)
                              real(8), intent (in) :: x
                              real(4), intent (in) :: y
                              res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                          end function
                          real(8) function fmax48(x, y) result(res)
                              real(4), intent (in) :: x
                              real(8), intent (in) :: y
                              res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                          end function
                          real(8) function fmin88(x, y) result (res)
                              real(8), intent (in) :: x
                              real(8), intent (in) :: y
                              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                          end function
                          real(4) function fmin44(x, y) result (res)
                              real(4), intent (in) :: x
                              real(4), intent (in) :: y
                              res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                          end function
                          real(8) function fmin84(x, y) result(res)
                              real(8), intent (in) :: x
                              real(4), intent (in) :: y
                              res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                          end function
                          real(8) function fmin48(x, y) result(res)
                              real(4), intent (in) :: x
                              real(8), intent (in) :: y
                              res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                          end function
                      end module
                      
                      real(8) function code(r, lambda1, lambda2, phi1, phi2)
                      use fmin_fmax_functions
                          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.15d+122) then
                              tmp = -phi1 * (r - (phi2 * (r / phi1)))
                          else
                              tmp = -r * (phi1 - phi2)
                          end if
                          code = tmp
                      end function
                      
                      assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
                      public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                      	double tmp;
                      	if (phi2 <= 3.15e+122) {
                      		tmp = -phi1 * (R - (phi2 * (R / phi1)));
                      	} else {
                      		tmp = -R * (phi1 - phi2);
                      	}
                      	return tmp;
                      }
                      
                      [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                      def code(R, lambda1, lambda2, phi1, phi2):
                      	tmp = 0
                      	if phi2 <= 3.15e+122:
                      		tmp = -phi1 * (R - (phi2 * (R / phi1)))
                      	else:
                      		tmp = -R * (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 (phi2 <= 3.15e+122)
                      		tmp = Float64(Float64(-phi1) * Float64(R - Float64(phi2 * Float64(R / phi1))));
                      	else
                      		tmp = Float64(Float64(-R) * Float64(phi1 - phi2));
                      	end
                      	return tmp
                      end
                      
                      R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
                      function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
                      	tmp = 0.0;
                      	if (phi2 <= 3.15e+122)
                      		tmp = -phi1 * (R - (phi2 * (R / phi1)));
                      	else
                      		tmp = -R * (phi1 - phi2);
                      	end
                      	tmp_2 = tmp;
                      end
                      
                      NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                      code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 3.15e+122], N[((-phi1) * N[(R - N[(phi2 * N[(R / phi1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-R) * N[(phi1 - 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 3.15 \cdot 10^{+122}:\\
                      \;\;\;\;\left(-\phi_1\right) \cdot \left(R - \phi_2 \cdot \frac{R}{\phi_1}\right)\\
                      
                      \mathbf{else}:\\
                      \;\;\;\;\left(-R\right) \cdot \left(\phi_1 - \phi_2\right)\\
                      
                      
                      \end{array}
                      \end{array}
                      
                      Derivation
                      1. Split input into 2 regimes
                      2. if phi2 < 3.1500000000000001e122

                        1. Initial program 61.8%

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

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

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

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

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

                            \[\leadsto \color{blue}{\left(-\phi_1\right)} \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right) \]
                          5. fp-cancel-sign-sub-invN/A

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

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

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

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

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

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

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

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

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

                          if 3.1500000000000001e122 < phi2

                          1. Initial program 45.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 \color{blue}{-1 \cdot \left(\phi_1 \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right)\right)} \]
                          4. Step-by-step derivation
                            1. associate-*r*N/A

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

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

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

                              \[\leadsto \color{blue}{\left(-\phi_1\right)} \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right) \]
                            5. fp-cancel-sign-sub-invN/A

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

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

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

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

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

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

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

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

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

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

                          Alternative 8: 58.6% accurate, 9.0× speedup?

                          \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;R \leq 10^{+36}:\\ \;\;\;\;\left(-R\right) \cdot \left(\phi_1 - \phi_2\right)\\ \mathbf{else}:\\ \;\;\;\;\left(R \cdot \left(-1 + \frac{\phi_2}{\phi_1}\right)\right) \cdot \phi_1\\ \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 (<= R 1e+36)
                             (* (- R) (- phi1 phi2))
                             (* (* R (+ -1.0 (/ phi2 phi1))) phi1)))
                          assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                          double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                          	double tmp;
                          	if (R <= 1e+36) {
                          		tmp = -R * (phi1 - phi2);
                          	} else {
                          		tmp = (R * (-1.0 + (phi2 / phi1))) * phi1;
                          	}
                          	return tmp;
                          }
                          
                          NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                          module fmin_fmax_functions
                              implicit none
                              private
                              public fmax
                              public fmin
                          
                              interface fmax
                                  module procedure fmax88
                                  module procedure fmax44
                                  module procedure fmax84
                                  module procedure fmax48
                              end interface
                              interface fmin
                                  module procedure fmin88
                                  module procedure fmin44
                                  module procedure fmin84
                                  module procedure fmin48
                              end interface
                          contains
                              real(8) function fmax88(x, y) result (res)
                                  real(8), intent (in) :: x
                                  real(8), intent (in) :: y
                                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                              end function
                              real(4) function fmax44(x, y) result (res)
                                  real(4), intent (in) :: x
                                  real(4), intent (in) :: y
                                  res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                              end function
                              real(8) function fmax84(x, y) result(res)
                                  real(8), intent (in) :: x
                                  real(4), intent (in) :: y
                                  res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                              end function
                              real(8) function fmax48(x, y) result(res)
                                  real(4), intent (in) :: x
                                  real(8), intent (in) :: y
                                  res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                              end function
                              real(8) function fmin88(x, y) result (res)
                                  real(8), intent (in) :: x
                                  real(8), intent (in) :: y
                                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                              end function
                              real(4) function fmin44(x, y) result (res)
                                  real(4), intent (in) :: x
                                  real(4), intent (in) :: y
                                  res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                              end function
                              real(8) function fmin84(x, y) result(res)
                                  real(8), intent (in) :: x
                                  real(4), intent (in) :: y
                                  res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                              end function
                              real(8) function fmin48(x, y) result(res)
                                  real(4), intent (in) :: x
                                  real(8), intent (in) :: y
                                  res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                              end function
                          end module
                          
                          real(8) function code(r, lambda1, lambda2, phi1, phi2)
                          use fmin_fmax_functions
                              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 (r <= 1d+36) then
                                  tmp = -r * (phi1 - phi2)
                              else
                                  tmp = (r * ((-1.0d0) + (phi2 / phi1))) * phi1
                              end if
                              code = tmp
                          end function
                          
                          assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
                          public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                          	double tmp;
                          	if (R <= 1e+36) {
                          		tmp = -R * (phi1 - phi2);
                          	} else {
                          		tmp = (R * (-1.0 + (phi2 / phi1))) * phi1;
                          	}
                          	return tmp;
                          }
                          
                          [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                          def code(R, lambda1, lambda2, phi1, phi2):
                          	tmp = 0
                          	if R <= 1e+36:
                          		tmp = -R * (phi1 - phi2)
                          	else:
                          		tmp = (R * (-1.0 + (phi2 / phi1))) * phi1
                          	return tmp
                          
                          R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                          function code(R, lambda1, lambda2, phi1, phi2)
                          	tmp = 0.0
                          	if (R <= 1e+36)
                          		tmp = Float64(Float64(-R) * Float64(phi1 - phi2));
                          	else
                          		tmp = Float64(Float64(R * Float64(-1.0 + Float64(phi2 / phi1))) * phi1);
                          	end
                          	return tmp
                          end
                          
                          R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
                          function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
                          	tmp = 0.0;
                          	if (R <= 1e+36)
                          		tmp = -R * (phi1 - phi2);
                          	else
                          		tmp = (R * (-1.0 + (phi2 / phi1))) * phi1;
                          	end
                          	tmp_2 = tmp;
                          end
                          
                          NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                          code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[R, 1e+36], N[((-R) * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision], N[(N[(R * N[(-1.0 + N[(phi2 / phi1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * phi1), $MachinePrecision]]
                          
                          \begin{array}{l}
                          [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                          \\
                          \begin{array}{l}
                          \mathbf{if}\;R \leq 10^{+36}:\\
                          \;\;\;\;\left(-R\right) \cdot \left(\phi_1 - \phi_2\right)\\
                          
                          \mathbf{else}:\\
                          \;\;\;\;\left(R \cdot \left(-1 + \frac{\phi_2}{\phi_1}\right)\right) \cdot \phi_1\\
                          
                          
                          \end{array}
                          \end{array}
                          
                          Derivation
                          1. Split input into 2 regimes
                          2. if R < 1.00000000000000004e36

                            1. Initial program 50.7%

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

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

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

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

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

                                \[\leadsto \color{blue}{\left(-\phi_1\right)} \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right) \]
                              5. fp-cancel-sign-sub-invN/A

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

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

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

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

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

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

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

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

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

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

                              if 1.00000000000000004e36 < R

                              1. Initial program 89.4%

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

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

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

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

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

                                  \[\leadsto \color{blue}{\left(-\phi_1\right)} \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right) \]
                                5. fp-cancel-sign-sub-invN/A

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

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

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

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

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

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

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

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

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

                                  \[\leadsto \left(R \cdot \left(-1 + \frac{\phi_2}{\phi_1}\right)\right) \cdot \color{blue}{\phi_1} \]
                              8. Recombined 2 regimes into one program.
                              9. Add Preprocessing

                              Alternative 9: 52.5% accurate, 19.9× speedup?

                              \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 8.5 \cdot 10^{+15}:\\ \;\;\;\;R \cdot \left(-\phi_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \phi_2\\ \end{array} \end{array} \]
                              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                              (FPCore (R lambda1 lambda2 phi1 phi2)
                               :precision binary64
                               (if (<= phi2 8.5e+15) (* R (- phi1)) (* R phi2)))
                              assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                              double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                              	double tmp;
                              	if (phi2 <= 8.5e+15) {
                              		tmp = R * -phi1;
                              	} else {
                              		tmp = R * phi2;
                              	}
                              	return tmp;
                              }
                              
                              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                              module fmin_fmax_functions
                                  implicit none
                                  private
                                  public fmax
                                  public fmin
                              
                                  interface fmax
                                      module procedure fmax88
                                      module procedure fmax44
                                      module procedure fmax84
                                      module procedure fmax48
                                  end interface
                                  interface fmin
                                      module procedure fmin88
                                      module procedure fmin44
                                      module procedure fmin84
                                      module procedure fmin48
                                  end interface
                              contains
                                  real(8) function fmax88(x, y) result (res)
                                      real(8), intent (in) :: x
                                      real(8), intent (in) :: y
                                      res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                                  end function
                                  real(4) function fmax44(x, y) result (res)
                                      real(4), intent (in) :: x
                                      real(4), intent (in) :: y
                                      res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                                  end function
                                  real(8) function fmax84(x, y) result(res)
                                      real(8), intent (in) :: x
                                      real(4), intent (in) :: y
                                      res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                                  end function
                                  real(8) function fmax48(x, y) result(res)
                                      real(4), intent (in) :: x
                                      real(8), intent (in) :: y
                                      res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                                  end function
                                  real(8) function fmin88(x, y) result (res)
                                      real(8), intent (in) :: x
                                      real(8), intent (in) :: y
                                      res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                                  end function
                                  real(4) function fmin44(x, y) result (res)
                                      real(4), intent (in) :: x
                                      real(4), intent (in) :: y
                                      res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                                  end function
                                  real(8) function fmin84(x, y) result(res)
                                      real(8), intent (in) :: x
                                      real(4), intent (in) :: y
                                      res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                                  end function
                                  real(8) function fmin48(x, y) result(res)
                                      real(4), intent (in) :: x
                                      real(8), intent (in) :: y
                                      res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                                  end function
                              end module
                              
                              real(8) function code(r, lambda1, lambda2, phi1, phi2)
                              use fmin_fmax_functions
                                  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 <= 8.5d+15) then
                                      tmp = r * -phi1
                                  else
                                      tmp = r * phi2
                                  end if
                                  code = tmp
                              end function
                              
                              assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
                              public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                              	double tmp;
                              	if (phi2 <= 8.5e+15) {
                              		tmp = R * -phi1;
                              	} else {
                              		tmp = R * phi2;
                              	}
                              	return tmp;
                              }
                              
                              [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                              def code(R, lambda1, lambda2, phi1, phi2):
                              	tmp = 0
                              	if phi2 <= 8.5e+15:
                              		tmp = R * -phi1
                              	else:
                              		tmp = R * phi2
                              	return tmp
                              
                              R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                              function code(R, lambda1, lambda2, phi1, phi2)
                              	tmp = 0.0
                              	if (phi2 <= 8.5e+15)
                              		tmp = Float64(R * Float64(-phi1));
                              	else
                              		tmp = Float64(R * phi2);
                              	end
                              	return tmp
                              end
                              
                              R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
                              function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
                              	tmp = 0.0;
                              	if (phi2 <= 8.5e+15)
                              		tmp = R * -phi1;
                              	else
                              		tmp = R * phi2;
                              	end
                              	tmp_2 = tmp;
                              end
                              
                              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                              code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 8.5e+15], N[(R * (-phi1)), $MachinePrecision], N[(R * phi2), $MachinePrecision]]
                              
                              \begin{array}{l}
                              [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                              \\
                              \begin{array}{l}
                              \mathbf{if}\;\phi_2 \leq 8.5 \cdot 10^{+15}:\\
                              \;\;\;\;R \cdot \left(-\phi_1\right)\\
                              
                              \mathbf{else}:\\
                              \;\;\;\;R \cdot \phi_2\\
                              
                              
                              \end{array}
                              \end{array}
                              
                              Derivation
                              1. Split input into 2 regimes
                              2. if phi2 < 8.5e15

                                1. Initial program 61.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.f6421.1

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

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

                                if 8.5e15 < phi2

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

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

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

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

                              Alternative 10: 58.0% accurate, 25.4× speedup?

                              \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \left(-R\right) \cdot \left(\phi_1 - \phi_2\right) \end{array} \]
                              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                              (FPCore (R lambda1 lambda2 phi1 phi2)
                               :precision binary64
                               (* (- R) (- phi1 phi2)))
                              assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                              double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                              	return -R * (phi1 - phi2);
                              }
                              
                              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                              module fmin_fmax_functions
                                  implicit none
                                  private
                                  public fmax
                                  public fmin
                              
                                  interface fmax
                                      module procedure fmax88
                                      module procedure fmax44
                                      module procedure fmax84
                                      module procedure fmax48
                                  end interface
                                  interface fmin
                                      module procedure fmin88
                                      module procedure fmin44
                                      module procedure fmin84
                                      module procedure fmin48
                                  end interface
                              contains
                                  real(8) function fmax88(x, y) result (res)
                                      real(8), intent (in) :: x
                                      real(8), intent (in) :: y
                                      res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                                  end function
                                  real(4) function fmax44(x, y) result (res)
                                      real(4), intent (in) :: x
                                      real(4), intent (in) :: y
                                      res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                                  end function
                                  real(8) function fmax84(x, y) result(res)
                                      real(8), intent (in) :: x
                                      real(4), intent (in) :: y
                                      res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                                  end function
                                  real(8) function fmax48(x, y) result(res)
                                      real(4), intent (in) :: x
                                      real(8), intent (in) :: y
                                      res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                                  end function
                                  real(8) function fmin88(x, y) result (res)
                                      real(8), intent (in) :: x
                                      real(8), intent (in) :: y
                                      res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                                  end function
                                  real(4) function fmin44(x, y) result (res)
                                      real(4), intent (in) :: x
                                      real(4), intent (in) :: y
                                      res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                                  end function
                                  real(8) function fmin84(x, y) result(res)
                                      real(8), intent (in) :: x
                                      real(4), intent (in) :: y
                                      res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                                  end function
                                  real(8) function fmin48(x, y) result(res)
                                      real(4), intent (in) :: x
                                      real(8), intent (in) :: y
                                      res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                                  end function
                              end module
                              
                              real(8) function code(r, lambda1, lambda2, phi1, phi2)
                              use fmin_fmax_functions
                                  real(8), intent (in) :: r
                                  real(8), intent (in) :: lambda1
                                  real(8), intent (in) :: lambda2
                                  real(8), intent (in) :: phi1
                                  real(8), intent (in) :: phi2
                                  code = -r * (phi1 - phi2)
                              end function
                              
                              assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
                              public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                              	return -R * (phi1 - phi2);
                              }
                              
                              [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                              def code(R, lambda1, lambda2, phi1, phi2):
                              	return -R * (phi1 - phi2)
                              
                              R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                              function code(R, lambda1, lambda2, phi1, phi2)
                              	return Float64(Float64(-R) * Float64(phi1 - phi2))
                              end
                              
                              R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
                              function tmp = code(R, lambda1, lambda2, phi1, phi2)
                              	tmp = -R * (phi1 - phi2);
                              end
                              
                              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                              code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[((-R) * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]
                              
                              \begin{array}{l}
                              [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                              \\
                              \left(-R\right) \cdot \left(\phi_1 - \phi_2\right)
                              \end{array}
                              
                              Derivation
                              1. Initial program 58.4%

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

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

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

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

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

                                  \[\leadsto \color{blue}{\left(-\phi_1\right)} \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right) \]
                                5. fp-cancel-sign-sub-invN/A

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

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

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

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

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

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

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

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

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

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

                                Alternative 11: 31.2% accurate, 46.5× speedup?

                                \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ R \cdot \phi_2 \end{array} \]
                                NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                                (FPCore (R lambda1 lambda2 phi1 phi2) :precision binary64 (* R phi2))
                                assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
                                double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                                	return R * phi2;
                                }
                                
                                NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                                module fmin_fmax_functions
                                    implicit none
                                    private
                                    public fmax
                                    public fmin
                                
                                    interface fmax
                                        module procedure fmax88
                                        module procedure fmax44
                                        module procedure fmax84
                                        module procedure fmax48
                                    end interface
                                    interface fmin
                                        module procedure fmin88
                                        module procedure fmin44
                                        module procedure fmin84
                                        module procedure fmin48
                                    end interface
                                contains
                                    real(8) function fmax88(x, y) result (res)
                                        real(8), intent (in) :: x
                                        real(8), intent (in) :: y
                                        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                                    end function
                                    real(4) function fmax44(x, y) result (res)
                                        real(4), intent (in) :: x
                                        real(4), intent (in) :: y
                                        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                                    end function
                                    real(8) function fmax84(x, y) result(res)
                                        real(8), intent (in) :: x
                                        real(4), intent (in) :: y
                                        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                                    end function
                                    real(8) function fmax48(x, y) result(res)
                                        real(4), intent (in) :: x
                                        real(8), intent (in) :: y
                                        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                                    end function
                                    real(8) function fmin88(x, y) result (res)
                                        real(8), intent (in) :: x
                                        real(8), intent (in) :: y
                                        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                                    end function
                                    real(4) function fmin44(x, y) result (res)
                                        real(4), intent (in) :: x
                                        real(4), intent (in) :: y
                                        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                                    end function
                                    real(8) function fmin84(x, y) result(res)
                                        real(8), intent (in) :: x
                                        real(4), intent (in) :: y
                                        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                                    end function
                                    real(8) function fmin48(x, y) result(res)
                                        real(4), intent (in) :: x
                                        real(8), intent (in) :: y
                                        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                                    end function
                                end module
                                
                                real(8) function code(r, lambda1, lambda2, phi1, phi2)
                                use fmin_fmax_functions
                                    real(8), intent (in) :: r
                                    real(8), intent (in) :: lambda1
                                    real(8), intent (in) :: lambda2
                                    real(8), intent (in) :: phi1
                                    real(8), intent (in) :: phi2
                                    code = r * phi2
                                end function
                                
                                assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
                                public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
                                	return R * phi2;
                                }
                                
                                [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                                def code(R, lambda1, lambda2, phi1, phi2):
                                	return R * phi2
                                
                                R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                                function code(R, lambda1, lambda2, phi1, phi2)
                                	return Float64(R * phi2)
                                end
                                
                                R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
                                function tmp = code(R, lambda1, lambda2, phi1, phi2)
                                	tmp = R * phi2;
                                end
                                
                                NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
                                code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * phi2), $MachinePrecision]
                                
                                \begin{array}{l}
                                [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                                \\
                                R \cdot \phi_2
                                \end{array}
                                
                                Derivation
                                1. Initial program 58.4%

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

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

                                    \[\leadsto \color{blue}{R \cdot \phi_2} \]
                                5. Applied rewrites19.4%

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

                                Reproduce

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