Equirectangular approximation to distance on a great circle

Percentage Accurate: 59.4% → 99.9%
Time: 7.2s
Alternatives: 10
Speedup: 1.7×

Specification

?
\[\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} \]
(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))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    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}
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}

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: 59.4% accurate, 1.0× speedup?

\[\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} \]
(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))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: t_0
    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}
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}

Alternative 1: 99.9% accurate, 0.6× speedup?

\[\mathsf{hypot}\left(\phi_2 - \phi_1, \mathsf{fma}\left(\lambda_1 - \lambda_2, \sin \left(-0.5 \cdot \phi_2\right) \cdot \sin \left(0.5 \cdot \phi_1\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \left(0.5 \cdot \phi_2\right) \cdot \cos \left(-0.5 \cdot \phi_1\right)\right)\right)\right) \cdot R \]
(FPCore (R lambda1 lambda2 phi1 phi2)
  :precision binary64
  (*
 (hypot
  (- phi2 phi1)
  (fma
   (- lambda1 lambda2)
   (* (sin (* -0.5 phi2)) (sin (* 0.5 phi1)))
   (*
    (- lambda1 lambda2)
    (* (cos (* 0.5 phi2)) (cos (* -0.5 phi1))))))
 R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return hypot((phi2 - phi1), fma((lambda1 - lambda2), (sin((-0.5 * phi2)) * sin((0.5 * phi1))), ((lambda1 - lambda2) * (cos((0.5 * phi2)) * cos((-0.5 * phi1)))))) * R;
}
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(hypot(Float64(phi2 - phi1), fma(Float64(lambda1 - lambda2), Float64(sin(Float64(-0.5 * phi2)) * sin(Float64(0.5 * phi1))), Float64(Float64(lambda1 - lambda2) * Float64(cos(Float64(0.5 * phi2)) * cos(Float64(-0.5 * phi1)))))) * R)
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[Sqrt[N[(phi2 - phi1), $MachinePrecision] ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(N[Sin[N[(-0.5 * phi2), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(N[Cos[N[(0.5 * phi2), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(-0.5 * phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision] * R), $MachinePrecision]
\mathsf{hypot}\left(\phi_2 - \phi_1, \mathsf{fma}\left(\lambda_1 - \lambda_2, \sin \left(-0.5 \cdot \phi_2\right) \cdot \sin \left(0.5 \cdot \phi_1\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\cos \left(0.5 \cdot \phi_2\right) \cdot \cos \left(-0.5 \cdot \phi_1\right)\right)\right)\right) \cdot R
Derivation
  1. Initial program 59.4%

    \[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. lift-*.f64N/A

      \[\leadsto \color{blue}{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. *-commutativeN/A

      \[\leadsto \color{blue}{\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)} \cdot R} \]
    3. lower-*.f6459.4%

      \[\leadsto \color{blue}{\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)} \cdot R} \]
  3. Applied rewrites95.8%

    \[\leadsto \color{blue}{\mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot -0.5\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R} \]
  4. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot \frac{-1}{2}\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
    2. flip--N/A

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot \frac{-1}{2}\right) \cdot \color{blue}{\frac{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}{\lambda_1 + \lambda_2}}\right) \cdot R \]
    3. lower-unsound-/.f64N/A

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot \frac{-1}{2}\right) \cdot \color{blue}{\frac{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}{\lambda_1 + \lambda_2}}\right) \cdot R \]
    4. lower-unsound--.f64N/A

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot \frac{-1}{2}\right) \cdot \frac{\color{blue}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}{\lambda_1 + \lambda_2}\right) \cdot R \]
    5. lower-unsound-*.f64N/A

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot \frac{-1}{2}\right) \cdot \frac{\color{blue}{\lambda_1 \cdot \lambda_1} - \lambda_2 \cdot \lambda_2}{\lambda_1 + \lambda_2}\right) \cdot R \]
    6. lower-unsound-*.f64N/A

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

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

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

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

Alternative 2: 99.9% accurate, 0.6× speedup?

