Spherical law of cosines

Percentage Accurate: 73.6% → 94.4%
Time: 21.9s
Alternatives: 22
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (acos
   (+
    (* (sin phi1) (sin phi2))
    (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
  R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
}
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 = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * r
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2))))) * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return math.acos(((math.sin(phi1) * math.sin(phi2)) + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2))))) * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R
\end{array}

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

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

\[\begin{array}{l} \\ \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (acos
   (+
    (* (sin phi1) (sin phi2))
    (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
  R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
}
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 = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * r
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + ((Math.cos(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2))))) * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return math.acos(((math.sin(phi1) * math.sin(phi2)) + ((math.cos(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2))))) * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R
\end{array}

Alternative 1: 94.4% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right) \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (acos
   (fma
    (sin phi1)
    (sin phi2)
    (*
     (* (cos phi1) (cos phi2))
     (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))
  R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return acos(fma(sin(phi1), sin(phi2), ((cos(phi1) * cos(phi2)) * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))))) * R;
}
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(fma(sin(phi1), sin(phi2), Float64(Float64(cos(phi1) * cos(phi2)) * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1)))))) * R)
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right) \cdot R
\end{array}
Derivation
  1. Initial program 73.6%

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Applied rewrites94.4%

    \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
  3. Applied rewrites94.4%

    \[\leadsto \color{blue}{\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R} \]
  4. Applied rewrites94.4%

    \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)}\right)\right) \cdot R \]
  5. Add Preprocessing

Alternative 2: 94.4% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (acos
   (fma
    (sin phi1)
    (sin phi2)
    (*
     (* (cos phi1) (cos phi2))
     (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
  R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return acos(fma(sin(phi1), sin(phi2), ((cos(phi1) * cos(phi2)) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))))) * R;
}
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(fma(sin(phi1), sin(phi2), Float64(Float64(cos(phi1) * cos(phi2)) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2)))))) * R)
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R
\end{array}
Derivation
  1. Initial program 73.6%

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Applied rewrites94.4%

    \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
  3. Applied rewrites94.4%

    \[\leadsto \color{blue}{\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R} \]
  4. Add Preprocessing

Alternative 3: 94.4% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (acos
   (fma
    (cos phi1)
    (*
     (cos phi2)
     (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))
    (* (sin phi1) (sin phi2))))
  R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return acos(fma(cos(phi1), (cos(phi2) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))), (sin(phi1) * sin(phi2)))) * R;
}
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(fma(cos(phi1), Float64(cos(phi2) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2)))), Float64(sin(phi1) * sin(phi2)))) * R)
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\cos^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R
\end{array}
Derivation
  1. Initial program 73.6%

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Applied rewrites94.4%

    \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
  3. Taylor expanded in lambda1 around inf

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
  4. Applied rewrites94.4%

    \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \phi_1, \cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right), \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
  5. Add Preprocessing

