Bearing on a great circle

Percentage Accurate: 79.8% → 99.7%
Time: 39.9s
Alternatives: 27
Speedup: 1.0×

Specification

?
\[\begin{array}{l} \\ \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (atan2
  (* (sin (- lambda1 lambda2)) (cos phi2))
  (-
   (* (cos phi1) (sin phi2))
   (* (* (sin phi1) (cos phi2)) (cos (- lambda1 lambda2))))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	return atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
}
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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))))
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	return Math.atan2((Math.sin((lambda1 - lambda2)) * Math.cos(phi2)), ((Math.cos(phi1) * Math.sin(phi2)) - ((Math.sin(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2)))));
}
def code(lambda1, lambda2, phi1, phi2):
	return math.atan2((math.sin((lambda1 - lambda2)) * math.cos(phi2)), ((math.cos(phi1) * math.sin(phi2)) - ((math.sin(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2)))))
function code(lambda1, lambda2, phi1, phi2)
	return atan(Float64(sin(Float64(lambda1 - lambda2)) * cos(phi2)), Float64(Float64(cos(phi1) * sin(phi2)) - Float64(Float64(sin(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2)))))
end
function tmp = code(lambda1, lambda2, phi1, phi2)
	tmp = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
end
code[lambda1_, lambda2_, phi1_, phi2_] := N[ArcTan[N[(N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

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

\[\begin{array}{l} \\ \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (atan2
  (* (sin (- lambda1 lambda2)) (cos phi2))
  (-
   (* (cos phi1) (sin phi2))
   (* (* (sin phi1) (cos phi2)) (cos (- lambda1 lambda2))))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	return atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
}
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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))))
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	return Math.atan2((Math.sin((lambda1 - lambda2)) * Math.cos(phi2)), ((Math.cos(phi1) * Math.sin(phi2)) - ((Math.sin(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2)))));
}
def code(lambda1, lambda2, phi1, phi2):
	return math.atan2((math.sin((lambda1 - lambda2)) * math.cos(phi2)), ((math.cos(phi1) * math.sin(phi2)) - ((math.sin(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2)))))
function code(lambda1, lambda2, phi1, phi2)
	return atan(Float64(sin(Float64(lambda1 - lambda2)) * cos(phi2)), Float64(Float64(cos(phi1) * sin(phi2)) - Float64(Float64(sin(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2)))))
end
function tmp = code(lambda1, lambda2, phi1, phi2)
	tmp = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
end
code[lambda1_, lambda2_, phi1_, phi2_] := N[ArcTan[N[(N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}
\end{array}

Alternative 1: 99.7% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (atan2
  (*
   (- (* (sin lambda1) (cos lambda2)) (* (cos lambda1) (sin lambda2)))
   (cos phi2))
  (-
   (* (cos phi1) (sin phi2))
   (*
    (* (sin phi1) (cos phi2))
    (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2)))))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	return atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2))))));
}
function code(lambda1, lambda2, phi1, phi2)
	return atan(Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2)), Float64(Float64(cos(phi1) * sin(phi2)) - Float64(Float64(sin(phi1) * cos(phi2)) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))))
end
code[lambda1_, lambda2_, phi1_, phi2_] := N[ArcTan[N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Sin[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]
\begin{array}{l}

\\
\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)}
\end{array}
Derivation
  1. Initial program 74.5%

    \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  2. Add Preprocessing
  3. Applied rewrites88.1%

    \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  4. Applied rewrites99.7%

    \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)}} \]
  5. Add Preprocessing

