Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.9% → 97.1%
Time: 19.2s
Alternatives: 13
Speedup: 2.0×

Specification

?
\[\begin{array}{l} \\ \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (*
  (cos (- (/ (* K (+ m n)) 2.0) M))
  (exp (- (- (pow (- (/ (+ m n) 2.0) M) 2.0)) (- l (fabs (- m n)))))))
double code(double K, double m, double n, double M, double l) {
	return cos((((K * (m + n)) / 2.0) - M)) * exp((-pow((((m + n) / 2.0) - M), 2.0) - (l - fabs((m - n)))));
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = cos((((k * (m + n)) / 2.0d0) - m_1)) * exp((-((((m + n) / 2.0d0) - m_1) ** 2.0d0) - (l - abs((m - n)))))
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos((((K * (m + n)) / 2.0) - M)) * Math.exp((-Math.pow((((m + n) / 2.0) - M), 2.0) - (l - Math.abs((m - n)))));
}
def code(K, m, n, M, l):
	return math.cos((((K * (m + n)) / 2.0) - M)) * math.exp((-math.pow((((m + n) / 2.0) - M), 2.0) - (l - math.fabs((m - n)))))
function code(K, m, n, M, l)
	return Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * exp(Float64(Float64(-(Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)) - Float64(l - abs(Float64(m - n))))))
end
function tmp = code(K, m, n, M, l)
	tmp = cos((((K * (m + n)) / 2.0) - M)) * exp((-((((m + n) / 2.0) - M) ^ 2.0) - (l - abs((m - n)))));
end
code[K_, m_, n_, M_, l_] := N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[((-N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]) - N[(l - N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)}
\end{array}

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

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

\[\begin{array}{l} \\ \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (*
  (cos (- (/ (* K (+ m n)) 2.0) M))
  (exp (- (- (pow (- (/ (+ m n) 2.0) M) 2.0)) (- l (fabs (- m n)))))))
double code(double K, double m, double n, double M, double l) {
	return cos((((K * (m + n)) / 2.0) - M)) * exp((-pow((((m + n) / 2.0) - M), 2.0) - (l - fabs((m - n)))));
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = cos((((k * (m + n)) / 2.0d0) - m_1)) * exp((-((((m + n) / 2.0d0) - m_1) ** 2.0d0) - (l - abs((m - n)))))
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos((((K * (m + n)) / 2.0) - M)) * Math.exp((-Math.pow((((m + n) / 2.0) - M), 2.0) - (l - Math.abs((m - n)))));
}
def code(K, m, n, M, l):
	return math.cos((((K * (m + n)) / 2.0) - M)) * math.exp((-math.pow((((m + n) / 2.0) - M), 2.0) - (l - math.fabs((m - n)))))
function code(K, m, n, M, l)
	return Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * exp(Float64(Float64(-(Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)) - Float64(l - abs(Float64(m - n))))))
end
function tmp = code(K, m, n, M, l)
	tmp = cos((((K * (m + n)) / 2.0) - M)) * exp((-((((m + n) / 2.0) - M) ^ 2.0) - (l - abs((m - n)))));
end
code[K_, m_, n_, M_, l_] := N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[((-N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]) - N[(l - N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)}
\end{array}

Alternative 1: 97.1% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt[3]{\left(K \cdot 0.5\right) \cdot \left(m + n\right)}\\ t_1 := \left|m - n\right|\\ t_2 := {\left(\frac{m + n}{2} - M\right)}^{2}\\ t_3 := e^{\left(t_1 - \ell\right) - t_2}\\ \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t_3 \leq \infty:\\ \;\;\;\;\cos \left(t_0 \cdot {t_0}^{2} - M\right) \cdot e^{t_1 - \left(\ell + t_2\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot t_3\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (cbrt (* (* K 0.5) (+ m n))))
        (t_1 (fabs (- m n)))
        (t_2 (pow (- (/ (+ m n) 2.0) M) 2.0))
        (t_3 (exp (- (- t_1 l) t_2))))
   (if (<= (* (cos (- (/ (* K (+ m n)) 2.0) M)) t_3) INFINITY)
     (* (cos (- (* t_0 (pow t_0 2.0)) M)) (exp (- t_1 (+ l t_2))))
     (* (cos M) t_3))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = cbrt(((K * 0.5) * (m + n)));
	double t_1 = fabs((m - n));
	double t_2 = pow((((m + n) / 2.0) - M), 2.0);
	double t_3 = exp(((t_1 - l) - t_2));
	double tmp;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_3) <= ((double) INFINITY)) {
		tmp = cos(((t_0 * pow(t_0, 2.0)) - M)) * exp((t_1 - (l + t_2)));
	} else {
		tmp = cos(M) * t_3;
	}
	return tmp;
}
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.cbrt(((K * 0.5) * (m + n)));
	double t_1 = Math.abs((m - n));
	double t_2 = Math.pow((((m + n) / 2.0) - M), 2.0);
	double t_3 = Math.exp(((t_1 - l) - t_2));
	double tmp;
	if ((Math.cos((((K * (m + n)) / 2.0) - M)) * t_3) <= Double.POSITIVE_INFINITY) {
		tmp = Math.cos(((t_0 * Math.pow(t_0, 2.0)) - M)) * Math.exp((t_1 - (l + t_2)));
	} else {
		tmp = Math.cos(M) * t_3;
	}
	return tmp;
}
function code(K, m, n, M, l)
	t_0 = cbrt(Float64(Float64(K * 0.5) * Float64(m + n)))
	t_1 = abs(Float64(m - n))
	t_2 = Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0
	t_3 = exp(Float64(Float64(t_1 - l) - t_2))
	tmp = 0.0
	if (Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * t_3) <= Inf)
		tmp = Float64(cos(Float64(Float64(t_0 * (t_0 ^ 2.0)) - M)) * exp(Float64(t_1 - Float64(l + t_2))));
	else
		tmp = Float64(cos(M) * t_3);
	end
	return tmp
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Power[N[(N[(K * 0.5), $MachinePrecision] * N[(m + n), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision]}, Block[{t$95$1 = N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]}, Block[{t$95$3 = N[Exp[N[(N[(t$95$1 - l), $MachinePrecision] - t$95$2), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * t$95$3), $MachinePrecision], Infinity], N[(N[Cos[N[(N[(t$95$0 * N[Power[t$95$0, 2.0], $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(t$95$1 - N[(l + t$95$2), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * t$95$3), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt[3]{\left(K \cdot 0.5\right) \cdot \left(m + n\right)}\\
t_1 := \left|m - n\right|\\
t_2 := {\left(\frac{m + n}{2} - M\right)}^{2}\\
t_3 := e^{\left(t_1 - \ell\right) - t_2}\\
\mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t_3 \leq \infty:\\
\;\;\;\;\cos \left(t_0 \cdot {t_0}^{2} - M\right) \cdot e^{t_1 - \left(\ell + t_2\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot t_3\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) 2) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) 2) M) 2)) (-.f64 l (fabs.f64 (-.f64 m n)))))) < +inf.0

    1. Initial program 96.3%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. associate-/l*95.7%

        \[\leadsto \cos \left(\color{blue}{\frac{K}{\frac{2}{m + n}}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate--r-95.7%

        \[\leadsto \cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
    3. Simplified95.7%

      \[\leadsto \color{blue}{\cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
    4. Step-by-step derivation
      1. associate-/r/96.3%

        \[\leadsto \cos \left(\color{blue}{\frac{K}{2} \cdot \left(m + n\right)} - M\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|} \]
      2. *-commutative96.3%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|} \]
      3. add-cube-cbrt96.9%

        \[\leadsto \cos \left(\color{blue}{\left(\sqrt[3]{\left(m + n\right) \cdot \frac{K}{2}} \cdot \sqrt[3]{\left(m + n\right) \cdot \frac{K}{2}}\right) \cdot \sqrt[3]{\left(m + n\right) \cdot \frac{K}{2}}} - M\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|} \]
      4. pow296.9%

        \[\leadsto \cos \left(\color{blue}{{\left(\sqrt[3]{\left(m + n\right) \cdot \frac{K}{2}}\right)}^{2}} \cdot \sqrt[3]{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|} \]
      5. div-inv96.9%

        \[\leadsto \cos \left({\left(\sqrt[3]{\left(m + n\right) \cdot \color{blue}{\left(K \cdot \frac{1}{2}\right)}}\right)}^{2} \cdot \sqrt[3]{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|} \]
      6. metadata-eval96.9%

        \[\leadsto \cos \left({\left(\sqrt[3]{\left(m + n\right) \cdot \left(K \cdot \color{blue}{0.5}\right)}\right)}^{2} \cdot \sqrt[3]{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|} \]
      7. div-inv96.9%

        \[\leadsto \cos \left({\left(\sqrt[3]{\left(m + n\right) \cdot \left(K \cdot 0.5\right)}\right)}^{2} \cdot \sqrt[3]{\left(m + n\right) \cdot \color{blue}{\left(K \cdot \frac{1}{2}\right)}} - M\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|} \]
      8. metadata-eval96.9%

        \[\leadsto \cos \left({\left(\sqrt[3]{\left(m + n\right) \cdot \left(K \cdot 0.5\right)}\right)}^{2} \cdot \sqrt[3]{\left(m + n\right) \cdot \left(K \cdot \color{blue}{0.5}\right)} - M\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|} \]
    5. Applied egg-rr96.9%

      \[\leadsto \cos \left(\color{blue}{{\left(\sqrt[3]{\left(m + n\right) \cdot \left(K \cdot 0.5\right)}\right)}^{2} \cdot \sqrt[3]{\left(m + n\right) \cdot \left(K \cdot 0.5\right)}} - M\right) \cdot e^{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|} \]

    if +inf.0 < (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) 2) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) 2) M) 2)) (-.f64 l (fabs.f64 (-.f64 m n))))))

    1. Initial program 0.0%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative0.0%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/0.0%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-0.0%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative0.0%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-0.0%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg0.0%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+0.0%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative0.0%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+0.0%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified0.0%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 100.0%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg100.0%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification97.5%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \leq \infty:\\ \;\;\;\;\cos \left(\sqrt[3]{\left(K \cdot 0.5\right) \cdot \left(m + n\right)} \cdot {\left(\sqrt[3]{\left(K \cdot 0.5\right) \cdot \left(m + n\right)}\right)}^{2} - M\right) \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}\\ \end{array} \]

Alternative 2: 96.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (* (cos M) (exp (- (- (fabs (- m n)) l) (pow (- (/ (+ m n) 2.0) M) 2.0)))))
double code(double K, double m, double n, double M, double l) {
	return cos(M) * exp(((fabs((m - n)) - l) - pow((((m + n) / 2.0) - M), 2.0)));
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = cos(m_1) * exp(((abs((m - n)) - l) - ((((m + n) / 2.0d0) - m_1) ** 2.0d0)))
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(M) * Math.exp(((Math.abs((m - n)) - l) - Math.pow((((m + n) / 2.0) - M), 2.0)));
}
def code(K, m, n, M, l):
	return math.cos(M) * math.exp(((math.fabs((m - n)) - l) - math.pow((((m + n) / 2.0) - M), 2.0)))
function code(K, m, n, M, l)
	return Float64(cos(M) * exp(Float64(Float64(abs(Float64(m - n)) - l) - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0))))