Alternative 4: 84.1% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ \mathbf{if}\;\phi_2 \leq -0.47:\\ \;\;\;\;\left(\frac{\pi}{2} - \sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, t\_0 \cdot \cos \phi_2, \sin \phi_2 \cdot \sin \phi_1\right)\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 0.00078:\\ \;\;\;\;\cos^{-1} \left(\phi_2 \cdot \sin \phi_1 + t\_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_1 \cdot t\_0\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))) (t_1 (* (cos phi1) (cos phi2))))
   (if (<= phi2 -0.47)
     (*
      (-
       (/ PI 2.0)
       (asin (fma (cos phi1) (* t_0 (cos phi2)) (* (sin phi2) (sin phi1)))))
      R)
     (if (<= phi2 0.00078)
       (*
        (acos
         (+
          (* phi2 (sin phi1))
          (*
           t_1
           (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
        R)
       (* (acos (+ (* (sin phi1) (sin phi2)) (* t_1 t_0))) R)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = cos(phi1) * cos(phi2);
	double tmp;
	if (phi2 <= -0.47) {
		tmp = ((((double) M_PI) / 2.0) - asin(fma(cos(phi1), (t_0 * cos(phi2)), (sin(phi2) * sin(phi1))))) * R;
	} else if (phi2 <= 0.00078) {
		tmp = acos(((phi2 * sin(phi1)) + (t_1 * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))))) * R;
	} else {
		tmp = acos(((sin(phi1) * sin(phi2)) + (t_1 * t_0))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if (phi2 <= -0.47)
		tmp = Float64(Float64(Float64(pi / 2.0) - asin(fma(cos(phi1), Float64(t_0 * cos(phi2)), Float64(sin(phi2) * sin(phi1))))) * R);
	elseif (phi2 <= 0.00078)
		tmp = Float64(acos(Float64(Float64(phi2 * sin(phi1)) + Float64(t_1 * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2)))))) * R);
	else
		tmp = Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(t_1 * t_0))) * R);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -0.47], N[(N[(N[(Pi / 2.0), $MachinePrecision] - N[ArcSin[N[(N[Cos[phi1], $MachinePrecision] * N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 0.00078], N[(N[ArcCos[N[(N[(phi2 * N[Sin[phi1], $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
\mathbf{if}\;\phi_2 \leq -0.47:\\
\;\;\;\;\left(\frac{\pi}{2} - \sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, t\_0 \cdot \cos \phi_2, \sin \phi_2 \cdot \sin \phi_1\right)\right)\right) \cdot R\\

\mathbf{elif}\;\phi_2 \leq 0.00078:\\
\;\;\;\;\cos^{-1} \left(\phi_2 \cdot \sin \phi_1 + t\_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_1 \cdot t\_0\right) \cdot R\\


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

    1. Initial program 80.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites79.9%

      \[\leadsto \color{blue}{\left(\frac{\pi}{2} - \sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \sin \phi_2 \cdot \sin \phi_1\right)\right)\right)} \cdot R \]

    if -0.46999999999999997 < phi2 < 7.79999999999999986e-4

    1. Initial program 68.1%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites89.5%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \left(\color{blue}{\phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]
    4. Applied rewrites89.4%

      \[\leadsto \cos^{-1} \left(\color{blue}{\phi_2 \cdot \sin \phi_1} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R \]

    if 7.79999999999999986e-4 < phi2

    1. Initial program 78.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 5: 84.1% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \cos \phi_1 \cdot \cos \phi_2\\ \mathbf{if}\;\phi_2 \leq -0.47:\\ \;\;\;\;\left(\frac{\pi}{2} - \sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, t\_0 \cdot \cos \phi_2, \sin \phi_2 \cdot \sin \phi_1\right)\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 0.00078:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \phi_2, t\_1 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_1 \cdot t\_0\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))) (t_1 (* (cos phi1) (cos phi2))))
   (if (<= phi2 -0.47)
     (*
      (-
       (/ PI 2.0)
       (asin (fma (cos phi1) (* t_0 (cos phi2)) (* (sin phi2) (sin phi1)))))
      R)
     (if (<= phi2 0.00078)
       (*
        (acos
         (fma
          (sin phi1)
          phi2
          (*
           t_1
           (fma (sin lambda2) (sin lambda1) (* (cos lambda2) (cos lambda1))))))
        R)
       (* (acos (+ (* (sin phi1) (sin phi2)) (* t_1 t_0))) R)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = cos(phi1) * cos(phi2);
	double tmp;
	if (phi2 <= -0.47) {
		tmp = ((((double) M_PI) / 2.0) - asin(fma(cos(phi1), (t_0 * cos(phi2)), (sin(phi2) * sin(phi1))))) * R;
	} else if (phi2 <= 0.00078) {
		tmp = acos(fma(sin(phi1), phi2, (t_1 * fma(sin(lambda2), sin(lambda1), (cos(lambda2) * cos(lambda1)))))) * R;
	} else {
		tmp = acos(((sin(phi1) * sin(phi2)) + (t_1 * t_0))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(cos(phi1) * cos(phi2))
	tmp = 0.0
	if (phi2 <= -0.47)
		tmp = Float64(Float64(Float64(pi / 2.0) - asin(fma(cos(phi1), Float64(t_0 * cos(phi2)), Float64(sin(phi2) * sin(phi1))))) * R);
	elseif (phi2 <= 0.00078)
		tmp = Float64(acos(fma(sin(phi1), phi2, Float64(t_1 * fma(sin(lambda2), sin(lambda1), Float64(cos(lambda2) * cos(lambda1)))))) * R);
	else
		tmp = Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(t_1 * t_0))) * R);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -0.47], N[(N[(N[(Pi / 2.0), $MachinePrecision] - N[ArcSin[N[(N[Cos[phi1], $MachinePrecision] * N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 0.00078], N[(N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * phi2 + N[(t$95$1 * N[(N[Sin[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision] + N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(t$95$1 * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \cos \phi_1 \cdot \cos \phi_2\\
\mathbf{if}\;\phi_2 \leq -0.47:\\
\;\;\;\;\left(\frac{\pi}{2} - \sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, t\_0 \cdot \cos \phi_2, \sin \phi_2 \cdot \sin \phi_1\right)\right)\right) \cdot R\\

\mathbf{elif}\;\phi_2 \leq 0.00078:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \phi_2, t\_1 \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_1 \cdot t\_0\right) \cdot R\\


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

    1. Initial program 80.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites79.9%

      \[\leadsto \color{blue}{\left(\frac{\pi}{2} - \sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \sin \phi_2 \cdot \sin \phi_1\right)\right)\right)} \cdot R \]

    if -0.46999999999999997 < phi2 < 7.79999999999999986e-4

    1. Initial program 68.1%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites89.5%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Applied rewrites89.5%

      \[\leadsto \color{blue}{\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R} \]
    4. Applied rewrites89.5%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)}\right)\right) \cdot R \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \color{blue}{\phi_2}, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right) \cdot R \]
    6. Applied rewrites89.4%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \color{blue}{\phi_2}, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\sin \lambda_2, \sin \lambda_1, \cos \lambda_2 \cdot \cos \lambda_1\right)\right)\right) \cdot R \]

    if 7.79999999999999986e-4 < phi2

    1. Initial program 78.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 6: 84.0% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq -2.35 \cdot 10^{-6}:\\ \;\;\;\;\left(\frac{\pi}{2} - \sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, t\_0 \cdot \cos \phi_2, \sin \phi_2 \cdot \sin \phi_1\right)\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 0.0001:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\phi_2, \sin \phi_1, \cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot t\_0\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (if (<= phi2 -2.35e-6)
     (*
      (-
       (/ PI 2.0)
       (asin (fma (cos phi1) (* t_0 (cos phi2)) (* (sin phi2) (sin phi1)))))
      R)
     (if (<= phi2 0.0001)
       (*
        (acos
         (fma
          phi2
          (sin phi1)
          (*
           (cos phi1)
           (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
        R)
       (*
        (acos (+ (* (sin phi1) (sin phi2)) (* (* (cos phi1) (cos phi2)) t_0)))
        R)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -2.35e-6) {
		tmp = ((((double) M_PI) / 2.0) - asin(fma(cos(phi1), (t_0 * cos(phi2)), (sin(phi2) * sin(phi1))))) * R;
	} else if (phi2 <= 0.0001) {
		tmp = acos(fma(phi2, sin(phi1), (cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)))))) * R;
	} else {
		tmp = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * t_0))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi2 <= -2.35e-6)
		tmp = Float64(Float64(Float64(pi / 2.0) - asin(fma(cos(phi1), Float64(t_0 * cos(phi2)), Float64(sin(phi2) * sin(phi1))))) * R);
	elseif (phi2 <= 0.0001)
		tmp = Float64(acos(fma(phi2, sin(phi1), Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2)))))) * R);
	else
		tmp = Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * t_0))) * R);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, -2.35e-6], N[(N[(N[(Pi / 2.0), $MachinePrecision] - N[ArcSin[N[(N[Cos[phi1], $MachinePrecision] * N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 0.0001], N[(N[ArcCos[N[(phi2 * N[Sin[phi1], $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq -2.35 \cdot 10^{-6}:\\
\;\;\;\;\left(\frac{\pi}{2} - \sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, t\_0 \cdot \cos \phi_2, \sin \phi_2 \cdot \sin \phi_1\right)\right)\right) \cdot R\\

\mathbf{elif}\;\phi_2 \leq 0.0001:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\phi_2, \sin \phi_1, \cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot t\_0\right) \cdot R\\


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

    1. Initial program 79.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites79.8%

      \[\leadsto \color{blue}{\left(\frac{\pi}{2} - \sin^{-1} \left(\mathsf{fma}\left(\cos \phi_1, \cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2, \sin \phi_2 \cdot \sin \phi_1\right)\right)\right)} \cdot R \]

    if -2.34999999999999995e-6 < phi2 < 1.00000000000000005e-4

    1. Initial program 68.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites89.4%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Taylor expanded in phi2 around 0

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\phi_2, \sin \phi_1, \cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)} \cdot R \]

    if 1.00000000000000005e-4 < phi2

    1. Initial program 78.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 7: 83.5% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \mathbf{if}\;\phi_2 \leq -1.85 \cdot 10^{-31}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 5.5 \cdot 10^{-11}:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (*
          (acos
           (+
            (* (sin phi1) (sin phi2))
            (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
          R)))
   (if (<= phi2 -1.85e-31)
     t_0
     (if (<= phi2 5.5e-11)
       (*
        (acos
         (*
          (cos phi1)
          (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2)))))
        R)
       t_0))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
	double tmp;
	if (phi2 <= -1.85e-31) {
		tmp = t_0;
	} else if (phi2 <= 5.5e-11) {
		tmp = acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2))))) * R;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) * R)
	tmp = 0.0
	if (phi2 <= -1.85e-31)
		tmp = t_0;
	elseif (phi2 <= 5.5e-11)
		tmp = Float64(acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))) * R);
	else
		tmp = t_0;
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]}, If[LessEqual[phi2, -1.85e-31], t$95$0, If[LessEqual[phi2, 5.5e-11], N[(N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\
\mathbf{if}\;\phi_2 \leq -1.85 \cdot 10^{-31}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\phi_2 \leq 5.5 \cdot 10^{-11}:\\
\;\;\;\;\cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -1.8499999999999999e-31 or 5.49999999999999975e-11 < phi2

    1. Initial program 78.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]

    if -1.8499999999999999e-31 < phi2 < 5.49999999999999975e-11

    1. Initial program 68.3%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites89.6%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    4. Applied rewrites89.6%

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

Alternative 8: 73.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_2 \cdot \cos \phi_1\\ t_1 := \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq -2.95 \cdot 10^{-12}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_0 \cdot \cos \lambda_2\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 2.85 \cdot 10^{-8}:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_1 \cdot t\_1\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 2.3 \cdot 10^{+194}:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_2 \cdot t\_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \lambda_1, \sin \phi_2 \cdot \sin \phi_1\right)\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi2) (cos phi1)))
        (t_1
         (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2)))))
   (if (<= phi2 -2.95e-12)
     (* (acos (fma (sin phi2) (sin phi1) (* t_0 (cos lambda2)))) R)
     (if (<= phi2 2.85e-8)
       (* (acos (* (cos phi1) t_1)) R)
       (if (<= phi2 2.3e+194)
         (* (acos (* (cos phi2) t_1)) R)
         (* (acos (fma t_0 (cos lambda1) (* (sin phi2) (sin phi1)))) R))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi2) * cos(phi1);
	double t_1 = fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2)));
	double tmp;
	if (phi2 <= -2.95e-12) {
		tmp = acos(fma(sin(phi2), sin(phi1), (t_0 * cos(lambda2)))) * R;
	} else if (phi2 <= 2.85e-8) {
		tmp = acos((cos(phi1) * t_1)) * R;
	} else if (phi2 <= 2.3e+194) {
		tmp = acos((cos(phi2) * t_1)) * R;
	} else {
		tmp = acos(fma(t_0, cos(lambda1), (sin(phi2) * sin(phi1)))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi2) * cos(phi1))
	t_1 = fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2)))
	tmp = 0.0
	if (phi2 <= -2.95e-12)
		tmp = Float64(acos(fma(sin(phi2), sin(phi1), Float64(t_0 * cos(lambda2)))) * R);
	elseif (phi2 <= 2.85e-8)
		tmp = Float64(acos(Float64(cos(phi1) * t_1)) * R);
	elseif (phi2 <= 2.3e+194)
		tmp = Float64(acos(Float64(cos(phi2) * t_1)) * R);
	else
		tmp = Float64(acos(fma(t_0, cos(lambda1), Float64(sin(phi2) * sin(phi1)))) * R);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -2.95e-12], N[(N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(t$95$0 * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 2.85e-8], N[(N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * t$95$1), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 2.3e+194], N[(N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * t$95$1), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(t$95$0 * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \phi_2 \cdot \cos \phi_1\\
t_1 := \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq -2.95 \cdot 10^{-12}:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_0 \cdot \cos \lambda_2\right)\right) \cdot R\\

\mathbf{elif}\;\phi_2 \leq 2.85 \cdot 10^{-8}:\\
\;\;\;\;\cos^{-1} \left(\cos \phi_1 \cdot t\_1\right) \cdot R\\

\mathbf{elif}\;\phi_2 \leq 2.3 \cdot 10^{+194}:\\
\;\;\;\;\cos^{-1} \left(\cos \phi_2 \cdot t\_1\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \lambda_1, \sin \phi_2 \cdot \sin \phi_1\right)\right) \cdot R\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if phi2 < -2.95e-12

    1. Initial program 79.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    3. Applied rewrites60.1%

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

    if -2.95e-12 < phi2 < 2.85000000000000004e-8

    1. Initial program 67.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites89.4%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    4. Applied rewrites89.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]

    if 2.85000000000000004e-8 < phi2 < 2.30000000000000005e194

    1. Initial program 77.5%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites99.0%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    4. Applied rewrites57.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]

    if 2.30000000000000005e194 < phi2

    1. Initial program 78.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    3. Applied rewrites59.2%

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

