Equirectangular approximation to distance on a great circle

Percentage Accurate: 60.5% → 96.1%
Time: 6.7s
Alternatives: 16
Speedup: 1.7×

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}

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 16 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: 60.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\ R \cdot \sqrt{t\_0 \cdot t\_0 + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0)))))
   (* R (sqrt (+ (* t_0 t_0) (* (- phi1 phi2) (- phi1 phi2)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	return R * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
}
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.1% accurate, 1.7× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -820:\\ \;\;\;\;\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 -820.0)
   (* (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 <= -820.0) {
		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 <= -820.0) {
		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 <= -820.0:
		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 <= -820.0)
		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 <= -820.0)
		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, -820.0], 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 -820:\\
\;\;\;\;\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 < -820

    1. Initial program 55.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. 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)}} \]
    3. Applied rewrites93.7%

      \[\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} \]
    4. 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 \]
    5. Step-by-step derivation
      1. lower-*.f6493.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 \]
    6. Applied rewrites93.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 \]

    if -820 < phi1

    1. Initial program 64.5%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. 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)}} \]
    3. Applied rewrites97.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} \]
    4. 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 \]
    5. Step-by-step derivation
      1. lower-*.f6497.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 \]
    6. Applied rewrites97.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 \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 2: 95.9% accurate, 1.7× 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 60.5%

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

Alternative 3: 91.0% accurate, 1.7× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.02 \cdot 10^{+40}:\\ \;\;\;\;\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_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.02e+40)
   (* (hypot (- phi1 phi2) (* (cos (* 0.5 phi1)) (- lambda1 lambda2))) R)
   (* (hypot (- phi1 phi2) (* (cos (* 0.5 phi2)) (- 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 (phi2 <= 1.02e+40) {
		tmp = hypot((phi1 - phi2), (cos((0.5 * phi1)) * (lambda1 - lambda2))) * R;
	} else {
		tmp = hypot((phi1 - phi2), (cos((0.5 * phi2)) * -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 (phi2 <= 1.02e+40) {
		tmp = Math.hypot((phi1 - phi2), (Math.cos((0.5 * phi1)) * (lambda1 - lambda2))) * R;
	} else {
		tmp = Math.hypot((phi1 - phi2), (Math.cos((0.5 * phi2)) * -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 phi2 <= 1.02e+40:
		tmp = math.hypot((phi1 - phi2), (math.cos((0.5 * phi1)) * (lambda1 - lambda2))) * R
	else:
		tmp = math.hypot((phi1 - phi2), (math.cos((0.5 * phi2)) * -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 (phi2 <= 1.02e+40)
		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(-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 (phi2 <= 1.02e+40)
		tmp = hypot((phi1 - phi2), (cos((0.5 * phi1)) * (lambda1 - lambda2))) * R;
	else
		tmp = hypot((phi1 - phi2), (cos((0.5 * phi2)) * -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[phi2, 1.02e+40], 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] * (-lambda2)), $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_2 \leq 1.02 \cdot 10^{+40}:\\
\;\;\;\;\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_2\right)\right) \cdot R\\


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

    1. Initial program 64.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. 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)}} \]
    3. Applied rewrites97.7%

      \[\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} \]
    4. 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 \]
    5. Step-by-step derivation
      1. lower-*.f6495.3

        \[\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 \]
    6. Applied rewrites95.3%

      \[\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.02e40 < phi2

    1. Initial program 53.9%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. 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)}} \]
    3. 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} \]
    4. 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 \]
    5. Step-by-step derivation
      1. lower-*.f6493.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 \]
    6. Applied rewrites93.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 \]
    7. Taylor expanded in lambda1 around 0

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(0.5 \cdot \phi_2\right) \cdot \left(-\lambda_2\right)\right) \cdot R \]
    9. Applied rewrites84.1%

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