end
function tmp = code(K, m, n, M, l)
	tmp = cos(M) * exp(((abs((m - n)) - l) - ((((m + n) / 2.0) - M) ^ 2.0)));
end
code[K_, m_, n_, M_, l_] := N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}
\end{array}
Derivation
  1. Initial program 78.6%

    \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  2. Step-by-step derivation
    1. *-commutative78.6%

      \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. associate-*r/78.6%

      \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    3. associate--r-78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
    4. +-commutative78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
    5. associate-+r-78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
    6. unsub-neg78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
    7. associate--r+78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
    8. +-commutative78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
    9. associate--r+78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
  3. Simplified78.6%

    \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
  4. Taylor expanded in K around 0 95.9%

    \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
  5. Step-by-step derivation
    1. cos-neg95.9%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
  6. Simplified95.9%

    \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
  7. Final simplification95.9%

    \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]

Alternative 3: 67.9% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ t_1 := \left|m - n\right|\\ t_2 := t_1 - \ell\\ \mathbf{if}\;m \leq -10.5:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;m \leq -8 \cdot 10^{-89}:\\ \;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{t_2 - M \cdot M}\\ \mathbf{elif}\;m \leq -2.32 \cdot 10^{-147}:\\ \;\;\;\;t_0\\ \mathbf{elif}\;m \leq -1.8 \cdot 10^{-226}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(\left(m + n\right) - M\right) - \left(\ell - t_1\right)}\\ \mathbf{elif}\;m \leq 5.7 \cdot 10^{-160}:\\ \;\;\;\;\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{t_2 - \left(n \cdot n\right) \cdot 0.25}\\ \mathbf{else}:\\ \;\;\;\;t_0\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* (cos M) (exp (* -0.25 (* n n)))))
        (t_1 (fabs (- m n)))
        (t_2 (- t_1 l)))
   (if (<= m -10.5)
     (* (cos M) (exp (* -0.25 (* m m))))
     (if (<= m -8e-89)
       (* (cos (* 0.5 (* K n))) (exp (- t_2 (* M M))))
       (if (<= m -2.32e-147)
         t_0
         (if (<= m -1.8e-226)
           (* (cos M) (exp (- (* M (- (+ m n) M)) (- l t_1))))
           (if (<= m 5.7e-160)
             (*
              (cos (- (* (+ m n) (/ K 2.0)) M))
              (exp (- t_2 (* (* n n) 0.25))))
             t_0)))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = cos(M) * exp((-0.25 * (n * n)));
	double t_1 = fabs((m - n));
	double t_2 = t_1 - l;
	double tmp;
	if (m <= -10.5) {
		tmp = cos(M) * exp((-0.25 * (m * m)));
	} else if (m <= -8e-89) {
		tmp = cos((0.5 * (K * n))) * exp((t_2 - (M * M)));
	} else if (m <= -2.32e-147) {
		tmp = t_0;
	} else if (m <= -1.8e-226) {
		tmp = cos(M) * exp(((M * ((m + n) - M)) - (l - t_1)));
	} else if (m <= 5.7e-160) {
		tmp = cos((((m + n) * (K / 2.0)) - M)) * exp((t_2 - ((n * n) * 0.25)));
	} else {
		tmp = t_0;
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: t_2
    real(8) :: tmp
    t_0 = cos(m_1) * exp(((-0.25d0) * (n * n)))
    t_1 = abs((m - n))
    t_2 = t_1 - l
    if (m <= (-10.5d0)) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m * m)))
    else if (m <= (-8d-89)) then
        tmp = cos((0.5d0 * (k * n))) * exp((t_2 - (m_1 * m_1)))
    else if (m <= (-2.32d-147)) then
        tmp = t_0
    else if (m <= (-1.8d-226)) then
        tmp = cos(m_1) * exp(((m_1 * ((m + n) - m_1)) - (l - t_1)))
    else if (m <= 5.7d-160) then
        tmp = cos((((m + n) * (k / 2.0d0)) - m_1)) * exp((t_2 - ((n * n) * 0.25d0)))
    else
        tmp = t_0
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	double t_1 = Math.abs((m - n));
	double t_2 = t_1 - l;
	double tmp;
	if (m <= -10.5) {
		tmp = Math.cos(M) * Math.exp((-0.25 * (m * m)));
	} else if (m <= -8e-89) {
		tmp = Math.cos((0.5 * (K * n))) * Math.exp((t_2 - (M * M)));
	} else if (m <= -2.32e-147) {
		tmp = t_0;
	} else if (m <= -1.8e-226) {
		tmp = Math.cos(M) * Math.exp(((M * ((m + n) - M)) - (l - t_1)));
	} else if (m <= 5.7e-160) {
		tmp = Math.cos((((m + n) * (K / 2.0)) - M)) * Math.exp((t_2 - ((n * n) * 0.25)));
	} else {
		tmp = t_0;
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.cos(M) * math.exp((-0.25 * (n * n)))
	t_1 = math.fabs((m - n))
	t_2 = t_1 - l
	tmp = 0
	if m <= -10.5:
		tmp = math.cos(M) * math.exp((-0.25 * (m * m)))
	elif m <= -8e-89:
		tmp = math.cos((0.5 * (K * n))) * math.exp((t_2 - (M * M)))
	elif m <= -2.32e-147:
		tmp = t_0
	elif m <= -1.8e-226:
		tmp = math.cos(M) * math.exp(((M * ((m + n) - M)) - (l - t_1)))
	elif m <= 5.7e-160:
		tmp = math.cos((((m + n) * (K / 2.0)) - M)) * math.exp((t_2 - ((n * n) * 0.25)))
	else:
		tmp = t_0
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))))
	t_1 = abs(Float64(m - n))
	t_2 = Float64(t_1 - l)
	tmp = 0.0
	if (m <= -10.5)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(m * m))));
	elseif (m <= -8e-89)
		tmp = Float64(cos(Float64(0.5 * Float64(K * n))) * exp(Float64(t_2 - Float64(M * M))));
	elseif (m <= -2.32e-147)
		tmp = t_0;
	elseif (m <= -1.8e-226)
		tmp = Float64(cos(M) * exp(Float64(Float64(M * Float64(Float64(m + n) - M)) - Float64(l - t_1))));
	elseif (m <= 5.7e-160)
		tmp = Float64(cos(Float64(Float64(Float64(m + n) * Float64(K / 2.0)) - M)) * exp(Float64(t_2 - Float64(Float64(n * n) * 0.25))));
	else
		tmp = t_0;
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = cos(M) * exp((-0.25 * (n * n)));
	t_1 = abs((m - n));
	t_2 = t_1 - l;
	tmp = 0.0;
	if (m <= -10.5)
		tmp = cos(M) * exp((-0.25 * (m * m)));
	elseif (m <= -8e-89)
		tmp = cos((0.5 * (K * n))) * exp((t_2 - (M * M)));
	elseif (m <= -2.32e-147)
		tmp = t_0;
	elseif (m <= -1.8e-226)
		tmp = cos(M) * exp(((M * ((m + n) - M)) - (l - t_1)));
	elseif (m <= 5.7e-160)
		tmp = cos((((m + n) * (K / 2.0)) - M)) * exp((t_2 - ((n * n) * 0.25)));
	else
		tmp = t_0;
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(t$95$1 - l), $MachinePrecision]}, If[LessEqual[m, -10.5], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(m * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, -8e-89], N[(N[Cos[N[(0.5 * N[(K * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(t$95$2 - N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, -2.32e-147], t$95$0, If[LessEqual[m, -1.8e-226], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(M * N[(N[(m + n), $MachinePrecision] - M), $MachinePrecision]), $MachinePrecision] - N[(l - t$95$1), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, 5.7e-160], N[(N[Cos[N[(N[(N[(m + n), $MachinePrecision] * N[(K / 2.0), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(t$95$2 - N[(N[(n * n), $MachinePrecision] * 0.25), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$0]]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\
t_1 := \left|m - n\right|\\
t_2 := t_1 - \ell\\
\mathbf{if}\;m \leq -10.5:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

\mathbf{elif}\;m \leq -8 \cdot 10^{-89}:\\
\;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{t_2 - M \cdot M}\\

\mathbf{elif}\;m \leq -2.32 \cdot 10^{-147}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;m \leq -1.8 \cdot 10^{-226}:\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(\left(m + n\right) - M\right) - \left(\ell - t_1\right)}\\

\mathbf{elif}\;m \leq 5.7 \cdot 10^{-160}:\\
\;\;\;\;\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{t_2 - \left(n \cdot n\right) \cdot 0.25}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 5 regimes
  2. if m < -10.5

    1. Initial program 72.9%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative72.9%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/72.9%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified72.9%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 98.6%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg98.6%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified98.6%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in m around inf 98.6%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. unpow298.6%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    9. Simplified98.6%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(m \cdot m\right)}} \]

    if -10.5 < m < -8.00000000000000031e-89

    1. Initial program 93.3%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative93.3%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/93.3%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified93.3%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in M around inf 80.2%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{{M}^{2}}} \]
    5. Step-by-step derivation
      1. unpow280.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot M}} \]
    6. Simplified80.2%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot M}} \]
    7. Taylor expanded in n around inf 86.9%

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(n \cdot K\right)\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M} \]

    if -8.00000000000000031e-89 < m < -2.3199999999999999e-147 or 5.70000000000000038e-160 < m

    1. Initial program 77.4%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative77.4%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/77.4%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-77.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative77.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-77.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg77.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+77.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative77.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+77.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified77.4%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 94.1%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg94.1%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified94.1%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in n around inf 53.2%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
    8. Step-by-step derivation
      1. unpow253.2%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(n \cdot n\right)}} \]
    9. Simplified53.2%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(n \cdot n\right)}} \]

    if -2.3199999999999999e-147 < m < -1.79999999999999997e-226

    1. Initial program 84.2%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative84.2%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/84.2%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-84.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative84.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-84.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg84.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+84.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative84.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+84.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified84.2%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 100.0%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg100.0%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in M around inf 63.6%

      \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{\left(-1 \cdot \left(\left(n + m\right) \cdot M\right) + {M}^{2}\right)}} \]
    8. Step-by-step derivation
      1. +-commutative63.6%

        \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{\left({M}^{2} + -1 \cdot \left(\left(n + m\right) \cdot M\right)\right)}} \]
      2. unpow263.6%

        \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \left(\color{blue}{M \cdot M} + -1 \cdot \left(\left(n + m\right) \cdot M\right)\right)} \]
      3. associate-*r*63.6%

        \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \left(M \cdot M + \color{blue}{\left(-1 \cdot \left(n + m\right)\right) \cdot M}\right)} \]
      4. distribute-rgt-out63.7%

        \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot \left(M + -1 \cdot \left(n + m\right)\right)}} \]
      5. mul-1-neg63.7%

        \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot \left(M + \color{blue}{\left(-\left(n + m\right)\right)}\right)} \]
    9. Simplified63.7%

      \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot \left(M + \left(-\left(n + m\right)\right)\right)}} \]

    if -1.79999999999999997e-226 < m < 5.70000000000000038e-160

    1. Initial program 83.6%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative83.6%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/83.6%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-83.6%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative83.6%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-83.6%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg83.6%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+83.6%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative83.6%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+83.6%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified83.6%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in n around inf 66.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{0.25 \cdot {n}^{2}}} \]
    5. Step-by-step derivation
      1. *-commutative66.6%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{{n}^{2} \cdot 0.25}} \]
      2. unpow266.6%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{\left(n \cdot n\right)} \cdot 0.25} \]
    6. Simplified66.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{\left(n \cdot n\right) \cdot 0.25}} \]
  3. Recombined 5 regimes into one program.
  4. Final simplification70.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -10.5:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;m \leq -8 \cdot 10^{-89}:\\ \;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{elif}\;m \leq -2.32 \cdot 10^{-147}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \mathbf{elif}\;m \leq -1.8 \cdot 10^{-226}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(\left(m + n\right) - M\right) - \left(\ell - \left|m - n\right|\right)}\\ \mathbf{elif}\;m \leq 5.7 \cdot 10^{-160}:\\ \;\;\;\;\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \left(n \cdot n\right) \cdot 0.25}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \]