Alternative 2: 94.2% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_1 \cdot \sin \phi_2\\ t_1 := \left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2\\ t_2 := \tan^{-1}_* \frac{t\_1}{t\_0 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\\ \mathbf{if}\;\phi_2 \leq -1300:\\ \;\;\;\;t\_2\\ \mathbf{elif}\;\phi_2 \leq 550000000:\\ \;\;\;\;\tan^{-1}_* \frac{t\_1}{t\_0 - \sin \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_2\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi1) (sin phi2)))
        (t_1
         (*
          (- (* (sin lambda1) (cos lambda2)) (* (cos lambda1) (sin lambda2)))
          (cos phi2)))
        (t_2
         (atan2
          t_1
          (- t_0 (* (* (sin phi1) (cos phi2)) (cos (- lambda1 lambda2)))))))
   (if (<= phi2 -1300.0)
     t_2
     (if (<= phi2 550000000.0)
       (atan2
        t_1
        (-
         t_0
         (*
          (sin phi1)
          (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
       t_2))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi1) * sin(phi2);
	double t_1 = ((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2);
	double t_2 = atan2(t_1, (t_0 - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	double tmp;
	if (phi2 <= -1300.0) {
		tmp = t_2;
	} else if (phi2 <= 550000000.0) {
		tmp = atan2(t_1, (t_0 - (sin(phi1) * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2))))));
	} else {
		tmp = t_2;
	}
	return tmp;
}
function code(lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi1) * sin(phi2))
	t_1 = Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2))
	t_2 = atan(t_1, Float64(t_0 - Float64(Float64(sin(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2)))))
	tmp = 0.0
	if (phi2 <= -1300.0)
		tmp = t_2;
	elseif (phi2 <= 550000000.0)
		tmp = atan(t_1, Float64(t_0 - Float64(sin(phi1) * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = t_2;
	end
	return tmp
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[ArcTan[t$95$1 / N[(t$95$0 - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, -1300.0], t$95$2, If[LessEqual[phi2, 550000000.0], N[ArcTan[t$95$1 / N[(t$95$0 - N[(N[Sin[phi1], $MachinePrecision] * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$2]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \sin \phi_2\\
t_1 := \left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2\\
t_2 := \tan^{-1}_* \frac{t\_1}{t\_0 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\\
\mathbf{if}\;\phi_2 \leq -1300:\\
\;\;\;\;t\_2\\

\mathbf{elif}\;\phi_2 \leq 550000000:\\
\;\;\;\;\tan^{-1}_* \frac{t\_1}{t\_0 - \sin \phi_1 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -1300 or 5.5e8 < phi2

    1. Initial program 72.1%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites90.7%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]

    if -1300 < phi2 < 5.5e8

    1. Initial program 76.8%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites85.8%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Applied rewrites99.9%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)}} \]
    5. Taylor expanded in phi2 around 0

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

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

Alternative 3: 94.4% accurate, 0.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \lambda_1 \cdot \sin \lambda_2\\ t_1 := \cos \phi_1 \cdot \sin \phi_2\\ t_2 := \sin \phi_1 \cdot \cos \phi_2\\ t_3 := \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - t\_0\right) \cdot \cos \phi_2}{t\_1 - t\_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)}\\ \mathbf{if}\;\phi_2 \leq -0.00098:\\ \;\;\;\;t\_3\\ \mathbf{elif}\;\phi_2 \leq 2.9 \cdot 10^{-35}:\\ \;\;\;\;\tan^{-1}_* \frac{\cos \lambda_2 \cdot \sin \lambda_1 - t\_0}{t\_1 - t\_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_3\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos lambda1) (sin lambda2)))
        (t_1 (* (cos phi1) (sin phi2)))
        (t_2 (* (sin phi1) (cos phi2)))
        (t_3
         (atan2
          (* (- (* (sin lambda1) (cos lambda2)) t_0) (cos phi2))
          (- t_1 (* t_2 (cos (- lambda1 lambda2)))))))
   (if (<= phi2 -0.00098)
     t_3
     (if (<= phi2 2.9e-35)
       (atan2
        (- (* (cos lambda2) (sin lambda1)) t_0)
        (-
         t_1
         (*
          t_2
          (fma (cos lambda1) (cos lambda2) (* (sin lambda1) (sin lambda2))))))
       t_3))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(lambda1) * sin(lambda2);
	double t_1 = cos(phi1) * sin(phi2);
	double t_2 = sin(phi1) * cos(phi2);
	double t_3 = atan2((((sin(lambda1) * cos(lambda2)) - t_0) * cos(phi2)), (t_1 - (t_2 * cos((lambda1 - lambda2)))));
	double tmp;
	if (phi2 <= -0.00098) {
		tmp = t_3;
	} else if (phi2 <= 2.9e-35) {
		tmp = atan2(((cos(lambda2) * sin(lambda1)) - t_0), (t_1 - (t_2 * fma(cos(lambda1), cos(lambda2), (sin(lambda1) * sin(lambda2))))));
	} else {
		tmp = t_3;
	}
	return tmp;
}
function code(lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(lambda1) * sin(lambda2))
	t_1 = Float64(cos(phi1) * sin(phi2))
	t_2 = Float64(sin(phi1) * cos(phi2))
	t_3 = atan(Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - t_0) * cos(phi2)), Float64(t_1 - Float64(t_2 * cos(Float64(lambda1 - lambda2)))))
	tmp = 0.0
	if (phi2 <= -0.00098)
		tmp = t_3;
	elseif (phi2 <= 2.9e-35)
		tmp = atan(Float64(Float64(cos(lambda2) * sin(lambda1)) - t_0), Float64(t_1 - Float64(t_2 * fma(cos(lambda1), cos(lambda2), Float64(sin(lambda1) * sin(lambda2))))));
	else
		tmp = t_3;
	end
	return tmp
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[ArcTan[N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(t$95$1 - N[(t$95$2 * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, -0.00098], t$95$3, If[LessEqual[phi2, 2.9e-35], N[ArcTan[N[(N[(N[Cos[lambda2], $MachinePrecision] * N[Sin[lambda1], $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision] / N[(t$95$1 - N[(t$95$2 * N[(N[Cos[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision] + N[(N[Sin[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$3]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \lambda_1 \cdot \sin \lambda_2\\
t_1 := \cos \phi_1 \cdot \sin \phi_2\\
t_2 := \sin \phi_1 \cdot \cos \phi_2\\
t_3 := \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - t\_0\right) \cdot \cos \phi_2}{t\_1 - t\_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)}\\
\mathbf{if}\;\phi_2 \leq -0.00098:\\
\;\;\;\;t\_3\\

\mathbf{elif}\;\phi_2 \leq 2.9 \cdot 10^{-35}:\\
\;\;\;\;\tan^{-1}_* \frac{\cos \lambda_2 \cdot \sin \lambda_1 - t\_0}{t\_1 - t\_2 \cdot \mathsf{fma}\left(\cos \lambda_1, \cos \lambda_2, \sin \lambda_1 \cdot \sin \lambda_2\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -9.7999999999999997e-4 or 2.9000000000000002e-35 < phi2

    1. Initial program 71.8%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites90.8%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]

    if -9.7999999999999997e-4 < phi2 < 2.9000000000000002e-35

    1. Initial program 77.5%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites85.2%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Applied rewrites99.9%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\cos \lambda_2 \cdot \sin \lambda_1 - \cos \lambda_1 \cdot \sin \lambda_2}}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)} \]
    6. Applied rewrites99.9%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\cos \lambda_2 \cdot \sin \lambda_1 - \cos \lambda_1 \cdot \sin \lambda_2}}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 4: 88.8% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_1 \cdot \sin \phi_2\\ t_1 := \left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2\\ t_2 := \tan^{-1}_* \frac{t\_1}{t\_0 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1}\\ \mathbf{if}\;\lambda_1 \leq -0.00038:\\ \;\;\;\;t\_2\\ \mathbf{elif}\;\lambda_1 \leq 5 \cdot 10^{+56}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_1}{t\_0 - \cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \sin \phi_1\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_2\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi1) (sin phi2)))
        (t_1
         (*
          (- (* (sin lambda1) (cos lambda2)) (* (cos lambda1) (sin lambda2)))
          (cos phi2)))
        (t_2 (atan2 t_1 (- t_0 (* (* (sin phi1) (cos phi2)) (cos lambda1))))))
   (if (<= lambda1 -0.00038)
     t_2
     (if (<= lambda1 5e+56)
       (atan2 t_1 (- t_0 (* (cos phi2) (* (cos lambda2) (sin phi1)))))
       t_2))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi1) * sin(phi2);
	double t_1 = ((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2);
	double t_2 = atan2(t_1, (t_0 - ((sin(phi1) * cos(phi2)) * cos(lambda1))));
	double tmp;
	if (lambda1 <= -0.00038) {
		tmp = t_2;
	} else if (lambda1 <= 5e+56) {
		tmp = atan2(t_1, (t_0 - (cos(phi2) * (cos(lambda2) * sin(phi1)))));
	} else {
		tmp = t_2;
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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) :: t_2
    real(8) :: tmp
    t_0 = cos(phi1) * sin(phi2)
    t_1 = ((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)
    t_2 = atan2(t_1, (t_0 - ((sin(phi1) * cos(phi2)) * cos(lambda1))))
    if (lambda1 <= (-0.00038d0)) then
        tmp = t_2
    else if (lambda1 <= 5d+56) then
        tmp = atan2(t_1, (t_0 - (cos(phi2) * (cos(lambda2) * sin(phi1)))))
    else
        tmp = t_2
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos(phi1) * Math.sin(phi2);
	double t_1 = ((Math.sin(lambda1) * Math.cos(lambda2)) - (Math.cos(lambda1) * Math.sin(lambda2))) * Math.cos(phi2);
	double t_2 = Math.atan2(t_1, (t_0 - ((Math.sin(phi1) * Math.cos(phi2)) * Math.cos(lambda1))));
	double tmp;
	if (lambda1 <= -0.00038) {
		tmp = t_2;
	} else if (lambda1 <= 5e+56) {
		tmp = Math.atan2(t_1, (t_0 - (Math.cos(phi2) * (Math.cos(lambda2) * Math.sin(phi1)))));
	} else {
		tmp = t_2;
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.cos(phi1) * math.sin(phi2)
	t_1 = ((math.sin(lambda1) * math.cos(lambda2)) - (math.cos(lambda1) * math.sin(lambda2))) * math.cos(phi2)
	t_2 = math.atan2(t_1, (t_0 - ((math.sin(phi1) * math.cos(phi2)) * math.cos(lambda1))))
	tmp = 0
	if lambda1 <= -0.00038:
		tmp = t_2
	elif lambda1 <= 5e+56:
		tmp = math.atan2(t_1, (t_0 - (math.cos(phi2) * (math.cos(lambda2) * math.sin(phi1)))))
	else:
		tmp = t_2
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi1) * sin(phi2))
	t_1 = Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2))
	t_2 = atan(t_1, Float64(t_0 - Float64(Float64(sin(phi1) * cos(phi2)) * cos(lambda1))))
	tmp = 0.0
	if (lambda1 <= -0.00038)
		tmp = t_2;
	elseif (lambda1 <= 5e+56)
		tmp = atan(t_1, Float64(t_0 - Float64(cos(phi2) * Float64(cos(lambda2) * sin(phi1)))));
	else
		tmp = t_2;
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = cos(phi1) * sin(phi2);
	t_1 = ((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2);
	t_2 = atan2(t_1, (t_0 - ((sin(phi1) * cos(phi2)) * cos(lambda1))));
	tmp = 0.0;
	if (lambda1 <= -0.00038)
		tmp = t_2;
	elseif (lambda1 <= 5e+56)
		tmp = atan2(t_1, (t_0 - (cos(phi2) * (cos(lambda2) * sin(phi1)))));
	else
		tmp = t_2;
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[ArcTan[t$95$1 / N[(t$95$0 - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[lambda1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[lambda1, -0.00038], t$95$2, If[LessEqual[lambda1, 5e+56], N[ArcTan[t$95$1 / N[(t$95$0 - N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$2]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \sin \phi_2\\
t_1 := \left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2\\
t_2 := \tan^{-1}_* \frac{t\_1}{t\_0 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \lambda_1}\\
\mathbf{if}\;\lambda_1 \leq -0.00038:\\
\;\;\;\;t\_2\\

\mathbf{elif}\;\lambda_1 \leq 5 \cdot 10^{+56}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_1}{t\_0 - \cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \sin \phi_1\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda1 < -3.8000000000000002e-4 or 5.00000000000000024e56 < lambda1

    1. Initial program 50.2%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites77.6%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\lambda_1}} \]
    5. Applied rewrites77.9%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \color{blue}{\lambda_1}} \]

    if -3.8000000000000002e-4 < lambda1 < 5.00000000000000024e56

    1. Initial program 93.4%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites96.3%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Taylor expanded in lambda1 around 0

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \color{blue}{\cos \phi_2 \cdot \left(\cos \left(\mathsf{neg}\left(\lambda_2\right)\right) \cdot \sin \phi_1\right)}} \]
    5. Applied rewrites96.3%

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

Alternative 5: 88.4% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \phi_1 \cdot \sin \phi_2\\ t_1 := \sin \lambda_1 \cdot \cos \lambda_2\\ t_2 := \tan^{-1}_* \frac{\left(t\_1 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{t\_0 - \cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \sin \phi_1\right)}\\ \mathbf{if}\;\lambda_2 \leq -95000000:\\ \;\;\;\;t\_2\\ \mathbf{elif}\;\lambda_2 \leq 10^{-90}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(t\_1 - \sin \lambda_2\right) \cdot \cos \phi_2}{t\_0 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_2\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos phi1) (sin phi2)))
        (t_1 (* (sin lambda1) (cos lambda2)))
        (t_2
         (atan2
          (* (- t_1 (* (cos lambda1) (sin lambda2))) (cos phi2))
          (- t_0 (* (cos phi2) (* (cos lambda2) (sin phi1)))))))
   (if (<= lambda2 -95000000.0)
     t_2
     (if (<= lambda2 1e-90)
       (atan2
        (* (- t_1 (sin lambda2)) (cos phi2))
        (- t_0 (* (* (sin phi1) (cos phi2)) (cos (- lambda1 lambda2)))))
       t_2))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos(phi1) * sin(phi2);
	double t_1 = sin(lambda1) * cos(lambda2);
	double t_2 = atan2(((t_1 - (cos(lambda1) * sin(lambda2))) * cos(phi2)), (t_0 - (cos(phi2) * (cos(lambda2) * sin(phi1)))));
	double tmp;
	if (lambda2 <= -95000000.0) {
		tmp = t_2;
	} else if (lambda2 <= 1e-90) {
		tmp = atan2(((t_1 - sin(lambda2)) * cos(phi2)), (t_0 - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	} else {
		tmp = t_2;
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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) :: t_2
    real(8) :: tmp
    t_0 = cos(phi1) * sin(phi2)
    t_1 = sin(lambda1) * cos(lambda2)
    t_2 = atan2(((t_1 - (cos(lambda1) * sin(lambda2))) * cos(phi2)), (t_0 - (cos(phi2) * (cos(lambda2) * sin(phi1)))))
    if (lambda2 <= (-95000000.0d0)) then
        tmp = t_2
    else if (lambda2 <= 1d-90) then
        tmp = atan2(((t_1 - sin(lambda2)) * cos(phi2)), (t_0 - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))))
    else
        tmp = t_2
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos(phi1) * Math.sin(phi2);
	double t_1 = Math.sin(lambda1) * Math.cos(lambda2);
	double t_2 = Math.atan2(((t_1 - (Math.cos(lambda1) * Math.sin(lambda2))) * Math.cos(phi2)), (t_0 - (Math.cos(phi2) * (Math.cos(lambda2) * Math.sin(phi1)))));
	double tmp;
	if (lambda2 <= -95000000.0) {
		tmp = t_2;
	} else if (lambda2 <= 1e-90) {
		tmp = Math.atan2(((t_1 - Math.sin(lambda2)) * Math.cos(phi2)), (t_0 - ((Math.sin(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2)))));
	} else {
		tmp = t_2;
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.cos(phi1) * math.sin(phi2)
	t_1 = math.sin(lambda1) * math.cos(lambda2)
	t_2 = math.atan2(((t_1 - (math.cos(lambda1) * math.sin(lambda2))) * math.cos(phi2)), (t_0 - (math.cos(phi2) * (math.cos(lambda2) * math.sin(phi1)))))
	tmp = 0
	if lambda2 <= -95000000.0:
		tmp = t_2
	elif lambda2 <= 1e-90:
		tmp = math.atan2(((t_1 - math.sin(lambda2)) * math.cos(phi2)), (t_0 - ((math.sin(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2)))))
	else:
		tmp = t_2
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(phi1) * sin(phi2))
	t_1 = Float64(sin(lambda1) * cos(lambda2))
	t_2 = atan(Float64(Float64(t_1 - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2)), Float64(t_0 - Float64(cos(phi2) * Float64(cos(lambda2) * sin(phi1)))))
	tmp = 0.0
	if (lambda2 <= -95000000.0)
		tmp = t_2;
	elseif (lambda2 <= 1e-90)
		tmp = atan(Float64(Float64(t_1 - sin(lambda2)) * cos(phi2)), Float64(t_0 - Float64(Float64(sin(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2)))));
	else
		tmp = t_2;
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = cos(phi1) * sin(phi2);
	t_1 = sin(lambda1) * cos(lambda2);
	t_2 = atan2(((t_1 - (cos(lambda1) * sin(lambda2))) * cos(phi2)), (t_0 - (cos(phi2) * (cos(lambda2) * sin(phi1)))));
	tmp = 0.0;
	if (lambda2 <= -95000000.0)
		tmp = t_2;
	elseif (lambda2 <= 1e-90)
		tmp = atan2(((t_1 - sin(lambda2)) * cos(phi2)), (t_0 - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	else
		tmp = t_2;
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[ArcTan[N[(N[(t$95$1 - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(t$95$0 - N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[lambda2, -95000000.0], t$95$2, If[LessEqual[lambda2, 1e-90], N[ArcTan[N[(N[(t$95$1 - N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(t$95$0 - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$2]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \phi_1 \cdot \sin \phi_2\\
t_1 := \sin \lambda_1 \cdot \cos \lambda_2\\
t_2 := \tan^{-1}_* \frac{\left(t\_1 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{t\_0 - \cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \sin \phi_1\right)}\\
\mathbf{if}\;\lambda_2 \leq -95000000:\\
\;\;\;\;t\_2\\

\mathbf{elif}\;\lambda_2 \leq 10^{-90}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(t\_1 - \sin \lambda_2\right) \cdot \cos \phi_2}{t\_0 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda2 < -9.5e7 or 9.99999999999999995e-91 < lambda2

    1. Initial program 57.9%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites80.8%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Taylor expanded in lambda1 around 0

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \color{blue}{\cos \phi_2 \cdot \left(\cos \left(\mathsf{neg}\left(\lambda_2\right)\right) \cdot \sin \phi_1\right)}} \]
    5. Applied rewrites80.9%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \color{blue}{\cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \sin \phi_1\right)}} \]

    if -9.5e7 < lambda2 < 9.99999999999999995e-91

    1. Initial program 99.1%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites99.1%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Taylor expanded in lambda1 around 0

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \color{blue}{\sin \lambda_2}\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    5. Applied rewrites99.1%

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

Alternative 6: 89.8% accurate, 0.7× speedup?

\[\begin{array}{l} \\ \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (atan2
  (*
   (- (* (sin lambda1) (cos lambda2)) (* (cos lambda1) (sin lambda2)))
   (cos phi2))
  (-
   (* (cos phi1) (sin phi2))
   (* (* (sin phi1) (cos phi2)) (cos (- lambda1 lambda2))))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	return atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
}
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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))))
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	return Math.atan2((((Math.sin(lambda1) * Math.cos(lambda2)) - (Math.cos(lambda1) * Math.sin(lambda2))) * Math.cos(phi2)), ((Math.cos(phi1) * Math.sin(phi2)) - ((Math.sin(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2)))));
}
def code(lambda1, lambda2, phi1, phi2):
	return math.atan2((((math.sin(lambda1) * math.cos(lambda2)) - (math.cos(lambda1) * math.sin(lambda2))) * math.cos(phi2)), ((math.cos(phi1) * math.sin(phi2)) - ((math.sin(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2)))))
function code(lambda1, lambda2, phi1, phi2)
	return atan(Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2)), Float64(Float64(cos(phi1) * sin(phi2)) - Float64(Float64(sin(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2)))))
end
function tmp = code(lambda1, lambda2, phi1, phi2)
	tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
end
code[lambda1_, lambda2_, phi1_, phi2_] := N[ArcTan[N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}
\end{array}
Derivation
  1. Initial program 74.5%

    \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  2. Add Preprocessing
  3. Applied rewrites88.1%

    \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  4. Add Preprocessing

Alternative 7: 88.8% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \lambda_1 \cdot \cos \lambda_2\\ t_1 := \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\\ t_2 := \cos \phi_1 \cdot \sin \phi_2 - t\_1\\ t_3 := \cos \lambda_1 \cdot \sin \lambda_2\\ \mathbf{if}\;\phi_1 \leq -7 \cdot 10^{-5}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 - t\_3\right) \cdot \cos \phi_2}{t\_2}\\ \mathbf{elif}\;\phi_1 \leq 7.8 \cdot 10^{-5}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(t\_0 - t\_3\right) \cdot \cos \phi_2}{\sin \phi_2 - t\_1}\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(t\_0 - \sin \lambda_2\right) \cdot \cos \phi_2}{t\_2}\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin lambda1) (cos lambda2)))
        (t_1 (* (* (sin phi1) (cos phi2)) (cos (- lambda1 lambda2))))
        (t_2 (- (* (cos phi1) (sin phi2)) t_1))
        (t_3 (* (cos lambda1) (sin lambda2))))
   (if (<= phi1 -7e-5)
     (atan2 (* (- (sin lambda1) t_3) (cos phi2)) t_2)
     (if (<= phi1 7.8e-5)
       (atan2 (* (- t_0 t_3) (cos phi2)) (- (sin phi2) t_1))
       (atan2 (* (- t_0 (sin lambda2)) (cos phi2)) t_2)))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin(lambda1) * cos(lambda2);
	double t_1 = (sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2));
	double t_2 = (cos(phi1) * sin(phi2)) - t_1;
	double t_3 = cos(lambda1) * sin(lambda2);
	double tmp;
	if (phi1 <= -7e-5) {
		tmp = atan2(((sin(lambda1) - t_3) * cos(phi2)), t_2);
	} else if (phi1 <= 7.8e-5) {
		tmp = atan2(((t_0 - t_3) * cos(phi2)), (sin(phi2) - t_1));
	} else {
		tmp = atan2(((t_0 - sin(lambda2)) * cos(phi2)), t_2);
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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) :: t_2
    real(8) :: t_3
    real(8) :: tmp
    t_0 = sin(lambda1) * cos(lambda2)
    t_1 = (sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2))
    t_2 = (cos(phi1) * sin(phi2)) - t_1
    t_3 = cos(lambda1) * sin(lambda2)
    if (phi1 <= (-7d-5)) then
        tmp = atan2(((sin(lambda1) - t_3) * cos(phi2)), t_2)
    else if (phi1 <= 7.8d-5) then
        tmp = atan2(((t_0 - t_3) * cos(phi2)), (sin(phi2) - t_1))
    else
        tmp = atan2(((t_0 - sin(lambda2)) * cos(phi2)), t_2)
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.sin(lambda1) * Math.cos(lambda2);
	double t_1 = (Math.sin(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2));
	double t_2 = (Math.cos(phi1) * Math.sin(phi2)) - t_1;
	double t_3 = Math.cos(lambda1) * Math.sin(lambda2);
	double tmp;
	if (phi1 <= -7e-5) {
		tmp = Math.atan2(((Math.sin(lambda1) - t_3) * Math.cos(phi2)), t_2);
	} else if (phi1 <= 7.8e-5) {
		tmp = Math.atan2(((t_0 - t_3) * Math.cos(phi2)), (Math.sin(phi2) - t_1));
	} else {
		tmp = Math.atan2(((t_0 - Math.sin(lambda2)) * Math.cos(phi2)), t_2);
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.sin(lambda1) * math.cos(lambda2)
	t_1 = (math.sin(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2))
	t_2 = (math.cos(phi1) * math.sin(phi2)) - t_1
	t_3 = math.cos(lambda1) * math.sin(lambda2)
	tmp = 0
	if phi1 <= -7e-5:
		tmp = math.atan2(((math.sin(lambda1) - t_3) * math.cos(phi2)), t_2)
	elif phi1 <= 7.8e-5:
		tmp = math.atan2(((t_0 - t_3) * math.cos(phi2)), (math.sin(phi2) - t_1))
	else:
		tmp = math.atan2(((t_0 - math.sin(lambda2)) * math.cos(phi2)), t_2)
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(lambda1) * cos(lambda2))
	t_1 = Float64(Float64(sin(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2)))
	t_2 = Float64(Float64(cos(phi1) * sin(phi2)) - t_1)
	t_3 = Float64(cos(lambda1) * sin(lambda2))
	tmp = 0.0
	if (phi1 <= -7e-5)
		tmp = atan(Float64(Float64(sin(lambda1) - t_3) * cos(phi2)), t_2);
	elseif (phi1 <= 7.8e-5)
		tmp = atan(Float64(Float64(t_0 - t_3) * cos(phi2)), Float64(sin(phi2) - t_1));
	else
		tmp = atan(Float64(Float64(t_0 - sin(lambda2)) * cos(phi2)), t_2);
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = sin(lambda1) * cos(lambda2);
	t_1 = (sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2));
	t_2 = (cos(phi1) * sin(phi2)) - t_1;
	t_3 = cos(lambda1) * sin(lambda2);
	tmp = 0.0;
	if (phi1 <= -7e-5)
		tmp = atan2(((sin(lambda1) - t_3) * cos(phi2)), t_2);
	elseif (phi1 <= 7.8e-5)
		tmp = atan2(((t_0 - t_3) * cos(phi2)), (sin(phi2) - t_1));
	else
		tmp = atan2(((t_0 - sin(lambda2)) * cos(phi2)), t_2);
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - t$95$1), $MachinePrecision]}, Block[{t$95$3 = N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -7e-5], N[ArcTan[N[(N[(N[Sin[lambda1], $MachinePrecision] - t$95$3), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / t$95$2], $MachinePrecision], If[LessEqual[phi1, 7.8e-5], N[ArcTan[N[(N[(t$95$0 - t$95$3), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[Sin[phi2], $MachinePrecision] - t$95$1), $MachinePrecision]], $MachinePrecision], N[ArcTan[N[(N[(t$95$0 - N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / t$95$2], $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \lambda_1 \cdot \cos \lambda_2\\
t_1 := \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\\
t_2 := \cos \phi_1 \cdot \sin \phi_2 - t\_1\\
t_3 := \cos \lambda_1 \cdot \sin \lambda_2\\
\mathbf{if}\;\phi_1 \leq -7 \cdot 10^{-5}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 - t\_3\right) \cdot \cos \phi_2}{t\_2}\\

\mathbf{elif}\;\phi_1 \leq 7.8 \cdot 10^{-5}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(t\_0 - t\_3\right) \cdot \cos \phi_2}{\sin \phi_2 - t\_1}\\

\mathbf{else}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(t\_0 - \sin \lambda_2\right) \cdot \cos \phi_2}{t\_2}\\


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

    1. Initial program 77.5%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites79.8%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\color{blue}{\sin \lambda_1} - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    5. Applied rewrites78.0%

      \[\leadsto \tan^{-1}_* \frac{\left(\color{blue}{\sin \lambda_1} - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]

    if -6.9999999999999994e-5 < phi1 < 7.7999999999999999e-5

    1. Initial program 72.1%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites98.5%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2} - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    5. Applied rewrites98.5%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2} - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]

    if 7.7999999999999999e-5 < phi1

    1. Initial program 75.5%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites78.6%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Taylor expanded in lambda1 around 0

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \color{blue}{\sin \lambda_2}\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    5. Applied rewrites76.4%

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

Alternative 8: 88.8% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \lambda_1 \cdot \cos \lambda_2\\ t_1 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_2 := \cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot t\_1\\ t_3 := \cos \lambda_1 \cdot \sin \lambda_2\\ \mathbf{if}\;\phi_1 \leq -3.6 \cdot 10^{-5}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 - t\_3\right) \cdot \cos \phi_2}{t\_2}\\ \mathbf{elif}\;\phi_1 \leq 3.5 \cdot 10^{-5}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(t\_0 - t\_3\right) \cdot \cos \phi_2}{\sin \phi_2 + -1 \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot t\_1\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(t\_0 - \sin \lambda_2\right) \cdot \cos \phi_2}{t\_2}\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (sin lambda1) (cos lambda2)))
        (t_1 (cos (- lambda1 lambda2)))
        (t_2 (- (* (cos phi1) (sin phi2)) (* (* (sin phi1) (cos phi2)) t_1)))
        (t_3 (* (cos lambda1) (sin lambda2))))
   (if (<= phi1 -3.6e-5)
     (atan2 (* (- (sin lambda1) t_3) (cos phi2)) t_2)
     (if (<= phi1 3.5e-5)
       (atan2
        (* (- t_0 t_3) (cos phi2))
        (+ (sin phi2) (* -1.0 (* phi1 (* (cos phi2) t_1)))))
       (atan2 (* (- t_0 (sin lambda2)) (cos phi2)) t_2)))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin(lambda1) * cos(lambda2);
	double t_1 = cos((lambda1 - lambda2));
	double t_2 = (cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * t_1);
	double t_3 = cos(lambda1) * sin(lambda2);
	double tmp;
	if (phi1 <= -3.6e-5) {
		tmp = atan2(((sin(lambda1) - t_3) * cos(phi2)), t_2);
	} else if (phi1 <= 3.5e-5) {
		tmp = atan2(((t_0 - t_3) * cos(phi2)), (sin(phi2) + (-1.0 * (phi1 * (cos(phi2) * t_1)))));
	} else {
		tmp = atan2(((t_0 - sin(lambda2)) * cos(phi2)), t_2);
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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) :: t_2
    real(8) :: t_3
    real(8) :: tmp
    t_0 = sin(lambda1) * cos(lambda2)
    t_1 = cos((lambda1 - lambda2))
    t_2 = (cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * t_1)
    t_3 = cos(lambda1) * sin(lambda2)
    if (phi1 <= (-3.6d-5)) then
        tmp = atan2(((sin(lambda1) - t_3) * cos(phi2)), t_2)
    else if (phi1 <= 3.5d-5) then
        tmp = atan2(((t_0 - t_3) * cos(phi2)), (sin(phi2) + ((-1.0d0) * (phi1 * (cos(phi2) * t_1)))))
    else
        tmp = atan2(((t_0 - sin(lambda2)) * cos(phi2)), t_2)
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.sin(lambda1) * Math.cos(lambda2);
	double t_1 = Math.cos((lambda1 - lambda2));
	double t_2 = (Math.cos(phi1) * Math.sin(phi2)) - ((Math.sin(phi1) * Math.cos(phi2)) * t_1);
	double t_3 = Math.cos(lambda1) * Math.sin(lambda2);
	double tmp;
	if (phi1 <= -3.6e-5) {
		tmp = Math.atan2(((Math.sin(lambda1) - t_3) * Math.cos(phi2)), t_2);
	} else if (phi1 <= 3.5e-5) {
		tmp = Math.atan2(((t_0 - t_3) * Math.cos(phi2)), (Math.sin(phi2) + (-1.0 * (phi1 * (Math.cos(phi2) * t_1)))));
	} else {
		tmp = Math.atan2(((t_0 - Math.sin(lambda2)) * Math.cos(phi2)), t_2);
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.sin(lambda1) * math.cos(lambda2)
	t_1 = math.cos((lambda1 - lambda2))
	t_2 = (math.cos(phi1) * math.sin(phi2)) - ((math.sin(phi1) * math.cos(phi2)) * t_1)
	t_3 = math.cos(lambda1) * math.sin(lambda2)
	tmp = 0
	if phi1 <= -3.6e-5:
		tmp = math.atan2(((math.sin(lambda1) - t_3) * math.cos(phi2)), t_2)
	elif phi1 <= 3.5e-5:
		tmp = math.atan2(((t_0 - t_3) * math.cos(phi2)), (math.sin(phi2) + (-1.0 * (phi1 * (math.cos(phi2) * t_1)))))
	else:
		tmp = math.atan2(((t_0 - math.sin(lambda2)) * math.cos(phi2)), t_2)
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = Float64(sin(lambda1) * cos(lambda2))
	t_1 = cos(Float64(lambda1 - lambda2))
	t_2 = Float64(Float64(cos(phi1) * sin(phi2)) - Float64(Float64(sin(phi1) * cos(phi2)) * t_1))
	t_3 = Float64(cos(lambda1) * sin(lambda2))
	tmp = 0.0
	if (phi1 <= -3.6e-5)
		tmp = atan(Float64(Float64(sin(lambda1) - t_3) * cos(phi2)), t_2);
	elseif (phi1 <= 3.5e-5)
		tmp = atan(Float64(Float64(t_0 - t_3) * cos(phi2)), Float64(sin(phi2) + Float64(-1.0 * Float64(phi1 * Float64(cos(phi2) * t_1)))));
	else
		tmp = atan(Float64(Float64(t_0 - sin(lambda2)) * cos(phi2)), t_2);
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = sin(lambda1) * cos(lambda2);
	t_1 = cos((lambda1 - lambda2));
	t_2 = (cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * t_1);
	t_3 = cos(lambda1) * sin(lambda2);
	tmp = 0.0;
	if (phi1 <= -3.6e-5)
		tmp = atan2(((sin(lambda1) - t_3) * cos(phi2)), t_2);
	elseif (phi1 <= 3.5e-5)
		tmp = atan2(((t_0 - t_3) * cos(phi2)), (sin(phi2) + (-1.0 * (phi1 * (cos(phi2) * t_1)))));
	else
		tmp = atan2(((t_0 - sin(lambda2)) * cos(phi2)), t_2);
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -3.6e-5], N[ArcTan[N[(N[(N[Sin[lambda1], $MachinePrecision] - t$95$3), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / t$95$2], $MachinePrecision], If[LessEqual[phi1, 3.5e-5], N[ArcTan[N[(N[(t$95$0 - t$95$3), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[Sin[phi2], $MachinePrecision] + N[(-1.0 * N[(phi1 * N[(N[Cos[phi2], $MachinePrecision] * t$95$1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[ArcTan[N[(N[(t$95$0 - N[Sin[lambda2], $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / t$95$2], $MachinePrecision]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \lambda_1 \cdot \cos \lambda_2\\
t_1 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_2 := \cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot t\_1\\
t_3 := \cos \lambda_1 \cdot \sin \lambda_2\\
\mathbf{if}\;\phi_1 \leq -3.6 \cdot 10^{-5}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 - t\_3\right) \cdot \cos \phi_2}{t\_2}\\

\mathbf{elif}\;\phi_1 \leq 3.5 \cdot 10^{-5}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(t\_0 - t\_3\right) \cdot \cos \phi_2}{\sin \phi_2 + -1 \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot t\_1\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(t\_0 - \sin \lambda_2\right) \cdot \cos \phi_2}{t\_2}\\


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

    1. Initial program 77.5%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites79.8%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\color{blue}{\sin \lambda_1} - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    5. Applied rewrites78.0%

      \[\leadsto \tan^{-1}_* \frac{\left(\color{blue}{\sin \lambda_1} - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]

    if -3.60000000000000009e-5 < phi1 < 3.4999999999999997e-5

    1. Initial program 72.1%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites98.5%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2 + -1 \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)}} \]
    5. Applied rewrites98.5%

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

    if 3.4999999999999997e-5 < phi1

    1. Initial program 75.5%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites78.6%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Taylor expanded in lambda1 around 0

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \color{blue}{\sin \lambda_2}\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    5. Applied rewrites76.4%

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

Alternative 9: 88.8% accurate, 0.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \cos \lambda_1 \cdot \sin \lambda_2\\ t_2 := \tan^{-1}_* \frac{\left(\sin \lambda_1 - t\_1\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot t\_0}\\ \mathbf{if}\;\phi_1 \leq -3.6 \cdot 10^{-5}:\\ \;\;\;\;t\_2\\ \mathbf{elif}\;\phi_1 \leq 4 \cdot 10^{-5}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - t\_1\right) \cdot \cos \phi_2}{\sin \phi_2 + -1 \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot t\_0\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_2\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2)))
        (t_1 (* (cos lambda1) (sin lambda2)))
        (t_2
         (atan2
          (* (- (sin lambda1) t_1) (cos phi2))
          (- (* (cos phi1) (sin phi2)) (* (* (sin phi1) (cos phi2)) t_0)))))
   (if (<= phi1 -3.6e-5)
     t_2
     (if (<= phi1 4e-5)
       (atan2
        (* (- (* (sin lambda1) (cos lambda2)) t_1) (cos phi2))
        (+ (sin phi2) (* -1.0 (* phi1 (* (cos phi2) t_0)))))
       t_2))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = cos(lambda1) * sin(lambda2);
	double t_2 = atan2(((sin(lambda1) - t_1) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * t_0)));
	double tmp;
	if (phi1 <= -3.6e-5) {
		tmp = t_2;
	} else if (phi1 <= 4e-5) {
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - t_1) * cos(phi2)), (sin(phi2) + (-1.0 * (phi1 * (cos(phi2) * t_0)))));
	} else {
		tmp = t_2;
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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) :: t_2
    real(8) :: tmp
    t_0 = cos((lambda1 - lambda2))
    t_1 = cos(lambda1) * sin(lambda2)
    t_2 = atan2(((sin(lambda1) - t_1) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * t_0)))
    if (phi1 <= (-3.6d-5)) then
        tmp = t_2
    else if (phi1 <= 4d-5) then
        tmp = atan2((((sin(lambda1) * cos(lambda2)) - t_1) * cos(phi2)), (sin(phi2) + ((-1.0d0) * (phi1 * (cos(phi2) * t_0)))))
    else
        tmp = t_2
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2));
	double t_1 = Math.cos(lambda1) * Math.sin(lambda2);
	double t_2 = Math.atan2(((Math.sin(lambda1) - t_1) * Math.cos(phi2)), ((Math.cos(phi1) * Math.sin(phi2)) - ((Math.sin(phi1) * Math.cos(phi2)) * t_0)));
	double tmp;
	if (phi1 <= -3.6e-5) {
		tmp = t_2;
	} else if (phi1 <= 4e-5) {
		tmp = Math.atan2((((Math.sin(lambda1) * Math.cos(lambda2)) - t_1) * Math.cos(phi2)), (Math.sin(phi2) + (-1.0 * (phi1 * (Math.cos(phi2) * t_0)))));
	} else {
		tmp = t_2;
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	t_1 = math.cos(lambda1) * math.sin(lambda2)
	t_2 = math.atan2(((math.sin(lambda1) - t_1) * math.cos(phi2)), ((math.cos(phi1) * math.sin(phi2)) - ((math.sin(phi1) * math.cos(phi2)) * t_0)))
	tmp = 0
	if phi1 <= -3.6e-5:
		tmp = t_2
	elif phi1 <= 4e-5:
		tmp = math.atan2((((math.sin(lambda1) * math.cos(lambda2)) - t_1) * math.cos(phi2)), (math.sin(phi2) + (-1.0 * (phi1 * (math.cos(phi2) * t_0)))))
	else:
		tmp = t_2
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(cos(lambda1) * sin(lambda2))
	t_2 = atan(Float64(Float64(sin(lambda1) - t_1) * cos(phi2)), Float64(Float64(cos(phi1) * sin(phi2)) - Float64(Float64(sin(phi1) * cos(phi2)) * t_0)))
	tmp = 0.0
	if (phi1 <= -3.6e-5)
		tmp = t_2;
	elseif (phi1 <= 4e-5)
		tmp = atan(Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - t_1) * cos(phi2)), Float64(sin(phi2) + Float64(-1.0 * Float64(phi1 * Float64(cos(phi2) * t_0)))));
	else
		tmp = t_2;
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda1 - lambda2));
	t_1 = cos(lambda1) * sin(lambda2);
	t_2 = atan2(((sin(lambda1) - t_1) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * t_0)));
	tmp = 0.0;
	if (phi1 <= -3.6e-5)
		tmp = t_2;
	elseif (phi1 <= 4e-5)
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - t_1) * cos(phi2)), (sin(phi2) + (-1.0 * (phi1 * (cos(phi2) * t_0)))));
	else
		tmp = t_2;
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[ArcTan[N[(N[(N[Sin[lambda1], $MachinePrecision] - t$95$1), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi1, -3.6e-5], t$95$2, If[LessEqual[phi1, 4e-5], N[ArcTan[N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - t$95$1), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[Sin[phi2], $MachinePrecision] + N[(-1.0 * N[(phi1 * N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$2]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \cos \lambda_1 \cdot \sin \lambda_2\\
t_2 := \tan^{-1}_* \frac{\left(\sin \lambda_1 - t\_1\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot t\_0}\\
\mathbf{if}\;\phi_1 \leq -3.6 \cdot 10^{-5}:\\
\;\;\;\;t\_2\\

\mathbf{elif}\;\phi_1 \leq 4 \cdot 10^{-5}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - t\_1\right) \cdot \cos \phi_2}{\sin \phi_2 + -1 \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot t\_0\right)\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -3.60000000000000009e-5 or 4.00000000000000033e-5 < phi1

    1. Initial program 76.6%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites79.3%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\color{blue}{\sin \lambda_1} - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    5. Applied rewrites77.0%

      \[\leadsto \tan^{-1}_* \frac{\left(\color{blue}{\sin \lambda_1} - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]

    if -3.60000000000000009e-5 < phi1 < 4.00000000000000033e-5

    1. Initial program 72.1%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites98.5%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2 + -1 \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)}} \]
    5. Applied rewrites98.5%

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

Alternative 10: 88.3% accurate, 0.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot t\_0}\\ \mathbf{if}\;\phi_1 \leq -3.6 \cdot 10^{-5}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;\phi_1 \leq 4 \cdot 10^{-5}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2 + -1 \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot t\_0\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2)))
        (t_1
         (atan2
          (* (sin (- lambda1 lambda2)) (cos phi2))
          (- (* (cos phi1) (sin phi2)) (* (* (sin phi1) (cos phi2)) t_0)))))
   (if (<= phi1 -3.6e-5)
     t_1
     (if (<= phi1 4e-5)
       (atan2
        (*
         (- (* (sin lambda1) (cos lambda2)) (* (cos lambda1) (sin lambda2)))
         (cos phi2))
        (+ (sin phi2) (* -1.0 (* phi1 (* (cos phi2) t_0)))))
       t_1))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * t_0)));
	double tmp;
	if (phi1 <= -3.6e-5) {
		tmp = t_1;
	} else if (phi1 <= 4e-5) {
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), (sin(phi2) + (-1.0 * (phi1 * (cos(phi2) * t_0)))));
	} else {
		tmp = t_1;
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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 = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * t_0)))
    if (phi1 <= (-3.6d-5)) then
        tmp = t_1
    else if (phi1 <= 4d-5) then
        tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), (sin(phi2) + ((-1.0d0) * (phi1 * (cos(phi2) * t_0)))))
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2));
	double t_1 = Math.atan2((Math.sin((lambda1 - lambda2)) * Math.cos(phi2)), ((Math.cos(phi1) * Math.sin(phi2)) - ((Math.sin(phi1) * Math.cos(phi2)) * t_0)));
	double tmp;
	if (phi1 <= -3.6e-5) {
		tmp = t_1;
	} else if (phi1 <= 4e-5) {
		tmp = Math.atan2((((Math.sin(lambda1) * Math.cos(lambda2)) - (Math.cos(lambda1) * Math.sin(lambda2))) * Math.cos(phi2)), (Math.sin(phi2) + (-1.0 * (phi1 * (Math.cos(phi2) * t_0)))));
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	t_1 = math.atan2((math.sin((lambda1 - lambda2)) * math.cos(phi2)), ((math.cos(phi1) * math.sin(phi2)) - ((math.sin(phi1) * math.cos(phi2)) * t_0)))
	tmp = 0
	if phi1 <= -3.6e-5:
		tmp = t_1
	elif phi1 <= 4e-5:
		tmp = math.atan2((((math.sin(lambda1) * math.cos(lambda2)) - (math.cos(lambda1) * math.sin(lambda2))) * math.cos(phi2)), (math.sin(phi2) + (-1.0 * (phi1 * (math.cos(phi2) * t_0)))))
	else:
		tmp = t_1
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = atan(Float64(sin(Float64(lambda1 - lambda2)) * cos(phi2)), Float64(Float64(cos(phi1) * sin(phi2)) - Float64(Float64(sin(phi1) * cos(phi2)) * t_0)))
	tmp = 0.0
	if (phi1 <= -3.6e-5)
		tmp = t_1;
	elseif (phi1 <= 4e-5)
		tmp = atan(Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2)), Float64(sin(phi2) + Float64(-1.0 * Float64(phi1 * Float64(cos(phi2) * t_0)))));
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda1 - lambda2));
	t_1 = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * t_0)));
	tmp = 0.0;
	if (phi1 <= -3.6e-5)
		tmp = t_1;
	elseif (phi1 <= 4e-5)
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), (sin(phi2) + (-1.0 * (phi1 * (cos(phi2) * t_0)))));
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[ArcTan[N[(N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi1, -3.6e-5], t$95$1, If[LessEqual[phi1, 4e-5], N[ArcTan[N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[Sin[phi2], $MachinePrecision] + N[(-1.0 * N[(phi1 * N[(N[Cos[phi2], $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot t\_0}\\
\mathbf{if}\;\phi_1 \leq -3.6 \cdot 10^{-5}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;\phi_1 \leq 4 \cdot 10^{-5}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2 + -1 \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot t\_0\right)\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -3.60000000000000009e-5 or 4.00000000000000033e-5 < phi1

    1. Initial program 76.6%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing

    if -3.60000000000000009e-5 < phi1 < 4.00000000000000033e-5

    1. Initial program 72.1%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites98.5%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2 + -1 \cdot \left(\phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right)}} \]
    5. Applied rewrites98.5%

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

Alternative 11: 86.8% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\\ \mathbf{if}\;\phi_1 \leq -8.8 \cdot 10^{-7}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\phi_1 \leq 3.4 \cdot 10^{-5}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (atan2
          (* (sin (- lambda1 lambda2)) (cos phi2))
          (-
           (* (cos phi1) (sin phi2))
           (* (* (sin phi1) (cos phi2)) (cos (- lambda1 lambda2)))))))
   (if (<= phi1 -8.8e-7)
     t_0
     (if (<= phi1 3.4e-5)
       (atan2
        (*
         (- (* (sin lambda1) (cos lambda2)) (* (cos lambda1) (sin lambda2)))
         (cos phi2))
        (sin phi2))
       t_0))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	double tmp;
	if (phi1 <= -8.8e-7) {
		tmp = t_0;
	} else if (phi1 <= 3.4e-5) {
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	} else {
		tmp = t_0;
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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 = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))))
    if (phi1 <= (-8.8d-7)) then
        tmp = t_0
    else if (phi1 <= 3.4d-5) then
        tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2))
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.atan2((Math.sin((lambda1 - lambda2)) * Math.cos(phi2)), ((Math.cos(phi1) * Math.sin(phi2)) - ((Math.sin(phi1) * Math.cos(phi2)) * Math.cos((lambda1 - lambda2)))));
	double tmp;
	if (phi1 <= -8.8e-7) {
		tmp = t_0;
	} else if (phi1 <= 3.4e-5) {
		tmp = Math.atan2((((Math.sin(lambda1) * Math.cos(lambda2)) - (Math.cos(lambda1) * Math.sin(lambda2))) * Math.cos(phi2)), Math.sin(phi2));
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.atan2((math.sin((lambda1 - lambda2)) * math.cos(phi2)), ((math.cos(phi1) * math.sin(phi2)) - ((math.sin(phi1) * math.cos(phi2)) * math.cos((lambda1 - lambda2)))))
	tmp = 0
	if phi1 <= -8.8e-7:
		tmp = t_0
	elif phi1 <= 3.4e-5:
		tmp = math.atan2((((math.sin(lambda1) * math.cos(lambda2)) - (math.cos(lambda1) * math.sin(lambda2))) * math.cos(phi2)), math.sin(phi2))
	else:
		tmp = t_0
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = atan(Float64(sin(Float64(lambda1 - lambda2)) * cos(phi2)), Float64(Float64(cos(phi1) * sin(phi2)) - Float64(Float64(sin(phi1) * cos(phi2)) * cos(Float64(lambda1 - lambda2)))))
	tmp = 0.0
	if (phi1 <= -8.8e-7)
		tmp = t_0;
	elseif (phi1 <= 3.4e-5)
		tmp = atan(Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - ((sin(phi1) * cos(phi2)) * cos((lambda1 - lambda2)))));
	tmp = 0.0;
	if (phi1 <= -8.8e-7)
		tmp = t_0;
	elseif (phi1 <= 3.4e-5)
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[ArcTan[N[(N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi1, -8.8e-7], t$95$0, If[LessEqual[phi1, 3.4e-5], N[ArcTan[N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)}\\
\mathbf{if}\;\phi_1 \leq -8.8 \cdot 10^{-7}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\phi_1 \leq 3.4 \cdot 10^{-5}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi1 < -8.8000000000000004e-7 or 3.4e-5 < phi1

    1. Initial program 76.6%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing

    if -8.8000000000000004e-7 < phi1 < 3.4e-5

    1. Initial program 72.1%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites98.5%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Applied rewrites99.7%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)}} \]
    5. Applied rewrites99.7%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    7. Applied rewrites97.0%

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

Alternative 12: 79.1% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\ \mathbf{if}\;\lambda_1 \leq -1.4 \cdot 10^{-11}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\lambda_1 \leq 3.05 \cdot 10^{-15}:\\ \;\;\;\;\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \sin \phi_1\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0
         (atan2
          (*
           (- (* (sin lambda1) (cos lambda2)) (* (cos lambda1) (sin lambda2)))
           (cos phi2))
          (sin phi2))))
   (if (<= lambda1 -1.4e-11)
     t_0
     (if (<= lambda1 3.05e-15)
       (atan2
        (* (sin (- lambda1 lambda2)) (cos phi2))
        (-
         (* (cos phi1) (sin phi2))
         (* (cos phi2) (* (cos lambda2) (sin phi1)))))
       t_0))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	double tmp;
	if (lambda1 <= -1.4e-11) {
		tmp = t_0;
	} else if (lambda1 <= 3.05e-15) {
		tmp = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - (cos(phi2) * (cos(lambda2) * sin(phi1)))));
	} else {
		tmp = t_0;
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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 = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2))
    if (lambda1 <= (-1.4d-11)) then
        tmp = t_0
    else if (lambda1 <= 3.05d-15) then
        tmp = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - (cos(phi2) * (cos(lambda2) * sin(phi1)))))
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.atan2((((Math.sin(lambda1) * Math.cos(lambda2)) - (Math.cos(lambda1) * Math.sin(lambda2))) * Math.cos(phi2)), Math.sin(phi2));
	double tmp;
	if (lambda1 <= -1.4e-11) {
		tmp = t_0;
	} else if (lambda1 <= 3.05e-15) {
		tmp = Math.atan2((Math.sin((lambda1 - lambda2)) * Math.cos(phi2)), ((Math.cos(phi1) * Math.sin(phi2)) - (Math.cos(phi2) * (Math.cos(lambda2) * Math.sin(phi1)))));
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.atan2((((math.sin(lambda1) * math.cos(lambda2)) - (math.cos(lambda1) * math.sin(lambda2))) * math.cos(phi2)), math.sin(phi2))
	tmp = 0
	if lambda1 <= -1.4e-11:
		tmp = t_0
	elif lambda1 <= 3.05e-15:
		tmp = math.atan2((math.sin((lambda1 - lambda2)) * math.cos(phi2)), ((math.cos(phi1) * math.sin(phi2)) - (math.cos(phi2) * (math.cos(lambda2) * math.sin(phi1)))))
	else:
		tmp = t_0
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = atan(Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2))
	tmp = 0.0
	if (lambda1 <= -1.4e-11)
		tmp = t_0;
	elseif (lambda1 <= 3.05e-15)
		tmp = atan(Float64(sin(Float64(lambda1 - lambda2)) * cos(phi2)), Float64(Float64(cos(phi1) * sin(phi2)) - Float64(cos(phi2) * Float64(cos(lambda2) * sin(phi1)))));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	tmp = 0.0;
	if (lambda1 <= -1.4e-11)
		tmp = t_0;
	elseif (lambda1 <= 3.05e-15)
		tmp = atan2((sin((lambda1 - lambda2)) * cos(phi2)), ((cos(phi1) * sin(phi2)) - (cos(phi2) * (cos(lambda2) * sin(phi1)))));
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[ArcTan[N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision]}, If[LessEqual[lambda1, -1.4e-11], t$95$0, If[LessEqual[lambda1, 3.05e-15], N[ArcTan[N[(N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[phi2], $MachinePrecision] * N[(N[Cos[lambda2], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\
\mathbf{if}\;\lambda_1 \leq -1.4 \cdot 10^{-11}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\lambda_1 \leq 3.05 \cdot 10^{-15}:\\
\;\;\;\;\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \cos \phi_2 \cdot \left(\cos \lambda_2 \cdot \sin \phi_1\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda1 < -1.4e-11 or 3.04999999999999986e-15 < lambda1

    1. Initial program 49.3%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites76.5%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Applied rewrites99.6%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)}} \]
    5. Applied rewrites99.6%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    7. Applied rewrites58.8%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]

    if -1.4e-11 < lambda1 < 3.04999999999999986e-15

    1. Initial program 99.7%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in lambda1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \color{blue}{\cos \phi_2 \cdot \left(\cos \left(\mathsf{neg}\left(\lambda_2\right)\right) \cdot \sin \phi_1\right)}} \]
    4. Applied rewrites99.8%

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

Alternative 13: 73.5% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\\ \mathbf{if}\;\phi_1 \leq -8.8 \cdot 10^{-7}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_1}{\sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot t\_0}\\ \mathbf{elif}\;\phi_1 \leq 3.4 \cdot 10^{-5}:\\ \;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_1}{\cos \phi_1 \cdot \sin \phi_2 - \sin \phi_1 \cdot t\_0}\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2)))
        (t_1 (* (sin (- lambda1 lambda2)) (cos phi2))))
   (if (<= phi1 -8.8e-7)
     (atan2 t_1 (- (sin phi2) (* (* (sin phi1) (cos phi2)) t_0)))
     (if (<= phi1 3.4e-5)
       (atan2
        (*
         (- (* (sin lambda1) (cos lambda2)) (* (cos lambda1) (sin lambda2)))
         (cos phi2))
        (sin phi2))
       (atan2 t_1 (- (* (cos phi1) (sin phi2)) (* (sin phi1) t_0)))))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = sin((lambda1 - lambda2)) * cos(phi2);
	double tmp;
	if (phi1 <= -8.8e-7) {
		tmp = atan2(t_1, (sin(phi2) - ((sin(phi1) * cos(phi2)) * t_0)));
	} else if (phi1 <= 3.4e-5) {
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	} else {
		tmp = atan2(t_1, ((cos(phi1) * sin(phi2)) - (sin(phi1) * t_0)));
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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((lambda1 - lambda2)) * cos(phi2)
    if (phi1 <= (-8.8d-7)) then
        tmp = atan2(t_1, (sin(phi2) - ((sin(phi1) * cos(phi2)) * t_0)))
    else if (phi1 <= 3.4d-5) then
        tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2))
    else
        tmp = atan2(t_1, ((cos(phi1) * sin(phi2)) - (sin(phi1) * t_0)))
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2));
	double t_1 = Math.sin((lambda1 - lambda2)) * Math.cos(phi2);
	double tmp;
	if (phi1 <= -8.8e-7) {
		tmp = Math.atan2(t_1, (Math.sin(phi2) - ((Math.sin(phi1) * Math.cos(phi2)) * t_0)));
	} else if (phi1 <= 3.4e-5) {
		tmp = Math.atan2((((Math.sin(lambda1) * Math.cos(lambda2)) - (Math.cos(lambda1) * Math.sin(lambda2))) * Math.cos(phi2)), Math.sin(phi2));
	} else {
		tmp = Math.atan2(t_1, ((Math.cos(phi1) * Math.sin(phi2)) - (Math.sin(phi1) * t_0)));
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	t_1 = math.sin((lambda1 - lambda2)) * math.cos(phi2)
	tmp = 0
	if phi1 <= -8.8e-7:
		tmp = math.atan2(t_1, (math.sin(phi2) - ((math.sin(phi1) * math.cos(phi2)) * t_0)))
	elif phi1 <= 3.4e-5:
		tmp = math.atan2((((math.sin(lambda1) * math.cos(lambda2)) - (math.cos(lambda1) * math.sin(lambda2))) * math.cos(phi2)), math.sin(phi2))
	else:
		tmp = math.atan2(t_1, ((math.cos(phi1) * math.sin(phi2)) - (math.sin(phi1) * t_0)))
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(sin(Float64(lambda1 - lambda2)) * cos(phi2))
	tmp = 0.0
	if (phi1 <= -8.8e-7)
		tmp = atan(t_1, Float64(sin(phi2) - Float64(Float64(sin(phi1) * cos(phi2)) * t_0)));
	elseif (phi1 <= 3.4e-5)
		tmp = atan(Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	else
		tmp = atan(t_1, Float64(Float64(cos(phi1) * sin(phi2)) - Float64(sin(phi1) * t_0)));
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda1 - lambda2));
	t_1 = sin((lambda1 - lambda2)) * cos(phi2);
	tmp = 0.0;
	if (phi1 <= -8.8e-7)
		tmp = atan2(t_1, (sin(phi2) - ((sin(phi1) * cos(phi2)) * t_0)));
	elseif (phi1 <= 3.4e-5)
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	else
		tmp = atan2(t_1, ((cos(phi1) * sin(phi2)) - (sin(phi1) * t_0)));
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -8.8e-7], N[ArcTan[t$95$1 / N[(N[Sin[phi2], $MachinePrecision] - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[phi1, 3.4e-5], N[ArcTan[N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision], N[ArcTan[t$95$1 / N[(N[(N[Cos[phi1], $MachinePrecision] * N[Sin[phi2], $MachinePrecision]), $MachinePrecision] - N[(N[Sin[phi1], $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\\
\mathbf{if}\;\phi_1 \leq -8.8 \cdot 10^{-7}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_1}{\sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot t\_0}\\

\mathbf{elif}\;\phi_1 \leq 3.4 \cdot 10^{-5}:\\
\;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\

\mathbf{else}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_1}{\cos \phi_1 \cdot \sin \phi_2 - \sin \phi_1 \cdot t\_0}\\


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

    1. Initial program 77.5%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2} - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Applied rewrites53.3%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2} - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]

    if -8.8000000000000004e-7 < phi1 < 3.4e-5

    1. Initial program 72.1%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites98.5%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Applied rewrites99.7%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)}} \]
    5. Applied rewrites99.7%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    7. Applied rewrites97.0%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]

    if 3.4e-5 < phi1

    1. Initial program 75.5%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \color{blue}{\sin \phi_1} \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Applied rewrites55.7%

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

Alternative 14: 72.8% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\ t_1 := \sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\\ \mathbf{if}\;\phi_1 \leq -8.8 \cdot 10^{-7}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_1}{\sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot t\_0}\\ \mathbf{elif}\;\phi_1 \leq 0.00024:\\ \;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_1}{-t\_0 \cdot \sin \phi_1}\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (cos (- lambda1 lambda2)))
        (t_1 (* (sin (- lambda1 lambda2)) (cos phi2))))
   (if (<= phi1 -8.8e-7)
     (atan2 t_1 (- (sin phi2) (* (* (sin phi1) (cos phi2)) t_0)))
     (if (<= phi1 0.00024)
       (atan2
        (*
         (- (* (sin lambda1) (cos lambda2)) (* (cos lambda1) (sin lambda2)))
         (cos phi2))
        (sin phi2))
       (atan2 t_1 (- (* t_0 (sin phi1))))))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2));
	double t_1 = sin((lambda1 - lambda2)) * cos(phi2);
	double tmp;
	if (phi1 <= -8.8e-7) {
		tmp = atan2(t_1, (sin(phi2) - ((sin(phi1) * cos(phi2)) * t_0)));
	} else if (phi1 <= 0.00024) {
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	} else {
		tmp = atan2(t_1, -(t_0 * sin(phi1)));
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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((lambda1 - lambda2)) * cos(phi2)
    if (phi1 <= (-8.8d-7)) then
        tmp = atan2(t_1, (sin(phi2) - ((sin(phi1) * cos(phi2)) * t_0)))
    else if (phi1 <= 0.00024d0) then
        tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2))
    else
        tmp = atan2(t_1, -(t_0 * sin(phi1)))
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2));
	double t_1 = Math.sin((lambda1 - lambda2)) * Math.cos(phi2);
	double tmp;
	if (phi1 <= -8.8e-7) {
		tmp = Math.atan2(t_1, (Math.sin(phi2) - ((Math.sin(phi1) * Math.cos(phi2)) * t_0)));
	} else if (phi1 <= 0.00024) {
		tmp = Math.atan2((((Math.sin(lambda1) * Math.cos(lambda2)) - (Math.cos(lambda1) * Math.sin(lambda2))) * Math.cos(phi2)), Math.sin(phi2));
	} else {
		tmp = Math.atan2(t_1, -(t_0 * Math.sin(phi1)));
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2))
	t_1 = math.sin((lambda1 - lambda2)) * math.cos(phi2)
	tmp = 0
	if phi1 <= -8.8e-7:
		tmp = math.atan2(t_1, (math.sin(phi2) - ((math.sin(phi1) * math.cos(phi2)) * t_0)))
	elif phi1 <= 0.00024:
		tmp = math.atan2((((math.sin(lambda1) * math.cos(lambda2)) - (math.cos(lambda1) * math.sin(lambda2))) * math.cos(phi2)), math.sin(phi2))
	else:
		tmp = math.atan2(t_1, -(t_0 * math.sin(phi1)))
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = cos(Float64(lambda1 - lambda2))
	t_1 = Float64(sin(Float64(lambda1 - lambda2)) * cos(phi2))
	tmp = 0.0
	if (phi1 <= -8.8e-7)
		tmp = atan(t_1, Float64(sin(phi2) - Float64(Float64(sin(phi1) * cos(phi2)) * t_0)));
	elseif (phi1 <= 0.00024)
		tmp = atan(Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	else
		tmp = atan(t_1, Float64(-Float64(t_0 * sin(phi1))));
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda1 - lambda2));
	t_1 = sin((lambda1 - lambda2)) * cos(phi2);
	tmp = 0.0;
	if (phi1 <= -8.8e-7)
		tmp = atan2(t_1, (sin(phi2) - ((sin(phi1) * cos(phi2)) * t_0)));
	elseif (phi1 <= 0.00024)
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	else
		tmp = atan2(t_1, -(t_0 * sin(phi1)));
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -8.8e-7], N[ArcTan[t$95$1 / N[(N[Sin[phi2], $MachinePrecision] - N[(N[(N[Sin[phi1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] * t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], If[LessEqual[phi1, 0.00024], N[ArcTan[N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision], N[ArcTan[t$95$1 / (-N[(t$95$0 * N[Sin[phi1], $MachinePrecision]), $MachinePrecision])], $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right)\\
t_1 := \sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\\
\mathbf{if}\;\phi_1 \leq -8.8 \cdot 10^{-7}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_1}{\sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot t\_0}\\

\mathbf{elif}\;\phi_1 \leq 0.00024:\\
\;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\

\mathbf{else}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_1}{-t\_0 \cdot \sin \phi_1}\\


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

    1. Initial program 77.5%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2} - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Applied rewrites53.3%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2} - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]

    if -8.8000000000000004e-7 < phi1 < 2.40000000000000006e-4

    1. Initial program 72.1%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites98.5%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Applied rewrites99.7%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)}} \]
    5. Applied rewrites99.7%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    7. Applied rewrites97.0%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]

    if 2.40000000000000006e-4 < phi1

    1. Initial program 75.5%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{-1 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1\right)}} \]
    4. Applied rewrites54.3%

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

