Equirectangular approximation to distance on a great circle

Percentage Accurate: 59.7% → 96.0%
Time: 12.1s
Alternatives: 15
Speedup: 2.1×

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 15 alternatives:

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

Initial Program: 59.7% accurate, 1.0× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -1.50000000000000004e-5

    1. Initial program 49.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. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

        \[\leadsto R \cdot \color{blue}{\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)}} \]
      3. lift-+.f64N/A

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

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

      \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    6. Step-by-step derivation
      1. lift-*.f6491.2

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

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

    if -1.50000000000000004e-5 < phi1

    1. Initial program 61.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. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

        \[\leadsto R \cdot \color{blue}{\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)}} \]
      3. lift-+.f64N/A

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

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

      \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    6. Step-by-step derivation
      1. lower-*.f6493.9

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

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

Alternative 2: 90.7% accurate, 1.2× speedup?

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

\mathbf{elif}\;\phi_2 \leq 7 \cdot 10^{+78}:\\
\;\;\;\;\mathsf{hypot}\left(\phi_1, t\_0 \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\mathsf{hypot}\left(\phi_1 - \phi_2, t\_0 \cdot \lambda_1\right) \cdot R\\


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

    1. Initial program 58.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. Step-by-step derivation
      1. lift-*.f64N/A

        \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

        \[\leadsto R \cdot \color{blue}{\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)}} \]
      3. lift-+.f64N/A

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

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

      \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    6. Step-by-step derivation
      1. lift-*.f6494.1

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

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

    if 3.40000000000000028e-48 < phi2 < 7.0000000000000003e78

    1. Initial program 51.8%

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

        \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

        \[\leadsto R \cdot \color{blue}{\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)}} \]
      3. lift-+.f64N/A

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

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

      \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    6. Step-by-step derivation
      1. lower-*.f6484.5

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

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

      \[\leadsto \mathsf{hypot}\left(\color{blue}{\phi_1}, \cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    9. Step-by-step derivation
      1. Applied rewrites84.5%

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

      if 7.0000000000000003e78 < phi2

      1. Initial program 59.6%

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

          \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

          \[\leadsto R \cdot \color{blue}{\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)}} \]
        3. lift-+.f64N/A

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. Step-by-step derivation
        1. lower-*.f6494.5

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \color{blue}{\lambda_1}\right) \cdot R \]
      9. Step-by-step derivation
        1. Applied rewrites86.3%

          \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(0.5 \cdot \phi_2\right) \cdot \color{blue}{\lambda_1}\right) \cdot R \]
      10. Recombined 3 regimes into one program.
      11. Add Preprocessing

      Alternative 3: 90.1% accurate, 1.2× speedup?

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

        1. Initial program 58.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. Step-by-step derivation
          1. lift-*.f64N/A

            \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

            \[\leadsto R \cdot \color{blue}{\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)}} \]
          3. lift-+.f64N/A

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

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

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

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

            \[\leadsto \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) + {\phi_1}^{2}} \cdot R \]
          3. pow2N/A

            \[\leadsto \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) + \phi_1 \cdot \phi_1} \cdot R \]
          4. lower-hypot.f64N/A

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

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

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

            \[\leadsto \mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \cdot R \]
          8. lift--.f6482.4

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

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

        if 3.40000000000000028e-48 < phi2 < 7.0000000000000003e78

        1. Initial program 51.8%

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

            \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

            \[\leadsto R \cdot \color{blue}{\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)}} \]
          3. lift-+.f64N/A

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

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

          \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
        6. Step-by-step derivation
          1. lower-*.f6484.5

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

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

          \[\leadsto \mathsf{hypot}\left(\color{blue}{\phi_1}, \cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
        9. Step-by-step derivation
          1. Applied rewrites84.5%

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

          if 7.0000000000000003e78 < phi2

          1. Initial program 59.6%

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

              \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

              \[\leadsto R \cdot \color{blue}{\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)}} \]
            3. lift-+.f64N/A

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

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

            \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
          6. Step-by-step derivation
            1. lower-*.f6494.5

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

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

            \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \color{blue}{\lambda_1}\right) \cdot R \]
          9. Step-by-step derivation
            1. Applied rewrites86.3%

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

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

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

            1. Initial program 58.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. Step-by-step derivation
              1. lift-*.f64N/A

                \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                \[\leadsto R \cdot \color{blue}{\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)}} \]
              3. lift-+.f64N/A

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

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

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

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

                \[\leadsto \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) + {\phi_1}^{2}} \cdot R \]
              3. pow2N/A

                \[\leadsto \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) + \phi_1 \cdot \phi_1} \cdot R \]
              4. lower-hypot.f64N/A

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

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

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

                \[\leadsto \mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \cdot R \]
              8. lift--.f6482.4

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

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

            if 3.40000000000000028e-48 < phi2 < 2.99999999999999987e80

            1. Initial program 51.8%

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

                \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                \[\leadsto R \cdot \color{blue}{\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)}} \]
              3. lift-+.f64N/A

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

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

              \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
            6. Step-by-step derivation
              1. lower-*.f6484.5

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

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

              \[\leadsto \mathsf{hypot}\left(\color{blue}{\phi_1}, \cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
            9. Step-by-step derivation
              1. Applied rewrites84.5%

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

              if 2.99999999999999987e80 < phi2

              1. Initial program 59.6%

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

                  \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                  \[\leadsto R \cdot \color{blue}{\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)}} \]
                3. lift-+.f64N/A

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

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

                \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
              6. Step-by-step derivation
                1. lift-*.f6487.9

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

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

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

                \[\leadsto \color{blue}{\left(\phi_2 \cdot \left(1 + \left(-\frac{\phi_1}{\phi_2}\right)\right)\right)} \cdot R \]
            10. Recombined 3 regimes into one program.
            11. Final simplification82.5%

              \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 3.4 \cdot 10^{-48}:\\ \;\;\;\;\mathsf{hypot}\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 3 \cdot 10^{+80}:\\ \;\;\;\;\mathsf{hypot}\left(\phi_1, \cos \left(0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\left(\phi_2 \cdot \left(1 - \frac{\phi_1}{\phi_2}\right)\right) \cdot R\\ \end{array} \]
            12. Add Preprocessing

            Alternative 5: 96.1% accurate, 1.2× speedup?

            \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(\frac{\phi_2 + \phi_1}{2}\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \end{array} \]
            NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
            (FPCore (R lambda1 lambda2 phi1 phi2)
             :precision binary64
             (*
              (hypot (- phi1 phi2) (* (cos (/ (+ phi2 phi1) 2.0)) (- lambda1 lambda2)))
              R))
            assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
            double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
            	return hypot((phi1 - phi2), (cos(((phi2 + phi1) / 2.0)) * (lambda1 - lambda2))) * R;
            }
            
            assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
            public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
            	return Math.hypot((phi1 - phi2), (Math.cos(((phi2 + phi1) / 2.0)) * (lambda1 - lambda2))) * R;
            }
            
            [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
            def code(R, lambda1, lambda2, phi1, phi2):
            	return math.hypot((phi1 - phi2), (math.cos(((phi2 + phi1) / 2.0)) * (lambda1 - lambda2))) * R
            
            R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
            function code(R, lambda1, lambda2, phi1, phi2)
            	return Float64(hypot(Float64(phi1 - phi2), Float64(cos(Float64(Float64(phi2 + phi1) / 2.0)) * Float64(lambda1 - lambda2))) * R)
            end
            
            R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
            function tmp = code(R, lambda1, lambda2, phi1, phi2)
            	tmp = hypot((phi1 - phi2), (cos(((phi2 + phi1) / 2.0)) * (lambda1 - lambda2))) * R;
            end
            
            NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
            code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[Sqrt[N[(phi1 - phi2), $MachinePrecision] ^ 2 + N[(N[Cos[N[(N[(phi2 + phi1), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision] * R), $MachinePrecision]
            
            \begin{array}{l}
            [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
            \\
            \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(\frac{\phi_2 + \phi_1}{2}\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R
            \end{array}
            
            Derivation
            1. Initial program 58.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. Step-by-step derivation
              1. lift-*.f64N/A

                \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                \[\leadsto R \cdot \color{blue}{\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)}} \]
              3. lift-+.f64N/A

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

              \[\leadsto \color{blue}{\mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(\frac{\phi_2 + \phi_1}{2}\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R} \]
            5. Add Preprocessing

            Alternative 6: 85.7% accurate, 1.2× speedup?

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

              1. Initial program 57.8%

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

                  \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                  \[\leadsto R \cdot \color{blue}{\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)}} \]
                3. lift-+.f64N/A

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

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

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

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

                  \[\leadsto \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) + {\phi_1}^{2}} \cdot R \]
                3. pow2N/A

                  \[\leadsto \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) + \phi_1 \cdot \phi_1} \cdot R \]
                4. lower-hypot.f64N/A

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

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

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

                  \[\leadsto \mathsf{hypot}\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1\right) \cdot R \]
                8. lift--.f6480.4

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

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

              if 8.2e79 < phi2

              1. Initial program 59.6%

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

                  \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                  \[\leadsto R \cdot \color{blue}{\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)}} \]
                3. lift-+.f64N/A

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

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

                \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
              6. Step-by-step derivation
                1. lift-*.f6487.9

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

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

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

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

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

            Alternative 7: 66.0% accurate, 2.0× speedup?

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

              1. Initial program 52.5%

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

                  \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                  \[\leadsto R \cdot \color{blue}{\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)}} \]
                3. lift-+.f64N/A

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

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

                \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
              6. Step-by-step derivation
                1. lift-*.f6488.2

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

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

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

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

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

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

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

                  \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                6. lower-*.f6419.8

                  \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
              10. Applied rewrites19.8%

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

              if -1.59999999999999997e-148 < phi2 < 2.4000000000000002e69

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

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

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

                  \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \color{blue}{\phi_1}, {\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}\right)} \]
                4. pow-prod-downN/A

                  \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \phi_1, {\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}^{2}\right)} \]
                5. lower-pow.f64N/A

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

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

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

                  \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \phi_1, {\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}^{2}\right)} \]
                9. lift--.f6460.4

                  \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \phi_1, {\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}^{2}\right)} \]
              5. Applied rewrites60.4%

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

                \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \phi_1, {\left(\lambda_1 - \lambda_2\right)}^{2}\right)} \]
              7. Step-by-step derivation
                1. lift--.f6458.5

                  \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \phi_1, {\left(\lambda_1 - \lambda_2\right)}^{2}\right)} \]
              8. Applied rewrites58.5%

                \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \phi_1, {\left(\lambda_1 - \lambda_2\right)}^{2}\right)} \]

              if 2.4000000000000002e69 < phi2

              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. Step-by-step derivation
                1. lift-*.f64N/A

                  \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                  \[\leadsto R \cdot \color{blue}{\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)}} \]
                3. lift-+.f64N/A

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

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

                \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
              6. Step-by-step derivation
                1. lift-*.f6488.1

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

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

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

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

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

            Alternative 8: 61.0% accurate, 2.1× speedup?

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

              1. Initial program 55.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. Step-by-step derivation
                1. lift-*.f64N/A

                  \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                  \[\leadsto R \cdot \color{blue}{\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)}} \]
                3. lift-+.f64N/A

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

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

                \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
              6. Step-by-step derivation
                1. lift-*.f6489.4

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

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

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

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

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

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

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

                  \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                6. lower-*.f6420.1

                  \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
              10. Applied rewrites20.1%

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

              if -2.3000000000000002e-196 < phi2 < 9.8000000000000003e27

              1. Initial program 64.4%

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

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

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

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

                  \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \color{blue}{\phi_1}, {\cos \left(\frac{1}{2} \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}\right)} \]
                4. pow-prod-downN/A

                  \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \phi_1, {\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}^{2}\right)} \]
                5. lower-pow.f64N/A

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

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

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

                  \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \phi_1, {\left(\cos \left(\frac{1}{2} \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}^{2}\right)} \]
                9. lift--.f6462.5

                  \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\phi_1, \phi_1, {\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}^{2}\right)} \]
              5. Applied rewrites62.5%

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

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

                  \[\leadsto R \cdot \sqrt{{\left(\lambda_1 - \lambda_2\right)}^{2}} \]
                2. lift--.f6445.5

                  \[\leadsto R \cdot \sqrt{{\left(\lambda_1 - \lambda_2\right)}^{2}} \]
              8. Applied rewrites45.5%

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

              if 9.8000000000000003e27 < phi2

              1. Initial program 52.3%

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

                  \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                  \[\leadsto R \cdot \color{blue}{\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)}} \]
                3. lift-+.f64N/A

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

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

                \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
              6. Step-by-step derivation
                1. lift-*.f6478.4

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

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

                \[\leadsto \color{blue}{\left(\phi_2 \cdot \left(1 + -1 \cdot \frac{\phi_1}{\phi_2}\right)\right)} \cdot R \]
              9. Applied rewrites68.0%

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

              \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -2.3 \cdot 10^{-196}:\\ \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\ \mathbf{elif}\;\phi_2 \leq 9.8 \cdot 10^{+27}:\\ \;\;\;\;R \cdot \sqrt{{\left(\lambda_1 - \lambda_2\right)}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\left(\phi_2 \cdot \left(1 - \frac{\phi_1}{\phi_2}\right)\right) \cdot R\\ \end{array} \]
            5. Add Preprocessing

            Alternative 9: 56.5% accurate, 2.2× speedup?

            \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.4 \cdot 10^{-227}:\\ \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\ \mathbf{elif}\;\phi_2 \leq 3.4 \cdot 10^{-282}:\\ \;\;\;\;\left(\lambda_2 \cdot \cos \left(0.5 \cdot \phi_2\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 6.2 \cdot 10^{+80}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \left(R - \frac{R \cdot \phi_2}{\phi_1}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\phi_2 \cdot \left(1 - \frac{\phi_1}{\phi_2}\right)\right) \cdot R\\ \end{array} \end{array} \]
            NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
            (FPCore (R lambda1 lambda2 phi1 phi2)
             :precision binary64
             (if (<= phi2 -1.4e-227)
               (* phi2 (- R (/ (* R phi1) phi2)))
               (if (<= phi2 3.4e-282)
                 (* (* lambda2 (cos (* 0.5 phi2))) R)
                 (if (<= phi2 6.2e+80)
                   (* (- phi1) (- R (/ (* R phi2) phi1)))
                   (* (* phi2 (- 1.0 (/ phi1 phi2))) R)))))
            assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
            double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
            	double tmp;
            	if (phi2 <= -1.4e-227) {
            		tmp = phi2 * (R - ((R * phi1) / phi2));
            	} else if (phi2 <= 3.4e-282) {
            		tmp = (lambda2 * cos((0.5 * phi2))) * R;
            	} else if (phi2 <= 6.2e+80) {
            		tmp = -phi1 * (R - ((R * phi2) / phi1));
            	} else {
            		tmp = (phi2 * (1.0 - (phi1 / phi2))) * R;
            	}
            	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 <= (-1.4d-227)) then
                    tmp = phi2 * (r - ((r * phi1) / phi2))
                else if (phi2 <= 3.4d-282) then
                    tmp = (lambda2 * cos((0.5d0 * phi2))) * r
                else if (phi2 <= 6.2d+80) then
                    tmp = -phi1 * (r - ((r * phi2) / phi1))
                else
                    tmp = (phi2 * (1.0d0 - (phi1 / phi2))) * r
                end if
                code = tmp
            end function
            
            assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
            public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
            	double tmp;
            	if (phi2 <= -1.4e-227) {
            		tmp = phi2 * (R - ((R * phi1) / phi2));
            	} else if (phi2 <= 3.4e-282) {
            		tmp = (lambda2 * Math.cos((0.5 * phi2))) * R;
            	} else if (phi2 <= 6.2e+80) {
            		tmp = -phi1 * (R - ((R * phi2) / phi1));
            	} else {
            		tmp = (phi2 * (1.0 - (phi1 / phi2))) * R;
            	}
            	return tmp;
            }
            
            [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
            def code(R, lambda1, lambda2, phi1, phi2):
            	tmp = 0
            	if phi2 <= -1.4e-227:
            		tmp = phi2 * (R - ((R * phi1) / phi2))
            	elif phi2 <= 3.4e-282:
            		tmp = (lambda2 * math.cos((0.5 * phi2))) * R
            	elif phi2 <= 6.2e+80:
            		tmp = -phi1 * (R - ((R * phi2) / phi1))
            	else:
            		tmp = (phi2 * (1.0 - (phi1 / phi2))) * R
            	return tmp
            
            R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
            function code(R, lambda1, lambda2, phi1, phi2)
            	tmp = 0.0
            	if (phi2 <= -1.4e-227)
            		tmp = Float64(phi2 * Float64(R - Float64(Float64(R * phi1) / phi2)));
            	elseif (phi2 <= 3.4e-282)
            		tmp = Float64(Float64(lambda2 * cos(Float64(0.5 * phi2))) * R);
            	elseif (phi2 <= 6.2e+80)
            		tmp = Float64(Float64(-phi1) * Float64(R - Float64(Float64(R * phi2) / phi1)));
            	else
            		tmp = Float64(Float64(phi2 * Float64(1.0 - Float64(phi1 / phi2))) * R);
            	end
            	return tmp
            end
            
            R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
            function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
            	tmp = 0.0;
            	if (phi2 <= -1.4e-227)
            		tmp = phi2 * (R - ((R * phi1) / phi2));
            	elseif (phi2 <= 3.4e-282)
            		tmp = (lambda2 * cos((0.5 * phi2))) * R;
            	elseif (phi2 <= 6.2e+80)
            		tmp = -phi1 * (R - ((R * phi2) / phi1));
            	else
            		tmp = (phi2 * (1.0 - (phi1 / phi2))) * R;
            	end
            	tmp_2 = tmp;
            end
            
            NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
            code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, -1.4e-227], N[(phi2 * N[(R - N[(N[(R * phi1), $MachinePrecision] / phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 3.4e-282], N[(N[(lambda2 * N[Cos[N[(0.5 * phi2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 6.2e+80], N[((-phi1) * N[(R - N[(N[(R * phi2), $MachinePrecision] / phi1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(phi2 * N[(1.0 - N[(phi1 / phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * R), $MachinePrecision]]]]
            
            \begin{array}{l}
            [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
            \\
            \begin{array}{l}
            \mathbf{if}\;\phi_2 \leq -1.4 \cdot 10^{-227}:\\
            \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\
            
            \mathbf{elif}\;\phi_2 \leq 3.4 \cdot 10^{-282}:\\
            \;\;\;\;\left(\lambda_2 \cdot \cos \left(0.5 \cdot \phi_2\right)\right) \cdot R\\
            
            \mathbf{elif}\;\phi_2 \leq 6.2 \cdot 10^{+80}:\\
            \;\;\;\;\left(-\phi_1\right) \cdot \left(R - \frac{R \cdot \phi_2}{\phi_1}\right)\\
            
            \mathbf{else}:\\
            \;\;\;\;\left(\phi_2 \cdot \left(1 - \frac{\phi_1}{\phi_2}\right)\right) \cdot R\\
            
            
            \end{array}
            \end{array}
            
            Derivation
            1. Split input into 4 regimes
            2. if phi2 < -1.3999999999999999e-227

              1. Initial program 57.6%

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

                  \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                  \[\leadsto R \cdot \color{blue}{\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)}} \]
                3. lift-+.f64N/A

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

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

                \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
              6. Step-by-step derivation
                1. lift-*.f6489.9

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

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

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

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

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

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

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

                  \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                6. lower-*.f6421.1

                  \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
              10. Applied rewrites21.1%

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

              if -1.3999999999999999e-227 < phi2 < 3.39999999999999999e-282

              1. Initial program 54.3%

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

                  \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                  \[\leadsto R \cdot \color{blue}{\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)}} \]
                3. lift-+.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

                  \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(\frac{\frac{\phi_2 \cdot \phi_2 - \phi_1 \cdot \phi_1}{\color{blue}{\phi_2 - \phi_1}}}{2}\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
              6. Applied rewrites64.8%

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

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

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

                  \[\leadsto \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \frac{{\phi_2}^{2} - \phi_1 \cdot \phi_1}{\phi_2 - \phi_1}\right)\right) \cdot R \]
                3. pow2N/A

                  \[\leadsto \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \frac{\phi_2 \cdot \phi_2 - \phi_1 \cdot \phi_1}{\phi_2 - \phi_1}\right)\right) \cdot R \]
                4. flip-+N/A

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

                  \[\leadsto \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \left(\phi_2 + \phi_1\right)\right)\right) \cdot R \]
                6. flip-+N/A

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

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

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

                  \[\leadsto \left(\lambda_2 \cdot \cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right)\right) \cdot R \]
              9. Applied rewrites37.3%

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

                \[\leadsto \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right) \cdot R \]
              11. Step-by-step derivation
                1. Applied rewrites32.5%

                  \[\leadsto \left(\lambda_2 \cdot \cos \left(0.5 \cdot \phi_2\right)\right) \cdot R \]

                if 3.39999999999999999e-282 < phi2 < 6.19999999999999976e80

                1. Initial program 58.7%

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

                    \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                    \[\leadsto R \cdot \color{blue}{\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)}} \]
                  3. lift-+.f64N/A

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

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

                  \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                6. Step-by-step derivation
                  1. lift-*.f6488.1

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

                  \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(0.5 \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                8. 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)} \]
                9. Step-by-step derivation
                  1. associate-*r*N/A

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

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

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

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

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

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

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

                    \[\leadsto \left(-\phi_1\right) \cdot \left(R + \left(-\frac{R \cdot \phi_2}{\phi_1}\right)\right) \]
                  9. lower-*.f6429.3

                    \[\leadsto \left(-\phi_1\right) \cdot \left(R + \left(-\frac{R \cdot \phi_2}{\phi_1}\right)\right) \]
                10. Applied rewrites29.3%

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

                if 6.19999999999999976e80 < phi2

                1. Initial program 59.6%

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

                    \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                    \[\leadsto R \cdot \color{blue}{\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)}} \]
                  3. lift-+.f64N/A

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

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

                  \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                6. Step-by-step derivation
                  1. lift-*.f6487.9

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

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

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

                  \[\leadsto \color{blue}{\left(\phi_2 \cdot \left(1 + \left(-\frac{\phi_1}{\phi_2}\right)\right)\right)} \cdot R \]
              12. Recombined 4 regimes into one program.
              13. Final simplification35.5%

                \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.4 \cdot 10^{-227}:\\ \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\ \mathbf{elif}\;\phi_2 \leq 3.4 \cdot 10^{-282}:\\ \;\;\;\;\left(\lambda_2 \cdot \cos \left(0.5 \cdot \phi_2\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 6.2 \cdot 10^{+80}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \left(R - \frac{R \cdot \phi_2}{\phi_1}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\phi_2 \cdot \left(1 - \frac{\phi_1}{\phi_2}\right)\right) \cdot R\\ \end{array} \]
              14. Add Preprocessing

              Alternative 10: 56.8% accurate, 2.2× speedup?

              \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.4 \cdot 10^{-227}:\\ \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\ \mathbf{elif}\;\phi_2 \leq 2.45 \cdot 10^{-262}:\\ \;\;\;\;\left(\lambda_2 \cdot \cos \left(0.5 \cdot \phi_1\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 6.2 \cdot 10^{+80}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \left(R - \frac{R \cdot \phi_2}{\phi_1}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\phi_2 \cdot \left(1 - \frac{\phi_1}{\phi_2}\right)\right) \cdot R\\ \end{array} \end{array} \]
              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
              (FPCore (R lambda1 lambda2 phi1 phi2)
               :precision binary64
               (if (<= phi2 -1.4e-227)
                 (* phi2 (- R (/ (* R phi1) phi2)))
                 (if (<= phi2 2.45e-262)
                   (* (* lambda2 (cos (* 0.5 phi1))) R)
                   (if (<= phi2 6.2e+80)
                     (* (- phi1) (- R (/ (* R phi2) phi1)))
                     (* (* phi2 (- 1.0 (/ phi1 phi2))) R)))))
              assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
              double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
              	double tmp;
              	if (phi2 <= -1.4e-227) {
              		tmp = phi2 * (R - ((R * phi1) / phi2));
              	} else if (phi2 <= 2.45e-262) {
              		tmp = (lambda2 * cos((0.5 * phi1))) * R;
              	} else if (phi2 <= 6.2e+80) {
              		tmp = -phi1 * (R - ((R * phi2) / phi1));
              	} else {
              		tmp = (phi2 * (1.0 - (phi1 / phi2))) * R;
              	}
              	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 <= (-1.4d-227)) then
                      tmp = phi2 * (r - ((r * phi1) / phi2))
                  else if (phi2 <= 2.45d-262) then
                      tmp = (lambda2 * cos((0.5d0 * phi1))) * r
                  else if (phi2 <= 6.2d+80) then
                      tmp = -phi1 * (r - ((r * phi2) / phi1))
                  else
                      tmp = (phi2 * (1.0d0 - (phi1 / phi2))) * r
                  end if
                  code = tmp
              end function
              
              assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
              public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
              	double tmp;
              	if (phi2 <= -1.4e-227) {
              		tmp = phi2 * (R - ((R * phi1) / phi2));
              	} else if (phi2 <= 2.45e-262) {
              		tmp = (lambda2 * Math.cos((0.5 * phi1))) * R;
              	} else if (phi2 <= 6.2e+80) {
              		tmp = -phi1 * (R - ((R * phi2) / phi1));
              	} else {
              		tmp = (phi2 * (1.0 - (phi1 / phi2))) * R;
              	}
              	return tmp;
              }
              
              [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
              def code(R, lambda1, lambda2, phi1, phi2):
              	tmp = 0
              	if phi2 <= -1.4e-227:
              		tmp = phi2 * (R - ((R * phi1) / phi2))
              	elif phi2 <= 2.45e-262:
              		tmp = (lambda2 * math.cos((0.5 * phi1))) * R
              	elif phi2 <= 6.2e+80:
              		tmp = -phi1 * (R - ((R * phi2) / phi1))
              	else:
              		tmp = (phi2 * (1.0 - (phi1 / phi2))) * R
              	return tmp
              
              R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
              function code(R, lambda1, lambda2, phi1, phi2)
              	tmp = 0.0
              	if (phi2 <= -1.4e-227)
              		tmp = Float64(phi2 * Float64(R - Float64(Float64(R * phi1) / phi2)));
              	elseif (phi2 <= 2.45e-262)
              		tmp = Float64(Float64(lambda2 * cos(Float64(0.5 * phi1))) * R);
              	elseif (phi2 <= 6.2e+80)
              		tmp = Float64(Float64(-phi1) * Float64(R - Float64(Float64(R * phi2) / phi1)));
              	else
              		tmp = Float64(Float64(phi2 * Float64(1.0 - Float64(phi1 / phi2))) * R);
              	end
              	return tmp
              end
              
              R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
              function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
              	tmp = 0.0;
              	if (phi2 <= -1.4e-227)
              		tmp = phi2 * (R - ((R * phi1) / phi2));
              	elseif (phi2 <= 2.45e-262)
              		tmp = (lambda2 * cos((0.5 * phi1))) * R;
              	elseif (phi2 <= 6.2e+80)
              		tmp = -phi1 * (R - ((R * phi2) / phi1));
              	else
              		tmp = (phi2 * (1.0 - (phi1 / phi2))) * R;
              	end
              	tmp_2 = tmp;
              end
              
              NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
              code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, -1.4e-227], N[(phi2 * N[(R - N[(N[(R * phi1), $MachinePrecision] / phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], If[LessEqual[phi2, 2.45e-262], N[(N[(lambda2 * N[Cos[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 6.2e+80], N[((-phi1) * N[(R - N[(N[(R * phi2), $MachinePrecision] / phi1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[(phi2 * N[(1.0 - N[(phi1 / phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * R), $MachinePrecision]]]]
              
              \begin{array}{l}
              [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
              \\
              \begin{array}{l}
              \mathbf{if}\;\phi_2 \leq -1.4 \cdot 10^{-227}:\\
              \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\
              
              \mathbf{elif}\;\phi_2 \leq 2.45 \cdot 10^{-262}:\\
              \;\;\;\;\left(\lambda_2 \cdot \cos \left(0.5 \cdot \phi_1\right)\right) \cdot R\\
              
              \mathbf{elif}\;\phi_2 \leq 6.2 \cdot 10^{+80}:\\
              \;\;\;\;\left(-\phi_1\right) \cdot \left(R - \frac{R \cdot \phi_2}{\phi_1}\right)\\
              
              \mathbf{else}:\\
              \;\;\;\;\left(\phi_2 \cdot \left(1 - \frac{\phi_1}{\phi_2}\right)\right) \cdot R\\
              
              
              \end{array}
              \end{array}
              
              Derivation
              1. Split input into 4 regimes
              2. if phi2 < -1.3999999999999999e-227

                1. Initial program 57.6%

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

                    \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                    \[\leadsto R \cdot \color{blue}{\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)}} \]
                  3. lift-+.f64N/A

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

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

                  \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                6. Step-by-step derivation
                  1. lift-*.f6489.9

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

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

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

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

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

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

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

                    \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                  6. lower-*.f6421.1

                    \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                10. Applied rewrites21.1%

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

                if -1.3999999999999999e-227 < phi2 < 2.4500000000000001e-262

                1. Initial program 50.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. Step-by-step derivation
                  1. lift-*.f64N/A

                    \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                    \[\leadsto R \cdot \color{blue}{\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)}} \]
                  3. lift-+.f64N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                    \[\leadsto \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \frac{{\phi_2}^{2} - \phi_1 \cdot \phi_1}{\phi_2 - \phi_1}\right)\right) \cdot R \]
                  3. pow2N/A

                    \[\leadsto \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \frac{\phi_2 \cdot \phi_2 - \phi_1 \cdot \phi_1}{\phi_2 - \phi_1}\right)\right) \cdot R \]
                  4. flip-+N/A

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

                    \[\leadsto \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \left(\phi_2 + \phi_1\right)\right)\right) \cdot R \]
                  6. flip-+N/A

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

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

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

                    \[\leadsto \left(\lambda_2 \cdot \cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right)\right) \cdot R \]
                9. Applied rewrites36.3%

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

                  \[\leadsto \left(\lambda_2 \cdot \cos \left(\frac{1}{2} \cdot \phi_1\right)\right) \cdot R \]
                11. Step-by-step derivation
                  1. Applied rewrites36.3%

                    \[\leadsto \left(\lambda_2 \cdot \cos \left(0.5 \cdot \phi_1\right)\right) \cdot R \]

                  if 2.4500000000000001e-262 < phi2 < 6.19999999999999976e80

                  1. Initial program 60.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. Step-by-step derivation
                    1. lift-*.f64N/A

                      \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                      \[\leadsto R \cdot \color{blue}{\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)}} \]
                    3. lift-+.f64N/A

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

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

                    \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                  6. Step-by-step derivation
                    1. lift-*.f6487.6

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

                    \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(0.5 \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                  8. 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)} \]
                  9. Step-by-step derivation
                    1. associate-*r*N/A

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

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

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

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

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

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

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

                      \[\leadsto \left(-\phi_1\right) \cdot \left(R + \left(-\frac{R \cdot \phi_2}{\phi_1}\right)\right) \]
                    9. lower-*.f6429.4

                      \[\leadsto \left(-\phi_1\right) \cdot \left(R + \left(-\frac{R \cdot \phi_2}{\phi_1}\right)\right) \]
                  10. Applied rewrites29.4%

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

                  if 6.19999999999999976e80 < phi2

                  1. Initial program 59.6%

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

                      \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                      \[\leadsto R \cdot \color{blue}{\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)}} \]
                    3. lift-+.f64N/A

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

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

                    \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                  6. Step-by-step derivation
                    1. lift-*.f6487.9

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

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

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

                    \[\leadsto \color{blue}{\left(\phi_2 \cdot \left(1 + \left(-\frac{\phi_1}{\phi_2}\right)\right)\right)} \cdot R \]
                12. Recombined 4 regimes into one program.
                13. Final simplification35.9%

                  \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.4 \cdot 10^{-227}:\\ \;\;\;\;\phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)\\ \mathbf{elif}\;\phi_2 \leq 2.45 \cdot 10^{-262}:\\ \;\;\;\;\left(\lambda_2 \cdot \cos \left(0.5 \cdot \phi_1\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 6.2 \cdot 10^{+80}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \left(R - \frac{R \cdot \phi_2}{\phi_1}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(\phi_2 \cdot \left(1 - \frac{\phi_1}{\phi_2}\right)\right) \cdot R\\ \end{array} \]
                14. Add Preprocessing

                Alternative 11: 58.2% accurate, 7.1× speedup?

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

                  1. Initial program 57.2%

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

                      \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                      \[\leadsto R \cdot \color{blue}{\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)}} \]
                    3. lift-+.f64N/A

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

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

                    \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                  6. Step-by-step derivation
                    1. lift-*.f6490.4

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

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

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

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

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

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

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

                      \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                    6. lower-*.f6420.0

                      \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                  10. Applied rewrites20.0%

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

                  if -2.2000000000000001e-259 < phi2 < 6.19999999999999976e80

                  1. Initial program 58.3%

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

                      \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                      \[\leadsto R \cdot \color{blue}{\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)}} \]
                    3. lift-+.f64N/A

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

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

                    \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                  6. Step-by-step derivation
                    1. lift-*.f6489.7

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

                    \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(0.5 \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                  8. 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)} \]
                  9. Step-by-step derivation
                    1. associate-*r*N/A

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

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

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

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

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

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

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

                      \[\leadsto \left(-\phi_1\right) \cdot \left(R + \left(-\frac{R \cdot \phi_2}{\phi_1}\right)\right) \]
                    9. lower-*.f6429.1

                      \[\leadsto \left(-\phi_1\right) \cdot \left(R + \left(-\frac{R \cdot \phi_2}{\phi_1}\right)\right) \]
                  10. Applied rewrites29.1%

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

                  if 6.19999999999999976e80 < phi2

                  1. Initial program 59.6%

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

                      \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                      \[\leadsto R \cdot \color{blue}{\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)}} \]
                    3. lift-+.f64N/A

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

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

                    \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                  6. Step-by-step derivation
                    1. lift-*.f6487.9

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

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

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

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

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

                Alternative 12: 55.9% accurate, 7.5× speedup?

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

                  1. Initial program 57.2%

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

                      \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                      \[\leadsto R \cdot \color{blue}{\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)}} \]
                    3. lift-+.f64N/A

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

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

                    \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                  6. Step-by-step derivation
                    1. lift-*.f6490.4

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

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

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

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

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

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

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

                      \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                    6. lower-*.f6420.0

                      \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                  10. Applied rewrites20.0%

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

                  if -2.2000000000000001e-259 < phi2 < 4.7e-163

                  1. Initial program 62.3%

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

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

                      \[\leadsto R \cdot \left(\mathsf{neg}\left(\phi_1\right)\right) \]
                    2. lower-neg.f6423.8

                      \[\leadsto R \cdot \left(-\phi_1\right) \]
                  5. Applied rewrites23.8%

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

                  if 4.7e-163 < phi2

                  1. Initial program 56.9%

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

                      \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                      \[\leadsto R \cdot \color{blue}{\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)}} \]
                    3. lift-+.f64N/A

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

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

                    \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                  6. Step-by-step derivation
                    1. lift-*.f6483.9

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

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

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

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

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

                Alternative 13: 55.2% accurate, 11.2× speedup?

                \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \phi_2 \cdot \left(R - \frac{R \cdot \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
                 (* phi2 (- R (/ (* 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 phi2 * (R - ((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 = phi2 * (r - ((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 phi2 * (R - ((R * phi1) / phi2));
                }
                
                [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
                def code(R, lambda1, lambda2, phi1, phi2):
                	return phi2 * (R - ((R * phi1) / phi2))
                
                R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
                function code(R, lambda1, lambda2, phi1, phi2)
                	return Float64(phi2 * Float64(R - Float64(Float64(R * phi1) / phi2)))
                end
                
                R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
                function tmp = code(R, lambda1, lambda2, phi1, phi2)
                	tmp = phi2 * (R - ((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[(phi2 * N[(R - N[(N[(R * phi1), $MachinePrecision] / phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
                
                \begin{array}{l}
                [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
                \\
                \phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right)
                \end{array}
                
                Derivation
                1. Initial program 58.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. Step-by-step derivation
                  1. lift-*.f64N/A

                    \[\leadsto \color{blue}{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. lift-sqrt.f64N/A

                    \[\leadsto R \cdot \color{blue}{\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)}} \]
                  3. lift-+.f64N/A

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

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

                  \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
                6. Step-by-step derivation
                  1. lift-*.f6489.7

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

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

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

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

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

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

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

                    \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                  6. lower-*.f6430.0

                    \[\leadsto \phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right) \]
                10. Applied rewrites30.0%

                  \[\leadsto \color{blue}{\phi_2 \cdot \left(R + \left(-\frac{R \cdot \phi_1}{\phi_2}\right)\right)} \]
                11. Final simplification30.0%

                  \[\leadsto \phi_2 \cdot \left(R - \frac{R \cdot \phi_1}{\phi_2}\right) \]
                12. Add Preprocessing

                Alternative 14: 51.2% 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 2.75 \cdot 10^{+76}:\\ \;\;\;\;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 2.75e+76) (* 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 <= 2.75e+76) {
                		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 <= 2.75d+76) 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 <= 2.75e+76) {
                		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 <= 2.75e+76:
                		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 <= 2.75e+76)
                		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 <= 2.75e+76)
                		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, 2.75e+76], 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 2.75 \cdot 10^{+76}:\\
                \;\;\;\;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 < 2.75e76

                  1. Initial program 57.8%

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

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

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

                      \[\leadsto R \cdot \left(-\phi_1\right) \]
                  5. Applied rewrites21.6%

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

                  if 2.75e76 < phi2

                  1. Initial program 59.6%

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

                    \[\leadsto R \cdot \color{blue}{\phi_2} \]
                  4. Step-by-step derivation
                    1. Applied rewrites77.7%

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

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

                  Alternative 15: 31.1% 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.1%

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

                    \[\leadsto R \cdot \color{blue}{\phi_2} \]
                  4. Step-by-step derivation
                    1. Applied rewrites17.6%

                      \[\leadsto R \cdot \color{blue}{\phi_2} \]
                    2. Final simplification17.6%

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

                    Reproduce

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