Alternative 9: 73.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R\\ t_1 := \cos \phi_2 \cdot \cos \phi_1\\ \mathbf{if}\;\lambda_1 \leq -1.35 \cdot 10^{+147}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\lambda_1 \leq -3 \cdot 10^{-14}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(t\_1, \cos \lambda_1, \sin \phi_2 \cdot \sin \phi_1\right)\right) \cdot R\\ \mathbf{elif}\;\lambda_1 \leq 6.8 \cdot 10^{-30}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot \cos \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (*
          (acos
           (*
            (cos phi1)
            (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2)))))
          R))
        (t_1 (* (cos phi2) (cos phi1))))
   (if (<= lambda1 -1.35e+147)
     t_0
     (if (<= lambda1 -3e-14)
       (* (acos (fma t_1 (cos lambda1) (* (sin phi2) (sin phi1)))) R)
       (if (<= lambda1 6.8e-30)
         (* (acos (fma (sin phi2) (sin phi1) (* t_1 (cos lambda2)))) R)
         t_0)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = acos((cos(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2))))) * R;
	double t_1 = cos(phi2) * cos(phi1);
	double tmp;
	if (lambda1 <= -1.35e+147) {
		tmp = t_0;
	} else if (lambda1 <= -3e-14) {
		tmp = acos(fma(t_1, cos(lambda1), (sin(phi2) * sin(phi1)))) * R;
	} else if (lambda1 <= 6.8e-30) {
		tmp = acos(fma(sin(phi2), sin(phi1), (t_1 * cos(lambda2)))) * R;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(acos(Float64(cos(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))) * R)
	t_1 = Float64(cos(phi2) * cos(phi1))
	tmp = 0.0
	if (lambda1 <= -1.35e+147)
		tmp = t_0;
	elseif (lambda1 <= -3e-14)
		tmp = Float64(acos(fma(t_1, cos(lambda1), Float64(sin(phi2) * sin(phi1)))) * R);
	elseif (lambda1 <= 6.8e-30)
		tmp = Float64(acos(fma(sin(phi2), sin(phi1), Float64(t_1 * cos(lambda2)))) * R);
	else
		tmp = t_0;
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[ArcCos[N[(N[Cos[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[lambda1, -1.35e+147], t$95$0, If[LessEqual[lambda1, -3e-14], N[(N[ArcCos[N[(t$95$1 * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], If[LessEqual[lambda1, 6.8e-30], N[(N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(t$95$1 * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], t$95$0]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos^{-1} \left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right) \cdot R\\
t_1 := \cos \phi_2 \cdot \cos \phi_1\\
\mathbf{if}\;\lambda_1 \leq -1.35 \cdot 10^{+147}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\lambda_1 \leq -3 \cdot 10^{-14}:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(t\_1, \cos \lambda_1, \sin \phi_2 \cdot \sin \phi_1\right)\right) \cdot R\\

\mathbf{elif}\;\lambda_1 \leq 6.8 \cdot 10^{-30}:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_1 \cdot \cos \lambda_2\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;t\_0\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if lambda1 < -1.34999999999999999e147 or 6.8000000000000006e-30 < lambda1

    1. Initial program 59.7%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites98.7%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]
    4. Applied rewrites59.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)} \cdot R \]

    if -1.34999999999999999e147 < lambda1 < -2.9999999999999998e-14

    1. Initial program 60.3%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    3. Applied rewrites58.9%

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

    if -2.9999999999999998e-14 < lambda1 < 6.8000000000000006e-30

    1. Initial program 89.5%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    3. Applied rewrites89.5%

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

Alternative 10: 63.7% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_2 \cdot \cos \phi_1\\ \mathbf{if}\;\lambda_1 \leq -3.9 \cdot 10^{-5}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \lambda_1, \sin \phi_2 \cdot \sin \phi_1\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_0 \cdot \cos \lambda_2\right)\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi2) (cos phi1))))
   (if (<= lambda1 -3.9e-5)
     (* (acos (fma t_0 (cos lambda1) (* (sin phi2) (sin phi1)))) R)
     (* (acos (fma (sin phi2) (sin phi1) (* t_0 (cos lambda2)))) R))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi2) * cos(phi1);
	double tmp;
	if (lambda1 <= -3.9e-5) {
		tmp = acos(fma(t_0, cos(lambda1), (sin(phi2) * sin(phi1)))) * R;
	} else {
		tmp = acos(fma(sin(phi2), sin(phi1), (t_0 * cos(lambda2)))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi2) * cos(phi1))
	tmp = 0.0
	if (lambda1 <= -3.9e-5)
		tmp = Float64(acos(fma(t_0, cos(lambda1), Float64(sin(phi2) * sin(phi1)))) * R);
	else
		tmp = Float64(acos(fma(sin(phi2), sin(phi1), Float64(t_0 * cos(lambda2)))) * R);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[lambda1, -3.9e-5], N[(N[ArcCos[N[(t$95$0 * N[Cos[lambda1], $MachinePrecision] + N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(t$95$0 * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \phi_2 \cdot \cos \phi_1\\
\mathbf{if}\;\lambda_1 \leq -3.9 \cdot 10^{-5}:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \lambda_1, \sin \phi_2 \cdot \sin \phi_1\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, t\_0 \cdot \cos \lambda_2\right)\right) \cdot R\\


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

    1. Initial program 59.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    3. Applied rewrites59.0%

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

    if -3.8999999999999999e-5 < lambda1

    1. Initial program 78.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    3. Applied rewrites65.3%

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

Alternative 11: 58.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1100000:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \phi_2 \cdot \cos \phi_1\right) \cdot \cos \lambda_2\right)\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -1100000.0)
   (*
    (acos (fma (sin phi1) (sin phi2) (* (cos (- lambda1 lambda2)) (cos phi1))))
    R)
   (*
    (acos
     (fma (sin phi2) (sin phi1) (* (* (cos phi2) (cos phi1)) (cos lambda2))))
    R)))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1100000.0) {
		tmp = acos(fma(sin(phi1), sin(phi2), (cos((lambda1 - lambda2)) * cos(phi1)))) * R;
	} else {
		tmp = acos(fma(sin(phi2), sin(phi1), ((cos(phi2) * cos(phi1)) * cos(lambda2)))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -1100000.0)
		tmp = Float64(acos(fma(sin(phi1), sin(phi2), Float64(cos(Float64(lambda1 - lambda2)) * cos(phi1)))) * R);
	else
		tmp = Float64(acos(fma(sin(phi2), sin(phi1), Float64(Float64(cos(phi2) * cos(phi1)) * cos(lambda2)))) * R);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -1100000.0], N[(N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[(N[Cos[phi2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -1100000:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)\right) \cdot R\\

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


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

    1. Initial program 58.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites99.2%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Applied rewrites99.2%

      \[\leadsto \color{blue}{\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R} \]
    4. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \color{blue}{\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right)\right) \cdot R \]
    5. Applied rewrites38.7%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1}\right)\right) \cdot R \]

    if -1.1e6 < lambda1

    1. Initial program 78.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\mathsf{neg}\left(\lambda_2\right)\right)\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    3. Applied rewrites64.8%

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