Alternative 15: 71.0% accurate, 1.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1\\ t_1 := \sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\\ \mathbf{if}\;\phi_1 \leq -3.8 \cdot 10^{+19}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_1}{\phi_2 \cdot \cos \phi_1 - t\_0}\\ \mathbf{elif}\;\phi_1 \leq 0.00024:\\ \;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_1}{-t\_0}\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (cos (- lambda1 lambda2)) (sin phi1)))
        (t_1 (* (sin (- lambda1 lambda2)) (cos phi2))))
   (if (<= phi1 -3.8e+19)
     (atan2 t_1 (- (* phi2 (cos phi1)) t_0))
     (if (<= phi1 0.00024)
       (atan2
        (*
         (- (* (sin lambda1) (cos lambda2)) (* (cos lambda1) (sin lambda2)))
         (cos phi2))
        (sin phi2))
       (atan2 t_1 (- t_0))))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = cos((lambda1 - lambda2)) * sin(phi1);
	double t_1 = sin((lambda1 - lambda2)) * cos(phi2);
	double tmp;
	if (phi1 <= -3.8e+19) {
		tmp = atan2(t_1, ((phi2 * cos(phi1)) - t_0));
	} else if (phi1 <= 0.00024) {
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	} else {
		tmp = atan2(t_1, -t_0);
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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)) * sin(phi1)
    t_1 = sin((lambda1 - lambda2)) * cos(phi2)
    if (phi1 <= (-3.8d+19)) then
        tmp = atan2(t_1, ((phi2 * cos(phi1)) - t_0))
    else if (phi1 <= 0.00024d0) then
        tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2))
    else
        tmp = atan2(t_1, -t_0)
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.cos((lambda1 - lambda2)) * Math.sin(phi1);
	double t_1 = Math.sin((lambda1 - lambda2)) * Math.cos(phi2);
	double tmp;
	if (phi1 <= -3.8e+19) {
		tmp = Math.atan2(t_1, ((phi2 * Math.cos(phi1)) - t_0));
	} else if (phi1 <= 0.00024) {
		tmp = Math.atan2((((Math.sin(lambda1) * Math.cos(lambda2)) - (Math.cos(lambda1) * Math.sin(lambda2))) * Math.cos(phi2)), Math.sin(phi2));
	} else {
		tmp = Math.atan2(t_1, -t_0);
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.cos((lambda1 - lambda2)) * math.sin(phi1)
	t_1 = math.sin((lambda1 - lambda2)) * math.cos(phi2)
	tmp = 0
	if phi1 <= -3.8e+19:
		tmp = math.atan2(t_1, ((phi2 * math.cos(phi1)) - t_0))
	elif phi1 <= 0.00024:
		tmp = math.atan2((((math.sin(lambda1) * math.cos(lambda2)) - (math.cos(lambda1) * math.sin(lambda2))) * math.cos(phi2)), math.sin(phi2))
	else:
		tmp = math.atan2(t_1, -t_0)
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = Float64(cos(Float64(lambda1 - lambda2)) * sin(phi1))
	t_1 = Float64(sin(Float64(lambda1 - lambda2)) * cos(phi2))
	tmp = 0.0
	if (phi1 <= -3.8e+19)
		tmp = atan(t_1, Float64(Float64(phi2 * cos(phi1)) - t_0));
	elseif (phi1 <= 0.00024)
		tmp = atan(Float64(Float64(Float64(sin(lambda1) * cos(lambda2)) - Float64(cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	else
		tmp = atan(t_1, Float64(-t_0));
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = cos((lambda1 - lambda2)) * sin(phi1);
	t_1 = sin((lambda1 - lambda2)) * cos(phi2);
	tmp = 0.0;
	if (phi1 <= -3.8e+19)
		tmp = atan2(t_1, ((phi2 * cos(phi1)) - t_0));
	elseif (phi1 <= 0.00024)
		tmp = atan2((((sin(lambda1) * cos(lambda2)) - (cos(lambda1) * sin(lambda2))) * cos(phi2)), sin(phi2));
	else
		tmp = atan2(t_1, -t_0);
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[phi1, -3.8e+19], N[ArcTan[t$95$1 / N[(N[(phi2 * N[Cos[phi1], $MachinePrecision]), $MachinePrecision] - t$95$0), $MachinePrecision]], $MachinePrecision], If[LessEqual[phi1, 0.00024], N[ArcTan[N[(N[(N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[lambda2], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[lambda1], $MachinePrecision] * N[Sin[lambda2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision], N[ArcTan[t$95$1 / (-t$95$0)], $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1\\
t_1 := \sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\\
\mathbf{if}\;\phi_1 \leq -3.8 \cdot 10^{+19}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_1}{\phi_2 \cdot \cos \phi_1 - t\_0}\\

\mathbf{elif}\;\phi_1 \leq 0.00024:\\
\;\;\;\;\tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\

\mathbf{else}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_1}{-t\_0}\\


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

    1. Initial program 77.2%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\phi_2 \cdot \cos \phi_1 - \cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1}} \]
    4. Applied rewrites50.1%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\phi_2 \cdot \cos \phi_1 - \cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1}} \]

    if -3.8e19 < phi1 < 2.40000000000000006e-4

    1. Initial program 72.3%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Applied rewrites98.5%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right)} \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    4. Applied rewrites99.7%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \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)}} \]
    5. Applied rewrites99.7%

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

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    7. Applied rewrites96.4%

      \[\leadsto \tan^{-1}_* \frac{\left(\sin \lambda_1 \cdot \cos \lambda_2 - \cos \lambda_1 \cdot \sin \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]

    if 2.40000000000000006e-4 < phi1

    1. Initial program 75.5%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{-1 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1\right)}} \]
    4. Applied rewrites54.3%

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