\[\mathsf{hypot}\left(\phi_2 - \phi_1, \mathsf{fma}\left(\lambda_1 - \lambda_2, \cos \left(0.5 \cdot \phi_2\right) \cdot \cos \left(-0.5 \cdot \phi_1\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\sin \left(-0.5 \cdot \phi_2\right) \cdot \sin \left(0.5 \cdot \phi_1\right)\right)\right)\right) \cdot R \]
(FPCore (R lambda1 lambda2 phi1 phi2)
  :precision binary64
  (*
 (hypot
  (- phi2 phi1)
  (fma
   (- lambda1 lambda2)
   (* (cos (* 0.5 phi2)) (cos (* -0.5 phi1)))
   (*
    (- lambda1 lambda2)
    (* (sin (* -0.5 phi2)) (sin (* 0.5 phi1))))))
 R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return hypot((phi2 - phi1), fma((lambda1 - lambda2), (cos((0.5 * phi2)) * cos((-0.5 * phi1))), ((lambda1 - lambda2) * (sin((-0.5 * phi2)) * sin((0.5 * phi1)))))) * R;
}
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(hypot(Float64(phi2 - phi1), fma(Float64(lambda1 - lambda2), Float64(cos(Float64(0.5 * phi2)) * cos(Float64(-0.5 * phi1))), Float64(Float64(lambda1 - lambda2) * Float64(sin(Float64(-0.5 * phi2)) * sin(Float64(0.5 * phi1)))))) * R)
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[Sqrt[N[(phi2 - phi1), $MachinePrecision] ^ 2 + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(N[Cos[N[(0.5 * phi2), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(-0.5 * phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision] + N[(N[(lambda1 - lambda2), $MachinePrecision] * N[(N[Sin[N[(-0.5 * phi2), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(0.5 * phi1), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision] * R), $MachinePrecision]
\mathsf{hypot}\left(\phi_2 - \phi_1, \mathsf{fma}\left(\lambda_1 - \lambda_2, \cos \left(0.5 \cdot \phi_2\right) \cdot \cos \left(-0.5 \cdot \phi_1\right), \left(\lambda_1 - \lambda_2\right) \cdot \left(\sin \left(-0.5 \cdot \phi_2\right) \cdot \sin \left(0.5 \cdot \phi_1\right)\right)\right)\right) \cdot R
Derivation
  1. Initial program 59.4%

    \[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. lift-*.f64N/A

      \[\leadsto \color{blue}{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. *-commutativeN/A

      \[\leadsto \color{blue}{\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)} \cdot R} \]
    3. lower-*.f6459.4%

      \[\leadsto \color{blue}{\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)} \cdot R} \]
  3. Applied rewrites95.8%

    \[\leadsto \color{blue}{\mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot -0.5\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R} \]
  4. Step-by-step derivation
    1. lift--.f64N/A

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot \frac{-1}{2}\right) \cdot \color{blue}{\left(\lambda_1 - \lambda_2\right)}\right) \cdot R \]
    2. flip--N/A

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot \frac{-1}{2}\right) \cdot \color{blue}{\frac{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}{\lambda_1 + \lambda_2}}\right) \cdot R \]
    3. lower-unsound-/.f64N/A

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot \frac{-1}{2}\right) \cdot \color{blue}{\frac{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}{\lambda_1 + \lambda_2}}\right) \cdot R \]
    4. lower-unsound--.f64N/A

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot \frac{-1}{2}\right) \cdot \frac{\color{blue}{\lambda_1 \cdot \lambda_1 - \lambda_2 \cdot \lambda_2}}{\lambda_1 + \lambda_2}\right) \cdot R \]
    5. lower-unsound-*.f64N/A

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot \frac{-1}{2}\right) \cdot \frac{\color{blue}{\lambda_1 \cdot \lambda_1} - \lambda_2 \cdot \lambda_2}{\lambda_1 + \lambda_2}\right) \cdot R \]
    6. lower-unsound-*.f64N/A

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

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

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

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

Alternative 3: 95.8% accurate, 1.4× speedup?

