?

Average Error: 16.8 → 16.1
Time: 28.0s
Precision: binary64
Cost: 78404

?

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

\mathbf{else}:\\
\;\;\;\;t_0 \cdot R\\


\end{array}

Error?

Try it out?

Your Program's Arguments

Results

Enter valid numbers for all inputs

Derivation?

  1. Split input into 2 regimes
  2. if (acos.f64 (+.f64 (*.f64 (sin.f64 phi1) (sin.f64 phi2)) (*.f64 (*.f64 (cos.f64 phi1) (cos.f64 phi2)) (cos.f64 (-.f64 lambda1 lambda2))))) < 0.0

    1. Initial program 53.5

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

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

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

      [Start]53.5

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

      trig.json-simplify-23 [=>]53.5

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

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

      \[\leadsto \cos^{-1} \color{blue}{\cos \lambda_2} \cdot R \]
    6. Taylor expanded in lambda2 around 0 40.6

      \[\leadsto \color{blue}{\lambda_2} \cdot R \]

    if 0.0 < (acos.f64 (+.f64 (*.f64 (sin.f64 phi1) (sin.f64 phi2)) (*.f64 (*.f64 (cos.f64 phi1) (cos.f64 phi2)) (cos.f64 (-.f64 lambda1 lambda2)))))

    1. Initial program 14.6

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \leq 0:\\ \;\;\;\;\lambda_2 \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \end{array} \]

Alternatives

