Equirectangular approximation to distance on a great circle

Percentage Accurate: 60.5% → 99.4%
Time: 23.3s
Alternatives: 10
Speedup: 3.0×

Specification

?
\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\\ R \cdot \sqrt{t_0 \cdot t_0 + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \end{array} \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
 :precision binary64
 (let* ((t_0 (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0)))))
   (* R (sqrt (+ (* t_0 t_0) (* (- phi1 phi2) (- phi1 phi2)))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	return R * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
}
real(8) function code(r, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0d0))
    code = r * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))))
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = (lambda1 - lambda2) * Math.cos(((phi1 + phi2) / 2.0));
	return R * Math.sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = (lambda1 - lambda2) * math.cos(((phi1 + phi2) / 2.0))
	return R * math.sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))))
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(Float64(lambda1 - lambda2) * cos(Float64(Float64(phi1 + phi2) / 2.0)))
	return Float64(R * sqrt(Float64(Float64(t_0 * t_0) + Float64(Float64(phi1 - phi2) * Float64(phi1 - phi2)))))
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = (lambda1 - lambda2) * cos(((phi1 + phi2) / 2.0));
	tmp = R * sqrt(((t_0 * t_0) + ((phi1 - phi2) * (phi1 - phi2))));
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(N[(phi1 + phi2), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, N[(R * N[Sqrt[N[(N[(t$95$0 * t$95$0), $MachinePrecision] + N[(N[(phi1 - phi2), $MachinePrecision] * N[(phi1 - phi2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 10 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 60.5% accurate, 1.0× speedup?

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

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

Alternative 1: 99.4% accurate, 0.5× speedup?

\[\begin{array}{l} R_m = \left|R\right| \\ R_s = \mathsf{copysign}\left(1, R\right) \\ R_s \cdot {\left(\sqrt{R_m \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(\phi_2 \cdot 0.5\right) - \sin \left(0.5 \cdot \phi_1\right) \cdot \sin \left(\phi_2 \cdot 0.5\right)\right), \phi_1 - \phi_2\right)}\right)}^{2} \end{array} \]
R_m = (fabs.f64 R)
R_s = (copysign.f64 1 R)
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (pow
   (sqrt
    (*
     R_m
     (hypot
      (*
       (- lambda1 lambda2)
       (-
        (* (cos (* 0.5 phi1)) (cos (* phi2 0.5)))
        (* (sin (* 0.5 phi1)) (sin (* phi2 0.5)))))
      (- phi1 phi2))))
   2.0)))
R_m = fabs(R);
R_s = copysign(1.0, R);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * pow(sqrt((R_m * hypot(((lambda1 - lambda2) * ((cos((0.5 * phi1)) * cos((phi2 * 0.5))) - (sin((0.5 * phi1)) * sin((phi2 * 0.5))))), (phi1 - phi2)))), 2.0);
}
R_m = Math.abs(R);
R_s = Math.copySign(1.0, R);
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * Math.pow(Math.sqrt((R_m * Math.hypot(((lambda1 - lambda2) * ((Math.cos((0.5 * phi1)) * Math.cos((phi2 * 0.5))) - (Math.sin((0.5 * phi1)) * Math.sin((phi2 * 0.5))))), (phi1 - phi2)))), 2.0);
}
R_m = math.fabs(R)
R_s = math.copysign(1.0, R)
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	return R_s * math.pow(math.sqrt((R_m * math.hypot(((lambda1 - lambda2) * ((math.cos((0.5 * phi1)) * math.cos((phi2 * 0.5))) - (math.sin((0.5 * phi1)) * math.sin((phi2 * 0.5))))), (phi1 - phi2)))), 2.0)
R_m = abs(R)
R_s = copysign(1.0, R)
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	return Float64(R_s * (sqrt(Float64(R_m * hypot(Float64(Float64(lambda1 - lambda2) * Float64(Float64(cos(Float64(0.5 * phi1)) * cos(Float64(phi2 * 0.5))) - Float64(sin(Float64(0.5 * phi1)) * sin(Float64(phi2 * 0.5))))), Float64(phi1 - phi2)))) ^ 2.0))
end
R_m = abs(R);
R_s = sign(R) * abs(1.0);
function tmp = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = R_s * (sqrt((R_m * hypot(((lambda1 - lambda2) * ((cos((0.5 * phi1)) * cos((phi2 * 0.5))) - (sin((0.5 * phi1)) * sin((phi2 * 0.5))))), (phi1 - phi2)))) ^ 2.0);
end
R_m = N[Abs[R], $MachinePrecision]
R_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * N[Power[N[Sqrt[N[(R$95$m * N[Sqrt[N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(N[(N[Cos[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(phi2 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - N[(N[Sin[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(phi2 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] ^ 2 + N[(phi1 - phi2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
R_m = \left|R\right|
\\
R_s = \mathsf{copysign}\left(1, R\right)

\\
R_s \cdot {\left(\sqrt{R_m \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(\phi_2 \cdot 0.5\right) - \sin \left(0.5 \cdot \phi_1\right) \cdot \sin \left(\phi_2 \cdot 0.5\right)\right), \phi_1 - \phi_2\right)}\right)}^{2}
\end{array}
Derivation
  1. Initial program 60.2%

    \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
  2. Step-by-step derivation
    1. hypot-def96.2%

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

    \[\leadsto \color{blue}{R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \]
  4. Add Preprocessing
  5. Step-by-step derivation
    1. add-sqr-sqrt49.6%

      \[\leadsto \color{blue}{\sqrt{R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \cdot \sqrt{R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)}} \]
    2. pow249.6%

      \[\leadsto \color{blue}{{\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)}\right)}^{2}} \]
    3. *-commutative49.6%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\color{blue}{\cos \left(\frac{\phi_1 + \phi_2}{2}\right) \cdot \left(\lambda_1 - \lambda_2\right)}, \phi_1 - \phi_2\right)}\right)}^{2} \]
    4. div-inv49.6%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(\left(\phi_1 + \phi_2\right) \cdot \frac{1}{2}\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    5. metadata-eval49.6%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \left(\left(\phi_1 + \phi_2\right) \cdot \color{blue}{0.5}\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
  6. Applied egg-rr49.6%

    \[\leadsto \color{blue}{{\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \left(\left(\phi_1 + \phi_2\right) \cdot 0.5\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2}} \]
  7. Step-by-step derivation
    1. *-commutative49.6%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(0.5 \cdot \left(\phi_1 + \phi_2\right)\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    2. +-commutative49.6%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \left(0.5 \cdot \color{blue}{\left(\phi_2 + \phi_1\right)}\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    3. distribute-lft-in49.6%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(0.5 \cdot \phi_2 + 0.5 \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    4. cos-sum52.5%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\color{blue}{\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right) - \sin \left(0.5 \cdot \phi_2\right) \cdot \sin \left(0.5 \cdot \phi_1\right)\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    5. *-commutative52.5%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\cos \color{blue}{\left(\phi_2 \cdot 0.5\right)} \cdot \cos \left(0.5 \cdot \phi_1\right) - \sin \left(0.5 \cdot \phi_2\right) \cdot \sin \left(0.5 \cdot \phi_1\right)\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    6. *-commutative52.5%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\cos \left(\phi_2 \cdot 0.5\right) \cdot \cos \color{blue}{\left(\phi_1 \cdot 0.5\right)} - \sin \left(0.5 \cdot \phi_2\right) \cdot \sin \left(0.5 \cdot \phi_1\right)\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    7. *-commutative52.5%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\cos \left(\phi_2 \cdot 0.5\right) \cdot \cos \left(\phi_1 \cdot 0.5\right) - \sin \color{blue}{\left(\phi_2 \cdot 0.5\right)} \cdot \sin \left(0.5 \cdot \phi_1\right)\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    8. *-commutative52.5%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\cos \left(\phi_2 \cdot 0.5\right) \cdot \cos \left(\phi_1 \cdot 0.5\right) - \sin \left(\phi_2 \cdot 0.5\right) \cdot \sin \color{blue}{\left(\phi_1 \cdot 0.5\right)}\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
  8. Applied egg-rr52.5%

    \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\color{blue}{\left(\cos \left(\phi_2 \cdot 0.5\right) \cdot \cos \left(\phi_1 \cdot 0.5\right) - \sin \left(\phi_2 \cdot 0.5\right) \cdot \sin \left(\phi_1 \cdot 0.5\right)\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
  9. Final simplification52.5%

    \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(\phi_2 \cdot 0.5\right) - \sin \left(0.5 \cdot \phi_1\right) \cdot \sin \left(\phi_2 \cdot 0.5\right)\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
  10. Add Preprocessing

Alternative 2: 96.3% accurate, 0.5× speedup?

\[\begin{array}{l} R_m = \left|R\right| \\ R_s = \mathsf{copysign}\left(1, R\right) \\ R_s \cdot \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1.05 \cdot 10^{+212}:\\ \;\;\;\;{\left(\sqrt{R_m \cdot \mathsf{hypot}\left(\lambda_1 \cdot \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(\phi_2 \cdot 0.5\right) - \sin \left(0.5 \cdot \phi_1\right) \cdot \sin \left(\phi_2 \cdot 0.5\right)\right), \phi_1 - \phi_2\right)}\right)}^{2}\\ \mathbf{else}:\\ \;\;\;\;R_m \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_2 + \phi_1}{2}\right), \phi_1 - \phi_2\right)\\ \end{array} \end{array} \]
R_m = (fabs.f64 R)
R_s = (copysign.f64 1 R)
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= lambda1 -1.05e+212)
    (pow
     (sqrt
      (*
       R_m
       (hypot
        (*
         lambda1
         (-
          (* (cos (* 0.5 phi1)) (cos (* phi2 0.5)))
          (* (sin (* 0.5 phi1)) (sin (* phi2 0.5)))))
        (- phi1 phi2))))
     2.0)
    (*
     R_m
     (hypot
      (* (- lambda1 lambda2) (cos (/ (+ phi2 phi1) 2.0)))
      (- phi1 phi2))))))
R_m = fabs(R);
R_s = copysign(1.0, R);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1.05e+212) {
		tmp = pow(sqrt((R_m * hypot((lambda1 * ((cos((0.5 * phi1)) * cos((phi2 * 0.5))) - (sin((0.5 * phi1)) * sin((phi2 * 0.5))))), (phi1 - phi2)))), 2.0);
	} else {
		tmp = R_m * hypot(((lambda1 - lambda2) * cos(((phi2 + phi1) / 2.0))), (phi1 - phi2));
	}
	return R_s * tmp;
}
R_m = Math.abs(R);
R_s = Math.copySign(1.0, R);
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (lambda1 <= -1.05e+212) {
		tmp = Math.pow(Math.sqrt((R_m * Math.hypot((lambda1 * ((Math.cos((0.5 * phi1)) * Math.cos((phi2 * 0.5))) - (Math.sin((0.5 * phi1)) * Math.sin((phi2 * 0.5))))), (phi1 - phi2)))), 2.0);
	} else {
		tmp = R_m * Math.hypot(((lambda1 - lambda2) * Math.cos(((phi2 + phi1) / 2.0))), (phi1 - phi2));
	}
	return R_s * tmp;
}
R_m = math.fabs(R)
R_s = math.copysign(1.0, R)
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if lambda1 <= -1.05e+212:
		tmp = math.pow(math.sqrt((R_m * math.hypot((lambda1 * ((math.cos((0.5 * phi1)) * math.cos((phi2 * 0.5))) - (math.sin((0.5 * phi1)) * math.sin((phi2 * 0.5))))), (phi1 - phi2)))), 2.0)
	else:
		tmp = R_m * math.hypot(((lambda1 - lambda2) * math.cos(((phi2 + phi1) / 2.0))), (phi1 - phi2))
	return R_s * tmp
R_m = abs(R)
R_s = copysign(1.0, R)
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (lambda1 <= -1.05e+212)
		tmp = sqrt(Float64(R_m * hypot(Float64(lambda1 * Float64(Float64(cos(Float64(0.5 * phi1)) * cos(Float64(phi2 * 0.5))) - Float64(sin(Float64(0.5 * phi1)) * sin(Float64(phi2 * 0.5))))), Float64(phi1 - phi2)))) ^ 2.0;
	else
		tmp = Float64(R_m * hypot(Float64(Float64(lambda1 - lambda2) * cos(Float64(Float64(phi2 + phi1) / 2.0))), Float64(phi1 - phi2)));
	end
	return Float64(R_s * tmp)
end
R_m = abs(R);
R_s = sign(R) * abs(1.0);
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (lambda1 <= -1.05e+212)
		tmp = sqrt((R_m * hypot((lambda1 * ((cos((0.5 * phi1)) * cos((phi2 * 0.5))) - (sin((0.5 * phi1)) * sin((phi2 * 0.5))))), (phi1 - phi2)))) ^ 2.0;
	else
		tmp = R_m * hypot(((lambda1 - lambda2) * cos(((phi2 + phi1) / 2.0))), (phi1 - phi2));
	end
	tmp_2 = R_s * tmp;
end
R_m = N[Abs[R], $MachinePrecision]
R_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[lambda1, -1.05e+212], N[Power[N[Sqrt[N[(R$95$m * N[Sqrt[N[(lambda1 * N[(N[(N[Cos[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(phi2 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] - N[(N[Sin[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(phi2 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] ^ 2 + N[(phi1 - phi2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision], 2.0], $MachinePrecision], N[(R$95$m * N[Sqrt[N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(N[(phi2 + phi1), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2 + N[(phi1 - phi2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R_m = \left|R\right|
\\
R_s = \mathsf{copysign}\left(1, R\right)

\\
R_s \cdot \begin{array}{l}
\mathbf{if}\;\lambda_1 \leq -1.05 \cdot 10^{+212}:\\
\;\;\;\;{\left(\sqrt{R_m \cdot \mathsf{hypot}\left(\lambda_1 \cdot \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(\phi_2 \cdot 0.5\right) - \sin \left(0.5 \cdot \phi_1\right) \cdot \sin \left(\phi_2 \cdot 0.5\right)\right), \phi_1 - \phi_2\right)}\right)}^{2}\\

\mathbf{else}:\\
\;\;\;\;R_m \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_2 + \phi_1}{2}\right), \phi_1 - \phi_2\right)\\


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

    1. Initial program 40.0%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def81.2%

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

      \[\leadsto \color{blue}{R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \]
    4. Add Preprocessing
    5. Step-by-step derivation
      1. add-sqr-sqrt34.8%

        \[\leadsto \color{blue}{\sqrt{R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \cdot \sqrt{R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)}} \]
      2. pow234.8%

        \[\leadsto \color{blue}{{\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)}\right)}^{2}} \]
      3. *-commutative34.8%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\color{blue}{\cos \left(\frac{\phi_1 + \phi_2}{2}\right) \cdot \left(\lambda_1 - \lambda_2\right)}, \phi_1 - \phi_2\right)}\right)}^{2} \]
      4. div-inv34.8%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(\left(\phi_1 + \phi_2\right) \cdot \frac{1}{2}\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
      5. metadata-eval34.8%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \left(\left(\phi_1 + \phi_2\right) \cdot \color{blue}{0.5}\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    6. Applied egg-rr34.8%

      \[\leadsto \color{blue}{{\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \left(\left(\phi_1 + \phi_2\right) \cdot 0.5\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2}} \]
    7. Step-by-step derivation
      1. *-commutative34.8%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(0.5 \cdot \left(\phi_1 + \phi_2\right)\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
      2. +-commutative34.8%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \left(0.5 \cdot \color{blue}{\left(\phi_2 + \phi_1\right)}\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
      3. distribute-lft-in34.8%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\cos \color{blue}{\left(0.5 \cdot \phi_2 + 0.5 \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
      4. cos-sum49.5%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\color{blue}{\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right) - \sin \left(0.5 \cdot \phi_2\right) \cdot \sin \left(0.5 \cdot \phi_1\right)\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
      5. *-commutative49.5%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\cos \color{blue}{\left(\phi_2 \cdot 0.5\right)} \cdot \cos \left(0.5 \cdot \phi_1\right) - \sin \left(0.5 \cdot \phi_2\right) \cdot \sin \left(0.5 \cdot \phi_1\right)\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
      6. *-commutative49.5%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\cos \left(\phi_2 \cdot 0.5\right) \cdot \cos \color{blue}{\left(\phi_1 \cdot 0.5\right)} - \sin \left(0.5 \cdot \phi_2\right) \cdot \sin \left(0.5 \cdot \phi_1\right)\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
      7. *-commutative49.5%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\cos \left(\phi_2 \cdot 0.5\right) \cdot \cos \left(\phi_1 \cdot 0.5\right) - \sin \color{blue}{\left(\phi_2 \cdot 0.5\right)} \cdot \sin \left(0.5 \cdot \phi_1\right)\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
      8. *-commutative49.5%

        \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\left(\cos \left(\phi_2 \cdot 0.5\right) \cdot \cos \left(\phi_1 \cdot 0.5\right) - \sin \left(\phi_2 \cdot 0.5\right) \cdot \sin \color{blue}{\left(\phi_1 \cdot 0.5\right)}\right) \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    8. Applied egg-rr49.5%

      \[\leadsto {\left(\sqrt{R \cdot \mathsf{hypot}\left(\color{blue}{\left(\cos \left(\phi_2 \cdot 0.5\right) \cdot \cos \left(\phi_1 \cdot 0.5\right) - \sin \left(\phi_2 \cdot 0.5\right) \cdot \sin \left(\phi_1 \cdot 0.5\right)\right)} \cdot \left(\lambda_1 - \lambda_2\right), \phi_1 - \phi_2\right)}\right)}^{2} \]
    9. Taylor expanded in lambda1 around inf 44.0%

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

    if -1.05e212 < lambda1

    1. Initial program 61.5%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def97.2%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\lambda_1 \leq -1.05 \cdot 10^{+212}:\\ \;\;\;\;{\left(\sqrt{R \cdot \mathsf{hypot}\left(\lambda_1 \cdot \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(\phi_2 \cdot 0.5\right) - \sin \left(0.5 \cdot \phi_1\right) \cdot \sin \left(\phi_2 \cdot 0.5\right)\right), \phi_1 - \phi_2\right)}\right)}^{2}\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_2 + \phi_1}{2}\right), \phi_1 - \phi_2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 82.4% accurate, 1.5× speedup?

\[\begin{array}{l} R_m = \left|R\right| \\ R_s = \mathsf{copysign}\left(1, R\right) \\ R_s \cdot \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2.6 \cdot 10^{-137}:\\ \;\;\;\;R_m \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R_m \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_2 \cdot 0.5\right), \phi_1 - \phi_2\right)\\ \end{array} \end{array} \]
R_m = (fabs.f64 R)
R_s = (copysign.f64 1 R)
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi2 2.6e-137)
    (* R_m (hypot phi1 (* (- lambda1 lambda2) (cos (* 0.5 phi1)))))
    (* R_m (hypot (* (- lambda1 lambda2) (cos (* phi2 0.5))) (- phi1 phi2))))))
R_m = fabs(R);
R_s = copysign(1.0, R);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 2.6e-137) {
		tmp = R_m * hypot(phi1, ((lambda1 - lambda2) * cos((0.5 * phi1))));
	} else {
		tmp = R_m * hypot(((lambda1 - lambda2) * cos((phi2 * 0.5))), (phi1 - phi2));
	}
	return R_s * tmp;
}
R_m = Math.abs(R);
R_s = Math.copySign(1.0, R);
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 2.6e-137) {
		tmp = R_m * Math.hypot(phi1, ((lambda1 - lambda2) * Math.cos((0.5 * phi1))));
	} else {
		tmp = R_m * Math.hypot(((lambda1 - lambda2) * Math.cos((phi2 * 0.5))), (phi1 - phi2));
	}
	return R_s * tmp;
}
R_m = math.fabs(R)
R_s = math.copysign(1.0, R)
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 2.6e-137:
		tmp = R_m * math.hypot(phi1, ((lambda1 - lambda2) * math.cos((0.5 * phi1))))
	else:
		tmp = R_m * math.hypot(((lambda1 - lambda2) * math.cos((phi2 * 0.5))), (phi1 - phi2))
	return R_s * tmp
R_m = abs(R)
R_s = copysign(1.0, R)
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 2.6e-137)
		tmp = Float64(R_m * hypot(phi1, Float64(Float64(lambda1 - lambda2) * cos(Float64(0.5 * phi1)))));
	else
		tmp = Float64(R_m * hypot(Float64(Float64(lambda1 - lambda2) * cos(Float64(phi2 * 0.5))), Float64(phi1 - phi2)));
	end
	return Float64(R_s * tmp)
end
R_m = abs(R);
R_s = sign(R) * abs(1.0);
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 2.6e-137)
		tmp = R_m * hypot(phi1, ((lambda1 - lambda2) * cos((0.5 * phi1))));
	else
		tmp = R_m * hypot(((lambda1 - lambda2) * cos((phi2 * 0.5))), (phi1 - phi2));
	end
	tmp_2 = R_s * tmp;
end
R_m = N[Abs[R], $MachinePrecision]
R_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi2, 2.6e-137], N[(R$95$m * N[Sqrt[phi1 ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[Sqrt[N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(phi2 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2 + N[(phi1 - phi2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R_m = \left|R\right|
\\
R_s = \mathsf{copysign}\left(1, R\right)

\\
R_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_2 \leq 2.6 \cdot 10^{-137}:\\
\;\;\;\;R_m \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)\\

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


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

    1. Initial program 66.2%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def98.2%

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

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

      \[\leadsto \color{blue}{R \cdot \sqrt{{\cos \left(0.5 \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_1}^{2}}} \]
    6. Step-by-step derivation
      1. +-commutative59.2%

        \[\leadsto R \cdot \sqrt{\color{blue}{{\phi_1}^{2} + {\cos \left(0.5 \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}}} \]
      2. unpow259.2%

        \[\leadsto R \cdot \sqrt{\color{blue}{\phi_1 \cdot \phi_1} + {\cos \left(0.5 \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} \]
      3. unpow259.2%

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \color{blue}{\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} \]
      4. unpow259.2%

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}} \]
      5. unswap-sqr59.2%

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \color{blue}{\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}} \]
      6. hypot-def81.6%

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

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

    if 2.6e-137 < phi2

    1. Initial program 50.0%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def92.7%

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \]
    3. Simplified92.7%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2.6 \cdot 10^{-137}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_2 \cdot 0.5\right), \phi_1 - \phi_2\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 76.3% accurate, 1.5× speedup?

\[\begin{array}{l} R_m = \left|R\right| \\ R_s = \mathsf{copysign}\left(1, R\right) \\ R_s \cdot \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2.05 \cdot 10^{+63}:\\ \;\;\;\;R_m \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R_m \cdot \left(\phi_2 - \phi_1\right)\\ \end{array} \end{array} \]
R_m = (fabs.f64 R)
R_s = (copysign.f64 1 R)
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi2 2.05e+63)
    (* R_m (hypot phi1 (* (- lambda1 lambda2) (cos (* 0.5 phi1)))))
    (* R_m (- phi2 phi1)))))
R_m = fabs(R);
R_s = copysign(1.0, R);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 2.05e+63) {
		tmp = R_m * hypot(phi1, ((lambda1 - lambda2) * cos((0.5 * phi1))));
	} else {
		tmp = R_m * (phi2 - phi1);
	}
	return R_s * tmp;
}
R_m = Math.abs(R);
R_s = Math.copySign(1.0, R);
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 2.05e+63) {
		tmp = R_m * Math.hypot(phi1, ((lambda1 - lambda2) * Math.cos((0.5 * phi1))));
	} else {
		tmp = R_m * (phi2 - phi1);
	}
	return R_s * tmp;
}
R_m = math.fabs(R)
R_s = math.copysign(1.0, R)
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 2.05e+63:
		tmp = R_m * math.hypot(phi1, ((lambda1 - lambda2) * math.cos((0.5 * phi1))))
	else:
		tmp = R_m * (phi2 - phi1)
	return R_s * tmp
R_m = abs(R)
R_s = copysign(1.0, R)
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 2.05e+63)
		tmp = Float64(R_m * hypot(phi1, Float64(Float64(lambda1 - lambda2) * cos(Float64(0.5 * phi1)))));
	else
		tmp = Float64(R_m * Float64(phi2 - phi1));
	end
	return Float64(R_s * tmp)
end
R_m = abs(R);
R_s = sign(R) * abs(1.0);
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 2.05e+63)
		tmp = R_m * hypot(phi1, ((lambda1 - lambda2) * cos((0.5 * phi1))));
	else
		tmp = R_m * (phi2 - phi1);
	end
	tmp_2 = R_s * tmp;
end
R_m = N[Abs[R], $MachinePrecision]
R_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi2, 2.05e+63], N[(R$95$m * N[Sqrt[phi1 ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[(phi2 - phi1), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R_m = \left|R\right|
\\
R_s = \mathsf{copysign}\left(1, R\right)

\\
R_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_2 \leq 2.05 \cdot 10^{+63}:\\
\;\;\;\;R_m \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R_m \cdot \left(\phi_2 - \phi_1\right)\\


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

    1. Initial program 65.9%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def97.3%

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

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

      \[\leadsto \color{blue}{R \cdot \sqrt{{\cos \left(0.5 \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_1}^{2}}} \]
    6. Step-by-step derivation
      1. +-commutative58.2%

        \[\leadsto R \cdot \sqrt{\color{blue}{{\phi_1}^{2} + {\cos \left(0.5 \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}}} \]
      2. unpow258.2%

        \[\leadsto R \cdot \sqrt{\color{blue}{\phi_1 \cdot \phi_1} + {\cos \left(0.5 \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} \]
      3. unpow258.2%

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \color{blue}{\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} \]
      4. unpow258.2%

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}} \]
      5. unswap-sqr58.2%

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \color{blue}{\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}} \]
      6. hypot-def81.0%

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

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

    if 2.04999999999999996e63 < phi2

    1. Initial program 36.1%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def91.6%

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \]
    3. Simplified91.6%

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

      \[\leadsto \color{blue}{-1 \cdot \left(R \cdot \phi_1\right) + R \cdot \phi_2} \]
    6. Step-by-step derivation
      1. +-commutative70.8%

        \[\leadsto \color{blue}{R \cdot \phi_2 + -1 \cdot \left(R \cdot \phi_1\right)} \]
      2. mul-1-neg70.8%

        \[\leadsto R \cdot \phi_2 + \color{blue}{\left(-R \cdot \phi_1\right)} \]
      3. unsub-neg70.8%

        \[\leadsto \color{blue}{R \cdot \phi_2 - R \cdot \phi_1} \]
      4. *-commutative70.8%

        \[\leadsto \color{blue}{\phi_2 \cdot R} - R \cdot \phi_1 \]
      5. *-commutative70.8%

        \[\leadsto \phi_2 \cdot R - \color{blue}{\phi_1 \cdot R} \]
    7. Simplified70.8%

      \[\leadsto \color{blue}{\phi_2 \cdot R - \phi_1 \cdot R} \]
    8. Taylor expanded in R around -inf 70.8%

      \[\leadsto \color{blue}{-1 \cdot \left(R \cdot \left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right)\right)} \]
    9. Step-by-step derivation
      1. associate-*r*70.8%

        \[\leadsto \color{blue}{\left(-1 \cdot R\right) \cdot \left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right)} \]
      2. neg-mul-170.8%

        \[\leadsto \color{blue}{\left(-R\right)} \cdot \left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right) \]
      3. distribute-lft-out--70.8%

        \[\leadsto \left(-R\right) \cdot \color{blue}{\left(-1 \cdot \left(\phi_2 - \phi_1\right)\right)} \]
      4. neg-mul-170.8%

        \[\leadsto \left(-R\right) \cdot \color{blue}{\left(-\left(\phi_2 - \phi_1\right)\right)} \]
      5. distribute-lft-neg-in70.8%

        \[\leadsto \color{blue}{-R \cdot \left(-\left(\phi_2 - \phi_1\right)\right)} \]
      6. neg-mul-170.8%

        \[\leadsto -R \cdot \color{blue}{\left(-1 \cdot \left(\phi_2 - \phi_1\right)\right)} \]
      7. distribute-lft-out--70.8%

        \[\leadsto -R \cdot \color{blue}{\left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right)} \]
      8. cancel-sign-sub-inv70.8%

        \[\leadsto -R \cdot \color{blue}{\left(-1 \cdot \phi_2 + \left(--1\right) \cdot \phi_1\right)} \]
      9. metadata-eval70.8%

        \[\leadsto -R \cdot \left(-1 \cdot \phi_2 + \color{blue}{1} \cdot \phi_1\right) \]
      10. *-lft-identity70.8%

        \[\leadsto -R \cdot \left(-1 \cdot \phi_2 + \color{blue}{\phi_1}\right) \]
      11. distribute-lft-in70.8%

        \[\leadsto -\color{blue}{\left(R \cdot \left(-1 \cdot \phi_2\right) + R \cdot \phi_1\right)} \]
      12. mul-1-neg70.8%

        \[\leadsto -\left(R \cdot \color{blue}{\left(-\phi_2\right)} + R \cdot \phi_1\right) \]
      13. distribute-rgt-neg-in70.8%

        \[\leadsto -\left(\color{blue}{\left(-R \cdot \phi_2\right)} + R \cdot \phi_1\right) \]
      14. mul-1-neg70.8%

        \[\leadsto -\left(\color{blue}{-1 \cdot \left(R \cdot \phi_2\right)} + R \cdot \phi_1\right) \]
      15. +-commutative70.8%

        \[\leadsto -\color{blue}{\left(R \cdot \phi_1 + -1 \cdot \left(R \cdot \phi_2\right)\right)} \]
      16. mul-1-neg70.8%

        \[\leadsto -\left(R \cdot \phi_1 + \color{blue}{\left(-R \cdot \phi_2\right)}\right) \]
      17. distribute-rgt-neg-in70.8%

        \[\leadsto -\left(R \cdot \phi_1 + \color{blue}{R \cdot \left(-\phi_2\right)}\right) \]
      18. mul-1-neg70.8%

        \[\leadsto -\left(R \cdot \phi_1 + R \cdot \color{blue}{\left(-1 \cdot \phi_2\right)}\right) \]
      19. distribute-lft-in70.8%

        \[\leadsto -\color{blue}{R \cdot \left(\phi_1 + -1 \cdot \phi_2\right)} \]
      20. mul-1-neg70.8%

        \[\leadsto -R \cdot \left(\phi_1 + \color{blue}{\left(-\phi_2\right)}\right) \]
      21. sub-neg70.8%

        \[\leadsto -R \cdot \color{blue}{\left(\phi_1 - \phi_2\right)} \]
      22. *-commutative70.8%

        \[\leadsto -\color{blue}{\left(\phi_1 - \phi_2\right) \cdot R} \]
    10. Simplified70.8%

      \[\leadsto \color{blue}{-\left(\phi_1 - \phi_2\right) \cdot R} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification79.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 2.05 \cdot 10^{+63}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 79.2% accurate, 1.5× speedup?

\[\begin{array}{l} R_m = \left|R\right| \\ R_s = \mathsf{copysign}\left(1, R\right) \\ R_s \cdot \begin{array}{l} \mathbf{if}\;\phi_1 \leq -3800:\\ \;\;\;\;R_m \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R_m \cdot \mathsf{hypot}\left(\phi_2, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_2 \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
R_m = (fabs.f64 R)
R_s = (copysign.f64 1 R)
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi1 -3800.0)
    (* R_m (hypot phi1 (* (- lambda1 lambda2) (cos (* 0.5 phi1)))))
    (* R_m (hypot phi2 (* (- lambda1 lambda2) (cos (* phi2 0.5))))))))
R_m = fabs(R);
R_s = copysign(1.0, R);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -3800.0) {
		tmp = R_m * hypot(phi1, ((lambda1 - lambda2) * cos((0.5 * phi1))));
	} else {
		tmp = R_m * hypot(phi2, ((lambda1 - lambda2) * cos((phi2 * 0.5))));
	}
	return R_s * tmp;
}
R_m = Math.abs(R);
R_s = Math.copySign(1.0, R);
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi1 <= -3800.0) {
		tmp = R_m * Math.hypot(phi1, ((lambda1 - lambda2) * Math.cos((0.5 * phi1))));
	} else {
		tmp = R_m * Math.hypot(phi2, ((lambda1 - lambda2) * Math.cos((phi2 * 0.5))));
	}
	return R_s * tmp;
}
R_m = math.fabs(R)
R_s = math.copysign(1.0, R)
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi1 <= -3800.0:
		tmp = R_m * math.hypot(phi1, ((lambda1 - lambda2) * math.cos((0.5 * phi1))))
	else:
		tmp = R_m * math.hypot(phi2, ((lambda1 - lambda2) * math.cos((phi2 * 0.5))))
	return R_s * tmp
R_m = abs(R)
R_s = copysign(1.0, R)
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi1 <= -3800.0)
		tmp = Float64(R_m * hypot(phi1, Float64(Float64(lambda1 - lambda2) * cos(Float64(0.5 * phi1)))));
	else
		tmp = Float64(R_m * hypot(phi2, Float64(Float64(lambda1 - lambda2) * cos(Float64(phi2 * 0.5)))));
	end
	return Float64(R_s * tmp)
end
R_m = abs(R);
R_s = sign(R) * abs(1.0);
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi1 <= -3800.0)
		tmp = R_m * hypot(phi1, ((lambda1 - lambda2) * cos((0.5 * phi1))));
	else
		tmp = R_m * hypot(phi2, ((lambda1 - lambda2) * cos((phi2 * 0.5))));
	end
	tmp_2 = R_s * tmp;
end
R_m = N[Abs[R], $MachinePrecision]
R_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi1, -3800.0], N[(R$95$m * N[Sqrt[phi1 ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[Sqrt[phi2 ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(phi2 * 0.5), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R_m = \left|R\right|
\\
R_s = \mathsf{copysign}\left(1, R\right)

\\
R_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_1 \leq -3800:\\
\;\;\;\;R_m \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)\\

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


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

    1. Initial program 50.2%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def91.9%

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \]
    3. Simplified91.9%

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

      \[\leadsto \color{blue}{R \cdot \sqrt{{\cos \left(0.5 \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_1}^{2}}} \]
    6. Step-by-step derivation
      1. +-commutative45.9%

        \[\leadsto R \cdot \sqrt{\color{blue}{{\phi_1}^{2} + {\cos \left(0.5 \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}}} \]
      2. unpow245.9%

        \[\leadsto R \cdot \sqrt{\color{blue}{\phi_1 \cdot \phi_1} + {\cos \left(0.5 \cdot \phi_1\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} \]
      3. unpow245.9%

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \color{blue}{\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} \]
      4. unpow245.9%

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}} \]
      5. unswap-sqr45.9%

        \[\leadsto R \cdot \sqrt{\phi_1 \cdot \phi_1 + \color{blue}{\left(\cos \left(0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(\cos \left(0.5 \cdot \phi_1\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}} \]
      6. hypot-def79.1%

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

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

    if -3800 < phi1

    1. Initial program 63.6%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def97.6%

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \]
    3. Simplified97.6%

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

      \[\leadsto \color{blue}{R \cdot \sqrt{{\cos \left(0.5 \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_2}^{2}}} \]
    6. Step-by-step derivation
      1. +-commutative53.5%

        \[\leadsto R \cdot \sqrt{\color{blue}{{\phi_2}^{2} + {\cos \left(0.5 \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}}} \]
      2. unpow253.5%

        \[\leadsto R \cdot \sqrt{\color{blue}{\phi_2 \cdot \phi_2} + {\cos \left(0.5 \cdot \phi_2\right)}^{2} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} \]
      3. unpow253.5%

        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \color{blue}{\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \cos \left(0.5 \cdot \phi_2\right)\right)} \cdot {\left(\lambda_1 - \lambda_2\right)}^{2}} \]
      4. unpow253.5%

        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \left(\cos \left(0.5 \cdot \phi_2\right) \cdot \cos \left(0.5 \cdot \phi_2\right)\right) \cdot \color{blue}{\left(\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}} \]
      5. unswap-sqr53.5%

        \[\leadsto R \cdot \sqrt{\phi_2 \cdot \phi_2 + \color{blue}{\left(\cos \left(0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot \left(\cos \left(0.5 \cdot \phi_2\right) \cdot \left(\lambda_1 - \lambda_2\right)\right)}} \]
      6. hypot-def80.6%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_1 \leq -3800:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_1, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(0.5 \cdot \phi_1\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\phi_2, \left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\phi_2 \cdot 0.5\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 96.1% accurate, 1.5× speedup?

\[\begin{array}{l} R_m = \left|R\right| \\ R_s = \mathsf{copysign}\left(1, R\right) \\ R_s \cdot \left(R_m \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_2 + \phi_1}{2}\right), \phi_1 - \phi_2\right)\right) \end{array} \]
R_m = (fabs.f64 R)
R_s = (copysign.f64 1 R)
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (*
   R_m
   (hypot (* (- lambda1 lambda2) (cos (/ (+ phi2 phi1) 2.0))) (- phi1 phi2)))))
R_m = fabs(R);
R_s = copysign(1.0, R);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (R_m * hypot(((lambda1 - lambda2) * cos(((phi2 + phi1) / 2.0))), (phi1 - phi2)));
}
R_m = Math.abs(R);
R_s = Math.copySign(1.0, R);
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (R_m * Math.hypot(((lambda1 - lambda2) * Math.cos(((phi2 + phi1) / 2.0))), (phi1 - phi2)));
}
R_m = math.fabs(R)
R_s = math.copysign(1.0, R)
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	return R_s * (R_m * math.hypot(((lambda1 - lambda2) * math.cos(((phi2 + phi1) / 2.0))), (phi1 - phi2)))
R_m = abs(R)
R_s = copysign(1.0, R)
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	return Float64(R_s * Float64(R_m * hypot(Float64(Float64(lambda1 - lambda2) * cos(Float64(Float64(phi2 + phi1) / 2.0))), Float64(phi1 - phi2))))
end
R_m = abs(R);
R_s = sign(R) * abs(1.0);
function tmp = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = R_s * (R_m * hypot(((lambda1 - lambda2) * cos(((phi2 + phi1) / 2.0))), (phi1 - phi2)));
end
R_m = N[Abs[R], $MachinePrecision]
R_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * N[(R$95$m * N[Sqrt[N[(N[(lambda1 - lambda2), $MachinePrecision] * N[Cos[N[(N[(phi2 + phi1), $MachinePrecision] / 2.0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] ^ 2 + N[(phi1 - phi2), $MachinePrecision] ^ 2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
R_m = \left|R\right|
\\
R_s = \mathsf{copysign}\left(1, R\right)

\\
R_s \cdot \left(R_m \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_2 + \phi_1}{2}\right), \phi_1 - \phi_2\right)\right)
\end{array}
Derivation
  1. Initial program 60.2%

    \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
  2. Step-by-step derivation
    1. hypot-def96.2%

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

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

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

Alternative 7: 71.2% accurate, 3.0× speedup?

\[\begin{array}{l} R_m = \left|R\right| \\ R_s = \mathsf{copysign}\left(1, R\right) \\ R_s \cdot \begin{array}{l} \mathbf{if}\;\phi_2 \leq 4.8 \cdot 10^{+61}:\\ \;\;\;\;R_m \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right)\\ \mathbf{else}:\\ \;\;\;\;R_m \cdot \left(\phi_2 - \phi_1\right)\\ \end{array} \end{array} \]
R_m = (fabs.f64 R)
R_s = (copysign.f64 1 R)
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (*
  R_s
  (if (<= phi2 4.8e+61)
    (* R_m (hypot (- lambda1 lambda2) phi1))
    (* R_m (- phi2 phi1)))))
R_m = fabs(R);
R_s = copysign(1.0, R);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 4.8e+61) {
		tmp = R_m * hypot((lambda1 - lambda2), phi1);
	} else {
		tmp = R_m * (phi2 - phi1);
	}
	return R_s * tmp;
}
R_m = Math.abs(R);
R_s = Math.copySign(1.0, R);
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 4.8e+61) {
		tmp = R_m * Math.hypot((lambda1 - lambda2), phi1);
	} else {
		tmp = R_m * (phi2 - phi1);
	}
	return R_s * tmp;
}
R_m = math.fabs(R)
R_s = math.copysign(1.0, R)
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 4.8e+61:
		tmp = R_m * math.hypot((lambda1 - lambda2), phi1)
	else:
		tmp = R_m * (phi2 - phi1)
	return R_s * tmp
R_m = abs(R)
R_s = copysign(1.0, R)
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 4.8e+61)
		tmp = Float64(R_m * hypot(Float64(lambda1 - lambda2), phi1));
	else
		tmp = Float64(R_m * Float64(phi2 - phi1));
	end
	return Float64(R_s * tmp)
end
R_m = abs(R);
R_s = sign(R) * abs(1.0);
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 4.8e+61)
		tmp = R_m * hypot((lambda1 - lambda2), phi1);
	else
		tmp = R_m * (phi2 - phi1);
	end
	tmp_2 = R_s * tmp;
end
R_m = N[Abs[R], $MachinePrecision]
R_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi2, 4.8e+61], N[(R$95$m * N[Sqrt[N[(lambda1 - lambda2), $MachinePrecision] ^ 2 + phi1 ^ 2], $MachinePrecision]), $MachinePrecision], N[(R$95$m * N[(phi2 - phi1), $MachinePrecision]), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R_m = \left|R\right|
\\
R_s = \mathsf{copysign}\left(1, R\right)

\\
R_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_2 \leq 4.8 \cdot 10^{+61}:\\
\;\;\;\;R_m \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right)\\

\mathbf{else}:\\
\;\;\;\;R_m \cdot \left(\phi_2 - \phi_1\right)\\


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

    1. Initial program 65.9%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def97.3%

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

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

      \[\leadsto R \cdot \mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \color{blue}{\cos \left(0.5 \cdot \phi_2\right)}, \phi_1 - \phi_2\right) \]
    6. Taylor expanded in phi2 around 0 55.9%

      \[\leadsto \color{blue}{R \cdot \sqrt{{\phi_1}^{2} + {\left(\lambda_1 - \lambda_2\right)}^{2}}} \]
    7. Step-by-step derivation
      1. +-commutative55.9%

        \[\leadsto R \cdot \sqrt{\color{blue}{{\left(\lambda_1 - \lambda_2\right)}^{2} + {\phi_1}^{2}}} \]
      2. unpow255.9%

        \[\leadsto R \cdot \sqrt{\color{blue}{\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right)} + {\phi_1}^{2}} \]
      3. unpow255.9%

        \[\leadsto R \cdot \sqrt{\left(\lambda_1 - \lambda_2\right) \cdot \left(\lambda_1 - \lambda_2\right) + \color{blue}{\phi_1 \cdot \phi_1}} \]
      4. hypot-def75.8%

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right)} \]
    8. Simplified75.8%

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

    if 4.7999999999999998e61 < phi2

    1. Initial program 36.1%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def91.6%

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \]
    3. Simplified91.6%

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

      \[\leadsto \color{blue}{-1 \cdot \left(R \cdot \phi_1\right) + R \cdot \phi_2} \]
    6. Step-by-step derivation
      1. +-commutative70.8%

        \[\leadsto \color{blue}{R \cdot \phi_2 + -1 \cdot \left(R \cdot \phi_1\right)} \]
      2. mul-1-neg70.8%

        \[\leadsto R \cdot \phi_2 + \color{blue}{\left(-R \cdot \phi_1\right)} \]
      3. unsub-neg70.8%

        \[\leadsto \color{blue}{R \cdot \phi_2 - R \cdot \phi_1} \]
      4. *-commutative70.8%

        \[\leadsto \color{blue}{\phi_2 \cdot R} - R \cdot \phi_1 \]
      5. *-commutative70.8%

        \[\leadsto \phi_2 \cdot R - \color{blue}{\phi_1 \cdot R} \]
    7. Simplified70.8%

      \[\leadsto \color{blue}{\phi_2 \cdot R - \phi_1 \cdot R} \]
    8. Taylor expanded in R around -inf 70.8%

      \[\leadsto \color{blue}{-1 \cdot \left(R \cdot \left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right)\right)} \]
    9. Step-by-step derivation
      1. associate-*r*70.8%

        \[\leadsto \color{blue}{\left(-1 \cdot R\right) \cdot \left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right)} \]
      2. neg-mul-170.8%

        \[\leadsto \color{blue}{\left(-R\right)} \cdot \left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right) \]
      3. distribute-lft-out--70.8%

        \[\leadsto \left(-R\right) \cdot \color{blue}{\left(-1 \cdot \left(\phi_2 - \phi_1\right)\right)} \]
      4. neg-mul-170.8%

        \[\leadsto \left(-R\right) \cdot \color{blue}{\left(-\left(\phi_2 - \phi_1\right)\right)} \]
      5. distribute-lft-neg-in70.8%

        \[\leadsto \color{blue}{-R \cdot \left(-\left(\phi_2 - \phi_1\right)\right)} \]
      6. neg-mul-170.8%

        \[\leadsto -R \cdot \color{blue}{\left(-1 \cdot \left(\phi_2 - \phi_1\right)\right)} \]
      7. distribute-lft-out--70.8%

        \[\leadsto -R \cdot \color{blue}{\left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right)} \]
      8. cancel-sign-sub-inv70.8%

        \[\leadsto -R \cdot \color{blue}{\left(-1 \cdot \phi_2 + \left(--1\right) \cdot \phi_1\right)} \]
      9. metadata-eval70.8%

        \[\leadsto -R \cdot \left(-1 \cdot \phi_2 + \color{blue}{1} \cdot \phi_1\right) \]
      10. *-lft-identity70.8%

        \[\leadsto -R \cdot \left(-1 \cdot \phi_2 + \color{blue}{\phi_1}\right) \]
      11. distribute-lft-in70.8%

        \[\leadsto -\color{blue}{\left(R \cdot \left(-1 \cdot \phi_2\right) + R \cdot \phi_1\right)} \]
      12. mul-1-neg70.8%

        \[\leadsto -\left(R \cdot \color{blue}{\left(-\phi_2\right)} + R \cdot \phi_1\right) \]
      13. distribute-rgt-neg-in70.8%

        \[\leadsto -\left(\color{blue}{\left(-R \cdot \phi_2\right)} + R \cdot \phi_1\right) \]
      14. mul-1-neg70.8%

        \[\leadsto -\left(\color{blue}{-1 \cdot \left(R \cdot \phi_2\right)} + R \cdot \phi_1\right) \]
      15. +-commutative70.8%

        \[\leadsto -\color{blue}{\left(R \cdot \phi_1 + -1 \cdot \left(R \cdot \phi_2\right)\right)} \]
      16. mul-1-neg70.8%

        \[\leadsto -\left(R \cdot \phi_1 + \color{blue}{\left(-R \cdot \phi_2\right)}\right) \]
      17. distribute-rgt-neg-in70.8%

        \[\leadsto -\left(R \cdot \phi_1 + \color{blue}{R \cdot \left(-\phi_2\right)}\right) \]
      18. mul-1-neg70.8%

        \[\leadsto -\left(R \cdot \phi_1 + R \cdot \color{blue}{\left(-1 \cdot \phi_2\right)}\right) \]
      19. distribute-lft-in70.8%

        \[\leadsto -\color{blue}{R \cdot \left(\phi_1 + -1 \cdot \phi_2\right)} \]
      20. mul-1-neg70.8%

        \[\leadsto -R \cdot \left(\phi_1 + \color{blue}{\left(-\phi_2\right)}\right) \]
      21. sub-neg70.8%

        \[\leadsto -R \cdot \color{blue}{\left(\phi_1 - \phi_2\right)} \]
      22. *-commutative70.8%

        \[\leadsto -\color{blue}{\left(\phi_1 - \phi_2\right) \cdot R} \]
    10. Simplified70.8%

      \[\leadsto \color{blue}{-\left(\phi_1 - \phi_2\right) \cdot R} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification74.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 4.8 \cdot 10^{+61}:\\ \;\;\;\;R \cdot \mathsf{hypot}\left(\lambda_1 - \lambda_2, \phi_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\phi_2 - \phi_1\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 28.2% accurate, 36.5× speedup?

\[\begin{array}{l} R_m = \left|R\right| \\ R_s = \mathsf{copysign}\left(1, R\right) \\ R_s \cdot \begin{array}{l} \mathbf{if}\;\phi_2 \leq 0.0078:\\ \;\;\;\;R_m \cdot \left(-\phi_1\right)\\ \mathbf{else}:\\ \;\;\;\;R_m \cdot \phi_2\\ \end{array} \end{array} \]
R_m = (fabs.f64 R)
R_s = (copysign.f64 1 R)
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R_s (if (<= phi2 0.0078) (* R_m (- phi1)) (* R_m phi2))))
R_m = fabs(R);
R_s = copysign(1.0, R);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 0.0078) {
		tmp = R_m * -phi1;
	} else {
		tmp = R_m * phi2;
	}
	return R_s * tmp;
}
R_m = abs(R)
R_s = copysign(1.0d0, R)
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (phi2 <= 0.0078d0) then
        tmp = r_m * -phi1
    else
        tmp = r_m * phi2
    end if
    code = r_s * tmp