\[\begin{array}{l} t_0 := \mathsf{max}\left(\phi_1, \phi_2\right) - \mathsf{min}\left(\phi_1, \phi_2\right)\\ \mathbf{if}\;\mathsf{min}\left(\phi_1, \phi_2\right) \leq -8.6 \cdot 10^{-7}:\\ \;\;\;\;\mathsf{hypot}\left(t\_0, \cos \left(-0.5 \cdot \mathsf{min}\left(\phi_1, \phi_2\right)\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \mathbf{else}:\\ \;\;\;\;\mathsf{hypot}\left(t\_0, \cos \left(-0.5 \cdot \mathsf{max}\left(\phi_1, \phi_2\right)\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\ \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
  :precision binary64
  (let* ((t_0 (- (fmax phi1 phi2) (fmin phi1 phi2))))
  (if (<= (fmin phi1 phi2) -8.6e-7)
    (*
     (hypot
      t_0
      (* (cos (* -0.5 (fmin phi1 phi2))) (- lambda1 lambda2)))
     R)
    (*
     (hypot
      t_0
      (* (cos (* -0.5 (fmax phi1 phi2))) (- lambda1 lambda2)))
     R))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = fmax(phi1, phi2) - fmin(phi1, phi2);
	double tmp;
	if (fmin(phi1, phi2) <= -8.6e-7) {
		tmp = hypot(t_0, (cos((-0.5 * fmin(phi1, phi2))) * (lambda1 - lambda2))) * R;
	} else {
		tmp = hypot(t_0, (cos((-0.5 * fmax(phi1, phi2))) * (lambda1 - lambda2))) * R;
	}
	return tmp;
}
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double t_0 = fmax(phi1, phi2) - fmin(phi1, phi2);
	double tmp;
	if (fmin(phi1, phi2) <= -8.6e-7) {
		tmp = Math.hypot(t_0, (Math.cos((-0.5 * fmin(phi1, phi2))) * (lambda1 - lambda2))) * R;
	} else {
		tmp = Math.hypot(t_0, (Math.cos((-0.5 * fmax(phi1, phi2))) * (lambda1 - lambda2))) * R;
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	t_0 = fmax(phi1, phi2) - fmin(phi1, phi2)
	tmp = 0
	if fmin(phi1, phi2) <= -8.6e-7:
		tmp = math.hypot(t_0, (math.cos((-0.5 * fmin(phi1, phi2))) * (lambda1 - lambda2))) * R
	else:
		tmp = math.hypot(t_0, (math.cos((-0.5 * fmax(phi1, phi2))) * (lambda1 - lambda2))) * R
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	t_0 = Float64(fmax(phi1, phi2) - fmin(phi1, phi2))
	tmp = 0.0
	if (fmin(phi1, phi2) <= -8.6e-7)
		tmp = Float64(hypot(t_0, Float64(cos(Float64(-0.5 * fmin(phi1, phi2))) * Float64(lambda1 - lambda2))) * R);
	else
		tmp = Float64(hypot(t_0, Float64(cos(Float64(-0.5 * fmax(phi1, phi2))) * Float64(lambda1 - lambda2))) * R);
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	t_0 = max(phi1, phi2) - min(phi1, phi2);
	tmp = 0.0;
	if (min(phi1, phi2) <= -8.6e-7)
		tmp = hypot(t_0, (cos((-0.5 * min(phi1, phi2))) * (lambda1 - lambda2))) * R;
	else
		tmp = hypot(t_0, (cos((-0.5 * max(phi1, phi2))) * (lambda1 - lambda2))) * R;
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := Block[{t$95$0 = N[(N[Max[phi1, phi2], $MachinePrecision] - N[Min[phi1, phi2], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[N[Min[phi1, phi2], $MachinePrecision], -8.6e-7], N[(N[Sqrt[t$95$0 ^ 2 + N[(N[Cos[N[(-0.5 * N[Min[phi1, phi2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision] * R), $MachinePrecision], N[(N[Sqrt[t$95$0 ^ 2 + N[(N[Cos[N[(-0.5 * N[Max[phi1, phi2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision] * R), $MachinePrecision]]]
\begin{array}{l}
t_0 := \mathsf{max}\left(\phi_1, \phi_2\right) - \mathsf{min}\left(\phi_1, \phi_2\right)\\
\mathbf{if}\;\mathsf{min}\left(\phi_1, \phi_2\right) \leq -8.6 \cdot 10^{-7}:\\
\;\;\;\;\mathsf{hypot}\left(t\_0, \cos \left(-0.5 \cdot \mathsf{min}\left(\phi_1, \phi_2\right)\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R\\

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


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

    1. Initial program 59.4%

      \[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. lift-*.f64N/A

        \[\leadsto \color{blue}{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. *-commutativeN/A

        \[\leadsto \color{blue}{\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)} \cdot R} \]
      3. lower-*.f6459.4%

        \[\leadsto \color{blue}{\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)} \cdot R} \]
    3. Applied rewrites95.8%

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

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \color{blue}{\left(\frac{-1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    5. Step-by-step derivation
      1. lower-*.f6490.3%

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

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

    if -8.6000000000000002e-7 < phi1

    1. Initial program 59.4%

      \[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. lift-*.f64N/A

        \[\leadsto \color{blue}{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. *-commutativeN/A

        \[\leadsto \color{blue}{\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)} \cdot R} \]
      3. lower-*.f6459.4%

        \[\leadsto \color{blue}{\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)} \cdot R} \]
    3. Applied rewrites95.8%

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

      \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \color{blue}{\left(\frac{-1}{2} \cdot \phi_2\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
    5. Step-by-step derivation
      1. lower-*.f6489.8%

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

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

Alternative 4: 95.7% accurate, 1.7× speedup?

\[\mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot -0.5\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
(FPCore (R lambda1 lambda2 phi1 phi2)
  :precision binary64
  (*
 (hypot
  (- phi2 phi1)
  (* (cos (* (+ phi2 phi1) -0.5)) (- lambda1 lambda2)))
 R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return hypot((phi2 - phi1), (cos(((phi2 + phi1) * -0.5)) * (lambda1 - lambda2))) * R;
}
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return Math.hypot((phi2 - phi1), (Math.cos(((phi2 + phi1) * -0.5)) * (lambda1 - lambda2))) * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return math.hypot((phi2 - phi1), (math.cos(((phi2 + phi1) * -0.5)) * (lambda1 - lambda2))) * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(hypot(Float64(phi2 - phi1), Float64(cos(Float64(Float64(phi2 + phi1) * -0.5)) * Float64(lambda1 - lambda2))) * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = hypot((phi2 - phi1), (cos(((phi2 + phi1) * -0.5)) * (lambda1 - lambda2))) * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[Sqrt[N[(phi2 - phi1), $MachinePrecision] ^ 2 + N[(N[Cos[N[(N[(phi2 + phi1), $MachinePrecision] * -0.5), $MachinePrecision]], $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision] * R), $MachinePrecision]
\mathsf{hypot}\left(\phi_2 - \phi_1, \cos \left(\left(\phi_2 + \phi_1\right) \cdot -0.5\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R
Derivation
  1. Initial program 59.4%

    \[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. lift-*.f64N/A

      \[\leadsto \color{blue}{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. *-commutativeN/A

      \[\leadsto \color{blue}{\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)} \cdot R} \]
    3. lower-*.f6459.4%

      \[\leadsto \color{blue}{\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)} \cdot R} \]
  3. Applied rewrites95.8%

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

Alternative 5: 89.8% accurate, 1.5× speedup?

\[\mathsf{hypot}\left(\mathsf{max}\left(\phi_1, \phi_2\right) - \mathsf{min}\left(\phi_1, \phi_2\right), \cos \left(-0.5 \cdot \mathsf{min}\left(\phi_1, \phi_2\right)\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
(FPCore (R lambda1 lambda2 phi1 phi2)
  :precision binary64
  (*
 (hypot
  (- (fmax phi1 phi2) (fmin phi1 phi2))
  (* (cos (* -0.5 (fmin phi1 phi2))) (- lambda1 lambda2)))
 R))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return hypot((fmax(phi1, phi2) - fmin(phi1, phi2)), (cos((-0.5 * fmin(phi1, phi2))) * (lambda1 - lambda2))) * R;
}
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return Math.hypot((fmax(phi1, phi2) - fmin(phi1, phi2)), (Math.cos((-0.5 * fmin(phi1, phi2))) * (lambda1 - lambda2))) * R;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return math.hypot((fmax(phi1, phi2) - fmin(phi1, phi2)), (math.cos((-0.5 * fmin(phi1, phi2))) * (lambda1 - lambda2))) * R
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(hypot(Float64(fmax(phi1, phi2) - fmin(phi1, phi2)), Float64(cos(Float64(-0.5 * fmin(phi1, phi2))) * Float64(lambda1 - lambda2))) * R)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = hypot((max(phi1, phi2) - min(phi1, phi2)), (cos((-0.5 * min(phi1, phi2))) * (lambda1 - lambda2))) * R;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(N[Sqrt[N[(N[Max[phi1, phi2], $MachinePrecision] - N[Min[phi1, phi2], $MachinePrecision]), $MachinePrecision] ^ 2 + N[(N[Cos[N[(-0.5 * N[Min[phi1, phi2], $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[(lambda1 - lambda2), $MachinePrecision]), $MachinePrecision] ^ 2], $MachinePrecision] * R), $MachinePrecision]
\mathsf{hypot}\left(\mathsf{max}\left(\phi_1, \phi_2\right) - \mathsf{min}\left(\phi_1, \phi_2\right), \cos \left(-0.5 \cdot \mathsf{min}\left(\phi_1, \phi_2\right)\right) \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R
Derivation
  1. Initial program 59.4%

    \[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. lift-*.f64N/A

      \[\leadsto \color{blue}{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. *-commutativeN/A

      \[\leadsto \color{blue}{\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)} \cdot R} \]
    3. lower-*.f6459.4%

      \[\leadsto \color{blue}{\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)} \cdot R} \]
  3. Applied rewrites95.8%

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

    \[\leadsto \mathsf{hypot}\left(\phi_2 - \phi_1, \cos \color{blue}{\left(\frac{-1}{2} \cdot \phi_1\right)} \cdot \left(\lambda_1 - \lambda_2\right)\right) \cdot R \]
  5. Step-by-step derivation
    1. lower-*.f6490.3%

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

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

Alternative 6: 56.4% accurate, 3.2× speedup?

\[\begin{array}{l} \mathbf{if}\;\mathsf{max}\left(\phi_1, \phi_2\right) \leq 3.4 \cdot 10^{+187}:\\ \;\;\;\;-1 \cdot \left(\mathsf{min}\left(\phi_1, \phi_2\right) \cdot \left(R + -1 \cdot \frac{R \cdot \mathsf{max}\left(\phi_1, \phi_2\right)}{\mathsf{min}\left(\phi_1, \phi_2\right)}\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\mathsf{max}\left(\phi_1, \phi_2\right) \cdot \left(1 + -1 \cdot \frac{\mathsf{min}\left(\phi_1, \phi_2\right)}{\mathsf{max}\left(\phi_1, \phi_2\right)}\right)\right)\\ \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
  :precision binary64
  (if (<= (fmax phi1 phi2) 3.4e+187)
  (*
   -1.0
   (*
    (fmin phi1 phi2)
    (+ R (* -1.0 (/ (* R (fmax phi1 phi2)) (fmin phi1 phi2))))))
  (*
   R
   (*
    (fmax phi1 phi2)
    (+ 1.0 (* -1.0 (/ (fmin phi1 phi2) (fmax phi1 phi2))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (fmax(phi1, phi2) <= 3.4e+187) {
		tmp = -1.0 * (fmin(phi1, phi2) * (R + (-1.0 * ((R * fmax(phi1, phi2)) / fmin(phi1, phi2)))));
	} else {
		tmp = R * (fmax(phi1, phi2) * (1.0 + (-1.0 * (fmin(phi1, phi2) / fmax(phi1, phi2)))));
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (fmax(phi1, phi2) <= 3.4d+187) then
        tmp = (-1.0d0) * (fmin(phi1, phi2) * (r + ((-1.0d0) * ((r * fmax(phi1, phi2)) / fmin(phi1, phi2)))))
    else
        tmp = r * (fmax(phi1, phi2) * (1.0d0 + ((-1.0d0) * (fmin(phi1, phi2) / fmax(phi1, phi2)))))
    end if
    code = tmp
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (fmax(phi1, phi2) <= 3.4e+187) {
		tmp = -1.0 * (fmin(phi1, phi2) * (R + (-1.0 * ((R * fmax(phi1, phi2)) / fmin(phi1, phi2)))));
	} else {
		tmp = R * (fmax(phi1, phi2) * (1.0 + (-1.0 * (fmin(phi1, phi2) / fmax(phi1, phi2)))));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if fmax(phi1, phi2) <= 3.4e+187:
		tmp = -1.0 * (fmin(phi1, phi2) * (R + (-1.0 * ((R * fmax(phi1, phi2)) / fmin(phi1, phi2)))))
	else:
		tmp = R * (fmax(phi1, phi2) * (1.0 + (-1.0 * (fmin(phi1, phi2) / fmax(phi1, phi2)))))
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (fmax(phi1, phi2) <= 3.4e+187)
		tmp = Float64(-1.0 * Float64(fmin(phi1, phi2) * Float64(R + Float64(-1.0 * Float64(Float64(R * fmax(phi1, phi2)) / fmin(phi1, phi2))))));
	else
		tmp = Float64(R * Float64(fmax(phi1, phi2) * Float64(1.0 + Float64(-1.0 * Float64(fmin(phi1, phi2) / fmax(phi1, phi2))))));
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (max(phi1, phi2) <= 3.4e+187)
		tmp = -1.0 * (min(phi1, phi2) * (R + (-1.0 * ((R * max(phi1, phi2)) / min(phi1, phi2)))));
	else
		tmp = R * (max(phi1, phi2) * (1.0 + (-1.0 * (min(phi1, phi2) / max(phi1, phi2)))));
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[N[Max[phi1, phi2], $MachinePrecision], 3.4e+187], N[(-1.0 * N[(N[Min[phi1, phi2], $MachinePrecision] * N[(R + N[(-1.0 * N[(N[(R * N[Max[phi1, phi2], $MachinePrecision]), $MachinePrecision] / N[Min[phi1, phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(R * N[(N[Max[phi1, phi2], $MachinePrecision] * N[(1.0 + N[(-1.0 * N[(N[Min[phi1, phi2], $MachinePrecision] / N[Max[phi1, phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\mathsf{max}\left(\phi_1, \phi_2\right) \leq 3.4 \cdot 10^{+187}:\\
\;\;\;\;-1 \cdot \left(\mathsf{min}\left(\phi_1, \phi_2\right) \cdot \left(R + -1 \cdot \frac{R \cdot \mathsf{max}\left(\phi_1, \phi_2\right)}{\mathsf{min}\left(\phi_1, \phi_2\right)}\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left(\mathsf{max}\left(\phi_1, \phi_2\right) \cdot \left(1 + -1 \cdot \frac{\mathsf{min}\left(\phi_1, \phi_2\right)}{\mathsf{max}\left(\phi_1, \phi_2\right)}\right)\right)\\


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

    1. Initial program 59.4%

      \[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. Taylor expanded in phi1 around inf

      \[\leadsto \color{blue}{R \cdot \phi_1} \]
    3. Step-by-step derivation
      1. lower-*.f6416.9%

        \[\leadsto R \cdot \color{blue}{\phi_1} \]
    4. Applied rewrites16.9%

      \[\leadsto \color{blue}{R \cdot \phi_1} \]
    5. Taylor expanded in phi1 around -inf

      \[\leadsto \color{blue}{-1 \cdot \left(\phi_1 \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right)\right)} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto -1 \cdot \color{blue}{\left(\phi_1 \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right)\right)} \]
      2. lower-*.f64N/A

        \[\leadsto -1 \cdot \left(\phi_1 \cdot \color{blue}{\left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right)}\right) \]
      3. lower-+.f64N/A

        \[\leadsto -1 \cdot \left(\phi_1 \cdot \left(R + \color{blue}{-1 \cdot \frac{R \cdot \phi_2}{\phi_1}}\right)\right) \]
      4. lower-*.f64N/A

        \[\leadsto -1 \cdot \left(\phi_1 \cdot \left(R + -1 \cdot \color{blue}{\frac{R \cdot \phi_2}{\phi_1}}\right)\right) \]
      5. lower-/.f64N/A

        \[\leadsto -1 \cdot \left(\phi_1 \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\color{blue}{\phi_1}}\right)\right) \]
      6. lower-*.f6427.8%

        \[\leadsto -1 \cdot \left(\phi_1 \cdot \left(R + -1 \cdot \frac{R \cdot \phi_2}{\phi_1}\right)\right) \]
    7. Applied rewrites27.8%

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

    if 3.4e187 < phi2

    1. Initial program 59.4%

      \[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. Taylor expanded in lambda1 around inf

      \[\leadsto R \cdot \color{blue}{\left(\lambda_1 \cdot \sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right)} \]
    3. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto R \cdot \left(\lambda_1 \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}}\right) \]
      2. lower-sqrt.f64N/A

        \[\leadsto R \cdot \left(\lambda_1 \cdot \sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right) \]
      3. lower-pow.f64N/A

        \[\leadsto R \cdot \left(\lambda_1 \cdot \sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right) \]
      4. lower-cos.f64N/A

        \[\leadsto R \cdot \left(\lambda_1 \cdot \sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right) \]
      5. lower-*.f64N/A

        \[\leadsto R \cdot \left(\lambda_1 \cdot \sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right) \]
      6. lower-+.f6416.5%

        \[\leadsto R \cdot \left(\lambda_1 \cdot \sqrt{{\cos \left(0.5 \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right) \]
    4. Applied rewrites16.5%

      \[\leadsto R \cdot \color{blue}{\left(\lambda_1 \cdot \sqrt{{\cos \left(0.5 \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right)} \]
    5. Taylor expanded in phi2 around inf

      \[\leadsto R \cdot \color{blue}{\left(\phi_2 \cdot \left(1 + -1 \cdot \frac{\phi_1}{\phi_2}\right)\right)} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto R \cdot \left(\phi_2 \cdot \color{blue}{\left(1 + -1 \cdot \frac{\phi_1}{\phi_2}\right)}\right) \]
      2. lower-+.f64N/A

        \[\leadsto R \cdot \left(\phi_2 \cdot \left(1 + \color{blue}{-1 \cdot \frac{\phi_1}{\phi_2}}\right)\right) \]
      3. lower-*.f64N/A

        \[\leadsto R \cdot \left(\phi_2 \cdot \left(1 + -1 \cdot \color{blue}{\frac{\phi_1}{\phi_2}}\right)\right) \]
      4. lower-/.f6426.1%

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

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

Alternative 7: 55.8% accurate, 3.4× speedup?

\[\begin{array}{l} \mathbf{if}\;\mathsf{max}\left(\phi_1, \phi_2\right) \leq 6.2 \cdot 10^{-42}:\\ \;\;\;\;R \cdot \left(-1 \cdot \mathsf{min}\left(\phi_1, \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \left(\mathsf{max}\left(\phi_1, \phi_2\right) \cdot \left(1 + -1 \cdot \frac{\mathsf{min}\left(\phi_1, \phi_2\right)}{\mathsf{max}\left(\phi_1, \phi_2\right)}\right)\right)\\ \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
  :precision binary64
  (if (<= (fmax phi1 phi2) 6.2e-42)
  (* R (* -1.0 (fmin phi1 phi2)))
  (*
   R
   (*
    (fmax phi1 phi2)
    (+ 1.0 (* -1.0 (/ (fmin phi1 phi2) (fmax phi1 phi2))))))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (fmax(phi1, phi2) <= 6.2e-42) {
		tmp = R * (-1.0 * fmin(phi1, phi2));
	} else {
		tmp = R * (fmax(phi1, phi2) * (1.0 + (-1.0 * (fmin(phi1, phi2) / fmax(phi1, phi2)))));
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (fmax(phi1, phi2) <= 6.2d-42) then
        tmp = r * ((-1.0d0) * fmin(phi1, phi2))
    else
        tmp = r * (fmax(phi1, phi2) * (1.0d0 + ((-1.0d0) * (fmin(phi1, phi2) / fmax(phi1, phi2)))))
    end if
    code = tmp
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (fmax(phi1, phi2) <= 6.2e-42) {
		tmp = R * (-1.0 * fmin(phi1, phi2));
	} else {
		tmp = R * (fmax(phi1, phi2) * (1.0 + (-1.0 * (fmin(phi1, phi2) / fmax(phi1, phi2)))));
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if fmax(phi1, phi2) <= 6.2e-42:
		tmp = R * (-1.0 * fmin(phi1, phi2))
	else:
		tmp = R * (fmax(phi1, phi2) * (1.0 + (-1.0 * (fmin(phi1, phi2) / fmax(phi1, phi2)))))
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (fmax(phi1, phi2) <= 6.2e-42)
		tmp = Float64(R * Float64(-1.0 * fmin(phi1, phi2)));
	else
		tmp = Float64(R * Float64(fmax(phi1, phi2) * Float64(1.0 + Float64(-1.0 * Float64(fmin(phi1, phi2) / fmax(phi1, phi2))))));
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (max(phi1, phi2) <= 6.2e-42)
		tmp = R * (-1.0 * min(phi1, phi2));
	else
		tmp = R * (max(phi1, phi2) * (1.0 + (-1.0 * (min(phi1, phi2) / max(phi1, phi2)))));
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[N[Max[phi1, phi2], $MachinePrecision], 6.2e-42], N[(R * N[(-1.0 * N[Min[phi1, phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(R * N[(N[Max[phi1, phi2], $MachinePrecision] * N[(1.0 + N[(-1.0 * N[(N[Min[phi1, phi2], $MachinePrecision] / N[Max[phi1, phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\mathsf{max}\left(\phi_1, \phi_2\right) \leq 6.2 \cdot 10^{-42}:\\
\;\;\;\;R \cdot \left(-1 \cdot \mathsf{min}\left(\phi_1, \phi_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \left(\mathsf{max}\left(\phi_1, \phi_2\right) \cdot \left(1 + -1 \cdot \frac{\mathsf{min}\left(\phi_1, \phi_2\right)}{\mathsf{max}\left(\phi_1, \phi_2\right)}\right)\right)\\


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

    1. Initial program 59.4%

      \[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. Taylor expanded in phi1 around -inf

      \[\leadsto R \cdot \color{blue}{\left(-1 \cdot \phi_1\right)} \]
    3. Step-by-step derivation
      1. lower-*.f6417.2%

        \[\leadsto R \cdot \left(-1 \cdot \color{blue}{\phi_1}\right) \]
    4. Applied rewrites17.2%

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

    if 6.2000000000000005e-42 < phi2

    1. Initial program 59.4%

      \[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. Taylor expanded in lambda1 around inf

      \[\leadsto R \cdot \color{blue}{\left(\lambda_1 \cdot \sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right)} \]
    3. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto R \cdot \left(\lambda_1 \cdot \color{blue}{\sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}}\right) \]
      2. lower-sqrt.f64N/A

        \[\leadsto R \cdot \left(\lambda_1 \cdot \sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right) \]
      3. lower-pow.f64N/A

        \[\leadsto R \cdot \left(\lambda_1 \cdot \sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right) \]
      4. lower-cos.f64N/A

        \[\leadsto R \cdot \left(\lambda_1 \cdot \sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right) \]
      5. lower-*.f64N/A

        \[\leadsto R \cdot \left(\lambda_1 \cdot \sqrt{{\cos \left(\frac{1}{2} \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right) \]
      6. lower-+.f6416.5%

        \[\leadsto R \cdot \left(\lambda_1 \cdot \sqrt{{\cos \left(0.5 \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right) \]
    4. Applied rewrites16.5%

      \[\leadsto R \cdot \color{blue}{\left(\lambda_1 \cdot \sqrt{{\cos \left(0.5 \cdot \left(\phi_1 + \phi_2\right)\right)}^{2}}\right)} \]
    5. Taylor expanded in phi2 around inf

      \[\leadsto R \cdot \color{blue}{\left(\phi_2 \cdot \left(1 + -1 \cdot \frac{\phi_1}{\phi_2}\right)\right)} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto R \cdot \left(\phi_2 \cdot \color{blue}{\left(1 + -1 \cdot \frac{\phi_1}{\phi_2}\right)}\right) \]
      2. lower-+.f64N/A

        \[\leadsto R \cdot \left(\phi_2 \cdot \left(1 + \color{blue}{-1 \cdot \frac{\phi_1}{\phi_2}}\right)\right) \]
      3. lower-*.f64N/A

        \[\leadsto R \cdot \left(\phi_2 \cdot \left(1 + -1 \cdot \color{blue}{\frac{\phi_1}{\phi_2}}\right)\right) \]
      4. lower-/.f6426.1%

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

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

Alternative 8: 51.6% accurate, 6.6× speedup?

\[\begin{array}{l} \mathbf{if}\;\mathsf{min}\left(\phi_1, \phi_2\right) \leq -1.4 \cdot 10^{-25}:\\ \;\;\;\;R \cdot \left(-1 \cdot \mathsf{min}\left(\phi_1, \phi_2\right)\right)\\ \mathbf{else}:\\ \;\;\;\;R \cdot \mathsf{max}\left(\phi_1, \phi_2\right)\\ \end{array} \]
(FPCore (R lambda1 lambda2 phi1 phi2)
  :precision binary64
  (if (<= (fmin phi1 phi2) -1.4e-25)
  (* R (* -1.0 (fmin phi1 phi2)))
  (* R (fmax phi1 phi2))))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (fmin(phi1, phi2) <= -1.4e-25) {
		tmp = R * (-1.0 * fmin(phi1, phi2));
	} else {
		tmp = R * fmax(phi1, phi2);
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    real(8) :: tmp
    if (fmin(phi1, phi2) <= (-1.4d-25)) then
        tmp = r * ((-1.0d0) * fmin(phi1, phi2))
    else
        tmp = r * fmax(phi1, phi2)
    end if
    code = tmp
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	double tmp;
	if (fmin(phi1, phi2) <= -1.4e-25) {
		tmp = R * (-1.0 * fmin(phi1, phi2));
	} else {
		tmp = R * fmax(phi1, phi2);
	}
	return tmp;
}
def code(R, lambda1, lambda2, phi1, phi2):
	tmp = 0
	if fmin(phi1, phi2) <= -1.4e-25:
		tmp = R * (-1.0 * fmin(phi1, phi2))
	else:
		tmp = R * fmax(phi1, phi2)
	return tmp
function code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0
	if (fmin(phi1, phi2) <= -1.4e-25)
		tmp = Float64(R * Float64(-1.0 * fmin(phi1, phi2)));
	else
		tmp = Float64(R * fmax(phi1, phi2));
	end
	return tmp
end
function tmp_2 = code(R, lambda1, lambda2, phi1, phi2)
	tmp = 0.0;
	if (min(phi1, phi2) <= -1.4e-25)
		tmp = R * (-1.0 * min(phi1, phi2));
	else
		tmp = R * max(phi1, phi2);
	end
	tmp_2 = tmp;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := If[LessEqual[N[Min[phi1, phi2], $MachinePrecision], -1.4e-25], N[(R * N[(-1.0 * N[Min[phi1, phi2], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(R * N[Max[phi1, phi2], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
\mathbf{if}\;\mathsf{min}\left(\phi_1, \phi_2\right) \leq -1.4 \cdot 10^{-25}:\\
\;\;\;\;R \cdot \left(-1 \cdot \mathsf{min}\left(\phi_1, \phi_2\right)\right)\\

\mathbf{else}:\\
\;\;\;\;R \cdot \mathsf{max}\left(\phi_1, \phi_2\right)\\


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

    1. Initial program 59.4%

      \[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. Taylor expanded in phi1 around -inf

      \[\leadsto R \cdot \color{blue}{\left(-1 \cdot \phi_1\right)} \]
    3. Step-by-step derivation
      1. lower-*.f6417.2%

        \[\leadsto R \cdot \left(-1 \cdot \color{blue}{\phi_1}\right) \]
    4. Applied rewrites17.2%

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

    if -1.3999999999999999e-25 < phi1

    1. Initial program 59.4%

      \[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. Taylor expanded in phi2 around inf

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

        \[\leadsto R \cdot \color{blue}{\phi_2} \]
    4. Applied rewrites16.6%

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

Alternative 9: 30.2% accurate, 15.9× speedup?

\[R \cdot \mathsf{max}\left(\phi_1, \phi_2\right) \]
(FPCore (R lambda1 lambda2 phi1 phi2)
  :precision binary64
  (* R (fmax phi1 phi2)))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * fmax(phi1, phi2);
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = r * fmax(phi1, phi2)
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * fmax(phi1, phi2);
}
def code(R, lambda1, lambda2, phi1, phi2):
	return R * fmax(phi1, phi2)
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(R * fmax(phi1, phi2))
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = R * max(phi1, phi2);
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * N[Max[phi1, phi2], $MachinePrecision]), $MachinePrecision]
R \cdot \mathsf{max}\left(\phi_1, \phi_2\right)
Derivation
  1. Initial program 59.4%

    \[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. Taylor expanded in phi2 around inf

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

      \[\leadsto R \cdot \color{blue}{\phi_2} \]
  4. Applied rewrites16.6%

    \[\leadsto \color{blue}{R \cdot \phi_2} \]
  5. Add Preprocessing

Alternative 10: 16.9% accurate, 27.8× speedup?

\[R \cdot \phi_1 \]
(FPCore (R lambda1 lambda2 phi1 phi2)
  :precision binary64
  (* R phi1))
double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * phi1;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

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

real(8) function code(r, lambda1, lambda2, phi1, phi2)
use fmin_fmax_functions
    real(8), intent (in) :: r
    real(8), intent (in) :: lambda1
    real(8), intent (in) :: lambda2
    real(8), intent (in) :: phi1
    real(8), intent (in) :: phi2
    code = r * phi1
end function
public static double code(double R, double lambda1, double lambda2, double phi1, double phi2) {
	return R * phi1;
}
def code(R, lambda1, lambda2, phi1, phi2):
	return R * phi1
function code(R, lambda1, lambda2, phi1, phi2)
	return Float64(R * phi1)
end
function tmp = code(R, lambda1, lambda2, phi1, phi2)
	tmp = R * phi1;
end
code[R_, lambda1_, lambda2_, phi1_, phi2_] := N[(R * phi1), $MachinePrecision]
R \cdot \phi_1
Derivation
  1. Initial program 59.4%

    \[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. Taylor expanded in phi1 around inf

    \[\leadsto \color{blue}{R \cdot \phi_1} \]
  3. Step-by-step derivation
    1. lower-*.f6416.9%

      \[\leadsto R \cdot \color{blue}{\phi_1} \]
  4. Applied rewrites16.9%

    \[\leadsto \color{blue}{R \cdot \phi_1} \]
  5. Add Preprocessing

Reproduce

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