Equirectangular approximation to distance on a great circle

Percentage Accurate: 59.2% → 95.9%
Time: 8.0s
Alternatives: 14
Speedup: 13.9×

Specification

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

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

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

The average percentage accuracy by input value. Horizontal axis shows value of an input variable; the variable is choosen in the title. Vertical axis is accuracy; higher is better. Red represent the original program, while blue represents Herbie's suggestion. These can be toggled with buttons below the plot. The line is an average while dots represent individual samples.

Accuracy vs Speed?

Herbie found 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: 59.2% accurate, 1.0× speedup?

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

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

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

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

Alternative 1: 95.9% accurate, 1.2× speedup?

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

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


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

    1. Initial program 52.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 rewrites96.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-*.f6496.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 rewrites96.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 -8.5 < phi1

    1. Initial program 60.4%

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

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

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

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

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

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

        \[\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 rewrites92.1%

      \[\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: 70.9% accurate, 0.6× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)\\ t_1 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\ \mathbf{if}\;\sqrt{t\_1 \cdot t\_1 + t\_0} \leq 10^{+150}:\\ \;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_2, \lambda_2 + -2 \cdot \lambda_1, \lambda_1 \cdot \lambda_1\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(2 \cdot \frac{\phi_2 + \phi_1}{2}\right)\right) + t\_0}\\ \mathbf{else}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, 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
 (let* ((t_0 (* (- phi1 phi2) (- phi1 phi2)))
        (t_1 (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0)))))
   (if (<= (sqrt (+ (* t_1 t_1) t_0)) 1e+150)
     (*
      R
      (sqrt
       (+
        (*
         (fma lambda2 (+ lambda2 (* -2.0 lambda1)) (* lambda1 lambda1))
         (+ 0.5 (* 0.5 (cos (* 2.0 (/ (+ phi2 phi1) 2.0))))))
        t_0)))
     (* (- phi1) (fma (* phi2 (/ R phi1)) -1.0 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 = (phi1 - phi2) * (phi1 - phi2);
	double t_1 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	double tmp;
	if (sqrt(((t_1 * t_1) + t_0)) <= 1e+150) {
		tmp = R * sqrt(((fma(lambda2, (lambda2 + (-2.0 * lambda1)), (lambda1 * lambda1)) * (0.5 + (0.5 * cos((2.0 * ((phi2 + phi1) / 2.0)))))) + t_0));
	} else {
		tmp = -phi1 * fma((phi2 * (R / phi1)), -1.0, R);
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(Float64(phi1 - phi2) * Float64(phi1 - phi2))
	t_1 = Float64(Float64(lambda1 - lambda2) * cos(Float64(Float64(phi1 + phi2) / 2.0)))
	tmp = 0.0
	if (sqrt(Float64(Float64(t_1 * t_1) + t_0)) <= 1e+150)
		tmp = Float64(R * sqrt(Float64(Float64(fma(lambda2, Float64(lambda2 + Float64(-2.0 * lambda1)), Float64(lambda1 * lambda1)) * Float64(0.5 + Float64(0.5 * cos(Float64(2.0 * Float64(Float64(phi2 + phi1) / 2.0)))))) + t_0)));
	else
		tmp = Float64(Float64(-phi1) * fma(Float64(phi2 * Float64(R / phi1)), -1.0, 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_] := Block[{t$95$0 = N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(N[(phi1 + phi2), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Sqrt[N[(N[(t$95$1 * t$95$1), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision], 1e+150], N[(R * N[Sqrt[N[(N[(N[(lambda2 * N[(lambda2 + N[(-2.0 * lambda1), $MachinePrecision]), $MachinePrecision] + N[(lambda1 * lambda1), $MachinePrecision]), $MachinePrecision] * N[(0.5 + N[(0.5 * N[Cos[N[(2.0 * N[(N[(phi2 + phi1), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-phi1) * N[(N[(phi2 * N[(R / phi1), $MachinePrecision]), $MachinePrecision] * -1.0 + R), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)\\
t_1 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\
\mathbf{if}\;\sqrt{t\_1 \cdot t\_1 + t\_0} \leq 10^{+150}:\\
\;\;\;\;R \cdot \sqrt{\mathsf{fma}\left(\lambda_2, \lambda_2 + -2 \cdot \lambda_1, \lambda_1 \cdot \lambda_1\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(2 \cdot \frac{\phi_2 + \phi_1}{2}\right)\right) + t\_0}\\

\mathbf{else}:\\
\;\;\;\;\left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sqrt.f64 (+.f64 (*.f64 (*.f64 (-.f64 lambda1 lambda2) (cos.f64 (/.f64 (+.f64 phi1 phi2) #s(literal 2 binary64)))) (*.f64 (-.f64 lambda1 lambda2) (cos.f64 (/.f64 (+.f64 phi1 phi2) #s(literal 2 binary64))))) (*.f64 (-.f64 phi1 phi2) (-.f64 phi1 phi2)))) < 9.99999999999999981e149

    1. Initial program 98.3%

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

        \[\leadsto 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)} \]
      2. lift--.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \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. lift-cos.f64N/A

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

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

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

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

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

        \[\leadsto 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 \color{blue}{\cos \left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      10. lift-+.f64N/A

        \[\leadsto 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{\color{blue}{\phi_1 + \phi_2}}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      11. lift-/.f64N/A

        \[\leadsto 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 \color{blue}{\left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      12. swap-sqrN/A

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

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

        \[\leadsto R \cdot \sqrt{\color{blue}{{\left(\lambda_1 - \lambda_2\right)}^{2} \cdot \left(\cos \left(\frac{\phi_1 + \phi_2}{2}\right) \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.3%

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

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

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

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

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

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

        \[\leadsto R \cdot \sqrt{\mathsf{fma}\left(\lambda_2, \lambda_2 + -2 \cdot \lambda_1, \lambda_1 \cdot \lambda_1\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(2 \cdot \frac{\phi_2 + \phi_1}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    7. Applied rewrites98.3%

      \[\leadsto R \cdot \sqrt{\color{blue}{\mathsf{fma}\left(\lambda_2, \lambda_2 + -2 \cdot \lambda_1, \lambda_1 \cdot \lambda_1\right)} \cdot \left(0.5 + 0.5 \cdot \cos \left(2 \cdot \frac{\phi_2 + \phi_1}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]

    if 9.99999999999999981e149 < (sqrt.f64 (+.f64 (*.f64 (*.f64 (-.f64 lambda1 lambda2) (cos.f64 (/.f64 (+.f64 phi1 phi2) #s(literal 2 binary64)))) (*.f64 (-.f64 lambda1 lambda2) (cos.f64 (/.f64 (+.f64 phi1 phi2) #s(literal 2 binary64))))) (*.f64 (-.f64 phi1 phi2) (-.f64 phi1 phi2))))

    1. Initial program 41.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-*.f6425.1

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

      \[\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-/.f6425.2

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

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

Alternative 3: 70.9% accurate, 0.6× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} t_0 := \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)\\ t_1 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\ \mathbf{if}\;\sqrt{t\_1 \cdot t\_1 + t\_0} \leq 10^{+150}:\\ \;\;\;\;R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(2 \cdot \frac{\phi_2 + \phi_1}{2}\right)\right) + t\_0}\\ \mathbf{else}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, 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
 (let* ((t_0 (* (- phi1 phi2) (- phi1 phi2)))
        (t_1 (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0)))))
   (if (<= (sqrt (+ (* t_1 t_1) t_0)) 1e+150)
     (*
      R
      (sqrt
       (+
        (*
         (* (- lambda1 lambda2) (- lambda1 lambda2))
         (+ 0.5 (* 0.5 (cos (* 2.0 (/ (+ phi2 phi1) 2.0))))))
        t_0)))
     (* (- phi1) (fma (* phi2 (/ R phi1)) -1.0 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 = (phi1 - phi2) * (phi1 - phi2);
	double t_1 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	double tmp;
	if (sqrt(((t_1 * t_1) + t_0)) <= 1e+150) {
		tmp = R * sqrt(((((lambda1 - lambda2) * (lambda1 - lambda2)) * (0.5 + (0.5 * cos((2.0 * ((phi2 + phi1) / 2.0)))))) + t_0));
	} else {
		tmp = -phi1 * fma((phi2 * (R / phi1)), -1.0, R);
	}
	return tmp;
}
R, lambda1, lambda2, phi1, phi2 = sort([R, lambda1, lambda2, phi1, phi2])
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(Float64(phi1 - phi2) * Float64(phi1 - phi2))
	t_1 = Float64(Float64(lambda1 - lambda2) * cos(Float64(Float64(phi1 + phi2) / 2.0)))
	tmp = 0.0
	if (sqrt(Float64(Float64(t_1 * t_1) + t_0)) <= 1e+150)
		tmp = Float64(R * sqrt(Float64(Float64(Float64(Float64(lambda1 - lambda2) * Float64(lambda1 - lambda2)) * Float64(0.5 + Float64(0.5 * cos(Float64(2.0 * Float64(Float64(phi2 + phi1) / 2.0)))))) + t_0)));
	else
		tmp = Float64(Float64(-phi1) * fma(Float64(phi2 * Float64(R / phi1)), -1.0, 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_] := Block[{t$95$0 = N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(N[(phi1 + phi2), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Sqrt[N[(N[(t$95$1 * t$95$1), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision], 1e+150], N[(R * N[Sqrt[N[(N[(N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] * N[(0.5 + N[(0.5 * N[Cos[N[(2.0 * N[(N[(phi2 + phi1), $MachinePrecision] / 2.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-phi1) * N[(N[(phi2 * N[(R / phi1), $MachinePrecision]), $MachinePrecision] * -1.0 + R), $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\
\\
\begin{array}{l}
t_0 := \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)\\
t_1 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\
\mathbf{if}\;\sqrt{t\_1 \cdot t\_1 + t\_0} \leq 10^{+150}:\\
\;\;\;\;R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(0.5 + 0.5 \cdot \cos \left(2 \cdot \frac{\phi_2 + \phi_1}{2}\right)\right) + t\_0}\\

\mathbf{else}:\\
\;\;\;\;\left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right)\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (sqrt.f64 (+.f64 (*.f64 (*.f64 (-.f64 lambda1 lambda2) (cos.f64 (/.f64 (+.f64 phi1 phi2) #s(literal 2 binary64)))) (*.f64 (-.f64 lambda1 lambda2) (cos.f64 (/.f64 (+.f64 phi1 phi2) #s(literal 2 binary64))))) (*.f64 (-.f64 phi1 phi2) (-.f64 phi1 phi2)))) < 9.99999999999999981e149

    1. Initial program 98.3%

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

        \[\leadsto 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)} \]
      2. lift--.f64N/A

        \[\leadsto R \cdot \sqrt{\left(\color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \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. lift-cos.f64N/A

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

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

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

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

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

        \[\leadsto 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 \color{blue}{\cos \left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      10. lift-+.f64N/A

        \[\leadsto 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{\color{blue}{\phi_1 + \phi_2}}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      11. lift-/.f64N/A

        \[\leadsto 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 \color{blue}{\left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
      12. swap-sqrN/A

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

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

        \[\leadsto R \cdot \sqrt{\color{blue}{{\left(\lambda_1 - \lambda_2\right)}^{2} \cdot \left(\cos \left(\frac{\phi_1 + \phi_2}{2}\right) \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.3%

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

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

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

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

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

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

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

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

    if 9.99999999999999981e149 < (sqrt.f64 (+.f64 (*.f64 (*.f64 (-.f64 lambda1 lambda2) (cos.f64 (/.f64 (+.f64 phi1 phi2) #s(literal 2 binary64)))) (*.f64 (-.f64 lambda1 lambda2) (cos.f64 (/.f64 (+.f64 phi1 phi2) #s(literal 2 binary64))))) (*.f64 (-.f64 phi1 phi2) (-.f64 phi1 phi2))))

    1. Initial program 41.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-*.f6425.1

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

      \[\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-/.f6425.2

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

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

Alternative 4: 96.0% accurate, 1.2× speedup?

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

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

Alternative 5: 78.7% accurate, 1.2× speedup?

\[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 4.7 \cdot 10^{-36}:\\ \;\;\;\;\mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(0.5 \cdot \phi_1\right) \cdot \lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(\phi_1, \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 (<= lambda2 4.7e-36)
   (* (hypot (- phi1 phi2) (* (cos (* 0.5 phi1)) lambda1)) R)
   (* (hypot phi1 (* (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 (lambda2 <= 4.7e-36) {
		tmp = hypot((phi1 - phi2), (cos((0.5 * phi1)) * lambda1)) * R;
	} else {
		tmp = hypot(phi1, (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 (lambda2 <= 4.7e-36) {
		tmp = Math.hypot((phi1 - phi2), (Math.cos((0.5 * phi1)) * lambda1)) * R;
	} else {
		tmp = Math.hypot(phi1, (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 lambda2 <= 4.7e-36:
		tmp = math.hypot((phi1 - phi2), (math.cos((0.5 * phi1)) * lambda1)) * R
	else:
		tmp = math.hypot(phi1, (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 (lambda2 <= 4.7e-36)
		tmp = Float64(hypot(Float64(phi1 - phi2), Float64(cos(Float64(0.5 * phi1)) * lambda1)) * R);
	else
		tmp = Float64(hypot(phi1, 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 (lambda2 <= 4.7e-36)
		tmp = hypot((phi1 - phi2), (cos((0.5 * phi1)) * lambda1)) * R;
	else
		tmp = hypot(phi1, (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[lambda2, 4.7e-36], N[(N[Sqrt[N[(phi1 - phi2), $MachinePrecision] ^ 2 + N[(N[Cos[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision] * lambda1), $MachinePrecision] ^ 2], $MachinePrecision] * R), $MachinePrecision], N[(N[Sqrt[phi1 ^ 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}\;\lambda_2 \leq 4.7 \cdot 10^{-36}:\\
\;\;\;\;\mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(0.5 \cdot \phi_1\right) \cdot \lambda_1\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\mathsf{hypot}\left(\phi_1, \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 lambda2 < 4.7000000000000003e-36

    1. Initial program 58.5%

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

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

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

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

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

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

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

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

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

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

      if 4.7000000000000003e-36 < lambda2

      1. Initial program 59.1%

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

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

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

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

        \[\leadsto \color{blue}{\mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(\frac{\phi_2 + \phi_1}{2}\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R} \]
      5. Taylor expanded in phi1 around 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-*.f6490.7

          \[\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 rewrites90.7%

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

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

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

      Alternative 6: 80.9% accurate, 1.2× speedup?

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

        1. Initial program 60.3%

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

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

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

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

          \[\leadsto \color{blue}{\mathsf{hypot}\left(\phi_1 - \phi_2, \cos \left(\frac{\phi_2 + \phi_1}{2}\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R} \]
        5. Taylor expanded in phi1 around 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-*.f6487.2

            \[\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 rewrites87.2%

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

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

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

          if 1.64999999999999994e34 < phi2

          1. Initial program 53.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-*.f6461.0

              \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\frac{\phi_2 \cdot R}{\phi_1}, -1, R\right) \]
          5. Applied rewrites61.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 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-*.f6462.9

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

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

        Alternative 7: 90.3% 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 58.7%

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

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

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

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

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

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

        Alternative 8: 60.5% accurate, 1.9× speedup?

        \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 2.55 \cdot 10^{+14}:\\ \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\ \mathbf{elif}\;\lambda_2 \leq 1.05 \cdot 10^{+46} \lor \neg \left(\lambda_2 \leq 1.7 \cdot 10^{+105}\right):\\ \;\;\;\;R \cdot \left(\lambda_2 \cdot \sqrt{0.5 + 0.5 \cdot \cos \left(\phi_1 + \phi_2\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, 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 (<= lambda2 2.55e+14)
           (fma (- R) phi1 (* R phi2))
           (if (or (<= lambda2 1.05e+46) (not (<= lambda2 1.7e+105)))
             (* R (* lambda2 (sqrt (+ 0.5 (* 0.5 (cos (+ phi1 phi2)))))))
             (* (- phi1) (fma (* phi2 (/ R phi1)) -1.0 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 (lambda2 <= 2.55e+14) {
        		tmp = fma(-R, phi1, (R * phi2));
        	} else if ((lambda2 <= 1.05e+46) || !(lambda2 <= 1.7e+105)) {
        		tmp = R * (lambda2 * sqrt((0.5 + (0.5 * cos((phi1 + phi2))))));
        	} else {
        		tmp = -phi1 * fma((phi2 * (R / phi1)), -1.0, 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 (lambda2 <= 2.55e+14)
        		tmp = fma(Float64(-R), phi1, Float64(R * phi2));
        	elseif ((lambda2 <= 1.05e+46) || !(lambda2 <= 1.7e+105))
        		tmp = Float64(R * Float64(lambda2 * sqrt(Float64(0.5 + Float64(0.5 * cos(Float64(phi1 + phi2)))))));
        	else
        		tmp = Float64(Float64(-phi1) * fma(Float64(phi2 * Float64(R / phi1)), -1.0, 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[lambda2, 2.55e+14], N[((-R) * phi1 + N[(R * phi2), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[lambda2, 1.05e+46], N[Not[LessEqual[lambda2, 1.7e+105]], $MachinePrecision]], N[(R * N[(lambda2 * N[Sqrt[N[(0.5 + N[(0.5 * N[Cos[N[(phi1 + phi2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[((-phi1) * N[(N[(phi2 * N[(R / phi1), $MachinePrecision]), $MachinePrecision] * -1.0 + R), $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 2.55 \cdot 10^{+14}:\\
        \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\
        
        \mathbf{elif}\;\lambda_2 \leq 1.05 \cdot 10^{+46} \lor \neg \left(\lambda_2 \leq 1.7 \cdot 10^{+105}\right):\\
        \;\;\;\;R \cdot \left(\lambda_2 \cdot \sqrt{0.5 + 0.5 \cdot \cos \left(\phi_1 + \phi_2\right)}\right)\\
        
        \mathbf{else}:\\
        \;\;\;\;\left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right)\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 3 regimes
        2. if lambda2 < 2.55e14

          1. Initial program 59.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-*.f6428.8

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

            \[\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-*.f6429.9

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

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

          if 2.55e14 < lambda2 < 1.05e46 or 1.7e105 < lambda2

          1. Initial program 56.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 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)} \]
            2. lift--.f64N/A

              \[\leadsto R \cdot \sqrt{\left(\color{blue}{\left(\lambda_1 - \lambda_2\right)} \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \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. lift-cos.f64N/A

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

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

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

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

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

              \[\leadsto 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 \color{blue}{\cos \left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
            10. lift-+.f64N/A

              \[\leadsto 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{\color{blue}{\phi_1 + \phi_2}}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
            11. lift-/.f64N/A

              \[\leadsto 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 \color{blue}{\left(\frac{\phi_1 + \phi_2}{2}\right)}\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
            12. swap-sqrN/A

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

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

              \[\leadsto R \cdot \sqrt{\color{blue}{{\left(\lambda_1 - \lambda_2\right)}^{2} \cdot \left(\cos \left(\frac{\phi_1 + \phi_2}{2}\right) \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 rewrites56.7%

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

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

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

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

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

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

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

              \[\leadsto R \cdot \left(\lambda_2 \cdot \sqrt{0.5 + 0.5 \cdot \cos \left(\phi_1 + \phi_2\right)}\right) \]
          7. Applied rewrites68.6%

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

          if 1.05e46 < lambda2 < 1.7e105

          1. Initial program 56.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-*.f6431.3

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

            \[\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-/.f6431.3

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

            \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right) \]
        3. Recombined 3 regimes into one program.
        4. Final simplification37.5%

          \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 2.55 \cdot 10^{+14}:\\ \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\ \mathbf{elif}\;\lambda_2 \leq 1.05 \cdot 10^{+46} \lor \neg \left(\lambda_2 \leq 1.7 \cdot 10^{+105}\right):\\ \;\;\;\;R \cdot \left(\lambda_2 \cdot \sqrt{0.5 + 0.5 \cdot \cos \left(\phi_1 + \phi_2\right)}\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right)\\ \end{array} \]
        5. Add Preprocessing

        Alternative 9: 57.2% accurate, 2.0× speedup?

        \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 2.55 \cdot 10^{+14}:\\ \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\ \mathbf{elif}\;\lambda_2 \leq 1.05 \cdot 10^{+46} \lor \neg \left(\lambda_2 \leq 4.5 \cdot 10^{+149}\right):\\ \;\;\;\;\left(R \cdot \lambda_2\right) \cdot \cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, 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 (<= lambda2 2.55e+14)
           (fma (- R) phi1 (* R phi2))
           (if (or (<= lambda2 1.05e+46) (not (<= lambda2 4.5e+149)))
             (* (* R lambda2) (cos (* 0.5 (+ phi2 phi1))))
             (* (- phi1) (fma (* phi2 (/ R phi1)) -1.0 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 (lambda2 <= 2.55e+14) {
        		tmp = fma(-R, phi1, (R * phi2));
        	} else if ((lambda2 <= 1.05e+46) || !(lambda2 <= 4.5e+149)) {
        		tmp = (R * lambda2) * cos((0.5 * (phi2 + phi1)));
        	} else {
        		tmp = -phi1 * fma((phi2 * (R / phi1)), -1.0, 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 (lambda2 <= 2.55e+14)
        		tmp = fma(Float64(-R), phi1, Float64(R * phi2));
        	elseif ((lambda2 <= 1.05e+46) || !(lambda2 <= 4.5e+149))
        		tmp = Float64(Float64(R * lambda2) * cos(Float64(0.5 * Float64(phi2 + phi1))));
        	else
        		tmp = Float64(Float64(-phi1) * fma(Float64(phi2 * Float64(R / phi1)), -1.0, 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[lambda2, 2.55e+14], N[((-R) * phi1 + N[(R * phi2), $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[lambda2, 1.05e+46], N[Not[LessEqual[lambda2, 4.5e+149]], $MachinePrecision]], N[(N[(R * lambda2), $MachinePrecision] * N[Cos[N[(0.5 * N[(phi2 + phi1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[((-phi1) * N[(N[(phi2 * N[(R / phi1), $MachinePrecision]), $MachinePrecision] * -1.0 + R), $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 2.55 \cdot 10^{+14}:\\
        \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\
        
        \mathbf{elif}\;\lambda_2 \leq 1.05 \cdot 10^{+46} \lor \neg \left(\lambda_2 \leq 4.5 \cdot 10^{+149}\right):\\
        \;\;\;\;\left(R \cdot \lambda_2\right) \cdot \cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right)\\
        
        \mathbf{else}:\\
        \;\;\;\;\left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right)\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 3 regimes
        2. if lambda2 < 2.55e14

          1. Initial program 59.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-*.f6428.8

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

            \[\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-*.f6429.9

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

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

          if 2.55e14 < lambda2 < 1.05e46 or 4.49999999999999982e149 < lambda2

          1. Initial program 48.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 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)} \]
          4. 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. lower-cos.f64N/A

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

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

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

              \[\leadsto \left(R \cdot \lambda_2\right) \cdot \cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right) \]
          5. Applied rewrites44.0%

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

          if 1.05e46 < lambda2 < 4.49999999999999982e149

          1. Initial program 69.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-*.f6428.9

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

            \[\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-/.f6424.9

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

            \[\leadsto \left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right) \]
        3. Recombined 3 regimes into one program.
        4. Final simplification31.5%

          \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 2.55 \cdot 10^{+14}:\\ \;\;\;\;\mathsf{fma}\left(-R, \phi_1, R \cdot \phi_2\right)\\ \mathbf{elif}\;\lambda_2 \leq 1.05 \cdot 10^{+46} \lor \neg \left(\lambda_2 \leq 4.5 \cdot 10^{+149}\right):\\ \;\;\;\;\left(R \cdot \lambda_2\right) \cdot \cos \left(0.5 \cdot \left(\phi_2 + \phi_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\left(-\phi_1\right) \cdot \mathsf{fma}\left(\phi_2 \cdot \frac{R}{\phi_1}, -1, R\right)\\ \end{array} \]
        5. Add Preprocessing

        Alternative 10: 58.3% accurate, 7.7× speedup?

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

          1. Initial program 49.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-*.f6423.8

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

            \[\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-*.f6424.9

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

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

          if 1.84999999999999999e59 < R

          1. Initial program 94.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-*.f6433.0

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

            \[\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-/.f6434.7

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

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

        Alternative 11: 59.5% accurate, 13.9× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

              \[\leadsto -\mathsf{fma}\left(\frac{-1}{2} \cdot R, \lambda_1 \cdot \left(\phi_1 \cdot \sin \left(\frac{1}{2} \cdot \phi_2\right)\right), R \cdot \left(\lambda_1 \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right)\right) \]
            4. associate-*r*N/A

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

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

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

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

              \[\leadsto -\mathsf{fma}\left(\frac{-1}{2} \cdot R, \left(\lambda_1 \cdot \phi_1\right) \cdot \sin \left(\frac{1}{2} \cdot \phi_2\right), R \cdot \left(\lambda_1 \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right)\right) \]
            9. associate-*r*N/A

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

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

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

              \[\leadsto -\mathsf{fma}\left(\frac{-1}{2} \cdot R, \left(\lambda_1 \cdot \phi_1\right) \cdot \sin \left(\frac{1}{2} \cdot \phi_2\right), \left(R \cdot \lambda_1\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right) \]
            13. lower-*.f6418.4

              \[\leadsto -\mathsf{fma}\left(-0.5 \cdot R, \left(\lambda_1 \cdot \phi_1\right) \cdot \sin \left(0.5 \cdot \phi_2\right), \left(R \cdot \lambda_1\right) \cdot \cos \left(0.5 \cdot \phi_2\right)\right) \]
          8. Applied rewrites18.4%

            \[\leadsto -\mathsf{fma}\left(-0.5 \cdot R, \left(\lambda_1 \cdot \phi_1\right) \cdot \sin \left(0.5 \cdot \phi_2\right), \left(R \cdot \lambda_1\right) \cdot \cos \left(0.5 \cdot \phi_2\right)\right) \]
          9. Taylor expanded in phi2 around 0

            \[\leadsto -R \cdot \lambda_1 \]
          10. Step-by-step derivation
            1. lift-*.f6466.3

              \[\leadsto -R \cdot \lambda_1 \]
          11. Applied rewrites66.3%

            \[\leadsto -R \cdot \lambda_1 \]

          if -3.60000000000000003e211 < lambda1

          1. Initial program 58.9%

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

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

            \[\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-*.f6426.6

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

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

        Alternative 12: 51.5% accurate, 19.9× speedup?

        \[\begin{array}{l} [R, lambda1, lambda2, phi1, phi2] = \mathsf{sort}([R, lambda1, lambda2, phi1, phi2])\\ \\ \begin{array}{l} \mathbf{if}\;\phi_1 \leq -1.1 \cdot 10^{+44}:\\ \;\;\;\;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 (<= phi1 -1.1e+44) (* 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 (phi1 <= -1.1e+44) {
        		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 (phi1 <= (-1.1d+44)) 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 (phi1 <= -1.1e+44) {
        		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 phi1 <= -1.1e+44:
        		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 (phi1 <= -1.1e+44)
        		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 (phi1 <= -1.1e+44)
        		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[phi1, -1.1e+44], 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_1 \leq -1.1 \cdot 10^{+44}:\\
        \;\;\;\;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 phi1 < -1.09999999999999998e44

          1. Initial program 51.6%

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

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

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

          if -1.09999999999999998e44 < phi1

          1. Initial program 60.4%

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

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

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

          Alternative 13: 38.4% 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 490000:\\ \;\;\;\;-R \cdot \lambda_1\\ \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 490000.0) (- (* R lambda1)) (* 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 <= 490000.0) {
          		tmp = -(R * lambda1);
          	} 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 <= 490000.0d0) then
                  tmp = -(r * lambda1)
              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 <= 490000.0) {
          		tmp = -(R * lambda1);
          	} 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 <= 490000.0:
          		tmp = -(R * lambda1)
          	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 <= 490000.0)
          		tmp = Float64(-Float64(R * lambda1));
          	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 <= 490000.0)
          		tmp = -(R * lambda1);
          	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, 490000.0], (-N[(R * lambda1), $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 490000:\\
          \;\;\;\;-R \cdot \lambda_1\\
          
          \mathbf{else}:\\
          \;\;\;\;R \cdot \phi_2\\
          
          
          \end{array}
          \end{array}
          
          Derivation
          1. Split input into 2 regimes
          2. if phi2 < 4.9e5

            1. Initial program 61.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 lambda1 around -inf

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                \[\leadsto -\mathsf{fma}\left(\frac{-1}{2} \cdot R, \lambda_1 \cdot \left(\phi_1 \cdot \sin \left(\frac{1}{2} \cdot \phi_2\right)\right), R \cdot \left(\lambda_1 \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right)\right) \]
              4. associate-*r*N/A

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

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

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

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

                \[\leadsto -\mathsf{fma}\left(\frac{-1}{2} \cdot R, \left(\lambda_1 \cdot \phi_1\right) \cdot \sin \left(\frac{1}{2} \cdot \phi_2\right), R \cdot \left(\lambda_1 \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right)\right) \]
              9. associate-*r*N/A

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

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

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

                \[\leadsto -\mathsf{fma}\left(\frac{-1}{2} \cdot R, \left(\lambda_1 \cdot \phi_1\right) \cdot \sin \left(\frac{1}{2} \cdot \phi_2\right), \left(R \cdot \lambda_1\right) \cdot \cos \left(\frac{1}{2} \cdot \phi_2\right)\right) \]
              13. lower-*.f6415.9

                \[\leadsto -\mathsf{fma}\left(-0.5 \cdot R, \left(\lambda_1 \cdot \phi_1\right) \cdot \sin \left(0.5 \cdot \phi_2\right), \left(R \cdot \lambda_1\right) \cdot \cos \left(0.5 \cdot \phi_2\right)\right) \]
            8. Applied rewrites15.9%

              \[\leadsto -\mathsf{fma}\left(-0.5 \cdot R, \left(\lambda_1 \cdot \phi_1\right) \cdot \sin \left(0.5 \cdot \phi_2\right), \left(R \cdot \lambda_1\right) \cdot \cos \left(0.5 \cdot \phi_2\right)\right) \]
            9. Taylor expanded in phi2 around 0

              \[\leadsto -R \cdot \lambda_1 \]
            10. Step-by-step derivation
              1. lift-*.f6414.5

                \[\leadsto -R \cdot \lambda_1 \]
            11. Applied rewrites14.5%

              \[\leadsto -R \cdot \lambda_1 \]

            if 4.9e5 < phi2

            1. Initial program 51.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 R \cdot \color{blue}{\phi_2} \]
            4. Step-by-step derivation
              1. Applied rewrites53.5%

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

            Alternative 14: 32.0% accurate, 46.5× speedup?

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

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

              Reproduce

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