Alternative 4: 68.1% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left|m - n\right|\\ \mathbf{if}\;m \leq -10.5:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;m \leq -2.4 \cdot 10^{-90}:\\ \;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{\left(t_0 - \ell\right) - M \cdot M}\\ \mathbf{elif}\;m \leq -4.7 \cdot 10^{-146} \lor \neg \left(m \leq -4.8 \cdot 10^{-222} \lor \neg \left(m \leq 7.5 \cdot 10^{-307}\right) \land m \leq 1.65 \cdot 10^{-159}\right):\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(\left(m + n\right) - M\right) - \left(\ell - t_0\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (fabs (- m n))))
   (if (<= m -10.5)
     (* (cos M) (exp (* -0.25 (* m m))))
     (if (<= m -2.4e-90)
       (* (cos (* 0.5 (* K n))) (exp (- (- t_0 l) (* M M))))
       (if (or (<= m -4.7e-146)
               (not
                (or (<= m -4.8e-222)
                    (and (not (<= m 7.5e-307)) (<= m 1.65e-159)))))
         (* (cos M) (exp (* -0.25 (* n n))))
         (* (cos M) (exp (- (* M (- (+ m n) M)) (- l t_0)))))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = fabs((m - n));
	double tmp;
	if (m <= -10.5) {
		tmp = cos(M) * exp((-0.25 * (m * m)));
	} else if (m <= -2.4e-90) {
		tmp = cos((0.5 * (K * n))) * exp(((t_0 - l) - (M * M)));
	} else if ((m <= -4.7e-146) || !((m <= -4.8e-222) || (!(m <= 7.5e-307) && (m <= 1.65e-159)))) {
		tmp = cos(M) * exp((-0.25 * (n * n)));
	} else {
		tmp = cos(M) * exp(((M * ((m + n) - M)) - (l - t_0)));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: t_0
    real(8) :: tmp
    t_0 = abs((m - n))
    if (m <= (-10.5d0)) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m * m)))
    else if (m <= (-2.4d-90)) then
        tmp = cos((0.5d0 * (k * n))) * exp(((t_0 - l) - (m_1 * m_1)))
    else if ((m <= (-4.7d-146)) .or. (.not. (m <= (-4.8d-222)) .or. (.not. (m <= 7.5d-307)) .and. (m <= 1.65d-159))) then
        tmp = cos(m_1) * exp(((-0.25d0) * (n * n)))
    else
        tmp = cos(m_1) * exp(((m_1 * ((m + n) - m_1)) - (l - t_0)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.abs((m - n));
	double tmp;
	if (m <= -10.5) {
		tmp = Math.cos(M) * Math.exp((-0.25 * (m * m)));
	} else if (m <= -2.4e-90) {
		tmp = Math.cos((0.5 * (K * n))) * Math.exp(((t_0 - l) - (M * M)));
	} else if ((m <= -4.7e-146) || !((m <= -4.8e-222) || (!(m <= 7.5e-307) && (m <= 1.65e-159)))) {
		tmp = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	} else {
		tmp = Math.cos(M) * Math.exp(((M * ((m + n) - M)) - (l - t_0)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.fabs((m - n))
	tmp = 0
	if m <= -10.5:
		tmp = math.cos(M) * math.exp((-0.25 * (m * m)))
	elif m <= -2.4e-90:
		tmp = math.cos((0.5 * (K * n))) * math.exp(((t_0 - l) - (M * M)))
	elif (m <= -4.7e-146) or not ((m <= -4.8e-222) or (not (m <= 7.5e-307) and (m <= 1.65e-159))):
		tmp = math.cos(M) * math.exp((-0.25 * (n * n)))
	else:
		tmp = math.cos(M) * math.exp(((M * ((m + n) - M)) - (l - t_0)))
	return tmp
function code(K, m, n, M, l)
	t_0 = abs(Float64(m - n))
	tmp = 0.0
	if (m <= -10.5)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(m * m))));
	elseif (m <= -2.4e-90)
		tmp = Float64(cos(Float64(0.5 * Float64(K * n))) * exp(Float64(Float64(t_0 - l) - Float64(M * M))));
	elseif ((m <= -4.7e-146) || !((m <= -4.8e-222) || (!(m <= 7.5e-307) && (m <= 1.65e-159))))
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))));
	else
		tmp = Float64(cos(M) * exp(Float64(Float64(M * Float64(Float64(m + n) - M)) - Float64(l - t_0))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = abs((m - n));
	tmp = 0.0;
	if (m <= -10.5)
		tmp = cos(M) * exp((-0.25 * (m * m)));
	elseif (m <= -2.4e-90)
		tmp = cos((0.5 * (K * n))) * exp(((t_0 - l) - (M * M)));
	elseif ((m <= -4.7e-146) || ~(((m <= -4.8e-222) || (~((m <= 7.5e-307)) && (m <= 1.65e-159)))))
		tmp = cos(M) * exp((-0.25 * (n * n)));
	else
		tmp = cos(M) * exp(((M * ((m + n) - M)) - (l - t_0)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[m, -10.5], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(m * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, -2.4e-90], N[(N[Cos[N[(0.5 * N[(K * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(t$95$0 - l), $MachinePrecision] - N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[m, -4.7e-146], N[Not[Or[LessEqual[m, -4.8e-222], And[N[Not[LessEqual[m, 7.5e-307]], $MachinePrecision], LessEqual[m, 1.65e-159]]]], $MachinePrecision]], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(M * N[(N[(m + n), $MachinePrecision] - M), $MachinePrecision]), $MachinePrecision] - N[(l - t$95$0), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left|m - n\right|\\
\mathbf{if}\;m \leq -10.5:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

\mathbf{elif}\;m \leq -2.4 \cdot 10^{-90}:\\
\;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{\left(t_0 - \ell\right) - M \cdot M}\\

\mathbf{elif}\;m \leq -4.7 \cdot 10^{-146} \lor \neg \left(m \leq -4.8 \cdot 10^{-222} \lor \neg \left(m \leq 7.5 \cdot 10^{-307}\right) \land m \leq 1.65 \cdot 10^{-159}\right):\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(\left(m + n\right) - M\right) - \left(\ell - t_0\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if m < -10.5

    1. Initial program 72.9%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative72.9%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/72.9%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+72.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified72.9%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 98.6%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg98.6%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified98.6%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in m around inf 98.6%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. unpow298.6%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    9. Simplified98.6%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(m \cdot m\right)}} \]

    if -10.5 < m < -2.4000000000000002e-90

    1. Initial program 93.3%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative93.3%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/93.3%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+93.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified93.3%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in M around inf 80.2%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{{M}^{2}}} \]
    5. Step-by-step derivation
      1. unpow280.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot M}} \]
    6. Simplified80.2%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot M}} \]
    7. Taylor expanded in n around inf 86.9%

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(n \cdot K\right)\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M} \]

    if -2.4000000000000002e-90 < m < -4.7e-146 or -4.79999999999999986e-222 < m < 7.5000000000000006e-307 or 1.6500000000000001e-159 < m

    1. Initial program 78.1%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative78.1%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/78.1%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-78.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative78.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-78.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg78.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+78.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative78.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+78.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified78.1%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 93.2%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg93.2%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified93.2%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in n around inf 54.3%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
    8. Step-by-step derivation
      1. unpow254.3%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(n \cdot n\right)}} \]
    9. Simplified54.3%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(n \cdot n\right)}} \]

    if -4.7e-146 < m < -4.79999999999999986e-222 or 7.5000000000000006e-307 < m < 1.6500000000000001e-159

    1. Initial program 84.3%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative84.3%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/84.3%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-84.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative84.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-84.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg84.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+84.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative84.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+84.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified84.3%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 99.9%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg99.9%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified99.9%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in M around inf 71.3%

      \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{\left(-1 \cdot \left(\left(n + m\right) \cdot M\right) + {M}^{2}\right)}} \]
    8. Step-by-step derivation
      1. +-commutative71.3%

        \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{\left({M}^{2} + -1 \cdot \left(\left(n + m\right) \cdot M\right)\right)}} \]
      2. unpow271.3%

        \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \left(\color{blue}{M \cdot M} + -1 \cdot \left(\left(n + m\right) \cdot M\right)\right)} \]
      3. associate-*r*71.3%

        \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \left(M \cdot M + \color{blue}{\left(-1 \cdot \left(n + m\right)\right) \cdot M}\right)} \]
      4. distribute-rgt-out71.4%

        \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot \left(M + -1 \cdot \left(n + m\right)\right)}} \]
      5. mul-1-neg71.4%

        \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot \left(M + \color{blue}{\left(-\left(n + m\right)\right)}\right)} \]
    9. Simplified71.4%

      \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot \left(M + \left(-\left(n + m\right)\right)\right)}} \]
  3. Recombined 4 regimes into one program.
  4. Final simplification71.3%

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -10.5:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;m \leq -2.4 \cdot 10^{-90}:\\ \;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{elif}\;m \leq -4.7 \cdot 10^{-146} \lor \neg \left(m \leq -4.8 \cdot 10^{-222} \lor \neg \left(m \leq 7.5 \cdot 10^{-307}\right) \land m \leq 1.65 \cdot 10^{-159}\right):\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(\left(m + n\right) - M\right) - \left(\ell - \left|m - n\right|\right)}\\ \end{array} \]

