Equirectangular approximation to distance on a great circle

Percentage Accurate: 60.6% → 95.8%
Time: 6.4s
Alternatives: 14
Speedup: 1.3×

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 14 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.6% 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.8% accurate, 1.2× speedup?

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

    1. Initial program 63.9%

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

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

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

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

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

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

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

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

    if 1.45000000000000013e-57 < phi2

    1. Initial program 57.4%

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

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

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

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

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

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

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

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

Alternative 2: 96.3% accurate, 1.2× speedup?

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

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

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

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

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

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

Alternative 3: 87.2% accurate, 1.2× speedup?

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

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


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

    1. Initial program 64.7%

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

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

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

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

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

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

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

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

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

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

      if 14 < phi2

      1. Initial program 55.4%

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

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

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

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

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

        \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \cos \color{blue}{\left(\frac{1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
      6. Step-by-step derivation
        1. lower-*.f6481.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 \]
      7. Applied rewrites81.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 \]
      8. 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 \]
      9. 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.f6477.3

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

        \[\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 \]
    10. Recombined 2 regimes into one program.
    11. Add Preprocessing

    Alternative 4: 85.1% accurate, 1.2× speedup?

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

      1. Initial program 64.8%

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

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

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

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

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

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

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

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

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

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

        if 36 < phi2 < 9.99999999999999981e149

        1. Initial program 67.7%

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

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

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

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

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

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

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

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

            \[\leadsto \mathsf{fma}\left(\frac{\phi_1 \cdot R}{\phi_2}, -1, R\right) \cdot \phi_2 \]
          8. lower-*.f6455.2

            \[\leadsto \mathsf{fma}\left(\frac{\phi_1 \cdot R}{\phi_2}, -1, R\right) \cdot \phi_2 \]
        5. Applied rewrites55.2%

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

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

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

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

            \[\leadsto \phi_1 \cdot \mathsf{fma}\left(-1, R, \frac{R \cdot \phi_2}{\phi_1}\right) \]
          4. lift-*.f6455.5

            \[\leadsto \phi_1 \cdot \mathsf{fma}\left(-1, R, \frac{R \cdot \phi_2}{\phi_1}\right) \]
        8. Applied rewrites55.5%

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

        if 9.99999999999999981e149 < phi2

        1. Initial program 45.7%

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

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

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

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

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

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

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

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

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

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

            \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
          10. lower-*.f6476.9

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      Alternative 5: 90.5% accurate, 1.3× 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.6%

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

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

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

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

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

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

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

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

      Alternative 6: 60.4% accurate, 2.3× speedup?

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

        1. Initial program 62.7%

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

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

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

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

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

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

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

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

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

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

            \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
          10. lower-*.f6458.1

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

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

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

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

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

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

            \[\leadsto \mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right) \]
          5. lower-*.f6460.2

            \[\leadsto \mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right) \]
        8. Applied rewrites60.2%

          \[\leadsto \mathsf{fma}\left(-R, \color{blue}{\phi_1}, R \cdot \phi_2\right) \]
        9. Taylor expanded in R around 0

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

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

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

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

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

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

        if 5.4000000000000002e175 < lambda2

        1. Initial program 41.8%

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

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

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

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

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

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

            \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \left(-1 \cdot \cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right) + \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 \mathsf{hypot}\left(\phi_1 - \phi_2, \left(-1 \cdot \cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right) + \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 \]
        7. Applied rewrites90.8%

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

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

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

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

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

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

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

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

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

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

            \[\leadsto \left(R \cdot \lambda_2\right) \cdot \cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right) \]
        10. Applied rewrites52.6%

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

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

            \[\leadsto \left(R \cdot \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_2\right) \]
        13. Recombined 2 regimes into one program.
        14. Add Preprocessing

        Alternative 7: 60.2% accurate, 2.3× speedup?

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

          1. Initial program 62.4%

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

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

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

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

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

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

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

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

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

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

              \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
            10. lower-*.f6457.9

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

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

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

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

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

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

              \[\leadsto \mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right) \]
            5. lower-*.f6459.9

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

            \[\leadsto \mathsf{fma}\left(-R, \color{blue}{\phi_1}, R \cdot \phi_2\right) \]
          9. Taylor expanded in R around 0

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

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

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

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

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

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

          if 4.6000000000000002e195 < lambda2

          1. Initial program 42.1%

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

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

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

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

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

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

              \[\leadsto \mathsf{hypot}\left(\phi_1 - \phi_2, \left(-1 \cdot \cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right) + \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 \mathsf{hypot}\left(\phi_1 - \phi_2, \left(-1 \cdot \cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right) + \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 \]
          7. Applied rewrites90.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

              \[\leadsto \left(R \cdot \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right) \]
          13. Recombined 2 regimes into one program.
          14. Add Preprocessing

          Alternative 8: 59.3% accurate, 5.8× speedup?

          \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_1 - \lambda_2 \leq -9.8 \cdot 10^{+209}:\\ \;\;\;\;\left(\phi_1 \cdot \mathsf{fma}\left(-1, \frac{R}{\phi_2}, \frac{R}{\phi_1}\right)\right) \cdot \phi_2\\ \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 (<= (- lambda1 lambda2) -9.8e+209)
             (* (* phi1 (fma -1.0 (/ R phi2) (/ R phi1))) phi2)
             (* 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 ((lambda1 - lambda2) <= -9.8e+209) {
          		tmp = (phi1 * fma(-1.0, (R / phi2), (R / phi1))) * phi2;
          	} 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 (Float64(lambda1 - lambda2) <= -9.8e+209)
          		tmp = Float64(Float64(phi1 * fma(-1.0, Float64(R / phi2), Float64(R / phi1))) * phi2);
          	else
          		tmp = Float64(R * Float64(phi2 + Float64(-phi1)));
          	end
          	return tmp
          end
          
          NOTE: R, lambda1, lambda2, phi1, and phi2 should be sorted in increasing order before calling this function.
          code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[N[(lambda1 - lambda2), $MachinePrecision], -9.8e+209], N[(N[(phi1 * N[(-1.0 * N[(R / phi2), $MachinePrecision] + N[(R / phi1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * phi2), $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}\;\lambda_1 - \lambda_2 \leq -9.8 \cdot 10^{+209}:\\
          \;\;\;\;\left(\phi_1 \cdot \mathsf{fma}\left(-1, \frac{R}{\phi_2}, \frac{R}{\phi_1}\right)\right) \cdot \phi_2\\
          
          \mathbf{else}:\\
          \;\;\;\;R \cdot \left(\phi_2 + \left(-\phi_1\right)\right)\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if (-.f64 lambda1 lambda2) < -9.7999999999999995e209

            1. Initial program 45.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \left(\phi_1 \cdot \mathsf{fma}\left(-1, \frac{R}{\phi_2}, \frac{R}{\phi_1}\right)\right) \cdot \phi_2 \]
              4. lower-/.f6439.2

                \[\leadsto \left(\phi_1 \cdot \mathsf{fma}\left(-1, \frac{R}{\phi_2}, \frac{R}{\phi_1}\right)\right) \cdot \phi_2 \]
            8. Applied rewrites39.2%

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

            if -9.7999999999999995e209 < (-.f64 lambda1 lambda2)

            1. Initial program 63.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right) \]
              5. lower-*.f6461.9

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

              \[\leadsto \mathsf{fma}\left(-R, \color{blue}{\phi_1}, R \cdot \phi_2\right) \]
            9. Taylor expanded in R around 0

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

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

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

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

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

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

          Alternative 9: 58.8% accurate, 7.7× speedup?

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

            1. Initial program 45.0%

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
              10. lower-*.f6435.0

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
            5. Applied rewrites35.0%

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \left(-\phi_2\right) \cdot \mathsf{fma}\left(R, \frac{\phi_1}{\phi_2}, \mathsf{neg}\left(R\right)\right) \]
              10. lower-neg.f6435.7

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

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

            if -1.05e209 < (-.f64 lambda1 lambda2)

            1. Initial program 63.3%

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
              10. lower-*.f6459.5

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

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

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

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

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

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

                \[\leadsto \mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right) \]
              5. lower-*.f6461.9

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

              \[\leadsto \mathsf{fma}\left(-R, \color{blue}{\phi_1}, R \cdot \phi_2\right) \]
            9. Taylor expanded in R around 0

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(\mathsf{neg}\left(\phi_1\right)\right)\right) \]
              4. lower-neg.f6462.8

                \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
            11. Applied rewrites62.8%

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

          Alternative 10: 57.5% accurate, 8.5× speedup?

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

            1. Initial program 45.1%

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

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

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

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

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

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

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

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

                \[\leadsto \mathsf{fma}\left(\frac{\phi_1 \cdot R}{\phi_2}, -1, R\right) \cdot \phi_2 \]
              8. lower-*.f6430.2

                \[\leadsto \mathsf{fma}\left(\frac{\phi_1 \cdot R}{\phi_2}, -1, R\right) \cdot \phi_2 \]
            5. Applied rewrites30.2%

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

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

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

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

                \[\leadsto \frac{\mathsf{neg}\left(R \cdot \phi_1\right)}{\phi_2} \cdot \phi_2 \]
              4. distribute-lft-neg-outN/A

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

                \[\leadsto \frac{\left(\mathsf{neg}\left(R\right)\right) \cdot \phi_1}{\phi_2} \cdot \phi_2 \]
              6. lift-neg.f6419.7

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

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

            if -2.55000000000000001e247 < (-.f64 lambda1 lambda2)

            1. Initial program 62.2%

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
              10. lower-*.f6458.3

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
            5. Applied rewrites58.3%

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

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

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

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

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

                \[\leadsto \mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right) \]
              5. lower-*.f6460.5

                \[\leadsto \mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right) \]
            8. Applied rewrites60.5%

              \[\leadsto \mathsf{fma}\left(-R, \color{blue}{\phi_1}, R \cdot \phi_2\right) \]
            9. Taylor expanded in R around 0

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(\mathsf{neg}\left(\phi_1\right)\right)\right) \]
              4. lower-neg.f6461.3

                \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
            11. Applied rewrites61.3%

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

          Alternative 11: 59.1% accurate, 9.0× speedup?

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

            1. Initial program 51.9%

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
              10. lower-*.f6453.4

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
            5. Applied rewrites53.4%

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

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

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

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

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

                \[\leadsto \mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right) \]
              5. lower-*.f6455.8

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

              \[\leadsto \mathsf{fma}\left(-R, \color{blue}{\phi_1}, R \cdot \phi_2\right) \]
            9. Taylor expanded in R around 0

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(\mathsf{neg}\left(\phi_1\right)\right)\right) \]
              4. lower-neg.f6456.3

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

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

            if 2.3000000000000002e53 < R

            1. Initial program 94.0%

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
              10. lower-*.f6465.5

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

              \[\leadsto \color{blue}{\left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right)} \]
            6. Step-by-step derivation
              1. lift-*.f64N/A

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

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

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

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right) \]
              5. lower-/.f6469.8

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right) \]
            7. Applied rewrites69.8%

              \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right) \]
            8. Taylor expanded in phi1 around inf

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

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

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

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

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

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

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

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

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

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

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

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

          Alternative 12: 52.9% accurate, 19.9× speedup?

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

            1. Initial program 64.9%

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

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

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

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

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

            if 1.45e16 < phi2

            1. Initial program 54.8%

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

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

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

            Alternative 13: 58.3% accurate, 25.4× 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.6%

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

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

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

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

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

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

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

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

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

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

                \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
              10. lower-*.f6455.9

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

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

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

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

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

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

                \[\leadsto \mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right) \]
              5. lower-*.f6457.4

                \[\leadsto \mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right) \]
            8. Applied rewrites57.4%

              \[\leadsto \mathsf{fma}\left(-R, \color{blue}{\phi_1}, R \cdot \phi_2\right) \]
            9. Taylor expanded in R around 0

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

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

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

                \[\leadsto R \cdot \left(\phi_2 + \left(\mathsf{neg}\left(\phi_1\right)\right)\right) \]
              4. lower-neg.f6458.3

                \[\leadsto R \cdot \left(\phi_2 + \left(-\phi_1\right)\right) \]
            11. Applied rewrites58.3%

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

            Alternative 14: 31.9% accurate, 46.5× speedup?

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

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

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

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

              Reproduce

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