Alternative 16: 65.2% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2 - \sin \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (atan2
  (* (sin (- lambda1 lambda2)) (cos phi2))
  (- (sin phi2) (* (sin phi1) (cos (- lambda1 lambda2))))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	return atan2((sin((lambda1 - lambda2)) * cos(phi2)), (sin(phi2) - (sin(phi1) * cos((lambda1 - lambda2)))));
}
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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = atan2((sin((lambda1 - lambda2)) * cos(phi2)), (sin(phi2) - (sin(phi1) * cos((lambda1 - lambda2)))))
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	return Math.atan2((Math.sin((lambda1 - lambda2)) * Math.cos(phi2)), (Math.sin(phi2) - (Math.sin(phi1) * Math.cos((lambda1 - lambda2)))));
}
def code(lambda1, lambda2, phi1, phi2):
	return math.atan2((math.sin((lambda1 - lambda2)) * math.cos(phi2)), (math.sin(phi2) - (math.sin(phi1) * math.cos((lambda1 - lambda2)))))
function code(lambda1, lambda2, phi1, phi2)
	return atan(Float64(sin(Float64(lambda1 - lambda2)) * cos(phi2)), Float64(sin(phi2) - Float64(sin(phi1) * cos(Float64(lambda1 - lambda2)))))
end
function tmp = code(lambda1, lambda2, phi1, phi2)
	tmp = atan2((sin((lambda1 - lambda2)) * cos(phi2)), (sin(phi2) - (sin(phi1) * cos((lambda1 - lambda2)))));