end function
R_m = Math.abs(R);
R_s = Math.copySign(1.0, R);
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (phi2 <= 0.0078) {
		tmp = R_m * -phi1;
	} else {
		tmp = R_m * phi2;
	}
	return R_s * tmp;
}
R_m = math.fabs(R)
R_s = math.copysign(1.0, R)
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if phi2 <= 0.0078:
		tmp = R_m * -phi1
	else:
		tmp = R_m * phi2
	return R_s * tmp
R_m = abs(R)
R_s = copysign(1.0, R)
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (phi2 <= 0.0078)
		tmp = Float64(R_m * Float64(-phi1));
	else
		tmp = Float64(R_m * phi2);
	end
	return Float64(R_s * tmp)
end
R_m = abs(R);
R_s = sign(R) * abs(1.0);
function tmp_2 = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (phi2 <= 0.0078)
		tmp = R_m * -phi1;
	else
		tmp = R_m * phi2;
	end
	tmp_2 = R_s * tmp;
end
R_m = N[Abs[R], $MachinePrecision]
R_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * If[LessEqual[phi2, 0.0078], N[(R$95$m * (-phi1)), $MachinePrecision], N[(R$95$m * phi2), $MachinePrecision]]), $MachinePrecision]
\begin{array}{l}
R_m = \left|R\right|
\\
R_s = \mathsf{copysign}\left(1, R\right)