Alternative 5: 68.8% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;n \leq 1.3 \cdot 10^{-298}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;n \leq 0.000365:\\ \;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= n 1.3e-298)
   (* (cos M) (exp (* -0.25 (* m m))))
   (if (<= n 0.000365)
     (* (cos (* 0.5 (* K n))) (exp (- (- (fabs (- m n)) l) (* M M))))
     (* (cos M) (exp (* -0.25 (* n n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 1.3e-298) {
		tmp = cos(M) * exp((-0.25 * (m * m)));
	} else if (n <= 0.000365) {
		tmp = cos((0.5 * (K * n))) * exp(((fabs((m - n)) - l) - (M * M)));
	} else {
		tmp = cos(M) * exp((-0.25 * (n * n)));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (n <= 1.3d-298) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m * m)))
    else if (n <= 0.000365d0) then
        tmp = cos((0.5d0 * (k * n))) * exp(((abs((m - n)) - l) - (m_1 * m_1)))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n * n)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 1.3e-298) {
		tmp = Math.cos(M) * Math.exp((-0.25 * (m * m)));
	} else if (n <= 0.000365) {
		tmp = Math.cos((0.5 * (K * n))) * Math.exp(((Math.abs((m - n)) - l) - (M * M)));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if n <= 1.3e-298:
		tmp = math.cos(M) * math.exp((-0.25 * (m * m)))
	elif n <= 0.000365:
		tmp = math.cos((0.5 * (K * n))) * math.exp(((math.fabs((m - n)) - l) - (M * M)))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * (n * n)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= 1.3e-298)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(m * m))));
	elseif (n <= 0.000365)
		tmp = Float64(cos(Float64(0.5 * Float64(K * n))) * exp(Float64(Float64(abs(Float64(m - n)) - l) - Float64(M * M))));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (n <= 1.3e-298)
		tmp = cos(M) * exp((-0.25 * (m * m)));
	elseif (n <= 0.000365)
		tmp = cos((0.5 * (K * n))) * exp(((abs((m - n)) - l) - (M * M)));
	else
		tmp = cos(M) * exp((-0.25 * (n * n)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[n, 1.3e-298], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(m * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 0.000365], N[(N[Cos[N[(0.5 * N[(K * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;n \leq 1.3 \cdot 10^{-298}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

\mathbf{elif}\;n \leq 0.000365:\\
\;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if n < 1.2999999999999999e-298

    1. Initial program 76.2%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative76.2%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/76.2%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified76.2%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 96.4%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg96.4%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified96.4%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in m around inf 59.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. unpow259.1%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    9. Simplified59.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(m \cdot m\right)}} \]

    if 1.2999999999999999e-298 < n < 3.6499999999999998e-4

    1. Initial program 80.7%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative80.7%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/80.7%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified80.7%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in M around inf 65.9%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{{M}^{2}}} \]
    5. Step-by-step derivation
      1. unpow265.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot M}} \]
    6. Simplified65.9%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot M}} \]
    7. Taylor expanded in n around inf 76.2%

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(n \cdot K\right)\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M} \]

    if 3.6499999999999998e-4 < n

    1. Initial program 82.5%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative82.5%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/82.5%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified82.5%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 100.0%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg100.0%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in n around inf 94.9%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
    8. Step-by-step derivation
      1. unpow294.9%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(n \cdot n\right)}} \]
    9. Simplified94.9%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(n \cdot n\right)}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification71.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 1.3 \cdot 10^{-298}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;n \leq 0.000365:\\ \;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \]

