Spherical law of cosines

Percentage Accurate: 73.1% → 93.8%
Time: 12.5s
Alternatives: 23
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 23 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.1% 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: 93.8% accurate, 0.5× speedup?

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

\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \cos \phi_2\\
\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_1, \sin \phi_2, \mathsf{fma}\left(t\_0, \cos \lambda_1 \cdot \cos \lambda_2, t\_0 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right)\right) \cdot R
\end{array}
\end{array}
Derivation
  1. Initial program 73.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 rewrites93.8%

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

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

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

Alternative 2: 93.8% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \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))
     (fma (sin lambda1) (sin lambda2) (* (cos lambda1) (cos lambda2))))))
  R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return acos(((sin(phi1) * sin(phi2)) + ((cos(phi1) * cos(phi2)) * fma(sin(lambda1), sin(lambda2), (cos(lambda1) * cos(lambda2)))))) * R;
}
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(Float64(cos(phi1) * cos(phi2)) * fma(sin(lambda1), sin(lambda2), Float64(cos(lambda1) * cos(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[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $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 \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)\right) \cdot R
\end{array}
Derivation
  1. Initial program 73.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 rewrites93.8%

    \[\leadsto \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \color{blue}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
  3. Applied rewrites93.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(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
  4. Add Preprocessing

Alternative 3: 93.8% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \cos^{-1} \left(\sin \phi_1 \cdot \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) \cdot R \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  (acos
   (+
    (* (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(((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(Float64(Float64(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[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $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(\sin \phi_1 \cdot \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) \cdot R
\end{array}
Derivation
  1. Initial program 73.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 rewrites93.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. Add Preprocessing

Alternative 4: 83.5% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right) \cdot \cos \phi_1\right)\right) \cdot R\\ \mathbf{if}\;\phi_2 \leq -2.2 \cdot 10^{-6}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 1.9 \cdot 10^{-7}:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \mathsf{fma}\left(\sin \lambda_1, \sin \lambda_2, \cos \lambda_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
           (fma
            (sin phi2)
            (sin phi1)
            (* (* (cos (- lambda1 lambda2)) (cos phi2)) (cos phi1))))
          R)))
   (if (<= phi2 -2.2e-6)
     t_0
     (if (<= phi2 1.9e-7)
       (*
        (acos
         (+
          (* (sin phi1) (sin phi2))
          (*
           (cos phi1)
           (fma (sin lambda1) (sin lambda2) (* (cos lambda1) (cos lambda2))))))
        R)
       t_0))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = acos(fma(sin(phi2), sin(phi1), ((cos((lambda1 - lambda2)) * cos(phi2)) * cos(phi1)))) * R;
	double tmp;
	if (phi2 <= -2.2e-6) {
		tmp = t_0;
	} else if (phi2 <= 1.9e-7) {
		tmp = acos(((sin(phi1) * sin(phi2)) + (cos(phi1) * fma(sin(lambda1), sin(lambda2), (cos(lambda1) * cos(lambda2)))))) * R;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(acos(fma(sin(phi2), sin(phi1), Float64(Float64(cos(Float64(lambda1 - lambda2)) * cos(phi2)) * cos(phi1)))) * R)
	tmp = 0.0
	if (phi2 <= -2.2e-6)
		tmp = t_0;
	elseif (phi2 <= 1.9e-7)
		tmp = Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(cos(phi1) * fma(sin(lambda1), sin(lambda2), Float64(cos(lambda1) * 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[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]}, If[LessEqual[phi2, -2.2e-6], t$95$0, If[LessEqual[phi2, 1.9e-7], N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], t$95$0]]]
\begin{array}{l}

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -2.2000000000000001e-6 or 1.90000000000000007e-7 < 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. Applied rewrites79.0%

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

    if -2.2000000000000001e-6 < phi2 < 1.90000000000000007e-7

    1. Initial program 67.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 rewrites88.3%

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

      \[\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(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
    4. Taylor expanded in phi2 around 0

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

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

Alternative 5: 83.5% accurate, 0.8× speedup?

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

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -2.2000000000000001e-6 or 1.90000000000000007e-7 < 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. Applied rewrites79.0%

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

    if -2.2000000000000001e-6 < phi2 < 1.90000000000000007e-7

    1. Initial program 67.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(\phi_2 \cdot \sin \phi_1 + \cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites67.0%

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

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

Alternative 6: 83.3% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right) \cdot \cos \phi_1\right)\right) \cdot R\\ \mathbf{if}\;\phi_2 \leq -2.9 \cdot 10^{-16}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 7 \cdot 10^{-9}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2 \cdot \cos \phi_1, \cos \phi_1 \cdot \left(\sin \lambda_1 \cdot \sin \lambda_2\right)\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (*
          (acos
           (fma
            (sin phi2)
            (sin phi1)
            (* (* (cos (- lambda1 lambda2)) (cos phi2)) (cos phi1))))
          R)))
   (if (<= phi2 -2.9e-16)
     t_0
     (if (<= phi2 7e-9)
       (*
        (acos
         (fma
          (cos lambda1)
          (* (cos lambda2) (cos phi1))
          (* (cos phi1) (* (sin lambda1) (sin lambda2)))))
        R)
       t_0))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = acos(fma(sin(phi2), sin(phi1), ((cos((lambda1 - lambda2)) * cos(phi2)) * cos(phi1)))) * R;
	double tmp;
	if (phi2 <= -2.9e-16) {
		tmp = t_0;
	} else if (phi2 <= 7e-9) {
		tmp = acos(fma(cos(lambda1), (cos(lambda2) * cos(phi1)), (cos(phi1) * (sin(lambda1) * sin(lambda2))))) * R;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(acos(fma(sin(phi2), sin(phi1), Float64(Float64(cos(Float64(lambda1 - lambda2)) * cos(phi2)) * cos(phi1)))) * R)
	tmp = 0.0
	if (phi2 <= -2.9e-16)
		tmp = t_0;
	elseif (phi2 <= 7e-9)
		tmp = Float64(acos(fma(cos(lambda1), Float64(cos(lambda2) * cos(phi1)), Float64(cos(phi1) * 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[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]}, If[LessEqual[phi2, -2.9e-16], t$95$0, If[LessEqual[phi2, 7e-9], N[(N[ArcCos[N[(N[Cos[lambda1], $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi1], $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(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right) \cdot \cos \phi_1\right)\right) \cdot R\\
\mathbf{if}\;\phi_2 \leq -2.9 \cdot 10^{-16}:\\
\;\;\;\;t\_0\\

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -2.8999999999999998e-16 or 6.9999999999999998e-9 < phi2

    1. Initial program 78.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 rewrites78.7%

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

    if -2.8999999999999998e-16 < phi2 < 6.9999999999999998e-9

    1. Initial program 67.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 rewrites88.4%

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

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

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

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

Alternative 7: 72.6% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_2 \cdot \cos \phi_2, \cos \phi_1, \sin \phi_2 \cdot \sin \phi_1\right)\right) \cdot R\\ \mathbf{if}\;\lambda_2 \leq -2.4 \cdot 10^{-6}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\lambda_2 \leq 300000:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \lambda_1 \cdot \left(\cos \phi_2 \cdot \cos \phi_1\right)\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (*
          (acos
           (fma
            (* (cos lambda2) (cos phi2))
            (cos phi1)
            (* (sin phi2) (sin phi1))))
          R)))
   (if (<= lambda2 -2.4e-6)
     t_0
     (if (<= lambda2 300000.0)
       (*
        (acos
         (fma
          (sin phi2)
          (sin phi1)
          (* (cos lambda1) (* (cos phi2) (cos phi1)))))
        R)
       t_0))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = acos(fma((cos(lambda2) * cos(phi2)), cos(phi1), (sin(phi2) * sin(phi1)))) * R;
	double tmp;
	if (lambda2 <= -2.4e-6) {
		tmp = t_0;
	} else if (lambda2 <= 300000.0) {
		tmp = acos(fma(sin(phi2), sin(phi1), (cos(lambda1) * (cos(phi2) * cos(phi1))))) * R;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(acos(fma(Float64(cos(lambda2) * cos(phi2)), cos(phi1), Float64(sin(phi2) * sin(phi1)))) * R)
	tmp = 0.0
	if (lambda2 <= -2.4e-6)
		tmp = t_0;
	elseif (lambda2 <= 300000.0)
		tmp = Float64(acos(fma(sin(phi2), sin(phi1), Float64(cos(lambda1) * Float64(cos(phi2) * cos(phi1))))) * 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[Cos[lambda2], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]}, If[LessEqual[lambda2, -2.4e-6], t$95$0, If[LessEqual[lambda2, 300000.0], N[(N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], t$95$0]]]
\begin{array}{l}

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda2 < -2.3999999999999999e-6 or 3e5 < lambda2

    1. Initial program 59.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 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 rewrites59.2%

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

    if -2.3999999999999999e-6 < lambda2 < 3e5

    1. Initial program 87.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 rewrites86.3%

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

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

Alternative 8: 63.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_2 \cdot \cos \phi_1\\ \mathbf{if}\;\phi_2 \leq -0.007:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, t\_0, \sin \phi_2 \cdot \sin \phi_1\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 0.038:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \cos \lambda_1 \cdot t\_0\right)\right) \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi2) (cos phi1))))
   (if (<= phi2 -0.007)
     (* (acos (fma (cos lambda1) t_0 (* (sin phi2) (sin phi1)))) R)
     (if (<= phi2 0.038)
       (*
        (acos
         (+
          (* (sin phi1) phi2)
          (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
        R)
       (* (acos (fma (sin phi2) (sin phi1) (* (cos lambda1) t_0))) R)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi2) * cos(phi1);
	double tmp;
	if (phi2 <= -0.007) {
		tmp = acos(fma(cos(lambda1), t_0, (sin(phi2) * sin(phi1)))) * R;
	} else if (phi2 <= 0.038) {
		tmp = acos(((sin(phi1) * phi2) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
	} else {
		tmp = acos(fma(sin(phi2), sin(phi1), (cos(lambda1) * t_0))) * R;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi2) * cos(phi1))
	tmp = 0.0
	if (phi2 <= -0.007)
		tmp = Float64(acos(fma(cos(lambda1), t_0, Float64(sin(phi2) * sin(phi1)))) * R);
	elseif (phi2 <= 0.038)
		tmp = Float64(acos(Float64(Float64(sin(phi1) * phi2) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2))))) * R);
	else
		tmp = Float64(acos(fma(sin(phi2), sin(phi1), Float64(cos(lambda1) * t_0))) * 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[phi2, -0.007], N[(N[ArcCos[N[(N[Cos[lambda1], $MachinePrecision] * t$95$0 + N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 0.038], N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * phi2), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision] + N[(N[Cos[lambda1], $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]]]]
\begin{array}{l}

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

\mathbf{elif}\;\phi_2 \leq 0.038:\\
\;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\

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


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

    1. Initial program 79.8%

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

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

    if -0.00700000000000000015 < phi2 < 0.0379999999999999991

    1. Initial program 67.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 \color{blue}{\phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    3. Applied rewrites67.1%

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

    if 0.0379999999999999991 < phi2

    1. Initial program 78.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 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.7%

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

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

Alternative 9: 63.2% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos^{-1} \left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_2 \cdot \cos \phi_1, \sin \phi_2 \cdot \sin \phi_1\right)\right) \cdot R\\ \mathbf{if}\;\phi_2 \leq -0.007:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_2 \leq 0.038:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \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
           (fma
            (cos lambda1)
            (* (cos phi2) (cos phi1))
            (* (sin phi2) (sin phi1))))
          R)))
   (if (<= phi2 -0.007)
     t_0
     (if (<= phi2 0.038)
       (*
        (acos
         (+
          (* (sin phi1) phi2)
          (* (* (cos phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
        R)
       t_0))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = acos(fma(cos(lambda1), (cos(phi2) * cos(phi1)), (sin(phi2) * sin(phi1)))) * R;
	double tmp;
	if (phi2 <= -0.007) {
		tmp = t_0;
	} else if (phi2 <= 0.038) {
		tmp = acos(((sin(phi1) * phi2) + ((cos(phi1) * cos(phi2)) * cos((lambda1 - lambda2))))) * R;
	} else {
		tmp = t_0;
	}
	return tmp;
}
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(acos(fma(cos(lambda1), Float64(cos(phi2) * cos(phi1)), Float64(sin(phi2) * sin(phi1)))) * R)
	tmp = 0.0
	if (phi2 <= -0.007)
		tmp = t_0;
	elseif (phi2 <= 0.038)
		tmp = Float64(acos(Float64(Float64(sin(phi1) * phi2) + Float64(Float64(cos(phi1) * cos(phi2)) * cos(Float64(lambda1 - 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[lambda1], $MachinePrecision] * N[(N[Cos[phi2], $MachinePrecision] * N[Cos[phi1], $MachinePrecision]), $MachinePrecision] + N[(N[Sin[phi2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision]}, If[LessEqual[phi2, -0.007], t$95$0, If[LessEqual[phi2, 0.038], N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * phi2), $MachinePrecision] + N[(N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], t$95$0]]]
\begin{array}{l}

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

\mathbf{elif}\;\phi_2 \leq 0.038:\\
\;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -0.00700000000000000015 or 0.0379999999999999991 < phi2

    1. Initial program 79.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 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.3%

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

    if -0.00700000000000000015 < phi2 < 0.0379999999999999991

    1. Initial program 67.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 \color{blue}{\phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    3. Applied rewrites67.1%

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

Alternative 10: 73.1% accurate, 1.0× speedup?

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

\\
\cos^{-1} \left(\mathsf{fma}\left(\sin \phi_2, \sin \phi_1, \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right) \cdot \cos \phi_1\right)\right) \cdot R
\end{array}
Derivation
  1. Initial program 73.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 rewrites73.1%

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

Alternative 11: 55.9% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_1 \cdot \cos \phi_2\\ t_1 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq -0.007:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_0\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 1.05:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \phi_2 + t\_0 \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 (* (cos phi1) (cos phi2))) (t_1 (cos (- lambda1 lambda2))))
   (if (<= phi2 -0.007)
     (* (acos (+ (* (sin phi1) (sin phi2)) t_0)) R)
     (if (<= phi2 1.05)
       (* (acos (+ (* (sin phi1) phi2) (* t_0 t_1))) R)
       (* (acos (* t_1 (cos phi2))) R)))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi1) * cos(phi2);
	double t_1 = cos((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -0.007) {
		tmp = acos(((sin(phi1) * sin(phi2)) + t_0)) * R;
	} else if (phi2 <= 1.05) {
		tmp = acos(((sin(phi1) * phi2) + (t_0 * t_1))) * R;
	} else {
		tmp = acos((t_1 * 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) :: t_1
    real(8) :: tmp
    t_0 = cos(phi1) * cos(phi2)
    t_1 = cos((lambda1 - lambda2))
    if (phi2 <= (-0.007d0)) then
        tmp = acos(((sin(phi1) * sin(phi2)) + t_0)) * r
    else if (phi2 <= 1.05d0) then
        tmp = acos(((sin(phi1) * phi2) + (t_0 * t_1))) * r
    else
        tmp = acos((t_1 * 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(phi1) * Math.cos(phi2);
	double t_1 = Math.cos((lambda1 - lambda2));
	double tmp;
	if (phi2 <= -0.007) {
		tmp = Math.acos(((Math.sin(phi1) * Math.sin(phi2)) + t_0)) * R;
	} else if (phi2 <= 1.05) {
		tmp = Math.acos(((Math.sin(phi1) * phi2) + (t_0 * t_1))) * R;
	} else {
		tmp = Math.acos((t_1 * Math.cos(phi2))) * R;
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos(phi1) * math.cos(phi2)
	t_1 = math.cos((lambda1 - lambda2))
	tmp = 0
	if phi2 <= -0.007:
		tmp = math.acos(((math.sin(phi1) * math.sin(phi2)) + t_0)) * R
	elif phi2 <= 1.05:
		tmp = math.acos(((math.sin(phi1) * phi2) + (t_0 * t_1))) * R
	else:
		tmp = math.acos((t_1 * math.cos(phi2))) * R
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi1) * cos(phi2))
	t_1 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi2 <= -0.007)
		tmp = Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + t_0)) * R);
	elseif (phi2 <= 1.05)
		tmp = Float64(acos(Float64(Float64(sin(phi1) * phi2) + Float64(t_0 * t_1))) * R);
	else
		tmp = Float64(acos(Float64(t_1 * cos(phi2))) * R);
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(phi1) * cos(phi2);
	t_1 = cos((lambda1 - lambda2));
	tmp = 0.0;
	if (phi2 <= -0.007)
		tmp = acos(((sin(phi1) * sin(phi2)) + t_0)) * R;
	elseif (phi2 <= 1.05)
		tmp = acos(((sin(phi1) * phi2) + (t_0 * t_1))) * R;
	else
		tmp = acos((t_1 * cos(phi2))) * R;
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, -0.007], N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 1.05], N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * phi2), $MachinePrecision] + N[(t$95$0 * 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 := \cos \phi_1 \cdot \cos \phi_2\\
t_1 := \cos \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq -0.007:\\
\;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t\_0\right) \cdot R\\

\mathbf{elif}\;\phi_2 \leq 1.05:\\
\;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \phi_2 + t\_0 \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 < -0.00700000000000000015

    1. Initial program 79.8%

      \[\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}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. 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(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
    4. Taylor expanded in lambda2 around 0

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

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

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

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

    if -0.00700000000000000015 < phi2 < 1.05000000000000004

    1. Initial program 67.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} \left(\sin \phi_1 \cdot \color{blue}{\phi_2} + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    3. Applied rewrites67.0%

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

    if 1.05000000000000004 < 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 rewrites49.0%

      \[\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 12: 55.7% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \sin \phi_1 \cdot \sin \phi_2\\ \mathbf{if}\;\phi_2 \leq -0.003:\\ \;\;\;\;\cos^{-1} \left(t\_1 + \cos \phi_1 \cdot \cos \phi_2\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 4.8 \cdot 10^{-5}:\\ \;\;\;\;\cos^{-1} \left(t\_1 + \cos \phi_1 \cdot t\_0\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))) (t_1 (* (sin phi1) (sin phi2))))
   (if (<= phi2 -0.003)
     (* (acos (+ t_1 (* (cos phi1) (cos phi2)))) R)
     (if (<= phi2 4.8e-5)
       (* (acos (+ t_1 (* (cos phi1) t_0))) 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 t_1 = sin(phi1) * sin(phi2);
	double tmp;
	if (phi2 <= -0.003) {
		tmp = acos((t_1 + (cos(phi1) * cos(phi2)))) * R;
	} else if (phi2 <= 4.8e-5) {
		tmp = acos((t_1 + (cos(phi1) * t_0))) * 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) :: t_1
    real(8) :: tmp
    t_0 = cos((lambda1 - lambda2))
    t_1 = sin(phi1) * sin(phi2)
    if (phi2 <= (-0.003d0)) then
        tmp = acos((t_1 + (cos(phi1) * cos(phi2)))) * r
    else if (phi2 <= 4.8d-5) then
        tmp = acos((t_1 + (cos(phi1) * t_0))) * 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 t_1 = Math.sin(phi1) * Math.sin(phi2);
	double tmp;
	if (phi2 <= -0.003) {
		tmp = Math.acos((t_1 + (Math.cos(phi1) * Math.cos(phi2)))) * R;
	} else if (phi2 <= 4.8e-5) {
		tmp = Math.acos((t_1 + (Math.cos(phi1) * t_0))) * 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))
	t_1 = math.sin(phi1) * math.sin(phi2)
	tmp = 0
	if phi2 <= -0.003:
		tmp = math.acos((t_1 + (math.cos(phi1) * math.cos(phi2)))) * R
	elif phi2 <= 4.8e-5:
		tmp = math.acos((t_1 + (math.cos(phi1) * t_0))) * 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))
	t_1 = Float64(sin(phi1) * sin(phi2))
	tmp = 0.0
	if (phi2 <= -0.003)
		tmp = Float64(acos(Float64(t_1 + Float64(cos(phi1) * cos(phi2)))) * R);
	elseif (phi2 <= 4.8e-5)
		tmp = Float64(acos(Float64(t_1 + Float64(cos(phi1) * t_0))) * 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));
	t_1 = sin(phi1) * sin(phi2);
	tmp = 0.0;
	if (phi2 <= -0.003)
		tmp = acos((t_1 + (cos(phi1) * cos(phi2)))) * R;
	elseif (phi2 <= 4.8e-5)
		tmp = acos((t_1 + (cos(phi1) * t_0))) * 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]}, Block[{t$95$1 = N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi2, -0.003], N[(N[ArcCos[N[(t$95$1 + N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 4.8e-5], N[(N[ArcCos[N[(t$95$1 + N[(N[Cos[phi1], $MachinePrecision] * t$95$0), $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)\\
t_1 := \sin \phi_1 \cdot \sin \phi_2\\
\mathbf{if}\;\phi_2 \leq -0.003:\\
\;\;\;\;\cos^{-1} \left(t\_1 + \cos \phi_1 \cdot \cos \phi_2\right) \cdot R\\

\mathbf{elif}\;\phi_2 \leq 4.8 \cdot 10^{-5}:\\
\;\;\;\;\cos^{-1} \left(t\_1 + \cos \phi_1 \cdot t\_0\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 < -0.0030000000000000001

    1. Initial program 79.8%

      \[\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}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. 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(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
    4. Taylor expanded in lambda2 around 0

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

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

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

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

    if -0.0030000000000000001 < phi2 < 4.8000000000000001e-5

    1. Initial program 67.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 rewrites66.8%

      \[\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 4.8000000000000001e-5 < 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 \]
    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 rewrites49.1%

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

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq -0.003:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \cos \phi_2\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 4.8 \cdot 10^{-5}:\\ \;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \phi_1, \sin \phi_1 \cdot \phi_2\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 -0.003)
     (* (acos (+ (* (sin phi1) (sin phi2)) (* (cos phi1) (cos phi2)))) R)
     (if (<= phi2 4.8e-5)
       (* (acos (fma t_0 (cos phi1) (* (sin phi1) phi2))) 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 <= -0.003) {
		tmp = acos(((sin(phi1) * sin(phi2)) + (cos(phi1) * cos(phi2)))) * R;
	} else if (phi2 <= 4.8e-5) {
		tmp = acos(fma(t_0, cos(phi1), (sin(phi1) * phi2))) * 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 <= -0.003)
		tmp = Float64(acos(Float64(Float64(sin(phi1) * sin(phi2)) + Float64(cos(phi1) * cos(phi2)))) * R);
	elseif (phi2 <= 4.8e-5)
		tmp = Float64(acos(fma(t_0, cos(phi1), Float64(sin(phi1) * phi2))) * 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, -0.003], N[(N[ArcCos[N[(N[(N[Sin[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] + N[(N[Cos[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * R), $MachinePrecision], If[LessEqual[phi2, 4.8e-5], N[(N[ArcCos[N[(t$95$0 * N[Cos[phi1], $MachinePrecision] + N[(N[Sin[phi1], $MachinePrecision] * phi2), $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 -0.003:\\
\;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \cos \phi_2\right) \cdot R\\

\mathbf{elif}\;\phi_2 \leq 4.8 \cdot 10^{-5}:\\
\;\;\;\;\cos^{-1} \left(\mathsf{fma}\left(t\_0, \cos \phi_1, \sin \phi_1 \cdot \phi_2\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 < -0.0030000000000000001

    1. Initial program 79.8%

      \[\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}{\left(\cos \lambda_1 \cdot \cos \lambda_2 + \sin \lambda_1 \cdot \sin \lambda_2\right)}\right) \cdot R \]
    3. 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(\sin \lambda_1, \sin \lambda_2, \cos \lambda_1 \cdot \cos \lambda_2\right)}\right) \cdot R \]
    4. Taylor expanded in lambda2 around 0

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

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

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

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

    if -0.0030000000000000001 < phi2 < 4.8000000000000001e-5

    1. Initial program 67.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} \color{blue}{\left(\phi_2 \cdot \sin \phi_1 + \cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
    3. Applied rewrites66.8%

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

    if 4.8000000000000001e-5 < 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 \]
    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 rewrites49.1%

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

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

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

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

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


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

    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 rewrites47.7%

      \[\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 0

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

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

    if 7.0000000000000004e-131 < phi2 < 7.99999999999999964e-6

    1. Initial program 66.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 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 rewrites65.0%

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

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

    if 7.99999999999999964e-6 < phi2

    1. Initial program 78.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 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.6%

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

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

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

Alternative 15: 49.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq 3.6 \cdot 10^{-5}:\\ \;\;\;\;\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 (<= phi2 3.6e-5)
     (* (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 (phi2 <= 3.6e-5) {
		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 (phi2 <= 3.6d-5) 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 (phi2 <= 3.6e-5) {
		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 phi2 <= 3.6e-5:
		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 (phi2 <= 3.6e-5)
		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 (phi2 <= 3.6e-5)
		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[phi2, 3.6e-5], 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_2 \leq 3.6 \cdot 10^{-5}:\\
\;\;\;\;\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 phi2 < 3.60000000000000009e-5

    1. Initial program 71.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 rewrites49.9%

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

    if 3.60000000000000009e-5 < 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 \]
    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 rewrites49.1%

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

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

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

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


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

    1. Initial program 71.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 rewrites49.9%

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

    if 0.010800000000000001 < 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 \]
    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.7%

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

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

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

Alternative 17: 38.0% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;t\_0 \leq 0.978:\\ \;\;\;\;\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.978) (* (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.978) {
		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.978d0) 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.978) {
		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.978:
		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.978)
		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.978)
		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.978], 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.978:\\
\;\;\;\;\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.97799999999999998

    1. Initial program 71.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 rewrites45.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 rewrites32.8%

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

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

    1. Initial program 76.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 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 rewrites72.2%

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_2 \cdot \cos \phi_1, \sin \phi_2 \cdot \sin \phi_1\right)\right)} \cdot R \]
    4. 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 \]
    5. Applied rewrites50.0%

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

Alternative 18: 31.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;t\_0 \leq 0.978:\\ \;\;\;\;\cos^{-1} t\_0 \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \cos \phi_1 \cdot R\\ \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2))))
   (if (<= t_0 0.978) (* (acos t_0) R) (* (acos (cos phi1)) 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.978) {
		tmp = acos(t_0) * R;
	} else {
		tmp = acos(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) :: t_0
    real(8) :: tmp
    t_0 = cos((lambda1 - lambda2))
    if (t_0 <= 0.978d0) then
        tmp = acos(t_0) * r
    else
        tmp = acos(cos(phi1)) * 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.978) {
		tmp = Math.acos(t_0) * R;
	} else {
		tmp = Math.acos(Math.cos(phi1)) * R;
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	tmp = 0
	if t_0 <= 0.978:
		tmp = math.acos(t_0) * R
	else:
		tmp = math.acos(math.cos(phi1)) * R
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (t_0 <= 0.978)
		tmp = Float64(acos(t_0) * R);
	else
		tmp = Float64(acos(cos(phi1)) * 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.978)
		tmp = acos(t_0) * R;
	else
		tmp = acos(cos(phi1)) * 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.978], N[(N[ArcCos[t$95$0], $MachinePrecision] * R), $MachinePrecision], N[(N[ArcCos[N[Cos[phi1], $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.978:\\
\;\;\;\;\cos^{-1} t\_0 \cdot R\\

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \cos \phi_1 \cdot R\\


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

    1. Initial program 71.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 rewrites45.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 rewrites32.8%

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

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

    1. Initial program 76.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 rewrites34.5%

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

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

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

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

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

Alternative 19: 36.4% accurate, 2.0× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < 5.8000000000000004e-34

    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 rewrites49.7%

      \[\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 0

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

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

    if 5.8000000000000004e-34 < phi2

    1. Initial program 77.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 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 rewrites57.7%

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

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

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

Alternative 20: 33.7% accurate, 2.0× speedup?

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

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

\mathbf{elif}\;\phi_1 \leq 2.2 \cdot 10^{-186}:\\
\;\;\;\;\cos^{-1} \cos \left(\lambda_1 - \lambda_2\right) \cdot R\\

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


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

    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 rewrites49.0%

      \[\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 0

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

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

    if -2.59999999999999977e-4 < phi1 < 2.20000000000000013e-186

    1. Initial program 68.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 rewrites36.5%

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

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

    if 2.20000000000000013e-186 < phi1

    1. Initial program 73.8%

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

      \[\leadsto \cos^{-1} \color{blue}{\left(\mathsf{fma}\left(\cos \lambda_1, \cos \phi_2 \cdot \cos \phi_1, \sin \phi_2 \cdot \sin \phi_1\right)\right)} \cdot R \]
    4. 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 \]
    5. Applied rewrites27.6%

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

Alternative 21: 21.2% accurate, 3.0× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;\cos^{-1} \cos \lambda_1 \cdot R\\


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

    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 rewrites49.0%

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

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

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

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

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

    if -2.59999999999999977e-4 < phi1

    1. Initial program 71.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 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 rewrites39.8%

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

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

      \[\leadsto \cos^{-1} \cos \lambda_1 \cdot R \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 22: 17.1% accurate, 3.0× speedup?

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

\\
\cos^{-1} \cos \phi_1 \cdot R
\end{array}
Derivation
  1. Initial program 73.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} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
  3. Applied rewrites42.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 lambda2 around 0

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

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

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

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

Alternative 23: 4.3% accurate, 5.9× speedup?

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

\\
\cos^{-1} 1 \cdot R
\end{array}
Derivation
  1. Initial program 73.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} \color{blue}{\left(\cos \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)} \cdot R \]
  3. Applied rewrites42.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 lambda2 around 0

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

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

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

    \[\leadsto \cos^{-1} \cos \phi_1 \cdot R \]
  8. Taylor expanded in phi1 around 0

    \[\leadsto \cos^{-1} 1 \cdot R \]
  9. Applied rewrites4.3%

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

Reproduce

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