Alternative 4: 85.7% accurate, 1.7× 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}\;\lambda_1 \leq -9.6 \cdot 10^{+56}:\\ \;\;\;\;\mathsf{hypot}\left(\phi_1 - \phi_2, t\_0 \cdot \lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\phi_1 - \phi_2, t\_0 \cdot \left(-\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
 (let* ((t_0 (cos (* 0.5 phi2))))
   (if (<= lambda1 -9.6e+56)
     (* (hypot (- phi1 phi2) (* t_0 lambda1)) R)
     (* (hypot (- phi1 phi2) (* t_0 (- lambda2))) 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 (lambda1 <= -9.6e+56) {
		tmp = hypot((phi1 - phi2), (t_0 * lambda1)) * R;
	} else {
		tmp = hypot((phi1 - phi2), (t_0 * -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 t_0 = Math.cos((0.5 * phi2));
	double tmp;
	if (lambda1 <= -9.6e+56) {
		tmp = Math.hypot((phi1 - phi2), (t_0 * lambda1)) * R;
	} else {
		tmp = Math.hypot((phi1 - phi2), (t_0 * -lambda2)) * 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 lambda1 <= -9.6e+56:
		tmp = math.hypot((phi1 - phi2), (t_0 * lambda1)) * R
	else:
		tmp = math.hypot((phi1 - phi2), (t_0 * -lambda2)) * 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 (lambda1 <= -9.6e+56)
		tmp = Float64(hypot(Float64(phi1 - phi2), Float64(t_0 * lambda1)) * R);
	else
		tmp = Float64(hypot(Float64(phi1 - phi2), Float64(t_0 * Float64(-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)
	t_0 = cos((0.5 * phi2));
	tmp = 0.0;
	if (lambda1 <= -9.6e+56)
		tmp = hypot((phi1 - phi2), (t_0 * lambda1)) * R;
	else
		tmp = hypot((phi1 - phi2), (t_0 * -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_] := Block[{t$95$0 = N[Cos[N[(0.5 * phi2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[lambda1, -9.6e+56], N[(N[Sqrt[N[(phi1 - phi2), $MachinePrecision] ^ 2 + N[(t$95$0 * lambda1), $MachinePrecision] ^ 2], $MachinePrecision] * R), $MachinePrecision], N[(N[Sqrt[N[(phi1 - phi2), $MachinePrecision] ^ 2 + N[(t$95$0 * (-lambda2)), $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}\;\lambda_1 \leq -9.6 \cdot 10^{+56}:\\
\;\;\;\;\mathsf{hypot}\left(\phi_1 - \phi_2, t\_0 \cdot \lambda_1\right) \cdot R\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda1 < -9.60000000000000053e56

    1. Initial program 51.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. 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)}} \]
    3. 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} \]
    4. 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 \]
    5. Step-by-step derivation
      1. lower-*.f6486.8

        \[\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 \]
    6. Applied rewrites86.8%

      \[\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 \]
    7. 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 \]
    8. Step-by-step derivation
      1. Applied rewrites79.5%

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

      if -9.60000000000000053e56 < lambda1

      1. Initial program 62.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. 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)}} \]
      3. Applied rewrites96.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} \]
      4. 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 \]
      5. Step-by-step derivation
        1. lower-*.f6492.0

          \[\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 \]
      6. Applied rewrites92.0%

        \[\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 \]
      7. Taylor expanded in lambda1 around 0

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

          \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot R \]
        2. lower-neg.f6479.8

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

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

    Alternative 5: 82.2% accurate, 1.7× 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 4.5 \cdot 10^{+47}:\\ \;\;\;\;\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 4.5e+47)
         (* (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 <= 4.5e+47) {
    		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 <= 4.5e+47) {
    		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 <= 4.5e+47:
    		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 <= 4.5e+47)
    		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 <= 4.5e+47)
    		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, 4.5e+47], 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 4.5 \cdot 10^{+47}:\\
    \;\;\;\;\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 2 regimes
    2. if phi2 < 4.49999999999999979e47

      1. Initial program 64.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. 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)}} \]
      3. Applied rewrites97.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} \]
      4. 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 \]
      5. Step-by-step derivation
        1. lower-*.f6489.3

          \[\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 \]
      6. Applied rewrites89.3%

        \[\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 \]
      7. 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 \]
      8. Step-by-step derivation
        1. Applied rewrites85.2%

          \[\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 4.49999999999999979e47 < phi2

        1. Initial program 53.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. 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)}} \]
        3. Applied rewrites93.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} \]
        4. 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 \]
        5. Step-by-step derivation
          1. lower-*.f6493.6

            \[\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 \]
        6. Applied rewrites93.6%

          \[\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 \]
        7. 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 \]
        8. Step-by-step derivation
          1. Applied rewrites86.4%

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

        Alternative 6: 79.8% accurate, 1.7× speedup?

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

          1. Initial program 65.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. 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)}} \]
          3. Applied rewrites96.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} \]
          4. 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 \]
          5. Step-by-step derivation
            1. lower-*.f6489.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 \]
          6. Applied rewrites89.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 \]
          7. 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 \]
          8. Step-by-step derivation
            1. Applied rewrites81.3%

              \[\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 1.42000000000000001e138 < phi2

            1. Initial program 46.0%

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

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

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

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

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

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

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

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

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

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

                \[\leadsto R \cdot \left(\left(-\phi_1\right) \cdot \left(\frac{\mathsf{neg}\left(\phi_2\right)}{\phi_1} + 1\right)\right) \]
              10. lower-neg.f6468.8

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

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
              3. +-commutativeN/A

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
              4. lower-+.f6484.9

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
            7. Applied rewrites84.9%

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

          Alternative 7: 59.2% accurate, 2.0× 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 \left(\phi_2 + \phi_1\right)\right)\\ \mathbf{if}\;\lambda_2 \leq -0.022:\\ \;\;\;\;-\left(t\_0 \cdot \lambda_1\right) \cdot R\\ \mathbf{elif}\;\lambda_2 \leq 3.8 \cdot 10^{+172}:\\ \;\;\;\;R \cdot \left(\left(-\phi_1\right) + \phi_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(t\_0 \cdot \lambda_2\right)\\ \end{array} \end{array} \]
          NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
          (FPCore (R lambda1 lambda2 phi1 phi2)
           :precision binary64
           (let* ((t_0 (cos (* 0.5 (+ phi2 phi1)))))
             (if (<= lambda2 -0.022)
               (- (* (* t_0 lambda1) R))
               (if (<= lambda2 3.8e+172)
                 (* R (+ (- phi1) phi2))
                 (* R (* t_0 lambda2))))))
          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 + phi1)));
          	double tmp;
          	if (lambda2 <= -0.022) {
          		tmp = -((t_0 * lambda1) * R);
          	} else if (lambda2 <= 3.8e+172) {
          		tmp = R * (-phi1 + phi2);
          	} else {
          		tmp = R * (t_0 * lambda2);
          	}
          	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) :: t_0
              real(8) :: tmp
              t_0 = cos((0.5d0 * (phi2 + phi1)))
              if (lambda2 <= (-0.022d0)) then
                  tmp = -((t_0 * lambda1) * r)
              else if (lambda2 <= 3.8d+172) then
                  tmp = r * (-phi1 + phi2)
              else
                  tmp = r * (t_0 * lambda2)
              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 t_0 = Math.cos((0.5 * (phi2 + phi1)));
          	double tmp;
          	if (lambda2 <= -0.022) {
          		tmp = -((t_0 * lambda1) * R);
          	} else if (lambda2 <= 3.8e+172) {
          		tmp = R * (-phi1 + phi2);
          	} else {
          		tmp = R * (t_0 * lambda2);
          	}
          	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 + phi1)))
          	tmp = 0
          	if lambda2 <= -0.022:
          		tmp = -((t_0 * lambda1) * R)
          	elif lambda2 <= 3.8e+172:
          		tmp = R * (-phi1 + phi2)
          	else:
          		tmp = R * (t_0 * lambda2)
          	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 * Float64(phi2 + phi1)))
          	tmp = 0.0
          	if (lambda2 <= -0.022)
          		tmp = Float64(-Float64(Float64(t_0 * lambda1) * R));
          	elseif (lambda2 <= 3.8e+172)
          		tmp = Float64(R * Float64(Float64(-phi1) + phi2));
          	else
          		tmp = Float64(R * Float64(t_0 * lambda2));
          	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 + phi1)));
          	tmp = 0.0;
          	if (lambda2 <= -0.022)
          		tmp = -((t_0 * lambda1) * R);
          	elseif (lambda2 <= 3.8e+172)
          		tmp = R * (-phi1 + phi2);
          	else
          		tmp = R * (t_0 * lambda2);
          	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 * N[(phi2 + phi1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[lambda2, -0.022], (-N[(N[(t$95$0 * lambda1), $MachinePrecision] * R), $MachinePrecision]), If[LessEqual[lambda2, 3.8e+172], N[(R * N[((-phi1) + phi2), $MachinePrecision]), $MachinePrecision], N[(R * N[(t$95$0 * lambda2), $MachinePrecision]), $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 \left(\phi_2 + \phi_1\right)\right)\\
          \mathbf{if}\;\lambda_2 \leq -0.022:\\
          \;\;\;\;-\left(t\_0 \cdot \lambda_1\right) \cdot R\\
          
          \mathbf{elif}\;\lambda_2 \leq 3.8 \cdot 10^{+172}:\\
          \;\;\;\;R \cdot \left(\left(-\phi_1\right) + \phi_2\right)\\
          
          \mathbf{else}:\\
          \;\;\;\;R \cdot \left(t\_0 \cdot \lambda_2\right)\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 3 regimes
          2. if lambda2 < -0.021999999999999999

            1. Initial program 55.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. Taylor expanded in lambda1 around -inf

              \[\leadsto \color{blue}{-1 \cdot \left(R \cdot \left(\lambda_1 \cdot \cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)\right)\right)} \]
            3. Step-by-step derivation
              1. mul-1-negN/A

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

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

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

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

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

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

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

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

                \[\leadsto -\left(\cos \left(\frac{1}{2} \cdot \left(\phi_2 + \phi_1\right)\right) \cdot \lambda_1\right) \cdot R \]
              10. lower-+.f6413.4

                \[\leadsto -\left(\cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right) \cdot \lambda_1\right) \cdot R \]
            4. Applied rewrites13.4%

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

            if -0.021999999999999999 < lambda2 < 3.7999999999999997e172

            1. Initial program 64.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. Taylor expanded in phi1 around -inf

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

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

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

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

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

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

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

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

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

                \[\leadsto R \cdot \left(\left(-\phi_1\right) \cdot \left(\frac{\mathsf{neg}\left(\phi_2\right)}{\phi_1} + 1\right)\right) \]
              10. lower-neg.f6460.4

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

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
              3. +-commutativeN/A

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
              4. lower-+.f6466.0

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
            7. Applied rewrites66.0%

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

            if 3.7999999999999997e172 < lambda2

            1. Initial program 45.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. Taylor expanded in lambda2 around inf

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

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

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

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

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

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

                \[\leadsto R \cdot \left(\cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right) \cdot \lambda_2\right) \]
            4. Applied rewrites48.8%

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

          Alternative 8: 59.1% accurate, 2.2× speedup?

          \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 3.8 \cdot 10^{+172}:\\ \;\;\;\;R \cdot \left(\left(-\phi_1\right) + \phi_2\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right) \cdot \lambda_2\right)\\ \end{array} \end{array} \]
          NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
          (FPCore (R lambda1 lambda2 phi1 phi2)
           :precision binary64
           (if (<= lambda2 3.8e+172)
             (* R (+ (- phi1) phi2))
             (* R (* (cos (* 0.5 (+ phi2 phi1))) lambda2))))
          assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
          double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
          	double tmp;
          	if (lambda2 <= 3.8e+172) {
          		tmp = R * (-phi1 + phi2);
          	} else {
          		tmp = R * (cos((0.5 * (phi2 + phi1))) * lambda2);
          	}
          	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 (lambda2 <= 3.8d+172) then
                  tmp = r * (-phi1 + phi2)
              else
                  tmp = r * (cos((0.5d0 * (phi2 + phi1))) * lambda2)
              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 (lambda2 <= 3.8e+172) {
          		tmp = R * (-phi1 + phi2);
          	} else {
          		tmp = R * (Math.cos((0.5 * (phi2 + phi1))) * lambda2);
          	}
          	return tmp;
          }
          
          [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
          def code(R, lambda1, lambda2, phi1, phi2):
          	tmp = 0
          	if lambda2 <= 3.8e+172:
          		tmp = R * (-phi1 + phi2)
          	else:
          		tmp = R * (math.cos((0.5 * (phi2 + phi1))) * lambda2)
          	return tmp
          
          R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
          function code(R, lambda1, lambda2, phi1, phi2)
          	tmp = 0.0
          	if (lambda2 <= 3.8e+172)
          		tmp = Float64(R * Float64(Float64(-phi1) + phi2));
          	else
          		tmp = Float64(R * Float64(cos(Float64(0.5 * Float64(phi2 + phi1))) * lambda2));
          	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 (lambda2 <= 3.8e+172)
          		tmp = R * (-phi1 + phi2);
          	else
          		tmp = R * (cos((0.5 * (phi2 + phi1))) * lambda2);
          	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[lambda2, 3.8e+172], N[(R * N[((-phi1) + phi2), $MachinePrecision]), $MachinePrecision], N[(R * N[(N[Cos[N[(0.5 * N[(phi2 + phi1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * lambda2), $MachinePrecision]), $MachinePrecision]]
          
          \begin{array}{l}
          [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
          \\
          \begin{array}{l}
          \mathbf{if}\;\lambda_2 \leq 3.8 \cdot 10^{+172}:\\
          \;\;\;\;R \cdot \left(\left(-\phi_1\right) + \phi_2\right)\\
          
          \mathbf{else}:\\
          \;\;\;\;R \cdot \left(\cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right) \cdot \lambda_2\right)\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if lambda2 < 3.7999999999999997e172

            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. Taylor expanded in phi1 around -inf

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

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

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

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

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

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

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

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

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

                \[\leadsto R \cdot \left(\left(-\phi_1\right) \cdot \left(\frac{\mathsf{neg}\left(\phi_2\right)}{\phi_1} + 1\right)\right) \]
              10. lower-neg.f6455.5

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

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
              3. +-commutativeN/A

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
              4. lower-+.f6460.5

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
            7. Applied rewrites60.5%

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

            if 3.7999999999999997e172 < lambda2

            1. Initial program 45.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. Taylor expanded in lambda2 around inf

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

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

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

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

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

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

                \[\leadsto R \cdot \left(\cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right) \cdot \lambda_2\right) \]
            4. Applied rewrites48.8%

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

          Alternative 9: 58.9% accurate, 5.1× speedup?

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

            1. Initial program 52.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. Taylor expanded in phi1 around -inf

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

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

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

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

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

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

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

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

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

                \[\leadsto R \cdot \left(\left(-\phi_1\right) \cdot \left(\frac{\mathsf{neg}\left(\phi_2\right)}{\phi_1} + 1\right)\right) \]
              10. lower-neg.f6450.6

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

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
              3. +-commutativeN/A

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
              4. lower-+.f6456.1

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
            7. Applied rewrites56.1%

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

            if 4.89999999999999983e101 < R

            1. Initial program 97.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. 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)}} \]
            3. Applied rewrites99.7%

              \[\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} \]
            4. Taylor expanded in phi2 around inf

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

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

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

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

                \[\leadsto \left(-1 \cdot \frac{R \cdot \phi_1}{\phi_2} + R\right) \cdot \phi_2 \]
              5. associate-*r/N/A

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

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \left(\left(\frac{R}{\phi_1} + -1 \cdot \frac{R}{\phi_2}\right) \cdot \phi_1\right) \cdot \phi_2 \]
              6. associate-*r/N/A

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

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

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

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

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

          Alternative 10: 58.2% accurate, 5.2× speedup?

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

            1. Initial program 51.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. Taylor expanded in phi2 around inf

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

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

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

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

                \[\leadsto \left(-1 \cdot \frac{R \cdot \phi_1}{\phi_2} + R\right) \cdot \phi_2 \]
              5. associate-*r/N/A

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

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

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

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

                \[\leadsto \left(\frac{-\phi_1 \cdot R}{\phi_2} + R\right) \cdot \phi_2 \]
              10. lower-*.f6442.5

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

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

            if -1.99999999999999993e90 < (-.f64 lambda1 lambda2)

            1. Initial program 64.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. Taylor expanded in phi1 around -inf

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

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

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

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

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

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

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

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

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

                \[\leadsto R \cdot \left(\left(-\phi_1\right) \cdot \left(\frac{\mathsf{neg}\left(\phi_2\right)}{\phi_1} + 1\right)\right) \]
              10. lower-neg.f6459.4

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

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
              3. +-commutativeN/A

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
              4. lower-+.f6464.6

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
            7. Applied rewrites64.6%

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

          Alternative 11: 58.1% accurate, 6.3× speedup?

          \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;R \leq 7.5 \cdot 10^{+56}:\\ \;\;\;\;R \cdot \left(\left(-\phi_1\right) + \phi_2\right)\\ \mathbf{else}:\\ \;\;\;\;\mathsf{fma}\left(R, \frac{\phi_2}{\phi_1}, -R\right) \cdot \phi_1\\ \end{array} \end{array} \]
          NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
          (FPCore (R lambda1 lambda2 phi1 phi2)
           :precision binary64
           (if (<= R 7.5e+56)
             (* R (+ (- phi1) phi2))
             (* (fma R (/ phi2 phi1) (- R)) phi1)))
          assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
          double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
          	double tmp;
          	if (R <= 7.5e+56) {
          		tmp = R * (-phi1 + phi2);
          	} else {
          		tmp = fma(R, (phi2 / phi1), -R) * phi1;
          	}
          	return tmp;
          }
          
          R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
          function code(R, lambda1, lambda2, phi1, phi2)
          	tmp = 0.0
          	if (R <= 7.5e+56)
          		tmp = Float64(R * Float64(Float64(-phi1) + phi2));
          	else
          		tmp = Float64(fma(R, Float64(phi2 / phi1), Float64(-R)) * phi1);
          	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[R, 7.5e+56], N[(R * N[((-phi1) + phi2), $MachinePrecision]), $MachinePrecision], N[(N[(R * N[(phi2 / phi1), $MachinePrecision] + (-R)), $MachinePrecision] * phi1), $MachinePrecision]]
          
          \begin{array}{l}
          [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
          \\
          \begin{array}{l}
          \mathbf{if}\;R \leq 7.5 \cdot 10^{+56}:\\
          \;\;\;\;R \cdot \left(\left(-\phi_1\right) + \phi_2\right)\\
          
          \mathbf{else}:\\
          \;\;\;\;\mathsf{fma}\left(R, \frac{\phi_2}{\phi_1}, -R\right) \cdot \phi_1\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if R < 7.4999999999999999e56

            1. Initial program 51.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. Taylor expanded in phi1 around -inf

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

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

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

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

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

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

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

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

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

                \[\leadsto R \cdot \left(\left(-\phi_1\right) \cdot \left(\frac{\mathsf{neg}\left(\phi_2\right)}{\phi_1} + 1\right)\right) \]
              10. lower-neg.f6450.4

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

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
              3. +-commutativeN/A

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
              4. lower-+.f6456.1

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
            7. Applied rewrites56.1%

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

            if 7.4999999999999999e56 < R

            1. Initial program 93.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. 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)}} \]
            3. Applied rewrites99.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} \]
            4. Taylor expanded in phi2 around inf

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

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

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

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

                \[\leadsto \left(-1 \cdot \frac{R \cdot \phi_1}{\phi_2} + R\right) \cdot \phi_2 \]
              5. associate-*r/N/A

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

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

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

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \mathsf{fma}\left(R, \frac{\phi_2}{\phi_1}, \mathsf{neg}\left(R\right)\right) \cdot \phi_1 \]
              8. lower-neg.f6465.9

                \[\leadsto \mathsf{fma}\left(R, \frac{\phi_2}{\phi_1}, -R\right) \cdot \phi_1 \]
            9. Applied rewrites65.9%

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

          Alternative 12: 57.9% accurate, 6.9× speedup?

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

            1. Initial program 46.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. Taylor expanded in lambda1 around inf

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

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

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

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

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

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

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

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

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

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

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

                \[\leadsto R \cdot \sqrt{\left(\lambda_1 \cdot \lambda_1\right) \cdot \left(\frac{1}{2} + \frac{1}{2} \cdot \cos \left(2 \cdot \left(\frac{1}{2} \cdot \left(\phi_2 + \phi_1\right)\right)\right)\right)} \]
              12. lower-+.f6446.7

                \[\leadsto R \cdot \sqrt{\left(\lambda_1 \cdot \lambda_1\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(2 \cdot \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right)\right)\right)} \]
            4. Applied rewrites46.7%

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

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

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

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

                \[\leadsto R \cdot \sqrt{\left(\lambda_1 \cdot \lambda_1\right) \cdot \mathsf{fma}\left(\cos \phi_2, \frac{1}{2}, \frac{1}{2}\right)} \]
              4. lower-cos.f6446.7

                \[\leadsto R \cdot \sqrt{\left(\lambda_1 \cdot \lambda_1\right) \cdot \mathsf{fma}\left(\cos \phi_2, 0.5, 0.5\right)} \]
            7. Applied rewrites46.7%

              \[\leadsto R \cdot \sqrt{\left(\lambda_1 \cdot \lambda_1\right) \cdot \mathsf{fma}\left(\cos \phi_2, \color{blue}{0.5}, 0.5\right)} \]
            8. Taylor expanded in phi2 around 0

              \[\leadsto R \cdot \sqrt{\left(\lambda_1 \cdot \lambda_1\right) \cdot 1} \]
            9. Step-by-step derivation
              1. Applied rewrites46.7%

                \[\leadsto R \cdot \sqrt{\left(\lambda_1 \cdot \lambda_1\right) \cdot 1} \]

              if -8.8000000000000006e219 < lambda1

              1. Initial program 61.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. Taylor expanded in phi1 around -inf

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

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

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

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

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

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

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

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

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

                  \[\leadsto R \cdot \left(\left(-\phi_1\right) \cdot \left(\frac{\mathsf{neg}\left(\phi_2\right)}{\phi_1} + 1\right)\right) \]
                10. lower-neg.f6455.1

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

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

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

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

                  \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
                3. +-commutativeN/A

                  \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
                4. lower-+.f6460.0

                  \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
              7. Applied rewrites60.0%

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

            Alternative 13: 52.3% accurate, 13.9× speedup?

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

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

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

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

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

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

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

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

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

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

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

                \[\leadsto R \cdot \left(\left(-\phi_1\right) \cdot \left(\frac{\mathsf{neg}\left(\phi_2\right)}{\phi_1} + 1\right)\right) \]
              10. lower-neg.f6453.3

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

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
              3. +-commutativeN/A

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
              4. lower-+.f6457.9

                \[\leadsto R \cdot \left(\left(-\phi_1\right) + \phi_2\right) \]
            7. Applied rewrites57.9%

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

            Alternative 14: 50.9% accurate, 12.2× speedup?

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

              1. Initial program 64.5%

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

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

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

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

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

              if 1.35000000000000005e-13 < phi2

              1. Initial program 55.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. Taylor expanded in phi2 around inf

                \[\leadsto R \cdot \color{blue}{\phi_2} \]
              3. Step-by-step derivation
                1. Applied rewrites61.9%

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

              Alternative 15: 30.9% accurate, 27.0× 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 60.5%

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

                \[\leadsto R \cdot \color{blue}{\phi_2} \]
              3. Step-by-step derivation
                1. Applied rewrites30.9%

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

                Alternative 16: 3.5% accurate, 27.0× speedup?

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

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

                  \[\leadsto R \cdot \color{blue}{\phi_1} \]
                3. Step-by-step derivation
                  1. Applied rewrites3.5%

                    \[\leadsto R \cdot \color{blue}{\phi_1} \]
                  2. Add Preprocessing

                  Reproduce

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