end
code[lambda1_, lambda2_, phi1_, phi2_] := N[ArcTan[N[(N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[(N[Sin[phi2], $MachinePrecision] - N[(N[Sin[phi1], $MachinePrecision] * N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2 - \sin \phi_1 \cdot \cos \left(\lambda_1 - \lambda_2\right)}
\end{array}
Derivation
  1. Initial program 74.5%

    \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in phi1 around 0

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2} - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  4. Applied rewrites61.8%

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2} - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  5. Taylor expanded in phi2 around 0

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2 - \color{blue}{\sin \phi_1} \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  6. Applied rewrites59.4%

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2 - \color{blue}{\sin \phi_1} \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  7. Add Preprocessing

Alternative 17: 64.8% accurate, 1.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\lambda_1 - \lambda_2\right)\\ t_1 := \tan^{-1}_* \frac{t\_0 \cdot \cos \phi_2}{\sin \phi_2}\\ \mathbf{if}\;\phi_2 \leq -0.0245:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;\phi_2 \leq 1.35 \cdot 10^{-23}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_0}{\phi_2 \cdot \cos \phi_1 - \cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1}\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (sin (- lambda1 lambda2)))
        (t_1 (atan2 (* t_0 (cos phi2)) (sin phi2))))
   (if (<= phi2 -0.0245)
     t_1
     (if (<= phi2 1.35e-23)
       (atan2
        t_0
        (- (* phi2 (cos phi1)) (* (cos (- lambda1 lambda2)) (sin phi1))))
       t_1))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin((lambda1 - lambda2));
	double t_1 = atan2((t_0 * cos(phi2)), sin(phi2));
	double tmp;
	if (phi2 <= -0.0245) {
		tmp = t_1;
	} else if (phi2 <= 1.35e-23) {
		tmp = atan2(t_0, ((phi2 * cos(phi1)) - (cos((lambda1 - lambda2)) * sin(phi1))));
	} else {
		tmp = t_1;
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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 = sin((lambda1 - lambda2))
    t_1 = atan2((t_0 * cos(phi2)), sin(phi2))
    if (phi2 <= (-0.0245d0)) then
        tmp = t_1
    else if (phi2 <= 1.35d-23) then
        tmp = atan2(t_0, ((phi2 * cos(phi1)) - (cos((lambda1 - lambda2)) * sin(phi1))))
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.sin((lambda1 - lambda2));
	double t_1 = Math.atan2((t_0 * Math.cos(phi2)), Math.sin(phi2));
	double tmp;
	if (phi2 <= -0.0245) {
		tmp = t_1;
	} else if (phi2 <= 1.35e-23) {
		tmp = Math.atan2(t_0, ((phi2 * Math.cos(phi1)) - (Math.cos((lambda1 - lambda2)) * Math.sin(phi1))));
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.sin((lambda1 - lambda2))
	t_1 = math.atan2((t_0 * math.cos(phi2)), math.sin(phi2))
	tmp = 0
	if phi2 <= -0.0245:
		tmp = t_1
	elif phi2 <= 1.35e-23:
		tmp = math.atan2(t_0, ((phi2 * math.cos(phi1)) - (math.cos((lambda1 - lambda2)) * math.sin(phi1))))
	else:
		tmp = t_1
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = sin(Float64(lambda1 - lambda2))
	t_1 = atan(Float64(t_0 * cos(phi2)), sin(phi2))
	tmp = 0.0
	if (phi2 <= -0.0245)
		tmp = t_1;
	elseif (phi2 <= 1.35e-23)
		tmp = atan(t_0, Float64(Float64(phi2 * cos(phi1)) - Float64(cos(Float64(lambda1 - lambda2)) * sin(phi1))));
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = sin((lambda1 - lambda2));
	t_1 = atan2((t_0 * cos(phi2)), sin(phi2));
	tmp = 0.0;
	if (phi2 <= -0.0245)
		tmp = t_1;
	elseif (phi2 <= 1.35e-23)
		tmp = atan2(t_0, ((phi2 * cos(phi1)) - (cos((lambda1 - lambda2)) * sin(phi1))));
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[ArcTan[N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, -0.0245], t$95$1, If[LessEqual[phi2, 1.35e-23], N[ArcTan[t$95$0 / N[(N[(phi2 * N[Cos[phi1], $MachinePrecision]), $MachinePrecision] - N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\lambda_1 - \lambda_2\right)\\
t_1 := \tan^{-1}_* \frac{t\_0 \cdot \cos \phi_2}{\sin \phi_2}\\
\mathbf{if}\;\phi_2 \leq -0.0245:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;\phi_2 \leq 1.35 \cdot 10^{-23}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_0}{\phi_2 \cdot \cos \phi_1 - \cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -0.024500000000000001 or 1.34999999999999992e-23 < phi2

    1. Initial program 71.2%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites44.6%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]

    if -0.024500000000000001 < phi2 < 1.34999999999999992e-23

    1. Initial program 78.0%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites41.4%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    6. Applied rewrites41.4%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    7. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\color{blue}{\phi_2 \cdot \cos \phi_1 - \cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1}} \]
    8. Applied rewrites78.0%

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

Alternative 18: 63.3% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\lambda_1 - \lambda_2\right)\\ t_1 := \tan^{-1}_* \frac{t\_0 \cdot \cos \phi_2}{\sin \phi_2}\\ \mathbf{if}\;\phi_2 \leq -2.5 \cdot 10^{-8}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;\phi_2 \leq 1.22 \cdot 10^{-23}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_0}{-1 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (sin (- lambda1 lambda2)))
        (t_1 (atan2 (* t_0 (cos phi2)) (sin phi2))))
   (if (<= phi2 -2.5e-8)
     t_1
     (if (<= phi2 1.22e-23)
       (atan2 t_0 (* -1.0 (* (cos (- lambda1 lambda2)) (sin phi1))))
       t_1))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin((lambda1 - lambda2));
	double t_1 = atan2((t_0 * cos(phi2)), sin(phi2));
	double tmp;
	if (phi2 <= -2.5e-8) {
		tmp = t_1;
	} else if (phi2 <= 1.22e-23) {
		tmp = atan2(t_0, (-1.0 * (cos((lambda1 - lambda2)) * sin(phi1))));
	} else {
		tmp = t_1;
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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 = sin((lambda1 - lambda2))
    t_1 = atan2((t_0 * cos(phi2)), sin(phi2))
    if (phi2 <= (-2.5d-8)) then
        tmp = t_1
    else if (phi2 <= 1.22d-23) then
        tmp = atan2(t_0, ((-1.0d0) * (cos((lambda1 - lambda2)) * sin(phi1))))
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.sin((lambda1 - lambda2));
	double t_1 = Math.atan2((t_0 * Math.cos(phi2)), Math.sin(phi2));
	double tmp;
	if (phi2 <= -2.5e-8) {
		tmp = t_1;
	} else if (phi2 <= 1.22e-23) {
		tmp = Math.atan2(t_0, (-1.0 * (Math.cos((lambda1 - lambda2)) * Math.sin(phi1))));
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.sin((lambda1 - lambda2))
	t_1 = math.atan2((t_0 * math.cos(phi2)), math.sin(phi2))
	tmp = 0
	if phi2 <= -2.5e-8:
		tmp = t_1
	elif phi2 <= 1.22e-23:
		tmp = math.atan2(t_0, (-1.0 * (math.cos((lambda1 - lambda2)) * math.sin(phi1))))
	else:
		tmp = t_1
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = sin(Float64(lambda1 - lambda2))
	t_1 = atan(Float64(t_0 * cos(phi2)), sin(phi2))
	tmp = 0.0
	if (phi2 <= -2.5e-8)
		tmp = t_1;
	elseif (phi2 <= 1.22e-23)
		tmp = atan(t_0, Float64(-1.0 * Float64(cos(Float64(lambda1 - lambda2)) * sin(phi1))));
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = sin((lambda1 - lambda2));
	t_1 = atan2((t_0 * cos(phi2)), sin(phi2));
	tmp = 0.0;
	if (phi2 <= -2.5e-8)
		tmp = t_1;
	elseif (phi2 <= 1.22e-23)
		tmp = atan2(t_0, (-1.0 * (cos((lambda1 - lambda2)) * sin(phi1))));
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[ArcTan[N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, -2.5e-8], t$95$1, If[LessEqual[phi2, 1.22e-23], N[ArcTan[t$95$0 / N[(-1.0 * N[(N[Cos[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Sin[phi1], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], t$95$1]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\lambda_1 - \lambda_2\right)\\
t_1 := \tan^{-1}_* \frac{t\_0 \cdot \cos \phi_2}{\sin \phi_2}\\
\mathbf{if}\;\phi_2 \leq -2.5 \cdot 10^{-8}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;\phi_2 \leq 1.22 \cdot 10^{-23}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_0}{-1 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if phi2 < -2.4999999999999999e-8 or 1.22000000000000007e-23 < phi2

    1. Initial program 71.4%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites45.1%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]

    if -2.4999999999999999e-8 < phi2 < 1.22000000000000007e-23

    1. Initial program 77.8%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites40.9%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    6. Applied rewrites40.9%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    7. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\color{blue}{-1 \cdot \left(\cos \left(\lambda_1 - \lambda_2\right) \cdot \sin \phi_1\right)}} \]
    8. Applied rewrites75.9%

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

Alternative 19: 43.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \tan^{-1}_* \frac{\sin \left(-\lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\ \mathbf{if}\;\lambda_2 \leq -3.6 \cdot 10^{-63}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\lambda_2 \leq 4.2 \cdot 10^{-45}:\\ \;\;\;\;\tan^{-1}_* \frac{\sin \lambda_1 \cdot \cos \phi_2}{\sin \phi_2}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (atan2 (* (sin (- lambda2)) (cos phi2)) (sin phi2))))
   (if (<= lambda2 -3.6e-63)
     t_0
     (if (<= lambda2 4.2e-45)
       (atan2 (* (sin lambda1) (cos phi2)) (sin phi2))
       t_0))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = atan2((sin(-lambda2) * cos(phi2)), sin(phi2));
	double tmp;
	if (lambda2 <= -3.6e-63) {
		tmp = t_0;
	} else if (lambda2 <= 4.2e-45) {
		tmp = atan2((sin(lambda1) * cos(phi2)), sin(phi2));
	} else {
		tmp = t_0;
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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 = atan2((sin(-lambda2) * cos(phi2)), sin(phi2))
    if (lambda2 <= (-3.6d-63)) then
        tmp = t_0
    else if (lambda2 <= 4.2d-45) then
        tmp = atan2((sin(lambda1) * cos(phi2)), sin(phi2))
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.atan2((Math.sin(-lambda2) * Math.cos(phi2)), Math.sin(phi2));
	double tmp;
	if (lambda2 <= -3.6e-63) {
		tmp = t_0;
	} else if (lambda2 <= 4.2e-45) {
		tmp = Math.atan2((Math.sin(lambda1) * Math.cos(phi2)), Math.sin(phi2));
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.atan2((math.sin(-lambda2) * math.cos(phi2)), math.sin(phi2))
	tmp = 0
	if lambda2 <= -3.6e-63:
		tmp = t_0
	elif lambda2 <= 4.2e-45:
		tmp = math.atan2((math.sin(lambda1) * math.cos(phi2)), math.sin(phi2))
	else:
		tmp = t_0
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = atan(Float64(sin(Float64(-lambda2)) * cos(phi2)), sin(phi2))
	tmp = 0.0
	if (lambda2 <= -3.6e-63)
		tmp = t_0;
	elseif (lambda2 <= 4.2e-45)
		tmp = atan(Float64(sin(lambda1) * cos(phi2)), sin(phi2));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = atan2((sin(-lambda2) * cos(phi2)), sin(phi2));
	tmp = 0.0;
	if (lambda2 <= -3.6e-63)
		tmp = t_0;
	elseif (lambda2 <= 4.2e-45)
		tmp = atan2((sin(lambda1) * cos(phi2)), sin(phi2));
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[ArcTan[N[(N[Sin[(-lambda2)], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision]}, If[LessEqual[lambda2, -3.6e-63], t$95$0, If[LessEqual[lambda2, 4.2e-45], N[ArcTan[N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision], t$95$0]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \tan^{-1}_* \frac{\sin \left(-\lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}\\
\mathbf{if}\;\lambda_2 \leq -3.6 \cdot 10^{-63}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\lambda_2 \leq 4.2 \cdot 10^{-45}:\\
\;\;\;\;\tan^{-1}_* \frac{\sin \lambda_1 \cdot \cos \phi_2}{\sin \phi_2}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if lambda2 < -3.60000000000000008e-63 or 4.1999999999999999e-45 < lambda2

    1. Initial program 58.0%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites35.3%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in lambda1 around 0

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\mathsf{neg}\left(\lambda_2\right)\right)} \cdot \cos \phi_2}{\sin \phi_2} \]
    6. Applied rewrites36.1%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(-\lambda_2\right)} \cdot \cos \phi_2}{\sin \phi_2} \]

    if -3.60000000000000008e-63 < lambda2 < 4.1999999999999999e-45

    1. Initial program 99.8%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites55.0%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in lambda1 around inf

      \[\leadsto \tan^{-1}_* \frac{\sin \color{blue}{\lambda_1} \cdot \cos \phi_2}{\sin \phi_2} \]
    6. Applied rewrites47.4%

      \[\leadsto \tan^{-1}_* \frac{\sin \color{blue}{\lambda_1} \cdot \cos \phi_2}{\sin \phi_2} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 20: 38.1% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\lambda_2 \leq -3.2 \cdot 10^{-7}:\\ \;\;\;\;\tan^{-1}_* \frac{\sin \left(-\lambda_2\right)}{\sin \phi_2}\\ \mathbf{elif}\;\lambda_2 \leq 7.5 \cdot 10^{-20}:\\ \;\;\;\;\tan^{-1}_* \frac{\sin \lambda_1 \cdot \cos \phi_2}{\sin \phi_2}\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1}_* \frac{\sin \left(\lambda_2 \cdot \left(\frac{\lambda_1}{\lambda_2} - 1\right)\right)}{\sin \phi_2}\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= lambda2 -3.2e-7)
   (atan2 (sin (- lambda2)) (sin phi2))
   (if (<= lambda2 7.5e-20)
     (atan2 (* (sin lambda1) (cos phi2)) (sin phi2))
     (atan2 (sin (* lambda2 (- (/ lambda1 lambda2) 1.0))) (sin phi2)))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= -3.2e-7) {
		tmp = atan2(sin(-lambda2), sin(phi2));
	} else if (lambda2 <= 7.5e-20) {
		tmp = atan2((sin(lambda1) * cos(phi2)), sin(phi2));
	} else {
		tmp = atan2(sin((lambda2 * ((lambda1 / lambda2) - 1.0))), sin(phi2));
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (lambda2 <= (-3.2d-7)) then
        tmp = atan2(sin(-lambda2), sin(phi2))
    else if (lambda2 <= 7.5d-20) then
        tmp = atan2((sin(lambda1) * cos(phi2)), sin(phi2))
    else
        tmp = atan2(sin((lambda2 * ((lambda1 / lambda2) - 1.0d0))), sin(phi2))
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda2 <= -3.2e-7) {
		tmp = Math.atan2(Math.sin(-lambda2), Math.sin(phi2));
	} else if (lambda2 <= 7.5e-20) {
		tmp = Math.atan2((Math.sin(lambda1) * Math.cos(phi2)), Math.sin(phi2));
	} else {
		tmp = Math.atan2(Math.sin((lambda2 * ((lambda1 / lambda2) - 1.0))), Math.sin(phi2));
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda2 <= -3.2e-7:
		tmp = math.atan2(math.sin(-lambda2), math.sin(phi2))
	elif lambda2 <= 7.5e-20:
		tmp = math.atan2((math.sin(lambda1) * math.cos(phi2)), math.sin(phi2))
	else:
		tmp = math.atan2(math.sin((lambda2 * ((lambda1 / lambda2) - 1.0))), math.sin(phi2))
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda2 <= -3.2e-7)
		tmp = atan(sin(Float64(-lambda2)), sin(phi2));
	elseif (lambda2 <= 7.5e-20)
		tmp = atan(Float64(sin(lambda1) * cos(phi2)), sin(phi2));
	else
		tmp = atan(sin(Float64(lambda2 * Float64(Float64(lambda1 / lambda2) - 1.0))), sin(phi2));
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda2 <= -3.2e-7)
		tmp = atan2(sin(-lambda2), sin(phi2));
	elseif (lambda2 <= 7.5e-20)
		tmp = atan2((sin(lambda1) * cos(phi2)), sin(phi2));
	else
		tmp = atan2(sin((lambda2 * ((lambda1 / lambda2) - 1.0))), sin(phi2));
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[lambda2, -3.2e-7], N[ArcTan[N[Sin[(-lambda2)], $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision], If[LessEqual[lambda2, 7.5e-20], N[ArcTan[N[(N[Sin[lambda1], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision], N[ArcTan[N[Sin[N[(lambda2 * N[(N[(lambda1 / lambda2), $MachinePrecision] - 1.0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\lambda_2 \leq -3.2 \cdot 10^{-7}:\\
\;\;\;\;\tan^{-1}_* \frac{\sin \left(-\lambda_2\right)}{\sin \phi_2}\\

\mathbf{elif}\;\lambda_2 \leq 7.5 \cdot 10^{-20}:\\
\;\;\;\;\tan^{-1}_* \frac{\sin \lambda_1 \cdot \cos \phi_2}{\sin \phi_2}\\

\mathbf{else}:\\
\;\;\;\;\tan^{-1}_* \frac{\sin \left(\lambda_2 \cdot \left(\frac{\lambda_1}{\lambda_2} - 1\right)\right)}{\sin \phi_2}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if lambda2 < -3.2000000000000001e-7

    1. Initial program 49.9%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites29.9%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    6. Applied rewrites18.7%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    7. Taylor expanded in lambda1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\mathsf{neg}\left(\lambda_2\right)\right)}{\sin \phi_2} \]
    8. Applied rewrites23.2%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(-\lambda_2\right)}{\sin \phi_2} \]

    if -3.2000000000000001e-7 < lambda2 < 7.49999999999999981e-20

    1. Initial program 99.8%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites54.2%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in lambda1 around inf

      \[\leadsto \tan^{-1}_* \frac{\sin \color{blue}{\lambda_1} \cdot \cos \phi_2}{\sin \phi_2} \]
    6. Applied rewrites45.2%

      \[\leadsto \tan^{-1}_* \frac{\sin \color{blue}{\lambda_1} \cdot \cos \phi_2}{\sin \phi_2} \]

    if 7.49999999999999981e-20 < lambda2

    1. Initial program 60.8%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites39.4%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    6. Applied rewrites29.9%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    7. Taylor expanded in lambda2 around inf

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_2 \cdot \left(\frac{\lambda_1}{\lambda_2} - 1\right)\right)}{\sin \phi_2} \]
    8. Applied rewrites30.4%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_2 \cdot \left(\frac{\lambda_1}{\lambda_2} - 1\right)\right)}{\sin \phi_2} \]
  3. Recombined 3 regimes into one program.
  4. Add Preprocessing

Alternative 21: 48.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (atan2 (* (sin (- lambda1 lambda2)) (cos phi2)) (sin phi2)))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	return atan2((sin((lambda1 - lambda2)) * cos(phi2)), sin(phi2));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = atan2((sin((lambda1 - lambda2)) * cos(phi2)), sin(phi2))
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	return Math.atan2((Math.sin((lambda1 - lambda2)) * Math.cos(phi2)), Math.sin(phi2));
}
def code(lambda1, lambda2, phi1, phi2):
	return math.atan2((math.sin((lambda1 - lambda2)) * math.cos(phi2)), math.sin(phi2))
function code(lambda1, lambda2, phi1, phi2)
	return atan(Float64(sin(Float64(lambda1 - lambda2)) * cos(phi2)), sin(phi2))
end
function tmp = code(lambda1, lambda2, phi1, phi2)
	tmp = atan2((sin((lambda1 - lambda2)) * cos(phi2)), sin(phi2));
end
code[lambda1_, lambda2_, phi1_, phi2_] := N[ArcTan[N[(N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\sin \phi_2}
\end{array}
Derivation
  1. Initial program 74.5%

    \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in phi1 around 0

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
  4. Applied rewrites43.1%

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
  5. Add Preprocessing

Alternative 22: 34.4% accurate, 2.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq 4.9 \cdot 10^{+14}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_0 \cdot \cos \phi_2}{\phi_2}\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_0}{\phi_2 \cdot \left(1 + -0.16666666666666666 \cdot {\phi_2}^{2}\right)}\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (sin (- lambda1 lambda2))))
   (if (<= phi2 4.9e+14)
     (atan2 (* t_0 (cos phi2)) phi2)
     (atan2 t_0 (* phi2 (+ 1.0 (* -0.16666666666666666 (pow phi2 2.0))))))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin((lambda1 - lambda2));
	double tmp;
	if (phi2 <= 4.9e+14) {
		tmp = atan2((t_0 * cos(phi2)), phi2);
	} else {
		tmp = atan2(t_0, (phi2 * (1.0 + (-0.16666666666666666 * pow(phi2, 2.0)))));
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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 = sin((lambda1 - lambda2))
    if (phi2 <= 4.9d+14) then
        tmp = atan2((t_0 * cos(phi2)), phi2)
    else
        tmp = atan2(t_0, (phi2 * (1.0d0 + ((-0.16666666666666666d0) * (phi2 ** 2.0d0)))))
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.sin((lambda1 - lambda2));
	double tmp;
	if (phi2 <= 4.9e+14) {
		tmp = Math.atan2((t_0 * Math.cos(phi2)), phi2);
	} else {
		tmp = Math.atan2(t_0, (phi2 * (1.0 + (-0.16666666666666666 * Math.pow(phi2, 2.0)))));
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.sin((lambda1 - lambda2))
	tmp = 0
	if phi2 <= 4.9e+14:
		tmp = math.atan2((t_0 * math.cos(phi2)), phi2)
	else:
		tmp = math.atan2(t_0, (phi2 * (1.0 + (-0.16666666666666666 * math.pow(phi2, 2.0)))))
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = sin(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi2 <= 4.9e+14)
		tmp = atan(Float64(t_0 * cos(phi2)), phi2);
	else
		tmp = atan(t_0, Float64(phi2 * Float64(1.0 + Float64(-0.16666666666666666 * (phi2 ^ 2.0)))));
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = sin((lambda1 - lambda2));
	tmp = 0.0;
	if (phi2 <= 4.9e+14)
		tmp = atan2((t_0 * cos(phi2)), phi2);
	else
		tmp = atan2(t_0, (phi2 * (1.0 + (-0.16666666666666666 * (phi2 ^ 2.0)))));
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, 4.9e+14], N[ArcTan[N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / phi2], $MachinePrecision], N[ArcTan[t$95$0 / N[(phi2 * N[(1.0 + N[(-0.16666666666666666 * N[Power[phi2, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq 4.9 \cdot 10^{+14}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_0 \cdot \cos \phi_2}{\phi_2}\\

\mathbf{else}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_0}{\phi_2 \cdot \left(1 + -0.16666666666666666 \cdot {\phi_2}^{2}\right)}\\


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

    1. Initial program 76.2%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites45.0%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\phi_2} \]
    6. Applied rewrites35.7%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\phi_2} \]

    if 4.9e14 < phi2

    1. Initial program 69.3%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites37.2%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    6. Applied rewrites14.7%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    7. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2 \cdot \color{blue}{\left(1 + \frac{-1}{6} \cdot {\phi_2}^{2}\right)}} \]
    8. Applied rewrites16.3%

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

Alternative 23: 34.3% accurate, 2.6× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sin \left(\lambda_1 - \lambda_2\right)\\ \mathbf{if}\;\phi_2 \leq 6.2 \cdot 10^{+15}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_0 \cdot \cos \phi_2}{\phi_2}\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1}_* \frac{t\_0}{\sin \phi_2}\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (sin (- lambda1 lambda2))))
   (if (<= phi2 6.2e+15)
     (atan2 (* t_0 (cos phi2)) phi2)
     (atan2 t_0 (sin phi2)))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = sin((lambda1 - lambda2));
	double tmp;
	if (phi2 <= 6.2e+15) {
		tmp = atan2((t_0 * cos(phi2)), phi2);
	} else {
		tmp = atan2(t_0, sin(phi2));
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    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 = sin((lambda1 - lambda2))
    if (phi2 <= 6.2d+15) then
        tmp = atan2((t_0 * cos(phi2)), phi2)
    else
        tmp = atan2(t_0, sin(phi2))
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = Math.sin((lambda1 - lambda2));
	double tmp;
	if (phi2 <= 6.2e+15) {
		tmp = Math.atan2((t_0 * Math.cos(phi2)), phi2);
	} else {
		tmp = Math.atan2(t_0, Math.sin(phi2));
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	t_0 = math.sin((lambda1 - lambda2))
	tmp = 0
	if phi2 <= 6.2e+15:
		tmp = math.atan2((t_0 * math.cos(phi2)), phi2)
	else:
		tmp = math.atan2(t_0, math.sin(phi2))
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	t_0 = sin(Float64(lambda1 - lambda2))
	tmp = 0.0
	if (phi2 <= 6.2e+15)
		tmp = atan(Float64(t_0 * cos(phi2)), phi2);
	else
		tmp = atan(t_0, sin(phi2));
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	t_0 = sin((lambda1 - lambda2));
	tmp = 0.0;
	if (phi2 <= 6.2e+15)
		tmp = atan2((t_0 * cos(phi2)), phi2);
	else
		tmp = atan2(t_0, sin(phi2));
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[phi2, 6.2e+15], N[ArcTan[N[(t$95$0 * N[Cos[phi2], $MachinePrecision]), $MachinePrecision] / phi2], $MachinePrecision], N[ArcTan[t$95$0 / N[Sin[phi2], $MachinePrecision]], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sin \left(\lambda_1 - \lambda_2\right)\\
\mathbf{if}\;\phi_2 \leq 6.2 \cdot 10^{+15}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_0 \cdot \cos \phi_2}{\phi_2}\\

\mathbf{else}:\\
\;\;\;\;\tan^{-1}_* \frac{t\_0}{\sin \phi_2}\\


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

    1. Initial program 76.2%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites45.0%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\phi_2} \]
    6. Applied rewrites35.7%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\phi_2} \]

    if 6.2e15 < phi2

    1. Initial program 69.3%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites37.2%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    6. Applied rewrites14.7%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 24: 31.2% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.65 \cdot 10^{+16}:\\ \;\;\;\;\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2}\\ \mathbf{else}:\\ \;\;\;\;\tan^{-1}_* \frac{\sin \lambda_1}{\sin \phi_2}\\ \end{array} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (if (<= phi2 1.65e+16)
   (atan2 (sin (- lambda1 lambda2)) phi2)
   (atan2 (sin lambda1) (sin phi2))))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 1.65e+16) {
		tmp = atan2(sin((lambda1 - lambda2)), phi2);
	} else {
		tmp = atan2(sin(lambda1), sin(phi2));
	}
	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(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (phi2 <= 1.65d+16) then
        tmp = atan2(sin((lambda1 - lambda2)), phi2)
    else
        tmp = atan2(sin(lambda1), sin(phi2))
    end if
    code = tmp
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 1.65e+16) {
		tmp = Math.atan2(Math.sin((lambda1 - lambda2)), phi2);
	} else {
		tmp = Math.atan2(Math.sin(lambda1), Math.sin(phi2));
	}
	return tmp;
}
def code(lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 1.65e+16:
		tmp = math.atan2(math.sin((lambda1 - lambda2)), phi2)
	else:
		tmp = math.atan2(math.sin(lambda1), math.sin(phi2))
	return tmp
function code(lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 1.65e+16)
		tmp = atan(sin(Float64(lambda1 - lambda2)), phi2);
	else
		tmp = atan(sin(lambda1), sin(phi2));
	end
	return tmp
end
function tmp_2 = code(lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 1.65e+16)
		tmp = atan2(sin((lambda1 - lambda2)), phi2);
	else
		tmp = atan2(sin(lambda1), sin(phi2));
	end
	tmp_2 = tmp;
end
code[lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[phi2, 1.65e+16], N[ArcTan[N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] / phi2], $MachinePrecision], N[ArcTan[N[Sin[lambda1], $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\phi_2 \leq 1.65 \cdot 10^{+16}:\\
\;\;\;\;\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2}\\

\mathbf{else}:\\
\;\;\;\;\tan^{-1}_* \frac{\sin \lambda_1}{\sin \phi_2}\\


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

    1. Initial program 76.2%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites45.0%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    6. Applied rewrites32.3%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    7. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2} \]
    8. Applied rewrites32.0%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2} \]

    if 1.65e16 < phi2

    1. Initial program 69.3%

      \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in phi1 around 0

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    4. Applied rewrites37.2%

      \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
    5. Taylor expanded in phi2 around 0

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    6. Applied rewrites14.7%

      \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
    7. Taylor expanded in lambda1 around inf

      \[\leadsto \tan^{-1}_* \frac{\sin \lambda_1}{\sin \phi_2} \]
    8. Applied rewrites13.5%

      \[\leadsto \tan^{-1}_* \frac{\sin \lambda_1}{\sin \phi_2} \]
  3. Recombined 2 regimes into one program.
  4. Add Preprocessing