Alternative 6: 68.8% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;n \leq 1.25 \cdot 10^{-293}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;n \leq 0.000365:\\ \;\;\;\;\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= n 1.25e-293)
   (* (cos M) (exp (* -0.25 (* m m))))
   (if (<= n 0.000365)
     (* (cos M) (exp (- (- (fabs (- m n)) l) (* M M))))
     (* (cos M) (exp (* -0.25 (* n n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 1.25e-293) {
		tmp = cos(M) * exp((-0.25 * (m * m)));
	} else if (n <= 0.000365) {
		tmp = cos(M) * exp(((fabs((m - n)) - l) - (M * M)));
	} else {
		tmp = cos(M) * exp((-0.25 * (n * n)));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (n <= 1.25d-293) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m * m)))
    else if (n <= 0.000365d0) then
        tmp = cos(m_1) * exp(((abs((m - n)) - l) - (m_1 * m_1)))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n * n)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 1.25e-293) {
		tmp = Math.cos(M) * Math.exp((-0.25 * (m * m)));
	} else if (n <= 0.000365) {
		tmp = Math.cos(M) * Math.exp(((Math.abs((m - n)) - l) - (M * M)));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if n <= 1.25e-293:
		tmp = math.cos(M) * math.exp((-0.25 * (m * m)))
	elif n <= 0.000365:
		tmp = math.cos(M) * math.exp(((math.fabs((m - n)) - l) - (M * M)))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * (n * n)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= 1.25e-293)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(m * m))));
	elseif (n <= 0.000365)
		tmp = Float64(cos(M) * exp(Float64(Float64(abs(Float64(m - n)) - l) - Float64(M * M))));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (n <= 1.25e-293)
		tmp = cos(M) * exp((-0.25 * (m * m)));
	elseif (n <= 0.000365)
		tmp = cos(M) * exp(((abs((m - n)) - l) - (M * M)));
	else
		tmp = cos(M) * exp((-0.25 * (n * n)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[n, 1.25e-293], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(m * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 0.000365], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;n \leq 1.25 \cdot 10^{-293}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

\mathbf{elif}\;n \leq 0.000365:\\
\;\;\;\;\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if n < 1.2500000000000001e-293

    1. Initial program 76.2%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative76.2%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/76.2%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified76.2%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 96.4%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg96.4%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified96.4%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in m around inf 59.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. unpow259.1%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    9. Simplified59.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(m \cdot m\right)}} \]

    if 1.2500000000000001e-293 < n < 3.6499999999999998e-4

    1. Initial program 80.7%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative80.7%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/80.7%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified80.7%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 90.6%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg90.6%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified90.6%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in M around inf 74.2%

      \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{{M}^{2}}} \]
    8. Step-by-step derivation
      1. unpow265.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot M}} \]
    9. Simplified74.2%

      \[\leadsto \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot M}} \]

    if 3.6499999999999998e-4 < n

    1. Initial program 82.5%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative82.5%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/82.5%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified82.5%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 100.0%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg100.0%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in n around inf 94.9%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
    8. Step-by-step derivation
      1. unpow294.9%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(n \cdot n\right)}} \]
    9. Simplified94.9%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(n \cdot n\right)}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification70.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 1.25 \cdot 10^{-293}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;n \leq 0.000365:\\ \;\;\;\;\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \]