\\
R_s \cdot \begin{array}{l}
\mathbf{if}\;\phi_2 \leq 0.0078:\\
\;\;\;\;R_m \cdot \left(-\phi_1\right)\\

\mathbf{else}:\\
\;\;\;\;R_m \cdot \phi_2\\


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

    1. Initial program 65.7%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def97.9%

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \]
    3. Simplified97.9%

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

      \[\leadsto \color{blue}{-1 \cdot \left(R \cdot \phi_1\right)} \]
    6. Step-by-step derivation
      1. mul-1-neg23.9%

        \[\leadsto \color{blue}{-R \cdot \phi_1} \]
      2. *-commutative23.9%

        \[\leadsto -\color{blue}{\phi_1 \cdot R} \]
      3. distribute-rgt-neg-in23.9%

        \[\leadsto \color{blue}{\phi_1 \cdot \left(-R\right)} \]
    7. Simplified23.9%

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

    if 0.0077999999999999996 < phi2

    1. Initial program 43.6%

      \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
    2. Step-by-step derivation
      1. hypot-def91.0%

        \[\leadsto R \cdot \color{blue}{\mathsf{hypot}\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right), \phi_1 - \phi_2\right)} \]
    3. Simplified91.0%

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

      \[\leadsto \color{blue}{R \cdot \phi_2} \]
    6. Step-by-step derivation
      1. *-commutative61.6%

        \[\leadsto \color{blue}{\phi_2 \cdot R} \]
    7. Simplified61.6%

      \[\leadsto \color{blue}{\phi_2 \cdot R} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification33.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\phi_2 \leq 0.0078:\\ \;\;\;\;R \cdot \left(-\phi_1\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \phi_2\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 29.3% accurate, 65.8× speedup?

\[\begin{array}{l} R_m = \left|R\right| \\ R_s = \mathsf{copysign}\left(1, R\right) \\ R_s \cdot \left(R_m \cdot \left(\phi_2 - \phi_1\right)\right) \end{array} \]
R_m = (fabs.f64 R)
R_s = (copysign.f64 1 R)
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R_s (* R_m (- phi2 phi1))))
R_m = fabs(R);
R_s = copysign(1.0, R);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (R_m * (phi2 - phi1));
}
R_m = abs(R)
R_s = copysign(1.0d0, R)
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = r_s * (r_m * (phi2 - phi1))
end function
R_m = Math.abs(R);
R_s = Math.copySign(1.0, R);
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (R_m * (phi2 - phi1));
}
R_m = math.fabs(R)
R_s = math.copysign(1.0, R)
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	return R_s * (R_m * (phi2 - phi1))
R_m = abs(R)
R_s = copysign(1.0, R)
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	return Float64(R_s * Float64(R_m * Float64(phi2 - phi1)))
end
R_m = abs(R);
R_s = sign(R) * abs(1.0);
function tmp = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = R_s * (R_m * (phi2 - phi1));
end
R_m = N[Abs[R], $MachinePrecision]
R_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * N[(R$95$m * N[(phi2 - phi1), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
R_m = \left|R\right|
\\
R_s = \mathsf{copysign}\left(1, R\right)

\\
R_s \cdot \left(R_m \cdot \left(\phi_2 - \phi_1\right)\right)
\end{array}
Derivation
  1. Initial program 60.2%

    \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
  2. Step-by-step derivation
    1. hypot-def96.2%

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

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

    \[\leadsto \color{blue}{-1 \cdot \left(R \cdot \phi_1\right) + R \cdot \phi_2} \]
  6. Step-by-step derivation
    1. +-commutative33.7%

      \[\leadsto \color{blue}{R \cdot \phi_2 + -1 \cdot \left(R \cdot \phi_1\right)} \]
    2. mul-1-neg33.7%

      \[\leadsto R \cdot \phi_2 + \color{blue}{\left(-R \cdot \phi_1\right)} \]
    3. unsub-neg33.7%

      \[\leadsto \color{blue}{R \cdot \phi_2 - R \cdot \phi_1} \]
    4. *-commutative33.7%

      \[\leadsto \color{blue}{\phi_2 \cdot R} - R \cdot \phi_1 \]
    5. *-commutative33.7%

      \[\leadsto \phi_2 \cdot R - \color{blue}{\phi_1 \cdot R} \]
  7. Simplified33.7%

    \[\leadsto \color{blue}{\phi_2 \cdot R - \phi_1 \cdot R} \]
  8. Taylor expanded in R around -inf 34.5%

    \[\leadsto \color{blue}{-1 \cdot \left(R \cdot \left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right)\right)} \]
  9. Step-by-step derivation
    1. associate-*r*34.5%

      \[\leadsto \color{blue}{\left(-1 \cdot R\right) \cdot \left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right)} \]
    2. neg-mul-134.5%

      \[\leadsto \color{blue}{\left(-R\right)} \cdot \left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right) \]
    3. distribute-lft-out--34.5%

      \[\leadsto \left(-R\right) \cdot \color{blue}{\left(-1 \cdot \left(\phi_2 - \phi_1\right)\right)} \]
    4. neg-mul-134.5%

      \[\leadsto \left(-R\right) \cdot \color{blue}{\left(-\left(\phi_2 - \phi_1\right)\right)} \]
    5. distribute-lft-neg-in34.5%

      \[\leadsto \color{blue}{-R \cdot \left(-\left(\phi_2 - \phi_1\right)\right)} \]
    6. neg-mul-134.5%

      \[\leadsto -R \cdot \color{blue}{\left(-1 \cdot \left(\phi_2 - \phi_1\right)\right)} \]
    7. distribute-lft-out--34.5%

      \[\leadsto -R \cdot \color{blue}{\left(-1 \cdot \phi_2 - -1 \cdot \phi_1\right)} \]
    8. cancel-sign-sub-inv34.5%

      \[\leadsto -R \cdot \color{blue}{\left(-1 \cdot \phi_2 + \left(--1\right) \cdot \phi_1\right)} \]
    9. metadata-eval34.5%

      \[\leadsto -R \cdot \left(-1 \cdot \phi_2 + \color{blue}{1} \cdot \phi_1\right) \]
    10. *-lft-identity34.5%

      \[\leadsto -R \cdot \left(-1 \cdot \phi_2 + \color{blue}{\phi_1}\right) \]
    11. distribute-lft-in33.7%

      \[\leadsto -\color{blue}{\left(R \cdot \left(-1 \cdot \phi_2\right) + R \cdot \phi_1\right)} \]
    12. mul-1-neg33.7%

      \[\leadsto -\left(R \cdot \color{blue}{\left(-\phi_2\right)} + R \cdot \phi_1\right) \]
    13. distribute-rgt-neg-in33.7%

      \[\leadsto -\left(\color{blue}{\left(-R \cdot \phi_2\right)} + R \cdot \phi_1\right) \]
    14. mul-1-neg33.7%

      \[\leadsto -\left(\color{blue}{-1 \cdot \left(R \cdot \phi_2\right)} + R \cdot \phi_1\right) \]
    15. +-commutative33.7%

      \[\leadsto -\color{blue}{\left(R \cdot \phi_1 + -1 \cdot \left(R \cdot \phi_2\right)\right)} \]
    16. mul-1-neg33.7%

      \[\leadsto -\left(R \cdot \phi_1 + \color{blue}{\left(-R \cdot \phi_2\right)}\right) \]
    17. distribute-rgt-neg-in33.7%

      \[\leadsto -\left(R \cdot \phi_1 + \color{blue}{R \cdot \left(-\phi_2\right)}\right) \]
    18. mul-1-neg33.7%

      \[\leadsto -\left(R \cdot \phi_1 + R \cdot \color{blue}{\left(-1 \cdot \phi_2\right)}\right) \]
    19. distribute-lft-in34.5%

      \[\leadsto -\color{blue}{R \cdot \left(\phi_1 + -1 \cdot \phi_2\right)} \]
    20. mul-1-neg34.5%

      \[\leadsto -R \cdot \left(\phi_1 + \color{blue}{\left(-\phi_2\right)}\right) \]
    21. sub-neg34.5%

      \[\leadsto -R \cdot \color{blue}{\left(\phi_1 - \phi_2\right)} \]
    22. *-commutative34.5%

      \[\leadsto -\color{blue}{\left(\phi_1 - \phi_2\right) \cdot R} \]
  10. Simplified34.5%

    \[\leadsto \color{blue}{-\left(\phi_1 - \phi_2\right) \cdot R} \]
  11. Final simplification34.5%

    \[\leadsto R \cdot \left(\phi_2 - \phi_1\right) \]
  12. Add Preprocessing

