Equirectangular approximation to distance on a great circle

Percentage Accurate: 60.4% → 95.9%
Time: 5.3s
Alternatives: 11
Speedup: 1.8×

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

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

Initial Program: 60.4% 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: 95.9% 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 -0.0046:\\ \;\;\;\;\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 -0.0046)
   (* (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 <= -0.0046) {
		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 <= -0.0046) {
		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 <= -0.0046:
		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 <= -0.0046)
		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 <= -0.0046)
		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, -0.0046], 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 -0.0046:\\
\;\;\;\;\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 < -0.0045999999999999999

    1. Initial program 60.4%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. 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 rewrites95.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 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-*.f6490.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 rewrites90.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 -0.0045999999999999999 < phi1

    1. Initial program 60.4%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. 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 rewrites95.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. lift-*.f6490.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 rewrites90.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.4%

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

Alternative 3: 90.3% accurate, 1.8× 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(0.5 \cdot \phi_1\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 (* 0.5 phi1)) (- 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((0.5 * phi1)) * (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((0.5 * phi1)) * (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((0.5 * phi1)) * (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(0.5 * phi1)) * 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((0.5 * phi1)) * (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[(0.5 * phi1), $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(0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R
\end{array}
Derivation
  1. Initial program 60.4%

    \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
  2. 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 rewrites95.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 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-*.f6490.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 rewrites90.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 \]
  7. Add Preprocessing

Alternative 4: 75.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}\;\lambda_1 \leq -4.8 \cdot 10^{+241}:\\ \;\;\;\;\left(-\cos \left(0.5 \cdot \phi_2\right) \cdot \lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(0.5 \cdot \phi_1\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 (<= lambda1 -4.8e+241)
   (* (- (* (cos (* 0.5 phi2)) lambda1)) R)
   (* (hypot (- phi1 phi2) (* (cos (* 0.5 phi1)) (- 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 (lambda1 <= -4.8e+241) {
		tmp = -(cos((0.5 * phi2)) * lambda1) * R;
	} else {
		tmp = hypot((phi1 - phi2), (cos((0.5 * phi1)) * -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 (lambda1 <= -4.8e+241) {
		tmp = -(Math.cos((0.5 * phi2)) * lambda1) * R;
	} else {
		tmp = Math.hypot((phi1 - phi2), (Math.cos((0.5 * phi1)) * -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 lambda1 <= -4.8e+241:
		tmp = -(math.cos((0.5 * phi2)) * lambda1) * R
	else:
		tmp = math.hypot((phi1 - phi2), (math.cos((0.5 * phi1)) * -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 (lambda1 <= -4.8e+241)
		tmp = Float64(Float64(-Float64(cos(Float64(0.5 * phi2)) * lambda1)) * R);
	else
		tmp = Float64(hypot(Float64(phi1 - phi2), Float64(cos(Float64(0.5 * phi1)) * 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 (lambda1 <= -4.8e+241)
		tmp = -(cos((0.5 * phi2)) * lambda1) * R;
	else
		tmp = hypot((phi1 - phi2), (cos((0.5 * phi1)) * -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[lambda1, -4.8e+241], N[((-N[(N[Cos[N[(0.5 * phi2), $MachinePrecision]], $MachinePrecision] * lambda1), $MachinePrecision]) * R), $MachinePrecision], N[(N[Sqrt[N[(phi1 - phi2), $MachinePrecision] ^ 2 + N[(N[Cos[N[(0.5 * phi1), $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}\;\lambda_1 \leq -4.8 \cdot 10^{+241}:\\
\;\;\;\;\left(-\cos \left(0.5 \cdot \phi_2\right) \cdot \lambda_1\right) \cdot R\\

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


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

    1. Initial program 60.4%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. 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 rewrites95.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 lambda1 around -inf

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

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

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

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

        \[\leadsto \left(-\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right) \cdot \lambda_1\right) \cdot R \]
      5. 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 \]
      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-+.f6416.8

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

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

      \[\leadsto \left(-\cos \left(\frac{1}{2} \cdot \phi_2\right) \cdot \lambda_1\right) \cdot R \]
    8. Step-by-step derivation
      1. Applied rewrites15.4%

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

      if -4.7999999999999998e241 < lambda1

      1. Initial program 60.4%

        \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      2. 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 rewrites95.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 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-*.f6490.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 rewrites90.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 \]
      7. Taylor expanded in lambda1 around 0

        \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(\frac{1}{2} \cdot \phi_1\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_1\right) \cdot \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) \cdot R \]
        2. lower-neg.f6474.1

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

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

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

      1. Initial program 60.4%

        \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      2. 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.f6431.5

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

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

      if -2.2e-250 < phi2 < 1.75

      1. Initial program 60.4%

        \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      2. 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 rewrites95.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 lambda2 around inf

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

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

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

        \[\leadsto \color{blue}{\left(\left(\cos \left(0.5 \cdot \left(\phi_1 + \phi_2\right)\right) + \left(-\lambda_1 \cdot \frac{\cos \left(0.5 \cdot \left(\phi_1 + \phi_2\right)\right)}{\lambda_2}\right)\right) \cdot \lambda_2\right)} \cdot R \]
      7. Taylor expanded in lambda2 around 0

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

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

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

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

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

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

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

          \[\leadsto \left(\frac{\cos \left(0.5 \cdot \left(\phi_1 + \phi_2\right)\right) \cdot \left(\lambda_2 - \lambda_1\right)}{\lambda_2} \cdot \lambda_2\right) \cdot R \]
      9. Applied rewrites26.4%

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

      if 1.75 < phi2

      1. Initial program 60.4%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \color{blue}{\left(\left(\frac{-\phi_1}{\phi_2} + 1\right) \cdot \phi_2\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. lower-+.f6457.1

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

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

    Alternative 6: 57.9% accurate, 5.0× speedup?

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

      1. Initial program 60.4%

        \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      2. 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.f6431.5

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

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

      if -2.2e-250 < phi2 < 6.19999999999999968e-44

      1. Initial program 60.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(0.5 + 0.5 \cdot \cos \left(2 \cdot \left(0.5 \cdot \phi_2\right)\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2 \cdot \phi_2\right)} \]
      4. Applied rewrites48.9%

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

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

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

          \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)} \]
        3. lift--.f64N/A

          \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)} \]
        4. lift-*.f6436.0

          \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \color{blue}{\lambda_2}\right)} \]
      7. Applied rewrites36.0%

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

      if 6.19999999999999968e-44 < phi2

      1. Initial program 60.4%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \color{blue}{\left(\left(\frac{-\phi_1}{\phi_2} + 1\right) \cdot \phi_2\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. lower-+.f6457.1

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

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

    Alternative 7: 57.1% accurate, 8.6× speedup?

    \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 7 \cdot 10^{+132}:\\ \;\;\;\;R \cdot \left(\phi_2 + \left(-\phi_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \sqrt{\lambda_2 \cdot \lambda_2}\\ \end{array} \end{array} \]
    NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
    (FPCore (R lambda1 lambda2 phi1 phi2)
     :precision binary64
     (if (<= lambda2 7e+132)
       (* R (+ phi2 (- phi1)))
       (* R (sqrt (* lambda2 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 <= 7e+132) {
    		tmp = R * (phi2 + -phi1);
    	} else {
    		tmp = R * sqrt((lambda2 * 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 <= 7d+132) then
            tmp = r * (phi2 + -phi1)
        else
            tmp = r * sqrt((lambda2 * 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 <= 7e+132) {
    		tmp = R * (phi2 + -phi1);
    	} else {
    		tmp = R * Math.sqrt((lambda2 * 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 <= 7e+132:
    		tmp = R * (phi2 + -phi1)
    	else:
    		tmp = R * math.sqrt((lambda2 * 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 <= 7e+132)
    		tmp = Float64(R * Float64(phi2 + Float64(-phi1)));
    	else
    		tmp = Float64(R * sqrt(Float64(lambda2 * 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 <= 7e+132)
    		tmp = R * (phi2 + -phi1);
    	else
    		tmp = R * sqrt((lambda2 * 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, 7e+132], N[(R * N[(phi2 + (-phi1)), $MachinePrecision]), $MachinePrecision], N[(R * N[Sqrt[N[(lambda2 * lambda2), $MachinePrecision]], $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 7 \cdot 10^{+132}:\\
    \;\;\;\;R \cdot \left(\phi_2 + \left(-\phi_1\right)\right)\\
    
    \mathbf{else}:\\
    \;\;\;\;R \cdot \sqrt{\lambda_2 \cdot \lambda_2}\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 2 regimes
    2. if lambda2 < 7.00000000000000041e132

      1. Initial program 60.4%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto R \cdot \color{blue}{\left(\left(\frac{-\phi_1}{\phi_2} + 1\right) \cdot \phi_2\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. lower-+.f6457.1

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

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

      if 7.00000000000000041e132 < lambda2

      1. Initial program 60.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

          \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(0.5 + 0.5 \cdot \cos \left(2 \cdot \left(0.5 \cdot \phi_2\right)\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_2 \cdot \phi_2\right)} \]
      4. Applied rewrites48.9%

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

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

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

          \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)} \]
        3. lift--.f64N/A

          \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)} \]
        4. lift-*.f6436.0

          \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \color{blue}{\lambda_2}\right)} \]
      7. Applied rewrites36.0%

        \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}} \]
      8. Taylor expanded in lambda1 around 0

        \[\leadsto R \cdot \sqrt{{\lambda_2}^{2}} \]
      9. Step-by-step derivation
        1. unpow2N/A

          \[\leadsto R \cdot \sqrt{\lambda_2 \cdot \lambda_2} \]
        2. lower-*.f6421.9

          \[\leadsto R \cdot \sqrt{\lambda_2 \cdot \lambda_2} \]
      10. Applied rewrites21.9%

        \[\leadsto R \cdot \sqrt{\lambda_2 \cdot \lambda_2} \]
    3. Recombined 2 regimes into one program.
    4. Add Preprocessing

    Alternative 8: 56.8% accurate, 13.9× speedup?

    \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ R \cdot \left(\phi_2 + \left(-\phi_1\right)\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 (+ phi2 (- phi1))))
    assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
    double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
    	return R * (phi2 + -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 * (phi2 + -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 * (phi2 + -phi1);
    }
    
    [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
    def code(R, lambda1, lambda2, phi1, phi2):
    	return R * (phi2 + -phi1)
    
    R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
    function code(R, lambda1, lambda2, phi1, phi2)
    	return Float64(R * Float64(phi2 + Float64(-phi1)))
    end
    
    R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
    function tmp = code(R, lambda1, lambda2, phi1, phi2)
    	tmp = R * (phi2 + -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 * N[(phi2 + (-phi1)), $MachinePrecision]), $MachinePrecision]
    
    \begin{array}{l}
    [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
    \\
    R \cdot \left(\phi_2 + \left(-\phi_1\right)\right)
    \end{array}
    
    Derivation
    1. Initial program 60.4%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto R \cdot \color{blue}{\left(\left(\frac{-\phi_1}{\phi_2} + 1\right) \cdot \phi_2\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. lower-+.f6457.1

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

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

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

      1. Initial program 60.4%

        \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      2. 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.f6431.5

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

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

      if -2.8e15 < phi1

      1. Initial program 60.4%

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

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

          \[\leadsto \phi_2 \cdot \color{blue}{R} \]
        2. lower-*.f6431.0

          \[\leadsto \phi_2 \cdot \color{blue}{R} \]
      4. Applied rewrites31.0%

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

    Alternative 10: 31.0% accurate, 27.0× speedup?

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

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

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

        \[\leadsto \phi_2 \cdot \color{blue}{R} \]
      2. lower-*.f6431.0

        \[\leadsto \phi_2 \cdot \color{blue}{R} \]
    4. Applied rewrites31.0%

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

    Alternative 11: 3.6% accurate, 27.0× speedup?

    \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \phi_1 \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 (* phi1 R))
    assert(R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2);
    double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
    	return phi1 * R;
    }
    
    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 = phi1 * r
    end function
    
    assert R < lambda1 && lambda1 < lambda2 && lambda2 < phi1 && phi1 < phi2;
    public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
    	return phi1 * R;
    }
    
    [R, lambda1, lambda2, phi1, phi2] = sort([R, lambda1, lambda2, phi1, phi2])
    def code(R, lambda1, lambda2, phi1, phi2):
    	return phi1 * R
    
    R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
    function code(R, lambda1, lambda2, phi1, phi2)
    	return Float64(phi1 * R)
    end
    
    R, lambda1, lambda2, phi1, phi2 = num2cell(sort([R, lambda1, lambda2, phi1, phi2])){:}
    function tmp = code(R, lambda1, lambda2, phi1, phi2)
    	tmp = phi1 * 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[(phi1 * R), $MachinePrecision]
    
    \begin{array}{l}
    [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
    \\
    \phi_1 \cdot R
    \end{array}
    
    Derivation
    1. Initial program 60.4%

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

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

        \[\leadsto \phi_1 \cdot \color{blue}{R} \]
      2. lower-*.f643.6

        \[\leadsto \phi_1 \cdot \color{blue}{R} \]
    4. Applied rewrites3.6%

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

    Reproduce

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