Alternative 25: 31.8% accurate, 2.7× speedup?

\[\begin{array}{l} \\ \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\sin \phi_2} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (atan2 (sin (- lambda1 lambda2)) (sin phi2)))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	return atan2(sin((lambda1 - lambda2)), sin(phi2));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = atan2(sin((lambda1 - lambda2)), sin(phi2))
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	return Math.atan2(Math.sin((lambda1 - lambda2)), Math.sin(phi2));
}
def code(lambda1, lambda2, phi1, phi2):
	return math.atan2(math.sin((lambda1 - lambda2)), math.sin(phi2))
function code(lambda1, lambda2, phi1, phi2)
	return atan(sin(Float64(lambda1 - lambda2)), sin(phi2))
end
function tmp = code(lambda1, lambda2, phi1, phi2)
	tmp = atan2(sin((lambda1 - lambda2)), sin(phi2));
end
code[lambda1_, lambda2_, phi1_, phi2_] := N[ArcTan[N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] / N[Sin[phi2], $MachinePrecision]], $MachinePrecision]
\begin{array}{l}

\\
\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\sin \phi_2}
\end{array}
Derivation
  1. Initial program 74.5%

    \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in phi1 around 0

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
  4. Applied rewrites43.1%

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
  5. Taylor expanded in phi2 around 0

    \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
  6. Applied rewrites27.9%

    \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
  7. Add Preprocessing