Alternative 10: 16.9% accurate, 109.7× speedup?

\[\begin{array}{l} R_m = \left|R\right| \\ R_s = \mathsf{copysign}\left(1, R\right) \\ R_s \cdot \left(R_m \cdot \phi_2\right) \end{array} \]
R_m = (fabs.f64 R)
R_s = (copysign.f64 1 R)
(FPCore (R_s R_m lambda1 lambda2 phi1 phi2)
 :precision binary64
 (* R_s (* R_m phi2)))
R_m = fabs(R);
R_s = copysign(1.0, R);
double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (R_m * phi2);
}
R_m = abs(R)
R_s = copysign(1.0d0, R)
real(8) function code(r_s, r_m, lambda1, lambda2, phi1, phi2)
    real(8), intent (in) :: r_s
    real(8), intent (in) :: r_m
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = r_s * (r_m * phi2)
end function
R_m = Math.abs(R);
R_s = Math.copySign(1.0, R);
public static double code(double R_s, double R_m, double lambda1, double lambda2, double phi1, double phi2) {
	return R_s * (R_m * phi2);
}
R_m = math.fabs(R)
R_s = math.copysign(1.0, R)
def code(R_s, R_m, lambda1, lambda2, phi1, phi2):
	return R_s * (R_m * phi2)