Alternative 7: 65.5% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;m \leq -4.6 \cdot 10^{-7}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;m \leq -2.7 \cdot 10^{-90} \lor \neg \left(m \leq -3 \cdot 10^{-147}\right) \land m \leq -4.8 \cdot 10^{-222}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= m -4.6e-7)
   (* (cos M) (exp (* -0.25 (* m m))))
   (if (or (<= m -2.7e-90) (and (not (<= m -3e-147)) (<= m -4.8e-222)))
     (* (cos M) (exp (* M (- M))))
     (* (cos M) (exp (* -0.25 (* n n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -4.6e-7) {
		tmp = cos(M) * exp((-0.25 * (m * m)));
	} else if ((m <= -2.7e-90) || (!(m <= -3e-147) && (m <= -4.8e-222))) {
		tmp = cos(M) * exp((M * -M));
	} else {
		tmp = cos(M) * exp((-0.25 * (n * n)));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (m <= (-4.6d-7)) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m * m)))
    else if ((m <= (-2.7d-90)) .or. (.not. (m <= (-3d-147))) .and. (m <= (-4.8d-222))) then
        tmp = cos(m_1) * exp((m_1 * -m_1))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n * n)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -4.6e-7) {
		tmp = Math.cos(M) * Math.exp((-0.25 * (m * m)));
	} else if ((m <= -2.7e-90) || (!(m <= -3e-147) && (m <= -4.8e-222))) {
		tmp = Math.cos(M) * Math.exp((M * -M));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if m <= -4.6e-7:
		tmp = math.cos(M) * math.exp((-0.25 * (m * m)))
	elif (m <= -2.7e-90) or (not (m <= -3e-147) and (m <= -4.8e-222)):
		tmp = math.cos(M) * math.exp((M * -M))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * (n * n)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (m <= -4.6e-7)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(m * m))));
	elseif ((m <= -2.7e-90) || (!(m <= -3e-147) && (m <= -4.8e-222)))
		tmp = Float64(cos(M) * exp(Float64(M * Float64(-M))));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (m <= -4.6e-7)
		tmp = cos(M) * exp((-0.25 * (m * m)));
	elseif ((m <= -2.7e-90) || (~((m <= -3e-147)) && (m <= -4.8e-222)))
		tmp = cos(M) * exp((M * -M));
	else
		tmp = cos(M) * exp((-0.25 * (n * n)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[m, -4.6e-7], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(m * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[m, -2.7e-90], And[N[Not[LessEqual[m, -3e-147]], $MachinePrecision], LessEqual[m, -4.8e-222]]], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(M * (-M)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;m \leq -4.6 \cdot 10^{-7}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

\mathbf{elif}\;m \leq -2.7 \cdot 10^{-90} \lor \neg \left(m \leq -3 \cdot 10^{-147}\right) \land m \leq -4.8 \cdot 10^{-222}:\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\


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

    1. Initial program 73.2%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative73.2%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/73.2%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-73.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative73.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-73.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg73.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+73.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative73.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+73.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified73.2%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 98.6%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg98.6%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified98.6%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in m around inf 97.2%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. unpow297.2%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    9. Simplified97.2%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(m \cdot m\right)}} \]

    if -4.5999999999999999e-7 < m < -2.69999999999999996e-90 or -3.0000000000000002e-147 < m < -4.79999999999999986e-222

    1. Initial program 87.1%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative87.1%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/87.1%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-87.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative87.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-87.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg87.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+87.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative87.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+87.1%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified87.1%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 96.8%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg96.8%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified96.8%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in M around inf 62.3%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    8. Step-by-step derivation
      1. mul-1-neg62.3%

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
      2. unpow262.3%

        \[\leadsto \cos M \cdot e^{-\color{blue}{M \cdot M}} \]
    9. Simplified62.3%

      \[\leadsto \cos M \cdot e^{\color{blue}{-M \cdot M}} \]

    if -2.69999999999999996e-90 < m < -3.0000000000000002e-147 or -4.79999999999999986e-222 < m

    1. Initial program 79.4%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative79.4%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/79.4%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-79.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative79.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-79.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg79.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+79.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative79.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+79.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified79.4%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 94.4%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg94.4%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified94.4%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in n around inf 55.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
    8. Step-by-step derivation
      1. unpow255.1%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(n \cdot n\right)}} \]
    9. Simplified55.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(n \cdot n\right)}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification67.7%

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -4.6 \cdot 10^{-7}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;m \leq -2.7 \cdot 10^{-90} \lor \neg \left(m \leq -3 \cdot 10^{-147}\right) \land m \leq -4.8 \cdot 10^{-222}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \]

Alternative 8: 68.7% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;n \leq 1.25 \cdot 10^{-296}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;n \leq 0.000365:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= n 1.25e-296)
   (* (cos M) (exp (* -0.25 (* m m))))
   (if (<= n 0.000365)
     (exp (- (- (fabs (- m n)) l) (* M M)))
     (* (cos M) (exp (* -0.25 (* n n)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 1.25e-296) {
		tmp = cos(M) * exp((-0.25 * (m * m)));
	} else if (n <= 0.000365) {
		tmp = exp(((fabs((m - n)) - l) - (M * M)));
	} else {
		tmp = cos(M) * exp((-0.25 * (n * n)));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (n <= 1.25d-296) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m * m)))
    else if (n <= 0.000365d0) then
        tmp = exp(((abs((m - n)) - l) - (m_1 * m_1)))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n * n)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 1.25e-296) {
		tmp = Math.cos(M) * Math.exp((-0.25 * (m * m)));
	} else if (n <= 0.000365) {
		tmp = Math.exp(((Math.abs((m - n)) - l) - (M * M)));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if n <= 1.25e-296:
		tmp = math.cos(M) * math.exp((-0.25 * (m * m)))
	elif n <= 0.000365:
		tmp = math.exp(((math.fabs((m - n)) - l) - (M * M)))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * (n * n)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= 1.25e-296)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(m * m))));
	elseif (n <= 0.000365)
		tmp = exp(Float64(Float64(abs(Float64(m - n)) - l) - Float64(M * M)));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (n <= 1.25e-296)
		tmp = cos(M) * exp((-0.25 * (m * m)));
	elseif (n <= 0.000365)
		tmp = exp(((abs((m - n)) - l) - (M * M)));
	else
		tmp = cos(M) * exp((-0.25 * (n * n)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[n, 1.25e-296], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(m * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 0.000365], N[Exp[N[(N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;n \leq 1.25 \cdot 10^{-296}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

\mathbf{elif}\;n \leq 0.000365:\\
\;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if n < 1.25000000000000008e-296

    1. Initial program 76.2%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative76.2%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/76.2%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+76.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified76.2%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 96.4%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg96.4%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified96.4%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in m around inf 59.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. unpow259.1%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    9. Simplified59.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(m \cdot m\right)}} \]

    if 1.25000000000000008e-296 < n < 3.6499999999999998e-4

    1. Initial program 80.7%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative80.7%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/80.7%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+80.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified80.7%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in M around inf 65.9%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{{M}^{2}}} \]
    5. Step-by-step derivation
      1. unpow265.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot M}} \]
    6. Simplified65.9%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - \color{blue}{M \cdot M}} \]
    7. Step-by-step derivation
      1. add-cube-cbrt66.3%

        \[\leadsto \cos \color{blue}{\left(\left(\sqrt[3]{\left(m + n\right) \cdot \frac{K}{2} - M} \cdot \sqrt[3]{\left(m + n\right) \cdot \frac{K}{2} - M}\right) \cdot \sqrt[3]{\left(m + n\right) \cdot \frac{K}{2} - M}\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M} \]
      2. pow366.2%

        \[\leadsto \cos \color{blue}{\left({\left(\sqrt[3]{\left(m + n\right) \cdot \frac{K}{2} - M}\right)}^{3}\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M} \]
      3. div-inv66.2%

        \[\leadsto \cos \left({\left(\sqrt[3]{\left(m + n\right) \cdot \color{blue}{\left(K \cdot \frac{1}{2}\right)} - M}\right)}^{3}\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M} \]
      4. metadata-eval66.2%

        \[\leadsto \cos \left({\left(\sqrt[3]{\left(m + n\right) \cdot \left(K \cdot \color{blue}{0.5}\right) - M}\right)}^{3}\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M} \]
    8. Applied egg-rr66.2%

      \[\leadsto \cos \color{blue}{\left({\left(\sqrt[3]{\left(m + n\right) \cdot \left(K \cdot 0.5\right) - M}\right)}^{3}\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M} \]
    9. Taylor expanded in m around inf 74.2%

      \[\leadsto \color{blue}{1} \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M} \]

    if 3.6499999999999998e-4 < n

    1. Initial program 82.5%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative82.5%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/82.5%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+82.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified82.5%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 100.0%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg100.0%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in n around inf 94.9%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
    8. Step-by-step derivation
      1. unpow294.9%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(n \cdot n\right)}} \]
    9. Simplified94.9%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(n \cdot n\right)}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification70.6%

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 1.25 \cdot 10^{-296}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{elif}\;n \leq 0.000365:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \]