Alternative 26: 29.0% accurate, 4.1× speedup?

\[\begin{array}{l} \\ \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (atan2 (sin (- lambda1 lambda2)) phi2))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	return atan2(sin((lambda1 - lambda2)), phi2);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = atan2(sin((lambda1 - lambda2)), phi2)
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	return Math.atan2(Math.sin((lambda1 - lambda2)), phi2);
}
def code(lambda1, lambda2, phi1, phi2):
	return math.atan2(math.sin((lambda1 - lambda2)), phi2)
function code(lambda1, lambda2, phi1, phi2)
	return atan(sin(Float64(lambda1 - lambda2)), phi2)
end
function tmp = code(lambda1, lambda2, phi1, phi2)
	tmp = atan2(sin((lambda1 - lambda2)), phi2);
end
code[lambda1_, lambda2_, phi1_, phi2_] := N[ArcTan[N[Sin[N[(lambda1 - lambda2), $MachinePrecision]], $MachinePrecision] / phi2], $MachinePrecision]
\begin{array}{l}

\\
\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2}
\end{array}
Derivation
  1. Initial program 74.5%

    \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in phi1 around 0

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
  4. Applied rewrites43.1%

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
  5. Taylor expanded in phi2 around 0

    \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
  6. Applied rewrites27.9%

    \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
  7. Taylor expanded in phi2 around 0

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2} \]
  8. Applied rewrites25.0%

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2} \]
  9. Add Preprocessing