R_m = abs(R)
R_s = copysign(1.0, R)
function code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	return Float64(R_s * Float64(R_m * phi2))
end
R_m = abs(R);
R_s = sign(R) * abs(1.0);
function tmp = code(R_s, R_m, lambda1, lambda2, phi1, phi2)
	tmp = R_s * (R_m * phi2);
end
R_m = N[Abs[R], $MachinePrecision]
R_s = N[With[{TMP1 = Abs[1.0], TMP2 = Sign[R]}, TMP1 * If[TMP2 == 0, 1, TMP2]], $MachinePrecision]
code[R$95$s_, R$95$m_, lambda1_, lambda2_, phi1_, phi2_] := N[(R$95$s * N[(R$95$m * phi2), $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
R_m = \left|R\right|
\\
R_s = \mathsf{copysign}\left(1, R\right)

\\
R_s \cdot \left(R_m \cdot \phi_2\right)
\end{array}
Derivation
  1. Initial program 60.2%

    \[R \cdot \sqrt{\left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) \cdot \left(\left(\lambda_1 - \lambda_2\right) \cdot \cos \left(\frac{\phi_1 + \phi_2}{2}\right)\right) + \left(\phi_1 - \phi_2\right) \cdot \left(\phi_1 - \phi_2\right)} \]
  2. Step-by-step derivation
    1. hypot-def96.2%

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

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

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

      \[\leadsto \color{blue}{\phi_2 \cdot R} \]
  7. Simplified19.4%

    \[\leadsto \color{blue}{\phi_2 \cdot R} \]
  8. Final simplification19.4%

    \[\leadsto R \cdot \phi_2 \]
  9. Add Preprocessing

Reproduce

?
herbie shell --seed 2024024 
(FPCore (R lambda1 lambda2 phi1 phi2)
  :name "Equirectangular approximation to distance on a great circle"
  :precision binary64
  (* R (sqrt (+ (* (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0))) (* (- lambda1 lambda2) (cos (/ (+ phi1 phi2) 2.0)))) (* (- phi1 phi2) (- phi1 phi2))))))