Alternative 9: 77.5% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;m \leq -4.6 \cdot 10^{-7} \lor \neg \left(m \leq 55\right):\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= m -4.6e-7) (not (<= m 55.0)))
   (* (cos M) (exp (* -0.25 (* m m))))
   (* (cos M) (exp (* M (- M))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((m <= -4.6e-7) || !(m <= 55.0)) {
		tmp = cos(M) * exp((-0.25 * (m * m)));
	} else {
		tmp = cos(M) * exp((M * -M));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if ((m <= (-4.6d-7)) .or. (.not. (m <= 55.0d0))) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m * m)))
    else
        tmp = cos(m_1) * exp((m_1 * -m_1))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((m <= -4.6e-7) || !(m <= 55.0)) {
		tmp = Math.cos(M) * Math.exp((-0.25 * (m * m)));
	} else {
		tmp = Math.cos(M) * Math.exp((M * -M));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if (m <= -4.6e-7) or not (m <= 55.0):
		tmp = math.cos(M) * math.exp((-0.25 * (m * m)))
	else:
		tmp = math.cos(M) * math.exp((M * -M))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if ((m <= -4.6e-7) || !(m <= 55.0))
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(m * m))));
	else
		tmp = Float64(cos(M) * exp(Float64(M * Float64(-M))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if ((m <= -4.6e-7) || ~((m <= 55.0)))
		tmp = cos(M) * exp((-0.25 * (m * m)));
	else
		tmp = cos(M) * exp((M * -M));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[Or[LessEqual[m, -4.6e-7], N[Not[LessEqual[m, 55.0]], $MachinePrecision]], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(m * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(M * (-M)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;m \leq -4.6 \cdot 10^{-7} \lor \neg \left(m \leq 55\right):\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < -4.5999999999999999e-7 or 55 < m

    1. Initial program 73.9%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative73.9%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/73.9%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-73.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative73.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-73.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg73.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+73.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative73.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+73.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified73.9%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 98.5%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg98.5%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified98.5%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in m around inf 97.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. unpow297.1%

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    9. Simplified97.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot \left(m \cdot m\right)}} \]

    if -4.5999999999999999e-7 < m < 55

    1. Initial program 83.9%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative83.9%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/83.9%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-83.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative83.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-83.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg83.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+83.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative83.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+83.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified83.9%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 93.0%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg93.0%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified93.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in M around inf 59.4%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    8. Step-by-step derivation
      1. mul-1-neg59.4%

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
      2. unpow259.4%

        \[\leadsto \cos M \cdot e^{-\color{blue}{M \cdot M}} \]
    9. Simplified59.4%

      \[\leadsto \cos M \cdot e^{\color{blue}{-M \cdot M}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification79.1%

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -4.6 \cdot 10^{-7} \lor \neg \left(m \leq 55\right):\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \end{array} \]

Alternative 10: 73.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\ell \leq -720:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{elif}\;\ell \leq 720:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= l -720.0)
   (* (cos M) (exp l))
   (if (<= l 720.0) (* (cos M) (exp (* M (- M)))) (exp (- l)))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -720.0) {
		tmp = cos(M) * exp(l);
	} else if (l <= 720.0) {
		tmp = cos(M) * exp((M * -M));
	} else {
		tmp = exp(-l);
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (l <= (-720.0d0)) then
        tmp = cos(m_1) * exp(l)
    else if (l <= 720.0d0) then
        tmp = cos(m_1) * exp((m_1 * -m_1))
    else
        tmp = exp(-l)
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -720.0) {
		tmp = Math.cos(M) * Math.exp(l);
	} else if (l <= 720.0) {
		tmp = Math.cos(M) * Math.exp((M * -M));
	} else {
		tmp = Math.exp(-l);
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if l <= -720.0:
		tmp = math.cos(M) * math.exp(l)
	elif l <= 720.0:
		tmp = math.cos(M) * math.exp((M * -M))
	else:
		tmp = math.exp(-l)
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (l <= -720.0)
		tmp = Float64(cos(M) * exp(l));
	elseif (l <= 720.0)
		tmp = Float64(cos(M) * exp(Float64(M * Float64(-M))));
	else
		tmp = exp(Float64(-l));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (l <= -720.0)
		tmp = cos(M) * exp(l);
	elseif (l <= 720.0)
		tmp = cos(M) * exp((M * -M));
	else
		tmp = exp(-l);
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[l, -720.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[l], $MachinePrecision]), $MachinePrecision], If[LessEqual[l, 720.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(M * (-M)), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Exp[(-l)], $MachinePrecision]]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -720:\\
\;\;\;\;\cos M \cdot e^{\ell}\\

\mathbf{elif}\;\ell \leq 720:\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\