Alternative 27: 21.7% accurate, 4.1× speedup?

\[\begin{array}{l} \\ \tan^{-1}_* \frac{\sin \left(-\lambda_2\right)}{\phi_2} \end{array} \]
(FPCore (lambda1 lambda2 phi1 phi2)
 :precision binary64
 (atan2 (sin (- lambda2)) phi2))
double code(double lambda1, double lambda2, double phi1, double phi2) {
	return atan2(sin(-lambda2), phi2);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = atan2(sin(-lambda2), phi2)
end function
public static double code(double lambda1, double lambda2, double phi1, double phi2) {
	return Math.atan2(Math.sin(-lambda2), phi2);
}
def code(lambda1, lambda2, phi1, phi2):
	return math.atan2(math.sin(-lambda2), phi2)
function code(lambda1, lambda2, phi1, phi2)
	return atan(sin(Float64(-lambda2)), phi2)
end
function tmp = code(lambda1, lambda2, phi1, phi2)
	tmp = atan2(sin(-lambda2), phi2);
end
code[lambda1_, lambda2_, phi1_, phi2_] := N[ArcTan[N[Sin[(-lambda2)], $MachinePrecision] / phi2], $MachinePrecision]
\begin{array}{l}

\\
\tan^{-1}_* \frac{\sin \left(-\lambda_2\right)}{\phi_2}
\end{array}
Derivation
  1. Initial program 74.5%

    \[\tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\cos \phi_1 \cdot \sin \phi_2 - \left(\sin \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)} \]
  2. Add Preprocessing
  3. Taylor expanded in phi1 around 0

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
  4. Applied rewrites43.1%

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2}{\color{blue}{\sin \phi_2}} \]
  5. Taylor expanded in phi2 around 0

    \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
  6. Applied rewrites27.9%

    \[\leadsto \tan^{-1}_* \frac{\color{blue}{\sin \left(\lambda_1 - \lambda_2\right)}}{\sin \phi_2} \]
  7. Taylor expanded in phi2 around 0

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2} \]
  8. Applied rewrites25.0%

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\lambda_1 - \lambda_2\right)}{\phi_2} \]
  9. Taylor expanded in lambda1 around 0

    \[\leadsto \tan^{-1}_* \frac{\sin \left(\mathsf{neg}\left(\lambda_2\right)\right)}{\phi_2} \]
  10. Applied rewrites20.6%

    \[\leadsto \tan^{-1}_* \frac{\sin \left(-\lambda_2\right)}{\phi_2} \]
  11. Add Preprocessing

Reproduce

?
herbie shell --seed 2025036 -o generate:simplify -o generate:proofs
(FPCore (lambda1 lambda2 phi1 phi2)
  :name "Bearing on a great circle"
  :precision binary64
  (atan2 (* (sin (- lambda1 lambda2)) (cos phi2)) (- (* (cos phi1) (sin phi2)) (* (* (sin phi1) (cos phi2)) (cos (- lambda1 lambda2))))))