Alternative 12: 56.0% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq -920:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 2.85 \cdot 10^{-8}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, t\_0 \cdot \cos \phi_1\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(t\_0 \cdot \cos \phi_2\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (if (<= phi2 -920.0)
     (* (acos (fma 1.0 (* (cos phi1) (cos phi2)) (* (sin phi1) (sin phi2)))) R)
     (if (<= phi2 2.85e-8)
       (* (acos (fma (sin phi1) (sin phi2) (* t_0 (cos phi1)))) R)
       (* (acos (* t_0 (cos phi2))) R)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -920.0) {
		tmp = acos(fma(1.0, (cos(phi1) * cos(phi2)), (sin(phi1) * sin(phi2)))) * R;
	} else if (phi2 <= 2.85e-8) {
		tmp = acos(fma(sin(phi1), sin(phi2), (t_0 * cos(phi1)))) * R;
	} else {
		tmp = acos((t_0 * cos(phi2))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi2 <= -920.0)
		tmp = Float64(acos(fma(1.0, Float64(cos(phi1) * cos(phi2)), Float64(sin(phi1) * sin(phi2)))) * R);
	elseif (phi2 <= 2.85e-8)
		tmp = Float64(acos(fma(sin(phi1), sin(phi2), Float64(t_0 * cos(phi1)))) * R);
	else
		tmp = Float64(acos(Float64(t_0 * cos(phi2))) * R);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, -920.0], N[(N[ArcCos[N[(1.0 * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 2.85e-8], N[(N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(t$95$0 * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq -920:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R\\

\mathbf{elif}\;\phi_2 \leq 2.85 \cdot 10^{-8}:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, t\_0 \cdot \cos \phi_1\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(t\_0 \cdot \cos \phi_2\right) \cdot R\\


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

    1. Initial program 80.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites99.2%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    4. Applied rewrites60.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(1, \color{blue}{\cos \phi_1} \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Applied rewrites41.2%

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

    if -920 < phi2 < 2.85000000000000004e-8

    1. Initial program 68.1%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites89.6%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Applied rewrites89.6%

      \[\leadsto \color{blue}{\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R} \]
    4. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \color{blue}{\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right)\right) \cdot R \]
    5. Applied rewrites67.4%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1}\right)\right) \cdot R \]

    if 2.85000000000000004e-8 < phi2

    1. Initial program 78.0%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites48.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 13: 56.0% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \phi_1 \cdot \sin \phi_2\\ t_1 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq -920:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(1, \cos \phi_1 \cdot \cos \phi_2, t\_0\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 2.85 \cdot 10^{-8}:\\ \;\;\;\;\cos^{-1} \left(t\_0 + \cos \phi_1 \cdot t\_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(t\_1 \cdot \cos \phi_2\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin phi1) (sin phi2))) (t_1 (cos (- lambda1 lambda2))))
   (if (<= phi2 -920.0)
     (* (acos (fma 1.0 (* (cos phi1) (cos phi2)) t_0)) R)
     (if (<= phi2 2.85e-8)
       (* (acos (+ t_0 (* (cos phi1) t_1))) R)
       (* (acos (* t_1 (cos phi2))) R)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin(phi1) * sin(phi2);
	double t_1 = cos((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -920.0) {
		tmp = acos(fma(1.0, (cos(phi1) * cos(phi2)), t_0)) * R;
	} else if (phi2 <= 2.85e-8) {
		tmp = acos((t_0 + (cos(phi1) * t_1))) * R;
	} else {
		tmp = acos((t_1 * cos(phi2))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(phi1) * sin(phi2))
	t_1 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi2 <= -920.0)
		tmp = Float64(acos(fma(1.0, Float64(cos(phi1) * cos(phi2)), t_0)) * R);
	elseif (phi2 <= 2.85e-8)
		tmp = Float64(acos(Float64(t_0 + Float64(cos(phi1) * t_1))) * R);
	else
		tmp = Float64(acos(Float64(t_1 * cos(phi2))) * R);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, -920.0], N[(N[ArcCos[N[(1.0 * N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 2.85e-8], N[(N[ArcCos[N[(t$95$0 + N[(N[Cos[phi1], $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(t$95$1 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \phi_1 \cdot \sin \phi_2\\
t_1 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq -920:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(1, \cos \phi_1 \cdot \cos \phi_2, t\_0\right)\right) \cdot R\\

\mathbf{elif}\;\phi_2 \leq 2.85 \cdot 10^{-8}:\\
\;\;\;\;\cos^{-1} \left(t\_0 + \cos \phi_1 \cdot t\_1\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(t\_1 \cdot \cos \phi_2\right) \cdot R\\


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

    1. Initial program 80.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites99.2%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    4. Applied rewrites60.8%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(1, \color{blue}{\cos \phi_1} \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right) \cdot R \]
    6. Applied rewrites41.2%

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

    if -920 < phi2 < 2.85000000000000004e-8

    1. Initial program 68.1%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \color{blue}{\cos \phi_1} \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    3. Applied rewrites67.4%

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

    if 2.85000000000000004e-8 < phi2

    1. Initial program 78.0%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites48.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right)} \cdot R \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 14: 50.7% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_1 \leq -0.000135:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, t\_0 \cdot \cos \phi_1\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, t\_0 \cdot \cos \phi_2\right)\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (if (<= phi1 -0.000135)
     (* (acos (fma (sin phi1) (sin phi2) (* t_0 (cos phi1)))) R)
     (* (acos (fma (sin phi1) (sin phi2) (* t_0 (cos phi2)))) R))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (phi1 <= -0.000135) {
		tmp = acos(fma(sin(phi1), sin(phi2), (t_0 * cos(phi1)))) * R;
	} else {
		tmp = acos(fma(sin(phi1), sin(phi2), (t_0 * cos(phi2)))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi1 <= -0.000135)
		tmp = Float64(acos(fma(sin(phi1), sin(phi2), Float64(t_0 * cos(phi1)))) * R);
	else
		tmp = Float64(acos(fma(sin(phi1), sin(phi2), Float64(t_0 * cos(phi2)))) * R);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi1, -0.000135], N[(N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(t$95$0 * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_1 \leq -0.000135:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, t\_0 \cdot \cos \phi_1\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, t\_0 \cdot \cos \phi_2\right)\right) \cdot R\\


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

    1. Initial program 78.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites99.1%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Applied rewrites99.1%

      \[\leadsto \color{blue}{\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R} \]
    4. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \color{blue}{\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right)\right) \cdot R \]
    5. Applied rewrites47.4%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1}\right)\right) \cdot R \]

    if -1.35000000000000002e-4 < phi1

    1. Initial program 72.1%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites92.8%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Applied rewrites92.8%

      \[\leadsto \color{blue}{\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R} \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \color{blue}{\cos \phi_2 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right)\right) \cdot R \]
    5. Applied rewrites51.4%

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

Alternative 15: 50.5% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_1 \leq -8.5 \cdot 10^{-6}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, t\_0 \cdot \cos \phi_1\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(t\_0 \cdot \cos \phi_2\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (if (<= phi1 -8.5e-6)
     (* (acos (fma (sin phi1) (sin phi2) (* t_0 (cos phi1)))) R)
     (* (acos (* t_0 (cos phi2))) R))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (phi1 <= -8.5e-6) {
		tmp = acos(fma(sin(phi1), sin(phi2), (t_0 * cos(phi1)))) * R;
	} else {
		tmp = acos((t_0 * cos(phi2))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi1 <= -8.5e-6)
		tmp = Float64(acos(fma(sin(phi1), sin(phi2), Float64(t_0 * cos(phi1)))) * R);
	else
		tmp = Float64(acos(Float64(t_0 * cos(phi2))) * R);
	end
	return tmp
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi1, -8.5e-6], N[(N[ArcCos[N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision] + N[(t$95$0 * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_1 \leq -8.5 \cdot 10^{-6}:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, t\_0 \cdot \cos \phi_1\right)\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(t\_0 \cdot \cos \phi_2\right) \cdot R\\


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

    1. Initial program 78.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites99.1%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Applied rewrites99.1%

      \[\leadsto \color{blue}{\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R} \]
    4. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \color{blue}{\cos \phi_1 \cdot \left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right)\right) \cdot R \]
    5. Applied rewrites47.4%

      \[\leadsto \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \color{blue}{\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1}\right)\right) \cdot R \]

    if -8.4999999999999999e-6 < phi1

    1. Initial program 72.1%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites51.5%

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

Alternative 16: 50.3% accurate, 2.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_1 \leq -8.5 \cdot 10^{-6}:\\ \;\;\;\;\cos^{-1} \left(t\_0 \cdot \cos \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(t\_0 \cdot \cos \phi_2\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (if (<= phi1 -8.5e-6)
     (* (acos (* t_0 (cos phi1))) R)
     (* (acos (* t_0 (cos phi2))) R))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (phi1 <= -8.5e-6) {
		tmp = acos((t_0 * cos(phi1))) * R;
	} else {
		tmp = acos((t_0 * cos(phi2))) * R;
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos((lambda1 - lambda2))
    if (phi1 <= (-8.5d-6)) then
        tmp = acos((t_0 * cos(phi1))) * r
    else
        tmp = acos((t_0 * cos(phi2))) * r
    end if
    code = tmp
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2));
	double tmp;
	if (phi1 <= -8.5e-6) {
		tmp = Math.acos((t_0 * Math.cos(phi1))) * R;
	} else {
		tmp = Math.acos((t_0 * Math.cos(phi2))) * R;
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	tmp = 0
	if phi1 <= -8.5e-6:
		tmp = math.acos((t_0 * math.cos(phi1))) * R
	else:
		tmp = math.acos((t_0 * math.cos(phi2))) * R
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi1 <= -8.5e-6)
		tmp = Float64(acos(Float64(t_0 * cos(phi1))) * R);
	else
		tmp = Float64(acos(Float64(t_0 * cos(phi2))) * R);
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda1 - lambda2));
	tmp = 0.0;
	if (phi1 <= -8.5e-6)
		tmp = acos((t_0 * cos(phi1))) * R;
	else
		tmp = acos((t_0 * cos(phi2))) * R;
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi1, -8.5e-6], N[(N[ArcCos[N[(t$95$0 * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_1 \leq -8.5 \cdot 10^{-6}:\\
\;\;\;\;\cos^{-1} \left(t\_0 \cdot \cos \phi_1\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(t\_0 \cdot \cos \phi_2\right) \cdot R\\


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

    1. Initial program 78.0%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites48.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]

    if -8.4999999999999999e-6 < phi1

    1. Initial program 72.1%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites51.5%

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

Alternative 17: 47.5% accurate, 2.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 0.00138:\\ \;\;\;\;\cos^{-1} \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 0.00138)
   (* (acos (* (cos (- lambda1 lambda2)) (cos phi1))) R)
   (* (acos (* (cos phi2) (cos lambda1))) R)))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 0.00138) {
		tmp = acos((cos((lambda1 - lambda2)) * cos(phi1))) * R;
	} else {
		tmp = acos((cos(phi2) * cos(lambda1))) * R;
	}
	return tmp;
}
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 <= 0.00138d0) then
        tmp = acos((cos((lambda1 - lambda2)) * cos(phi1))) * r
    else
        tmp = acos((cos(phi2) * cos(lambda1))) * r
    end if
    code = tmp
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 0.00138) {
		tmp = Math.acos((Math.cos((lambda1 - lambda2)) * Math.cos(phi1))) * R;
	} else {
		tmp = Math.acos((Math.cos(phi2) * Math.cos(lambda1))) * R;
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 0.00138:
		tmp = math.acos((math.cos((lambda1 - lambda2)) * math.cos(phi1))) * R
	else:
		tmp = math.acos((math.cos(phi2) * math.cos(lambda1))) * R
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 0.00138)
		tmp = Float64(acos(Float64(cos(Float64(lambda1 - lambda2)) * cos(phi1))) * R);
	else
		tmp = Float64(acos(Float64(cos(phi2) * cos(lambda1))) * R);
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 0.00138)
		tmp = acos((cos((lambda1 - lambda2)) * cos(phi1))) * R;
	else
		tmp = acos((cos(phi2) * cos(lambda1))) * R;
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 0.00138], N[(N[ArcCos[N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(N[Cos[phi2], $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 0.00138:\\
\;\;\;\;\cos^{-1} \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_1\right) \cdot R\\


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

    1. Initial program 72.2%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites50.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]

    if 0.00137999999999999993 < phi2

    1. Initial program 77.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites99.1%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    4. Applied rewrites58.9%

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

      \[\leadsto \cos^{-1} \left(\cos \lambda_1 \cdot \color{blue}{\cos \phi_2}\right) \cdot R \]
    6. Applied rewrites38.2%

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

Alternative 18: 38.8% accurate, 2.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq 1.95 \cdot 10^{-5}:\\ \;\;\;\;\cos^{-1} \left(\cos \lambda_1 \cdot \cos \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\cos \lambda_2 \cdot \cos \phi_1\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda2 1.95e-5)
   (* (acos (* (cos lambda1) (cos phi1))) R)
   (* (acos (* (cos lambda2) (cos phi1))) R)))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 1.95e-5) {
		tmp = acos((cos(lambda1) * cos(phi1))) * R;
	} else {
		tmp = acos((cos(lambda2) * cos(phi1))) * R;
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (lambda2 <= 1.95d-5) then
        tmp = acos((cos(lambda1) * cos(phi1))) * r
    else
        tmp = acos((cos(lambda2) * cos(phi1))) * r
    end if
    code = tmp
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= 1.95e-5) {
		tmp = Math.acos((Math.cos(lambda1) * Math.cos(phi1))) * R;
	} else {
		tmp = Math.acos((Math.cos(lambda2) * Math.cos(phi1))) * R;
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= 1.95e-5:
		tmp = math.acos((math.cos(lambda1) * math.cos(phi1))) * R
	else:
		tmp = math.acos((math.cos(lambda2) * math.cos(phi1))) * R
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= 1.95e-5)
		tmp = Float64(acos(Float64(cos(lambda1) * cos(phi1))) * R);
	else
		tmp = Float64(acos(Float64(cos(lambda2) * cos(phi1))) * R);
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= 1.95e-5)
		tmp = acos((cos(lambda1) * cos(phi1))) * R;
	else
		tmp = acos((cos(lambda2) * cos(phi1))) * R;
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, 1.95e-5], N[(N[ArcCos[N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq 1.95 \cdot 10^{-5}:\\
\;\;\;\;\cos^{-1} \left(\cos \lambda_1 \cdot \cos \phi_1\right) \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \left(\cos \lambda_2 \cdot \cos \phi_1\right) \cdot R\\


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

    1. Initial program 78.6%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites44.1%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    4. Taylor expanded in lambda1 around inf

      \[\leadsto \cos^{-1} \left(\cos \lambda_1 \cdot \cos \phi_1\right) \cdot R \]
    5. Applied rewrites35.7%

      \[\leadsto \cos^{-1} \left(\cos \lambda_1 \cdot \cos \phi_1\right) \cdot R \]

    if 1.95e-5 < lambda2

    1. Initial program 58.7%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites38.4%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
    5. Applied rewrites29.4%

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
    6. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \left(\cos \phi_1 \cdot \color{blue}{\cos \left(\mathsf{neg}\left(\lambda_2\right)\right)}\right) \cdot R \]
    7. Applied rewrites38.3%

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

Alternative 19: 36.4% accurate, 2.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;t\_0 \leq 0.9999:\\ \;\;\;\;\cos^{-1} t\_0 \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \cos \left(\phi_1 - \phi_2\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (if (<= t_0 0.9999) (* (acos t_0) R) (* (acos (cos (- phi1 phi2))) R))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.9999) {
		tmp = acos(t_0) * R;
	} else {
		tmp = acos(cos((phi1 - phi2))) * R;
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos((lambda1 - lambda2))
    if (t_0 <= 0.9999d0) then
        tmp = acos(t_0) * r
    else
        tmp = acos(cos((phi1 - phi2))) * r
    end if
    code = tmp
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2));
	double tmp;
	if (t_0 <= 0.9999) {
		tmp = Math.acos(t_0) * R;
	} else {
		tmp = Math.acos(Math.cos((phi1 - phi2))) * R;
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	tmp = 0
	if t_0 <= 0.9999:
		tmp = math.acos(t_0) * R
	else:
		tmp = math.acos(math.cos((phi1 - phi2))) * R
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (t_0 <= 0.9999)
		tmp = Float64(acos(t_0) * R);
	else
		tmp = Float64(acos(cos(Float64(phi1 - phi2))) * R);
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda1 - lambda2));
	tmp = 0.0;
	if (t_0 <= 0.9999)
		tmp = acos(t_0) * R;
	else
		tmp = acos(cos((phi1 - phi2))) * R;
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[t$95$0, 0.9999], N[(N[ArcCos[t$95$0], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[Cos[N[(phi1 - phi2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;t\_0 \leq 0.9999:\\
\;\;\;\;\cos^{-1} t\_0 \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \cos \left(\phi_1 - \phi_2\right) \cdot R\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (cos.f64 (-.f64 lambda1 lambda2)) < 0.99990000000000001

    1. Initial program 71.7%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites45.6%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
    5. Applied rewrites33.1%

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]

    if 0.99990000000000001 < (cos.f64 (-.f64 lambda1 lambda2))

    1. Initial program 79.4%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Applied rewrites79.8%

      \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. Taylor expanded in lambda2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \lambda_1 \cdot \left(\cos \phi_1 \cdot \cos \phi_2\right) + \sin \phi_1 \cdot \sin \phi_2\right)} \cdot R \]
    4. Applied rewrites78.3%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_1 \cdot \cos \phi_2, \sin \phi_1 \cdot \sin \phi_2\right)\right)} \cdot R \]
    5. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \left(\cos \phi_1 \cdot \cos \phi_2 + \color{blue}{\sin \phi_1 \cdot \sin \phi_2}\right) \cdot R \]
    6. Applied rewrites55.9%

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

Alternative 20: 26.5% accurate, 4.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -300000:\\ \;\;\;\;\cos^{-1} \cos \lambda_1 \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \cos \left(-\lambda_2\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda1 -300000.0)
   (* (acos (cos lambda1)) R)
   (* (acos (cos (- lambda2))) R)))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -300000.0) {
		tmp = acos(cos(lambda1)) * R;
	} else {
		tmp = acos(cos(-lambda2)) * R;
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (lambda1 <= (-300000.0d0)) then
        tmp = acos(cos(lambda1)) * r
    else
        tmp = acos(cos(-lambda2)) * r
    end if
    code = tmp
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -300000.0) {
		tmp = Math.acos(Math.cos(lambda1)) * R;
	} else {
		tmp = Math.acos(Math.cos(-lambda2)) * R;
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -300000.0:
		tmp = math.acos(math.cos(lambda1)) * R
	else:
		tmp = math.acos(math.cos(-lambda2)) * R
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -300000.0)
		tmp = Float64(acos(cos(lambda1)) * R);
	else
		tmp = Float64(acos(cos(Float64(-lambda2))) * R);
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda1 <= -300000.0)
		tmp = acos(cos(lambda1)) * R;
	else
		tmp = acos(cos(-lambda2)) * R;
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda1, -300000.0], N[(N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[Cos[(-lambda2)], $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -300000:\\
\;\;\;\;\cos^{-1} \cos \lambda_1 \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \cos \left(-\lambda_2\right) \cdot R\\


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

    1. Initial program 58.9%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites38.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
    5. Applied rewrites29.0%

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
    6. Taylor expanded in lambda1 around inf

      \[\leadsto \cos^{-1} \cos \lambda_1 \cdot R \]
    7. Applied rewrites29.0%

      \[\leadsto \cos^{-1} \cos \lambda_1 \cdot R \]

    if -3e5 < lambda1

    1. Initial program 78.5%

      \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    2. Taylor expanded in phi2 around 0

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites43.9%

      \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
    4. Taylor expanded in phi1 around 0

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
    5. Applied rewrites25.7%

      \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
    6. Taylor expanded in lambda1 around 0

      \[\leadsto \cos^{-1} \cos \left(-1 \cdot \lambda_2\right) \cdot R \]
    7. Applied rewrites19.5%

      \[\leadsto \cos^{-1} \cos \left(-\lambda_2\right) \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 21: 21.9% accurate, 4.3× speedup?

\[\begin{array}{l} \\ \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* (acos (cos (- lambda1 lambda2))) R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return acos(cos((lambda1 - lambda2))) * R;
}
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 = acos(cos((lambda1 - lambda2))) * r
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return Math.acos(Math.cos((lambda1 - lambda2))) * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return math.acos(math.cos((lambda1 - lambda2))) * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(cos(Float64(lambda1 - lambda2))) * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = acos(cos((lambda1 - lambda2))) * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R
\end{array}
Derivation
  1. Initial program 73.6%

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Taylor expanded in phi2 around 0

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
  3. Applied rewrites42.6%

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
  4. Taylor expanded in phi1 around 0

    \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
  5. Applied rewrites26.5%

    \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
  6. Add Preprocessing

Alternative 22: 17.3% accurate, 4.5× speedup?

\[\begin{array}{l} \\ \cos^{-1} \cos \lambda_1 \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* (acos (cos lambda1)) R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return acos(cos(lambda1)) * R;
}
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 = acos(cos(lambda1)) * r
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return Math.acos(Math.cos(lambda1)) * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return math.acos(math.cos(lambda1)) * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(cos(lambda1)) * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = acos(cos(lambda1)) * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[ArcCos[N[Cos[lambda1], $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]
\begin{array}{l}

\\
\cos^{-1} \cos \lambda_1 \cdot R
\end{array}
Derivation
  1. Initial program 73.6%

    \[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  2. Taylor expanded in phi2 around 0

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
  3. Applied rewrites42.6%

    \[\leadsto \cos^{-1} \color{blue}{\left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right)} \cdot R \]
  4. Taylor expanded in phi1 around 0

    \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
  5. Applied rewrites26.5%

    \[\leadsto \cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R \]
  6. Taylor expanded in lambda1 around inf

    \[\leadsto \cos^{-1} \cos \lambda_1 \cdot R \]
  7. Applied rewrites17.3%

    \[\leadsto \cos^{-1} \cos \lambda_1 \cdot R \]
  8. Add Preprocessing

Reproduce

?
herbie shell --seed 2025121 
(FPCore (R lambda1 lambda2 phi1 phi2)
  :name "Spherical law of cosines"
  :precision binary64
  (* (acos (+ (* (sin phi1) (sin phi2)) (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2))))) R))