\mathbf{else}:\\
\;\;\;\;e^{-\ell}\\


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

    1. Initial program 72.2%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative72.2%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/72.2%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-72.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative72.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-72.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg72.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+72.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative72.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+72.2%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified72.2%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 92.6%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg92.6%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified92.6%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in l around inf 17.9%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    8. Step-by-step derivation
      1. mul-1-neg17.9%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    9. Simplified17.9%

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    10. Step-by-step derivation
      1. expm1-log1p-u15.8%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)\right)} \]
      2. expm1-udef15.8%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)} - 1} \]
      3. add-sqr-sqrt15.8%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{-\ell} \cdot \sqrt{-\ell}}}\right)} - 1 \]
      4. sqrt-unprod15.8%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\left(-\ell\right) \cdot \left(-\ell\right)}}}\right)} - 1 \]
      5. sqr-neg15.8%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\sqrt{\color{blue}{\ell \cdot \ell}}}\right)} - 1 \]
      6. sqrt-unprod0.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\ell} \cdot \sqrt{\ell}}}\right)} - 1 \]
      7. add-sqr-sqrt76.3%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\ell}}\right)} - 1 \]
    11. Applied egg-rr76.3%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)} - 1} \]
    12. Step-by-step derivation
      1. expm1-def76.3%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)\right)} \]
      2. expm1-log1p76.3%

        \[\leadsto \color{blue}{\cos M \cdot e^{\ell}} \]
    13. Simplified76.3%

      \[\leadsto \color{blue}{\cos M \cdot e^{\ell}} \]

    if -720 < l < 720

    1. Initial program 77.9%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative77.9%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/77.9%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-77.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative77.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-77.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg77.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+77.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative77.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+77.9%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified77.9%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 95.3%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg95.3%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified95.3%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in M around inf 61.6%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    8. Step-by-step derivation
      1. mul-1-neg61.6%

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
      2. unpow261.6%

        \[\leadsto \cos M \cdot e^{-\color{blue}{M \cdot M}} \]
    9. Simplified61.6%

      \[\leadsto \cos M \cdot e^{\color{blue}{-M \cdot M}} \]

    if 720 < l

    1. Initial program 85.7%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative85.7%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/85.7%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-85.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative85.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-85.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg85.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+85.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative85.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+85.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified85.7%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 100.0%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg100.0%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified100.0%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in l around inf 100.0%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    8. Step-by-step derivation
      1. mul-1-neg100.0%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    9. Simplified100.0%

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    10. Taylor expanded in M around 0 100.0%

      \[\leadsto \color{blue}{e^{-\ell}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification74.2%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq -720:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{elif}\;\ell \leq 720:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \]

Alternative 11: 49.8% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\ell \leq -5.5 \cdot 10^{-15}:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= l -5.5e-15) (* (cos M) (exp l)) (exp (- l))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -5.5e-15) {
		tmp = cos(M) * exp(l);
	} else {
		tmp = exp(-l);
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (l <= (-5.5d-15)) then
        tmp = cos(m_1) * exp(l)
    else
        tmp = exp(-l)
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -5.5e-15) {
		tmp = Math.cos(M) * Math.exp(l);
	} else {
		tmp = Math.exp(-l);
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if l <= -5.5e-15:
		tmp = math.cos(M) * math.exp(l)
	else:
		tmp = math.exp(-l)
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (l <= -5.5e-15)
		tmp = Float64(cos(M) * exp(l));
	else
		tmp = exp(Float64(-l));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (l <= -5.5e-15)
		tmp = cos(M) * exp(l);
	else
		tmp = exp(-l);
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[l, -5.5e-15], N[(N[Cos[M], $MachinePrecision] * N[Exp[l], $MachinePrecision]), $MachinePrecision], N[Exp[(-l)], $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -5.5 \cdot 10^{-15}:\\
\;\;\;\;\cos M \cdot e^{\ell}\\

\mathbf{else}:\\
\;\;\;\;e^{-\ell}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if l < -5.5000000000000002e-15

    1. Initial program 72.4%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative72.4%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/72.4%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-72.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative72.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-72.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg72.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+72.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative72.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+72.4%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified72.4%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 93.1%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg93.1%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified93.1%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in l around inf 16.8%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    8. Step-by-step derivation
      1. mul-1-neg16.8%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    9. Simplified16.8%

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    10. Step-by-step derivation
      1. expm1-log1p-u15.0%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)\right)} \]
      2. expm1-udef15.0%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)} - 1} \]
      3. add-sqr-sqrt15.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{-\ell} \cdot \sqrt{-\ell}}}\right)} - 1 \]
      4. sqrt-unprod15.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\left(-\ell\right) \cdot \left(-\ell\right)}}}\right)} - 1 \]
      5. sqr-neg15.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\sqrt{\color{blue}{\ell \cdot \ell}}}\right)} - 1 \]
      6. sqrt-unprod0.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\ell} \cdot \sqrt{\ell}}}\right)} - 1 \]
      7. add-sqr-sqrt71.3%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\ell}}\right)} - 1 \]
    11. Applied egg-rr71.3%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)} - 1} \]
    12. Step-by-step derivation
      1. expm1-def71.3%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)\right)} \]
      2. expm1-log1p71.3%

        \[\leadsto \color{blue}{\cos M \cdot e^{\ell}} \]
    13. Simplified71.3%

      \[\leadsto \color{blue}{\cos M \cdot e^{\ell}} \]

    if -5.5000000000000002e-15 < l

    1. Initial program 80.5%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative80.5%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/80.5%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-80.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative80.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-80.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg80.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+80.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative80.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+80.5%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified80.5%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 96.7%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg96.7%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified96.7%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in l around inf 37.2%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    8. Step-by-step derivation
      1. mul-1-neg37.2%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    9. Simplified37.2%

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    10. Taylor expanded in M around 0 37.2%

      \[\leadsto \color{blue}{e^{-\ell}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification44.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq -5.5 \cdot 10^{-15}:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \]

Alternative 12: 49.8% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\ell \leq -1.6 \cdot 10^{-9}:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{else}:\\ \;\;\;\;\frac{\cos M}{e^{\ell}}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= l -1.6e-9) (* (cos M) (exp l)) (/ (cos M) (exp l))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -1.6e-9) {
		tmp = cos(M) * exp(l);
	} else {
		tmp = cos(M) / exp(l);
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (l <= (-1.6d-9)) then
        tmp = cos(m_1) * exp(l)
    else
        tmp = cos(m_1) / exp(l)
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -1.6e-9) {
		tmp = Math.cos(M) * Math.exp(l);
	} else {
		tmp = Math.cos(M) / Math.exp(l);
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if l <= -1.6e-9:
		tmp = math.cos(M) * math.exp(l)
	else:
		tmp = math.cos(M) / math.exp(l)
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (l <= -1.6e-9)
		tmp = Float64(cos(M) * exp(l));
	else
		tmp = Float64(cos(M) / exp(l));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (l <= -1.6e-9)
		tmp = cos(M) * exp(l);
	else
		tmp = cos(M) / exp(l);
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[l, -1.6e-9], N[(N[Cos[M], $MachinePrecision] * N[Exp[l], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] / N[Exp[l], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -1.6 \cdot 10^{-9}:\\
\;\;\;\;\cos M \cdot e^{\ell}\\

\mathbf{else}:\\
\;\;\;\;\frac{\cos M}{e^{\ell}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if l < -1.60000000000000006e-9

    1. Initial program 72.7%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative72.7%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/72.7%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-72.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative72.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-72.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg72.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+72.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative72.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+72.7%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified72.7%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 92.7%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg92.7%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified92.7%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in l around inf 17.6%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    8. Step-by-step derivation
      1. mul-1-neg17.6%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    9. Simplified17.6%

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    10. Step-by-step derivation
      1. expm1-log1p-u15.6%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)\right)} \]
      2. expm1-udef15.6%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)} - 1} \]
      3. add-sqr-sqrt15.6%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{-\ell} \cdot \sqrt{-\ell}}}\right)} - 1 \]
      4. sqrt-unprod15.6%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\left(-\ell\right) \cdot \left(-\ell\right)}}}\right)} - 1 \]
      5. sqr-neg15.6%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\sqrt{\color{blue}{\ell \cdot \ell}}}\right)} - 1 \]
      6. sqrt-unprod0.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\ell} \cdot \sqrt{\ell}}}\right)} - 1 \]
      7. add-sqr-sqrt75.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\ell}}\right)} - 1 \]
    11. Applied egg-rr75.0%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)} - 1} \]
    12. Step-by-step derivation
      1. expm1-def75.0%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)\right)} \]
      2. expm1-log1p75.0%

        \[\leadsto \color{blue}{\cos M \cdot e^{\ell}} \]
    13. Simplified75.0%

      \[\leadsto \color{blue}{\cos M \cdot e^{\ell}} \]

    if -1.60000000000000006e-9 < l

    1. Initial program 80.3%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Step-by-step derivation
      1. *-commutative80.3%

        \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      2. associate-*r/80.3%

        \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. associate--r-80.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
      4. +-commutative80.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
      5. associate-+r-80.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
      6. unsub-neg80.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
      7. associate--r+80.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
      8. +-commutative80.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
      9. associate--r+80.3%

        \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    3. Simplified80.3%

      \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
    4. Taylor expanded in K around 0 96.7%

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    5. Step-by-step derivation
      1. cos-neg96.7%

        \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    6. Simplified96.7%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
    7. Taylor expanded in l around inf 36.7%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    8. Step-by-step derivation
      1. mul-1-neg36.7%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    9. Simplified36.7%

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    10. Step-by-step derivation
      1. exp-neg36.7%

        \[\leadsto \cos M \cdot \color{blue}{\frac{1}{e^{\ell}}} \]
      2. un-div-inv36.7%

        \[\leadsto \color{blue}{\frac{\cos M}{e^{\ell}}} \]
    11. Applied egg-rr36.7%

      \[\leadsto \color{blue}{\frac{\cos M}{e^{\ell}}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification44.9%

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq -1.6 \cdot 10^{-9}:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{else}:\\ \;\;\;\;\frac{\cos M}{e^{\ell}}\\ \end{array} \]

Alternative 13: 36.5% accurate, 4.2× speedup?

\[\begin{array}{l} \\ e^{-\ell} \end{array} \]
(FPCore (K m n M l) :precision binary64 (exp (- l)))
double code(double K, double m, double n, double M, double l) {
	return exp(-l);
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = exp(-l)
end function
public static double code(double K, double m, double n, double M, double l) {
	return Math.exp(-l);
}
def code(K, m, n, M, l):
	return math.exp(-l)
function code(K, m, n, M, l)
	return exp(Float64(-l))
end
function tmp = code(K, m, n, M, l)
	tmp = exp(-l);
end
code[K_, m_, n_, M_, l_] := N[Exp[(-l)], $MachinePrecision]
\begin{array}{l}

\\
e^{-\ell}
\end{array}
Derivation
  1. Initial program 78.6%

    \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  2. Step-by-step derivation
    1. *-commutative78.6%

      \[\leadsto \cos \left(\frac{\color{blue}{\left(m + n\right) \cdot K}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. associate-*r/78.6%

      \[\leadsto \cos \left(\color{blue}{\left(m + n\right) \cdot \frac{K}{2}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    3. associate--r-78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right) + \left|m - n\right|}} \]
    4. +-commutative78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| + \left(\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \ell\right)}} \]
    5. associate-+r-78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| + \left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right)\right) - \ell}} \]
    6. unsub-neg78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - {\left(\frac{m + n}{2} - M\right)}^{2}\right)} - \ell} \]
    7. associate--r+78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left|m - n\right| - \left({\left(\frac{m + n}{2} - M\right)}^{2} + \ell\right)}} \]
    8. +-commutative78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left|m - n\right| - \color{blue}{\left(\ell + {\left(\frac{m + n}{2} - M\right)}^{2}\right)}} \]
    9. associate--r+78.6%

      \[\leadsto \cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\color{blue}{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
  3. Simplified78.6%

    \[\leadsto \color{blue}{\cos \left(\left(m + n\right) \cdot \frac{K}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}} \]
  4. Taylor expanded in K around 0 95.9%

    \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
  5. Step-by-step derivation
    1. cos-neg95.9%

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
  6. Simplified95.9%

    \[\leadsto \color{blue}{\cos M} \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \]
  7. Taylor expanded in l around inf 32.6%

    \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
  8. Step-by-step derivation
    1. mul-1-neg32.6%

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
  9. Simplified32.6%

    \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
  10. Taylor expanded in M around 0 32.2%

    \[\leadsto \color{blue}{e^{-\ell}} \]
  11. Final simplification32.2%

    \[\leadsto e^{-\ell} \]

Reproduce

?
herbie shell --seed 2023176 
(FPCore (K m n M l)
  :name "Maksimov and Kolovsky, Equation (32)"
  :precision binary64
  (* (cos (- (/ (* K (+ m n)) 2.0) M)) (exp (- (- (pow (- (/ (+ m n) 2.0) M) 2.0)) (- l (fabs (- m n)))))))