Alternative 1
Error23.9
Cost39368
\[\begin{array}{l} t_0 := \cos \phi_1 \cdot \cos \phi_2\\ t_1 := \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + t_0 \cdot \cos \lambda_1\right) \cdot R\\ \mathbf{if}\;\phi_1 \leq -680000000000:\\ \;\;\;\;t_1\\ \mathbf{elif}\;\phi_1 \leq 5 \cdot 10^{-32}:\\ \;\;\;\;\cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + t_0 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;t_1\\ \end{array} \]
Alternative 2
Error16.9
Cost39236
\[\begin{array}{l} t_0 := \cos \phi_1 \cdot \cos \phi_2\\ t_1 := \sin \phi_1 \cdot \sin \phi_2\\ \mathbf{if}\;\lambda_1 \leq -1.05 \cdot 10^{-5}:\\ \;\;\;\;\cos^{-1} \left(t_1 + t_0 \cdot \cos \lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(t_1 + t_0 \cdot \cos \lambda_2\right) \cdot R\\ \end{array} \]
Alternative 3
Error16.8
Cost39232
\[\cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_1 \cdot \left(\cos \phi_2 \cdot \cos \left(\lambda_1 - \lambda_2\right)\right)\right) \cdot R \]
Alternative 4
Error29.8
Cost33096
\[\begin{array}{l} t_0 := \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_2 \cdot \cos \phi_1\right) \cdot R\\ \mathbf{if}\;\phi_1 \leq -3.1 \cdot 10^{+33}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\phi_1 \leq 2500:\\ \;\;\;\;\cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 5
Error29.6
Cost33096
\[\begin{array}{l} t_0 := \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_2 \cdot \cos \phi_1\right) \cdot R\\ \mathbf{if}\;\phi_2 \leq -8 \cdot 10^{-6}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\phi_2 \leq 8.5 \cdot 10^{+27}:\\ \;\;\;\;\cos^{-1} \left(\sin \phi_1 \cdot \phi_2 + \left(\cos \phi_1 \cdot \cos \phi_2\right) \cdot \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 4.2 \cdot 10^{+177}:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_2\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 6
Error30.0
Cost32840
\[\begin{array}{l} t_0 := \cos^{-1} \left(\sin \phi_1 \cdot \sin \phi_2 + \cos \phi_2 \cdot \cos \phi_1\right) \cdot R\\ \mathbf{if}\;\phi_1 \leq -0.32:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\phi_1 \leq 0.052:\\ \;\;\;\;\cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 7
Error39.3
Cost26568
\[\begin{array}{l} \mathbf{if}\;\phi_2 \leq -8.5 \cdot 10^{-182}:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_2\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 2.8 \cdot 10^{-11}:\\ \;\;\;\;\cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_2\right) \cdot R\\ \end{array} \]
Alternative 8
Error36.1
Cost26436
\[\begin{array}{l} \mathbf{if}\;\phi_1 \leq -0.32:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\phi_1 \cdot \sin \phi_2 + \cos \left(\lambda_1 - \lambda_2\right) \cdot \cos \phi_2\right) \cdot R\\ \end{array} \]
Alternative 9
Error41.8
Cost19784
\[\begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.06 \cdot 10^{-188}:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 2.9 \cdot 10^{-11}:\\ \;\;\;\;\cos^{-1} \left(\phi_1 \cdot \phi_2 + \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_2 \cdot \cos \lambda_2\right) \cdot R\\ \end{array} \]
Alternative 10
Error43.5
Cost19652
\[\begin{array}{l} \mathbf{if}\;\phi_2 \leq 1.1 \cdot 10^{-188}:\\ \;\;\;\;\cos^{-1} \left(\cos \phi_1 \cdot \cos \lambda_1\right) \cdot R\\ \mathbf{elif}\;\phi_2 \leq 4.2 \cdot 10^{-11}:\\ \;\;\;\;\cos^{-1} \left(\phi_1 \cdot \phi_2 + \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \cos \phi_2 \cdot R\\ \end{array} \]
Alternative 11
Error48.4
Cost13640
\[\begin{array}{l} \mathbf{if}\;\phi_2 \leq -1.6 \cdot 10^{-254}:\\ \;\;\;\;\cos^{-1} \cos \phi_1 \cdot R\\ \mathbf{elif}\;\phi_2 \leq 4.2 \cdot 10^{-11}:\\ \;\;\;\;\cos^{-1} \left(\phi_1 \cdot \phi_2 + \cos \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \cos \phi_2 \cdot R\\ \end{array} \]
Alternative 12
Error47.4
Cost13388
\[\begin{array}{l} t_0 := \cos^{-1} \cos \lambda_1 \cdot R\\ \mathbf{if}\;\lambda_2 \leq 3.5 \cdot 10^{-135}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\lambda_2 \leq 1.8 \cdot 10^{-31}:\\ \;\;\;\;\lambda_2 \cdot R\\ \mathbf{elif}\;\lambda_2 \leq 1.25 \cdot 10^{-5}:\\ \;\;\;\;t_0\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \cos \lambda_2 \cdot R\\ \end{array} \]
Alternative 13
Error51.8
Cost13256
\[\begin{array}{l} t_0 := \cos^{-1} \cos \lambda_1 \cdot R\\ \mathbf{if}\;\lambda_1 \leq -1.2 \cdot 10^{-9}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;\lambda_1 \leq 1.22 \cdot 10^{-112}:\\ \;\;\;\;\lambda_2 \cdot R\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \]
Alternative 14
Error46.1
Cost13256
\[\begin{array}{l} \mathbf{if}\;\lambda_2 \leq 2.9 \cdot 10^{-233}:\\ \;\;\;\;\cos^{-1} \cos \lambda_1 \cdot R\\ \mathbf{elif}\;\lambda_2 \leq 7.8 \cdot 10^{-12}:\\ \;\;\;\;\cos^{-1} \cos \phi_1 \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \cos \lambda_2 \cdot R\\ \end{array} \]
Alternative 15
Error46.3
Cost13256
\[\begin{array}{l} \mathbf{if}\;\lambda_2 \leq 1.56 \cdot 10^{-233}:\\ \;\;\;\;\cos^{-1} \cos \lambda_1 \cdot R\\ \mathbf{elif}\;\lambda_2 \leq 5.8 \cdot 10^{-16}:\\ \;\;\;\;\cos^{-1} \cos \phi_2 \cdot R\\ \mathbf{else}:\\ \;\;\;\;\cos^{-1} \cos \lambda_2 \cdot R\\ \end{array} \]
Alternative 16
Error60.0
Cost192
\[\lambda_2 \cdot R \]

Error

Reproduce?

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