Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.9% → 96.5%
Time: 27.9s
Alternatives: 13
Speedup: 1.3×

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: 96.5% accurate, 0.5× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := \left|m - n\right|\\ t_1 := e^{\left(t\_0 - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}\\ t_2 := \left(m + n\right) \cdot 0.5\\ \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t\_1 \leq \infty:\\ \;\;\;\;t\_1 \cdot \cos \left(\frac{1}{\frac{\frac{2}{n}}{K}} - M\right)\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{t\_0 + \left(t\_2 - M\right) \cdot \left(M - t\_2\right)}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (fabs (- m n)))
        (t_1 (exp (- (- t_0 l) (pow (- (/ (+ m n) 2.0) M) 2.0))))
        (t_2 (* (+ m n) 0.5)))
   (if (<= (* (cos (- (/ (* K (+ m n)) 2.0) M)) t_1) INFINITY)
     (* t_1 (cos (- (/ 1.0 (/ (/ 2.0 n) K)) M)))
     (* (cos M) (exp (+ t_0 (* (- t_2 M) (- M t_2))))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = fabs((m - n));
	double t_1 = exp(((t_0 - l) - pow((((m + n) / 2.0) - M), 2.0)));
	double t_2 = (m + n) * 0.5;
	double tmp;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_1) <= ((double) INFINITY)) {
		tmp = t_1 * cos(((1.0 / ((2.0 / n) / K)) - M));
	} else {
		tmp = cos(M) * exp((t_0 + ((t_2 - M) * (M - t_2))));
	}
	return tmp;
}
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.abs((m - n));
	double t_1 = Math.exp(((t_0 - l) - Math.pow((((m + n) / 2.0) - M), 2.0)));
	double t_2 = (m + n) * 0.5;
	double tmp;
	if ((Math.cos((((K * (m + n)) / 2.0) - M)) * t_1) <= Double.POSITIVE_INFINITY) {
		tmp = t_1 * Math.cos(((1.0 / ((2.0 / n) / K)) - M));
	} else {
		tmp = Math.cos(M) * Math.exp((t_0 + ((t_2 - M) * (M - t_2))));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.fabs((m - n))
	t_1 = math.exp(((t_0 - l) - math.pow((((m + n) / 2.0) - M), 2.0)))
	t_2 = (m + n) * 0.5
	tmp = 0
	if (math.cos((((K * (m + n)) / 2.0) - M)) * t_1) <= math.inf:
		tmp = t_1 * math.cos(((1.0 / ((2.0 / n) / K)) - M))
	else:
		tmp = math.cos(M) * math.exp((t_0 + ((t_2 - M) * (M - t_2))))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = abs(Float64(m - n))
	t_1 = exp(Float64(Float64(t_0 - l) - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)))
	t_2 = Float64(Float64(m + n) * 0.5)
	tmp = 0.0
	if (Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * t_1) <= Inf)
		tmp = Float64(t_1 * cos(Float64(Float64(1.0 / Float64(Float64(2.0 / n) / K)) - M)));
	else
		tmp = Float64(cos(M) * exp(Float64(t_0 + Float64(Float64(t_2 - M) * Float64(M - t_2)))));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	t_0 = abs((m - n));
	t_1 = exp(((t_0 - l) - ((((m + n) / 2.0) - M) ^ 2.0)));
	t_2 = (m + n) * 0.5;
	tmp = 0.0;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_1) <= Inf)
		tmp = t_1 * cos(((1.0 / ((2.0 / n) / K)) - M));
	else
		tmp = cos(M) * exp((t_0 + ((t_2 - M) * (M - t_2))));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[Exp[N[(N[(t$95$0 - l), $MachinePrecision] - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision]}, If[LessEqual[N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * t$95$1), $MachinePrecision], Infinity], N[(t$95$1 * N[Cos[N[(N[(1.0 / N[(N[(2.0 / n), $MachinePrecision] / K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$0 + N[(N[(t$95$2 - M), $MachinePrecision] * N[(M - t$95$2), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \left|m - n\right|\\
t_1 := e^{\left(t\_0 - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}\\
t_2 := \left(m + n\right) \cdot 0.5\\
\mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t\_1 \leq \infty:\\
\;\;\;\;t\_1 \cdot \cos \left(\frac{1}{\frac{\frac{2}{n}}{K}} - M\right)\\

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


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

    1. Initial program 94.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. Add Preprocessing
    3. Taylor expanded in m around 0 94.9%

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

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

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

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

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

        \[\leadsto \cos \left(\color{blue}{\frac{1}{\frac{2}{n \cdot K}}} - 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.5%

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

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

    if +inf.0 < (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.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. Add Preprocessing
    3. Taylor expanded in K around 0 98.3%

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \color{blue}{\left(0.5 \cdot \left(m + n\right) - M\right) \cdot \left(0.5 \cdot \left(m + n\right) - M\right)}} \]
    8. Applied egg-rr98.4%

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

    \[\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:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos \left(\frac{1}{\frac{\frac{2}{n}}{K}} - M\right)\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left|m - n\right| + \left(\left(m + n\right) \cdot 0.5 - M\right) \cdot \left(M - \left(m + n\right) \cdot 0.5\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 96.6% accurate, 1.0× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos M \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (* (exp (- (- (fabs (- m n)) l) (pow (- (/ (+ m n) 2.0) M) 2.0))) (cos M)))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	return exp(((fabs((m - n)) - l) - pow((((m + n) / 2.0) - M), 2.0))) * cos(M);
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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(((abs((m - n)) - l) - ((((m + n) / 2.0d0) - m_1) ** 2.0d0))) * cos(m_1)
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	return Math.exp(((Math.abs((m - n)) - l) - Math.pow((((m + n) / 2.0) - M), 2.0))) * Math.cos(M);
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.exp(((math.fabs((m - n)) - l) - math.pow((((m + n) / 2.0) - M), 2.0))) * math.cos(M)
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return Float64(exp(Float64(Float64(abs(Float64(m - n)) - l) - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0))) * cos(M))
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	tmp = exp(((abs((m - n)) - l) - ((((m + n) / 2.0) - M) ^ 2.0))) * cos(M);
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := N[(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] * N[Cos[M], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos M
\end{array}
Derivation
  1. Initial program 72.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. Add Preprocessing
  3. Taylor expanded in K around 0 94.2%

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

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

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

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

Alternative 3: 82.0% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\ t_1 := \cos M \cdot e^{-{M}^{2}}\\ t_2 := M \cdot \left(M - n\right)\\ t_3 := \cos \left(-M\right) \cdot e^{t\_2 - \left(n + \ell\right)}\\ \mathbf{if}\;M \leq -27:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;M \leq -7.8 \cdot 10^{-64}:\\ \;\;\;\;t\_0 \cdot e^{\left(m - n\right) + \left(t\_2 - \ell\right)}\\ \mathbf{elif}\;M \leq 3.7 \cdot 10^{-100}:\\ \;\;\;\;t\_3\\ \mathbf{elif}\;M \leq 3 \cdot 10^{-25}:\\ \;\;\;\;t\_0 \cdot e^{\left(m + t\_2\right) - n}\\ \mathbf{elif}\;M \leq 27:\\ \;\;\;\;t\_3\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (cos (- (/ (* K (+ m n)) 2.0) M)))
        (t_1 (* (cos M) (exp (- (pow M 2.0)))))
        (t_2 (* M (- M n)))
        (t_3 (* (cos (- M)) (exp (- t_2 (+ n l))))))
   (if (<= M -27.0)
     t_1
     (if (<= M -7.8e-64)
       (* t_0 (exp (+ (- m n) (- t_2 l))))
       (if (<= M 3.7e-100)
         t_3
         (if (<= M 3e-25)
           (* t_0 (exp (- (+ m t_2) n)))
           (if (<= M 27.0) t_3 t_1)))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = cos((((K * (m + n)) / 2.0) - M));
	double t_1 = cos(M) * exp(-pow(M, 2.0));
	double t_2 = M * (M - n);
	double t_3 = cos(-M) * exp((t_2 - (n + l)));
	double tmp;
	if (M <= -27.0) {
		tmp = t_1;
	} else if (M <= -7.8e-64) {
		tmp = t_0 * exp(((m - n) + (t_2 - l)));
	} else if (M <= 3.7e-100) {
		tmp = t_3;
	} else if (M <= 3e-25) {
		tmp = t_0 * exp(((m + t_2) - n));
	} else if (M <= 27.0) {
		tmp = t_3;
	} else {
		tmp = t_1;
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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) :: t_3
    real(8) :: tmp
    t_0 = cos((((k * (m + n)) / 2.0d0) - m_1))
    t_1 = cos(m_1) * exp(-(m_1 ** 2.0d0))
    t_2 = m_1 * (m_1 - n)
    t_3 = cos(-m_1) * exp((t_2 - (n + l)))
    if (m_1 <= (-27.0d0)) then
        tmp = t_1
    else if (m_1 <= (-7.8d-64)) then
        tmp = t_0 * exp(((m - n) + (t_2 - l)))
    else if (m_1 <= 3.7d-100) then
        tmp = t_3
    else if (m_1 <= 3d-25) then
        tmp = t_0 * exp(((m + t_2) - n))
    else if (m_1 <= 27.0d0) then
        tmp = t_3
    else
        tmp = t_1
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.cos((((K * (m + n)) / 2.0) - M));
	double t_1 = Math.cos(M) * Math.exp(-Math.pow(M, 2.0));
	double t_2 = M * (M - n);
	double t_3 = Math.cos(-M) * Math.exp((t_2 - (n + l)));
	double tmp;
	if (M <= -27.0) {
		tmp = t_1;
	} else if (M <= -7.8e-64) {
		tmp = t_0 * Math.exp(((m - n) + (t_2 - l)));
	} else if (M <= 3.7e-100) {
		tmp = t_3;
	} else if (M <= 3e-25) {
		tmp = t_0 * Math.exp(((m + t_2) - n));
	} else if (M <= 27.0) {
		tmp = t_3;
	} else {
		tmp = t_1;
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.cos((((K * (m + n)) / 2.0) - M))
	t_1 = math.cos(M) * math.exp(-math.pow(M, 2.0))
	t_2 = M * (M - n)
	t_3 = math.cos(-M) * math.exp((t_2 - (n + l)))
	tmp = 0
	if M <= -27.0:
		tmp = t_1
	elif M <= -7.8e-64:
		tmp = t_0 * math.exp(((m - n) + (t_2 - l)))
	elif M <= 3.7e-100:
		tmp = t_3
	elif M <= 3e-25:
		tmp = t_0 * math.exp(((m + t_2) - n))
	elif M <= 27.0:
		tmp = t_3
	else:
		tmp = t_1
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M))
	t_1 = Float64(cos(M) * exp(Float64(-(M ^ 2.0))))
	t_2 = Float64(M * Float64(M - n))
	t_3 = Float64(cos(Float64(-M)) * exp(Float64(t_2 - Float64(n + l))))
	tmp = 0.0
	if (M <= -27.0)
		tmp = t_1;
	elseif (M <= -7.8e-64)
		tmp = Float64(t_0 * exp(Float64(Float64(m - n) + Float64(t_2 - l))));
	elseif (M <= 3.7e-100)
		tmp = t_3;
	elseif (M <= 3e-25)
		tmp = Float64(t_0 * exp(Float64(Float64(m + t_2) - n)));
	elseif (M <= 27.0)
		tmp = t_3;
	else
		tmp = t_1;
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	t_0 = cos((((K * (m + n)) / 2.0) - M));
	t_1 = cos(M) * exp(-(M ^ 2.0));
	t_2 = M * (M - n);
	t_3 = cos(-M) * exp((t_2 - (n + l)));
	tmp = 0.0;
	if (M <= -27.0)
		tmp = t_1;
	elseif (M <= -7.8e-64)
		tmp = t_0 * exp(((m - n) + (t_2 - l)));
	elseif (M <= 3.7e-100)
		tmp = t_3;
	elseif (M <= 3e-25)
		tmp = t_0 * exp(((m + t_2) - n));
	elseif (M <= 27.0)
		tmp = t_3;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[M], $MachinePrecision] * N[Exp[(-N[Power[M, 2.0], $MachinePrecision])], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(M * N[(M - n), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[Cos[(-M)], $MachinePrecision] * N[Exp[N[(t$95$2 - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[M, -27.0], t$95$1, If[LessEqual[M, -7.8e-64], N[(t$95$0 * N[Exp[N[(N[(m - n), $MachinePrecision] + N[(t$95$2 - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[M, 3.7e-100], t$95$3, If[LessEqual[M, 3e-25], N[(t$95$0 * N[Exp[N[(N[(m + t$95$2), $MachinePrecision] - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[M, 27.0], t$95$3, t$95$1]]]]]]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\
t_1 := \cos M \cdot e^{-{M}^{2}}\\
t_2 := M \cdot \left(M - n\right)\\
t_3 := \cos \left(-M\right) \cdot e^{t\_2 - \left(n + \ell\right)}\\
\mathbf{if}\;M \leq -27:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;M \leq -7.8 \cdot 10^{-64}:\\
\;\;\;\;t\_0 \cdot e^{\left(m - n\right) + \left(t\_2 - \ell\right)}\\

\mathbf{elif}\;M \leq 3.7 \cdot 10^{-100}:\\
\;\;\;\;t\_3\\

\mathbf{elif}\;M \leq 3 \cdot 10^{-25}:\\
\;\;\;\;t\_0 \cdot e^{\left(m + t\_2\right) - n}\\

\mathbf{elif}\;M \leq 27:\\
\;\;\;\;t\_3\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if M < -27 or 27 < 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. Add Preprocessing
    3. Taylor expanded in K around 0 97.4%

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

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

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

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

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

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

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

    if -27 < M < -7.7999999999999994e-64

    1. Initial program 84.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity84.6%

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

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

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

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

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

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

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

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

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

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

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

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

    if -7.7999999999999994e-64 < M < 3.70000000000000018e-100 or 2.9999999999999998e-25 < M < 27

    1. Initial program 66.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity66.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 3.70000000000000018e-100 < M < 2.9999999999999998e-25

    1. Initial program 81.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity81.1%

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

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

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

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

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

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

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

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot \left(\color{blue}{e^{\left(m + \left(0.5 \cdot m - M\right) \cdot \left(\left(n + 0.5 \cdot m\right) - M\right)\right) - n}} \cdot 1\right) \]
    9. Taylor expanded in m around 0 37.8%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot \left(e^{\left(m + \color{blue}{-1 \cdot \left(M \cdot \left(n - M\right)\right)}\right) - n} \cdot 1\right) \]
    10. Step-by-step derivation
      1. mul-1-neg37.8%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -27:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;M \leq -7.8 \cdot 10^{-64}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m - n\right) + \left(M \cdot \left(M - n\right) - \ell\right)}\\ \mathbf{elif}\;M \leq 3.7 \cdot 10^{-100}:\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{elif}\;M \leq 3 \cdot 10^{-25}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m + M \cdot \left(M - n\right)\right) - n}\\ \mathbf{elif}\;M \leq 27:\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 88.5% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := \left(m + n\right) \cdot 0.5\\ t_1 := \left|m - n\right|\\ \mathbf{if}\;n \leq 9.5 \cdot 10^{-102}:\\ \;\;\;\;\cos M \cdot e^{t\_1 + \left(t\_0 - M\right) \cdot \left(M - t\_0\right)}\\ \mathbf{elif}\;n \leq 128000:\\ \;\;\;\;\cos M \cdot e^{t\_1 + \left(M \cdot \left(m - M\right) - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* (+ m n) 0.5)) (t_1 (fabs (- m n))))
   (if (<= n 9.5e-102)
     (* (cos M) (exp (+ t_1 (* (- t_0 M) (- M t_0)))))
     (if (<= n 128000.0)
       (* (cos M) (exp (+ t_1 (- (* M (- m M)) l))))
       (* (cos M) (exp (* -0.25 (pow n 2.0))))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = (m + n) * 0.5;
	double t_1 = fabs((m - n));
	double tmp;
	if (n <= 9.5e-102) {
		tmp = cos(M) * exp((t_1 + ((t_0 - M) * (M - t_0))));
	} else if (n <= 128000.0) {
		tmp = cos(M) * exp((t_1 + ((M * (m - M)) - l)));
	} else {
		tmp = cos(M) * exp((-0.25 * pow(n, 2.0)));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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) :: tmp
    t_0 = (m + n) * 0.5d0
    t_1 = abs((m - n))
    if (n <= 9.5d-102) then
        tmp = cos(m_1) * exp((t_1 + ((t_0 - m_1) * (m_1 - t_0))))
    else if (n <= 128000.0d0) then
        tmp = cos(m_1) * exp((t_1 + ((m_1 * (m - m_1)) - l)))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n ** 2.0d0)))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = (m + n) * 0.5;
	double t_1 = Math.abs((m - n));
	double tmp;
	if (n <= 9.5e-102) {
		tmp = Math.cos(M) * Math.exp((t_1 + ((t_0 - M) * (M - t_0))));
	} else if (n <= 128000.0) {
		tmp = Math.cos(M) * Math.exp((t_1 + ((M * (m - M)) - l)));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(n, 2.0)));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = (m + n) * 0.5
	t_1 = math.fabs((m - n))
	tmp = 0
	if n <= 9.5e-102:
		tmp = math.cos(M) * math.exp((t_1 + ((t_0 - M) * (M - t_0))))
	elif n <= 128000.0:
		tmp = math.cos(M) * math.exp((t_1 + ((M * (m - M)) - l)))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(n, 2.0)))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(Float64(m + n) * 0.5)
	t_1 = abs(Float64(m - n))
	tmp = 0.0
	if (n <= 9.5e-102)
		tmp = Float64(cos(M) * exp(Float64(t_1 + Float64(Float64(t_0 - M) * Float64(M - t_0)))));
	elseif (n <= 128000.0)
		tmp = Float64(cos(M) * exp(Float64(t_1 + Float64(Float64(M * Float64(m - M)) - l))));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (n ^ 2.0))));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	t_0 = (m + n) * 0.5;
	t_1 = abs((m - n));
	tmp = 0.0;
	if (n <= 9.5e-102)
		tmp = cos(M) * exp((t_1 + ((t_0 - M) * (M - t_0))));
	elseif (n <= 128000.0)
		tmp = cos(M) * exp((t_1 + ((M * (m - M)) - l)));
	else
		tmp = cos(M) * exp((-0.25 * (n ^ 2.0)));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision]}, Block[{t$95$1 = N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[n, 9.5e-102], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$1 + N[(N[(t$95$0 - M), $MachinePrecision] * N[(M - t$95$0), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 128000.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$1 + N[(N[(M * N[(m - M), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \left(m + n\right) \cdot 0.5\\
t_1 := \left|m - n\right|\\
\mathbf{if}\;n \leq 9.5 \cdot 10^{-102}:\\
\;\;\;\;\cos M \cdot e^{t\_1 + \left(t\_0 - M\right) \cdot \left(M - t\_0\right)}\\

\mathbf{elif}\;n \leq 128000:\\
\;\;\;\;\cos M \cdot e^{t\_1 + \left(M \cdot \left(m - M\right) - \ell\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\


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

    1. Initial program 75.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. Add Preprocessing
    3. Taylor expanded in K around 0 92.8%

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

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

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

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

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

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

    if 9.50000000000000025e-102 < n < 128000

    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. Add Preprocessing
    3. Taylor expanded in K around 0 87.9%

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(0.5 \cdot n - M\right)} + m \cdot \left(0.5 \cdot n - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out87.9%

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

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

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

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

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

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

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

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

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

    if 128000 < n

    1. Initial program 58.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. Add Preprocessing
    3. Taylor expanded in K around 0 100.0%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 9.5 \cdot 10^{-102}:\\ \;\;\;\;\cos M \cdot e^{\left|m - n\right| + \left(\left(m + n\right) \cdot 0.5 - M\right) \cdot \left(M - \left(m + n\right) \cdot 0.5\right)}\\ \mathbf{elif}\;n \leq 128000:\\ \;\;\;\;\cos M \cdot e^{\left|m - n\right| + \left(M \cdot \left(m - M\right) - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 95.1% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;n \leq 1.12 \cdot 10^{+39}:\\ \;\;\;\;\cos M \cdot e^{\left(n + \left(m \cdot 0.5 - M\right)\right) \cdot \left(M - m \cdot 0.5\right) + \left(\left|m - n\right| - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (if (<= n 1.12e+39)
   (*
    (cos M)
    (exp (+ (* (+ n (- (* m 0.5) M)) (- M (* m 0.5))) (- (fabs (- m n)) l))))
   (* (cos M) (exp (* -0.25 (pow n 2.0))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 1.12e+39) {
		tmp = cos(M) * exp((((n + ((m * 0.5) - M)) * (M - (m * 0.5))) + (fabs((m - n)) - l)));
	} else {
		tmp = cos(M) * exp((-0.25 * pow(n, 2.0)));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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.12d+39) then
        tmp = cos(m_1) * exp((((n + ((m * 0.5d0) - m_1)) * (m_1 - (m * 0.5d0))) + (abs((m - n)) - l)))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n ** 2.0d0)))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 1.12e+39) {
		tmp = Math.cos(M) * Math.exp((((n + ((m * 0.5) - M)) * (M - (m * 0.5))) + (Math.abs((m - n)) - l)));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(n, 2.0)));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if n <= 1.12e+39:
		tmp = math.cos(M) * math.exp((((n + ((m * 0.5) - M)) * (M - (m * 0.5))) + (math.fabs((m - n)) - l)))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(n, 2.0)))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= 1.12e+39)
		tmp = Float64(cos(M) * exp(Float64(Float64(Float64(n + Float64(Float64(m * 0.5) - M)) * Float64(M - Float64(m * 0.5))) + Float64(abs(Float64(m - n)) - l))));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (n ^ 2.0))));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (n <= 1.12e+39)
		tmp = cos(M) * exp((((n + ((m * 0.5) - M)) * (M - (m * 0.5))) + (abs((m - n)) - l)));
	else
		tmp = cos(M) * exp((-0.25 * (n ^ 2.0)));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := If[LessEqual[n, 1.12e+39], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[(n + N[(N[(m * 0.5), $MachinePrecision] - M), $MachinePrecision]), $MachinePrecision] * N[(M - N[(m * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;n \leq 1.12 \cdot 10^{+39}:\\
\;\;\;\;\cos M \cdot e^{\left(n + \left(m \cdot 0.5 - M\right)\right) \cdot \left(M - m \cdot 0.5\right) + \left(\left|m - n\right| - \ell\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if n < 1.12e39

    1. Initial program 76.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. Add Preprocessing
    3. Taylor expanded in K around 0 92.3%

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

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

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

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

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

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

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

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

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

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

    if 1.12e39 < n

    1. Initial program 58.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. Add Preprocessing
    3. Taylor expanded in K around 0 100.0%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 1.12 \cdot 10^{+39}:\\ \;\;\;\;\cos M \cdot e^{\left(n + \left(m \cdot 0.5 - M\right)\right) \cdot \left(M - m \cdot 0.5\right) + \left(\left|m - n\right| - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 83.8% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;n \leq 1.1 \cdot 10^{-302}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{elif}\;n \leq 140000:\\ \;\;\;\;\cos M \cdot e^{\left|m - n\right| + \left(M \cdot \left(m - M\right) - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (if (<= n 1.1e-302)
   (* (cos M) (exp (* -0.25 (pow m 2.0))))
   (if (<= n 140000.0)
     (* (cos M) (exp (+ (fabs (- m n)) (- (* M (- m M)) l))))
     (* (cos M) (exp (* -0.25 (pow n 2.0)))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 1.1e-302) {
		tmp = cos(M) * exp((-0.25 * pow(m, 2.0)));
	} else if (n <= 140000.0) {
		tmp = cos(M) * exp((fabs((m - n)) + ((M * (m - M)) - l)));
	} else {
		tmp = cos(M) * exp((-0.25 * pow(n, 2.0)));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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.1d-302) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m ** 2.0d0)))
    else if (n <= 140000.0d0) then
        tmp = cos(m_1) * exp((abs((m - n)) + ((m_1 * (m - m_1)) - l)))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n ** 2.0d0)))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 1.1e-302) {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(m, 2.0)));
	} else if (n <= 140000.0) {
		tmp = Math.cos(M) * Math.exp((Math.abs((m - n)) + ((M * (m - M)) - l)));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(n, 2.0)));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if n <= 1.1e-302:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(m, 2.0)))
	elif n <= 140000.0:
		tmp = math.cos(M) * math.exp((math.fabs((m - n)) + ((M * (m - M)) - l)))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(n, 2.0)))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= 1.1e-302)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (m ^ 2.0))));
	elseif (n <= 140000.0)
		tmp = Float64(cos(M) * exp(Float64(abs(Float64(m - n)) + Float64(Float64(M * Float64(m - M)) - l))));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (n ^ 2.0))));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (n <= 1.1e-302)
		tmp = cos(M) * exp((-0.25 * (m ^ 2.0)));
	elseif (n <= 140000.0)
		tmp = cos(M) * exp((abs((m - n)) + ((M * (m - M)) - l)));
	else
		tmp = cos(M) * exp((-0.25 * (n ^ 2.0)));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := If[LessEqual[n, 1.1e-302], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 140000.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] + N[(N[(M * N[(m - M), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;n \leq 1.1 \cdot 10^{-302}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\

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

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\


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

    1. Initial program 72.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. Add Preprocessing
    3. Taylor expanded in K around 0 95.9%

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

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

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

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

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

    if 1.10000000000000004e-302 < n < 1.4e5

    1. Initial program 87.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. Add Preprocessing
    3. Taylor expanded in K around 0 84.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.4e5 < n

    1. Initial program 58.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. Add Preprocessing
    3. Taylor expanded in K around 0 100.0%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 1.1 \cdot 10^{-302}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{elif}\;n \leq 140000:\\ \;\;\;\;\cos M \cdot e^{\left|m - n\right| + \left(M \cdot \left(m - M\right) - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 81.2% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;n \leq -2.6 \cdot 10^{-104}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{elif}\;n \leq 1750000:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m - n\right) + \left(n \cdot \left(m \cdot 0.5 - M\right) - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (if (<= n -2.6e-104)
   (* (cos M) (exp (* -0.25 (pow m 2.0))))
   (if (<= n 1750000.0)
     (*
      (cos (- (/ (* K (+ m n)) 2.0) M))
      (exp (+ (- m n) (- (* n (- (* m 0.5) M)) l))))
     (* (cos M) (exp (* -0.25 (pow n 2.0)))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= -2.6e-104) {
		tmp = cos(M) * exp((-0.25 * pow(m, 2.0)));
	} else if (n <= 1750000.0) {
		tmp = cos((((K * (m + n)) / 2.0) - M)) * exp(((m - n) + ((n * ((m * 0.5) - M)) - l)));
	} else {
		tmp = cos(M) * exp((-0.25 * pow(n, 2.0)));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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 <= (-2.6d-104)) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m ** 2.0d0)))
    else if (n <= 1750000.0d0) then
        tmp = cos((((k * (m + n)) / 2.0d0) - m_1)) * exp(((m - n) + ((n * ((m * 0.5d0) - m_1)) - l)))
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n ** 2.0d0)))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= -2.6e-104) {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(m, 2.0)));
	} else if (n <= 1750000.0) {
		tmp = Math.cos((((K * (m + n)) / 2.0) - M)) * Math.exp(((m - n) + ((n * ((m * 0.5) - M)) - l)));
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(n, 2.0)));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if n <= -2.6e-104:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(m, 2.0)))
	elif n <= 1750000.0:
		tmp = math.cos((((K * (m + n)) / 2.0) - M)) * math.exp(((m - n) + ((n * ((m * 0.5) - M)) - l)))
	else:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(n, 2.0)))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= -2.6e-104)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (m ^ 2.0))));
	elseif (n <= 1750000.0)
		tmp = Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * exp(Float64(Float64(m - n) + Float64(Float64(n * Float64(Float64(m * 0.5) - M)) - l))));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (n ^ 2.0))));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (n <= -2.6e-104)
		tmp = cos(M) * exp((-0.25 * (m ^ 2.0)));
	elseif (n <= 1750000.0)
		tmp = cos((((K * (m + n)) / 2.0) - M)) * exp(((m - n) + ((n * ((m * 0.5) - M)) - l)));
	else
		tmp = cos(M) * exp((-0.25 * (n ^ 2.0)));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := If[LessEqual[n, -2.6e-104], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[n, 1750000.0], N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(m - n), $MachinePrecision] + N[(N[(n * N[(N[(m * 0.5), $MachinePrecision] - M), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;n \leq -2.6 \cdot 10^{-104}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\

\mathbf{elif}\;n \leq 1750000:\\
\;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m - n\right) + \left(n \cdot \left(m \cdot 0.5 - M\right) - \ell\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\


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

    1. Initial program 68.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. Add Preprocessing
    3. Taylor expanded in K around 0 98.9%

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

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

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

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

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

    if -2.60000000000000003e-104 < n < 1.75e6

    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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity84.3%

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

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

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

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

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

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

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

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

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

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

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

    if 1.75e6 < n

    1. Initial program 58.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. Add Preprocessing
    3. Taylor expanded in K around 0 100.0%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -2.6 \cdot 10^{-104}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{elif}\;n \leq 1750000:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m - n\right) + \left(n \cdot \left(m \cdot 0.5 - M\right) - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 60.0% accurate, 1.8× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := K \cdot \left(m + n\right)\\ \mathbf{if}\;n \leq 9.5 \cdot 10^{+118}:\\ \;\;\;\;\cos \left(\frac{t\_0}{2} - M\right) \cdot e^{\left(m - n\right) + \left(n \cdot \left(m \cdot 0.5 - M\right) - \ell\right)}\\ \mathbf{elif}\;n \leq 4.8 \cdot 10^{+207} \lor \neg \left(n \leq 1.02 \cdot 10^{+219}\right):\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(t\_0 \cdot 0.5\right) \cdot e^{\left(m + 0.5 \cdot \left(m \cdot \left(n + m \cdot 0.5\right)\right)\right) - n}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* K (+ m n))))
   (if (<= n 9.5e+118)
     (* (cos (- (/ t_0 2.0) M)) (exp (+ (- m n) (- (* n (- (* m 0.5) M)) l))))
     (if (or (<= n 4.8e+207) (not (<= n 1.02e+219)))
       (* (cos (- M)) (exp (- (* M (- M n)) (+ n l))))
       (*
        (cos (* t_0 0.5))
        (exp (- (+ m (* 0.5 (* m (+ n (* m 0.5))))) n)))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = K * (m + n);
	double tmp;
	if (n <= 9.5e+118) {
		tmp = cos(((t_0 / 2.0) - M)) * exp(((m - n) + ((n * ((m * 0.5) - M)) - l)));
	} else if ((n <= 4.8e+207) || !(n <= 1.02e+219)) {
		tmp = cos(-M) * exp(((M * (M - n)) - (n + l)));
	} else {
		tmp = cos((t_0 * 0.5)) * exp(((m + (0.5 * (m * (n + (m * 0.5))))) - n));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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 = k * (m + n)
    if (n <= 9.5d+118) then
        tmp = cos(((t_0 / 2.0d0) - m_1)) * exp(((m - n) + ((n * ((m * 0.5d0) - m_1)) - l)))
    else if ((n <= 4.8d+207) .or. (.not. (n <= 1.02d+219))) then
        tmp = cos(-m_1) * exp(((m_1 * (m_1 - n)) - (n + l)))
    else
        tmp = cos((t_0 * 0.5d0)) * exp(((m + (0.5d0 * (m * (n + (m * 0.5d0))))) - n))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = K * (m + n);
	double tmp;
	if (n <= 9.5e+118) {
		tmp = Math.cos(((t_0 / 2.0) - M)) * Math.exp(((m - n) + ((n * ((m * 0.5) - M)) - l)));
	} else if ((n <= 4.8e+207) || !(n <= 1.02e+219)) {
		tmp = Math.cos(-M) * Math.exp(((M * (M - n)) - (n + l)));
	} else {
		tmp = Math.cos((t_0 * 0.5)) * Math.exp(((m + (0.5 * (m * (n + (m * 0.5))))) - n));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = K * (m + n)
	tmp = 0
	if n <= 9.5e+118:
		tmp = math.cos(((t_0 / 2.0) - M)) * math.exp(((m - n) + ((n * ((m * 0.5) - M)) - l)))
	elif (n <= 4.8e+207) or not (n <= 1.02e+219):
		tmp = math.cos(-M) * math.exp(((M * (M - n)) - (n + l)))
	else:
		tmp = math.cos((t_0 * 0.5)) * math.exp(((m + (0.5 * (m * (n + (m * 0.5))))) - n))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(K * Float64(m + n))
	tmp = 0.0
	if (n <= 9.5e+118)
		tmp = Float64(cos(Float64(Float64(t_0 / 2.0) - M)) * exp(Float64(Float64(m - n) + Float64(Float64(n * Float64(Float64(m * 0.5) - M)) - l))));
	elseif ((n <= 4.8e+207) || !(n <= 1.02e+219))
		tmp = Float64(cos(Float64(-M)) * exp(Float64(Float64(M * Float64(M - n)) - Float64(n + l))));
	else
		tmp = Float64(cos(Float64(t_0 * 0.5)) * exp(Float64(Float64(m + Float64(0.5 * Float64(m * Float64(n + Float64(m * 0.5))))) - n)));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	t_0 = K * (m + n);
	tmp = 0.0;
	if (n <= 9.5e+118)
		tmp = cos(((t_0 / 2.0) - M)) * exp(((m - n) + ((n * ((m * 0.5) - M)) - l)));
	elseif ((n <= 4.8e+207) || ~((n <= 1.02e+219)))
		tmp = cos(-M) * exp(((M * (M - n)) - (n + l)));
	else
		tmp = cos((t_0 * 0.5)) * exp(((m + (0.5 * (m * (n + (m * 0.5))))) - n));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision]}, If[LessEqual[n, 9.5e+118], N[(N[Cos[N[(N[(t$95$0 / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(m - n), $MachinePrecision] + N[(N[(n * N[(N[(m * 0.5), $MachinePrecision] - M), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[n, 4.8e+207], N[Not[LessEqual[n, 1.02e+219]], $MachinePrecision]], N[(N[Cos[(-M)], $MachinePrecision] * N[Exp[N[(N[(M * N[(M - n), $MachinePrecision]), $MachinePrecision] - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[N[(t$95$0 * 0.5), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(m + N[(0.5 * N[(m * N[(n + N[(m * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := K \cdot \left(m + n\right)\\
\mathbf{if}\;n \leq 9.5 \cdot 10^{+118}:\\
\;\;\;\;\cos \left(\frac{t\_0}{2} - M\right) \cdot e^{\left(m - n\right) + \left(n \cdot \left(m \cdot 0.5 - M\right) - \ell\right)}\\

\mathbf{elif}\;n \leq 4.8 \cdot 10^{+207} \lor \neg \left(n \leq 1.02 \cdot 10^{+219}\right):\\
\;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\

\mathbf{else}:\\
\;\;\;\;\cos \left(t\_0 \cdot 0.5\right) \cdot e^{\left(m + 0.5 \cdot \left(m \cdot \left(n + m \cdot 0.5\right)\right)\right) - n}\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if n < 9.49999999999999974e118

    1. Initial program 77.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity77.2%

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

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

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

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

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

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

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

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

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

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

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

    if 9.49999999999999974e118 < n < 4.8000000000000002e207 or 1.02e219 < n

    1. Initial program 41.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity41.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 4.8000000000000002e207 < n < 1.02e219

    1. Initial program 100.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity100.0%

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right) \cdot e^{\left(m + 0.5 \cdot \left(m \cdot \left(n + 0.5 \cdot m\right)\right)\right) - n}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification53.8%

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 9.5 \cdot 10^{+118}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m - n\right) + \left(n \cdot \left(m \cdot 0.5 - M\right) - \ell\right)}\\ \mathbf{elif}\;n \leq 4.8 \cdot 10^{+207} \lor \neg \left(n \leq 1.02 \cdot 10^{+219}\right):\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\left(K \cdot \left(m + n\right)\right) \cdot 0.5\right) \cdot e^{\left(m + 0.5 \cdot \left(m \cdot \left(n + m \cdot 0.5\right)\right)\right) - n}\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 53.0% accurate, 1.8× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\ t_1 := t\_0 \cdot e^{\left(-n\right) - \ell}\\ t_2 := M \cdot \left(M - n\right)\\ t_3 := \cos \left(-M\right) \cdot e^{t\_2 - \left(n + \ell\right)}\\ \mathbf{if}\;K \leq -6.2 \cdot 10^{+88}:\\ \;\;\;\;t\_3\\ \mathbf{elif}\;K \leq 8 \cdot 10^{-248}:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;K \leq 1.22 \cdot 10^{-144}:\\ \;\;\;\;t\_0 \cdot e^{\left(m + t\_2\right) - n}\\ \mathbf{elif}\;K \leq 3.5 \cdot 10^{+127}:\\ \;\;\;\;t\_1\\ \mathbf{else}:\\ \;\;\;\;t\_3\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (cos (- (/ (* K (+ m n)) 2.0) M)))
        (t_1 (* t_0 (exp (- (- n) l))))
        (t_2 (* M (- M n)))
        (t_3 (* (cos (- M)) (exp (- t_2 (+ n l))))))
   (if (<= K -6.2e+88)
     t_3
     (if (<= K 8e-248)
       t_1
       (if (<= K 1.22e-144)
         (* t_0 (exp (- (+ m t_2) n)))
         (if (<= K 3.5e+127) t_1 t_3))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = cos((((K * (m + n)) / 2.0) - M));
	double t_1 = t_0 * exp((-n - l));
	double t_2 = M * (M - n);
	double t_3 = cos(-M) * exp((t_2 - (n + l)));
	double tmp;
	if (K <= -6.2e+88) {
		tmp = t_3;
	} else if (K <= 8e-248) {
		tmp = t_1;
	} else if (K <= 1.22e-144) {
		tmp = t_0 * exp(((m + t_2) - n));
	} else if (K <= 3.5e+127) {
		tmp = t_1;
	} else {
		tmp = t_3;
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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) :: t_3
    real(8) :: tmp
    t_0 = cos((((k * (m + n)) / 2.0d0) - m_1))
    t_1 = t_0 * exp((-n - l))
    t_2 = m_1 * (m_1 - n)
    t_3 = cos(-m_1) * exp((t_2 - (n + l)))
    if (k <= (-6.2d+88)) then
        tmp = t_3
    else if (k <= 8d-248) then
        tmp = t_1
    else if (k <= 1.22d-144) then
        tmp = t_0 * exp(((m + t_2) - n))
    else if (k <= 3.5d+127) then
        tmp = t_1
    else
        tmp = t_3
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.cos((((K * (m + n)) / 2.0) - M));
	double t_1 = t_0 * Math.exp((-n - l));
	double t_2 = M * (M - n);
	double t_3 = Math.cos(-M) * Math.exp((t_2 - (n + l)));
	double tmp;
	if (K <= -6.2e+88) {
		tmp = t_3;
	} else if (K <= 8e-248) {
		tmp = t_1;
	} else if (K <= 1.22e-144) {
		tmp = t_0 * Math.exp(((m + t_2) - n));
	} else if (K <= 3.5e+127) {
		tmp = t_1;
	} else {
		tmp = t_3;
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.cos((((K * (m + n)) / 2.0) - M))
	t_1 = t_0 * math.exp((-n - l))
	t_2 = M * (M - n)
	t_3 = math.cos(-M) * math.exp((t_2 - (n + l)))
	tmp = 0
	if K <= -6.2e+88:
		tmp = t_3
	elif K <= 8e-248:
		tmp = t_1
	elif K <= 1.22e-144:
		tmp = t_0 * math.exp(((m + t_2) - n))
	elif K <= 3.5e+127:
		tmp = t_1
	else:
		tmp = t_3
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M))
	t_1 = Float64(t_0 * exp(Float64(Float64(-n) - l)))
	t_2 = Float64(M * Float64(M - n))
	t_3 = Float64(cos(Float64(-M)) * exp(Float64(t_2 - Float64(n + l))))
	tmp = 0.0
	if (K <= -6.2e+88)
		tmp = t_3;
	elseif (K <= 8e-248)
		tmp = t_1;
	elseif (K <= 1.22e-144)
		tmp = Float64(t_0 * exp(Float64(Float64(m + t_2) - n)));
	elseif (K <= 3.5e+127)
		tmp = t_1;
	else
		tmp = t_3;
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	t_0 = cos((((K * (m + n)) / 2.0) - M));
	t_1 = t_0 * exp((-n - l));
	t_2 = M * (M - n);
	t_3 = cos(-M) * exp((t_2 - (n + l)));
	tmp = 0.0;
	if (K <= -6.2e+88)
		tmp = t_3;
	elseif (K <= 8e-248)
		tmp = t_1;
	elseif (K <= 1.22e-144)
		tmp = t_0 * exp(((m + t_2) - n));
	elseif (K <= 3.5e+127)
		tmp = t_1;
	else
		tmp = t_3;
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$1 = N[(t$95$0 * N[Exp[N[((-n) - l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[(M * N[(M - n), $MachinePrecision]), $MachinePrecision]}, Block[{t$95$3 = N[(N[Cos[(-M)], $MachinePrecision] * N[Exp[N[(t$95$2 - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[K, -6.2e+88], t$95$3, If[LessEqual[K, 8e-248], t$95$1, If[LessEqual[K, 1.22e-144], N[(t$95$0 * N[Exp[N[(N[(m + t$95$2), $MachinePrecision] - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[K, 3.5e+127], t$95$1, t$95$3]]]]]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\
t_1 := t\_0 \cdot e^{\left(-n\right) - \ell}\\
t_2 := M \cdot \left(M - n\right)\\
t_3 := \cos \left(-M\right) \cdot e^{t\_2 - \left(n + \ell\right)}\\
\mathbf{if}\;K \leq -6.2 \cdot 10^{+88}:\\
\;\;\;\;t\_3\\

\mathbf{elif}\;K \leq 8 \cdot 10^{-248}:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;K \leq 1.22 \cdot 10^{-144}:\\
\;\;\;\;t\_0 \cdot e^{\left(m + t\_2\right) - n}\\

\mathbf{elif}\;K \leq 3.5 \cdot 10^{+127}:\\
\;\;\;\;t\_1\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if K < -6.2000000000000003e88 or 3.49999999999999978e127 < K

    1. Initial program 30.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity30.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -6.2000000000000003e88 < K < 7.99999999999999984e-248 or 1.22e-144 < K < 3.49999999999999978e127

    1. Initial program 95.8%

      \[\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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity95.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 7.99999999999999984e-248 < K < 1.22e-144

    1. Initial program 100.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity100.0%

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

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

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

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

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

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

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

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot \left(\color{blue}{e^{\left(m + \left(0.5 \cdot m - M\right) \cdot \left(\left(n + 0.5 \cdot m\right) - M\right)\right) - n}} \cdot 1\right) \]
    9. Taylor expanded in m around 0 65.5%

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot \left(e^{\left(m + \color{blue}{\left(-M \cdot \left(n - M\right)\right)}\right) - n} \cdot 1\right) \]
      2. distribute-lft-neg-out65.5%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot \left(e^{\left(m + \color{blue}{\left(-M\right) \cdot \left(n - M\right)}\right) - n} \cdot 1\right) \]
    11. Simplified65.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;K \leq -6.2 \cdot 10^{+88}:\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{elif}\;K \leq 8 \cdot 10^{-248}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-n\right) - \ell}\\ \mathbf{elif}\;K \leq 1.22 \cdot 10^{-144}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m + M \cdot \left(M - n\right)\right) - n}\\ \mathbf{elif}\;K \leq 3.5 \cdot 10^{+127}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-n\right) - \ell}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 10: 58.2% accurate, 1.9× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\ \mathbf{if}\;m \leq -1.9 \cdot 10^{+34}:\\ \;\;\;\;t\_0 \cdot e^{\left(m + n \cdot \left(m \cdot 0.5 - M\right)\right) - n}\\ \mathbf{elif}\;m \leq 8 \cdot 10^{-8}:\\ \;\;\;\;t\_0 \cdot e^{\left(-n\right) - \ell}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (cos (- (/ (* K (+ m n)) 2.0) M))))
   (if (<= m -1.9e+34)
     (* t_0 (exp (- (+ m (* n (- (* m 0.5) M))) n)))
     (if (<= m 8e-8)
       (* t_0 (exp (- (- n) l)))
       (* (cos (- M)) (exp (- (* M (- M n)) (+ n l))))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = cos((((K * (m + n)) / 2.0) - M));
	double tmp;
	if (m <= -1.9e+34) {
		tmp = t_0 * exp(((m + (n * ((m * 0.5) - M))) - n));
	} else if (m <= 8e-8) {
		tmp = t_0 * exp((-n - l));
	} else {
		tmp = cos(-M) * exp(((M * (M - n)) - (n + l)));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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 = cos((((k * (m + n)) / 2.0d0) - m_1))
    if (m <= (-1.9d+34)) then
        tmp = t_0 * exp(((m + (n * ((m * 0.5d0) - m_1))) - n))
    else if (m <= 8d-8) then
        tmp = t_0 * exp((-n - l))
    else
        tmp = cos(-m_1) * exp(((m_1 * (m_1 - n)) - (n + l)))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.cos((((K * (m + n)) / 2.0) - M));
	double tmp;
	if (m <= -1.9e+34) {
		tmp = t_0 * Math.exp(((m + (n * ((m * 0.5) - M))) - n));
	} else if (m <= 8e-8) {
		tmp = t_0 * Math.exp((-n - l));
	} else {
		tmp = Math.cos(-M) * Math.exp(((M * (M - n)) - (n + l)));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.cos((((K * (m + n)) / 2.0) - M))
	tmp = 0
	if m <= -1.9e+34:
		tmp = t_0 * math.exp(((m + (n * ((m * 0.5) - M))) - n))
	elif m <= 8e-8:
		tmp = t_0 * math.exp((-n - l))
	else:
		tmp = math.cos(-M) * math.exp(((M * (M - n)) - (n + l)))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M))
	tmp = 0.0
	if (m <= -1.9e+34)
		tmp = Float64(t_0 * exp(Float64(Float64(m + Float64(n * Float64(Float64(m * 0.5) - M))) - n)));
	elseif (m <= 8e-8)
		tmp = Float64(t_0 * exp(Float64(Float64(-n) - l)));
	else
		tmp = Float64(cos(Float64(-M)) * exp(Float64(Float64(M * Float64(M - n)) - Float64(n + l))));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	t_0 = cos((((K * (m + n)) / 2.0) - M));
	tmp = 0.0;
	if (m <= -1.9e+34)
		tmp = t_0 * exp(((m + (n * ((m * 0.5) - M))) - n));
	elseif (m <= 8e-8)
		tmp = t_0 * exp((-n - l));
	else
		tmp = cos(-M) * exp(((M * (M - n)) - (n + l)));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[m, -1.9e+34], N[(t$95$0 * N[Exp[N[(N[(m + N[(n * N[(N[(m * 0.5), $MachinePrecision] - M), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, 8e-8], N[(t$95$0 * N[Exp[N[((-n) - l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[(-M)], $MachinePrecision] * N[Exp[N[(N[(M * N[(M - n), $MachinePrecision]), $MachinePrecision] - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right)\\
\mathbf{if}\;m \leq -1.9 \cdot 10^{+34}:\\
\;\;\;\;t\_0 \cdot e^{\left(m + n \cdot \left(m \cdot 0.5 - M\right)\right) - n}\\

\mathbf{elif}\;m \leq 8 \cdot 10^{-8}:\\
\;\;\;\;t\_0 \cdot e^{\left(-n\right) - \ell}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if m < -1.9000000000000001e34

    1. Initial program 66.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity66.1%

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

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

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

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

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

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

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

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot \left(\color{blue}{e^{\left(m + \left(0.5 \cdot m - M\right) \cdot \left(\left(n + 0.5 \cdot m\right) - M\right)\right) - n}} \cdot 1\right) \]
    9. Taylor expanded in n around inf 48.5%

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot \left(e^{\left(m + n \cdot \left(\color{blue}{m \cdot 0.5} - M\right)\right) - n} \cdot 1\right) \]
    11. Simplified48.5%

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

    if -1.9000000000000001e34 < m < 8.0000000000000002e-8

    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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity73.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 8.0000000000000002e-8 < m

    1. Initial program 74.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity74.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -1.9 \cdot 10^{+34}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m + n \cdot \left(m \cdot 0.5 - M\right)\right) - n}\\ \mathbf{elif}\;m \leq 8 \cdot 10^{-8}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-n\right) - \ell}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 11: 54.2% accurate, 1.9× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;K \leq -1.26 \cdot 10^{+89} \lor \neg \left(K \leq 1.6 \cdot 10^{+127}\right):\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-n\right) - \ell}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= K -1.26e+89) (not (<= K 1.6e+127)))
   (* (cos (- M)) (exp (- (* M (- M n)) (+ n l))))
   (* (cos (- (/ (* K (+ m n)) 2.0) M)) (exp (- (- n) l)))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((K <= -1.26e+89) || !(K <= 1.6e+127)) {
		tmp = cos(-M) * exp(((M * (M - n)) - (n + l)));
	} else {
		tmp = cos((((K * (m + n)) / 2.0) - M)) * exp((-n - l));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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 ((k <= (-1.26d+89)) .or. (.not. (k <= 1.6d+127))) then
        tmp = cos(-m_1) * exp(((m_1 * (m_1 - n)) - (n + l)))
    else
        tmp = cos((((k * (m + n)) / 2.0d0) - m_1)) * exp((-n - l))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((K <= -1.26e+89) || !(K <= 1.6e+127)) {
		tmp = Math.cos(-M) * Math.exp(((M * (M - n)) - (n + l)));
	} else {
		tmp = Math.cos((((K * (m + n)) / 2.0) - M)) * Math.exp((-n - l));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if (K <= -1.26e+89) or not (K <= 1.6e+127):
		tmp = math.cos(-M) * math.exp(((M * (M - n)) - (n + l)))
	else:
		tmp = math.cos((((K * (m + n)) / 2.0) - M)) * math.exp((-n - l))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if ((K <= -1.26e+89) || !(K <= 1.6e+127))
		tmp = Float64(cos(Float64(-M)) * exp(Float64(Float64(M * Float64(M - n)) - Float64(n + l))));
	else
		tmp = Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * exp(Float64(Float64(-n) - l)));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if ((K <= -1.26e+89) || ~((K <= 1.6e+127)))
		tmp = cos(-M) * exp(((M * (M - n)) - (n + l)));
	else
		tmp = cos((((K * (m + n)) / 2.0) - M)) * exp((-n - l));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := If[Or[LessEqual[K, -1.26e+89], N[Not[LessEqual[K, 1.6e+127]], $MachinePrecision]], N[(N[Cos[(-M)], $MachinePrecision] * N[Exp[N[(N[(M * N[(M - n), $MachinePrecision]), $MachinePrecision] - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[((-n) - l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;K \leq -1.26 \cdot 10^{+89} \lor \neg \left(K \leq 1.6 \cdot 10^{+127}\right):\\
\;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if K < -1.26e89 or 1.59999999999999988e127 < K

    1. Initial program 30.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity30.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -1.26e89 < K < 1.59999999999999988e127

    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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity96.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;K \leq -1.26 \cdot 10^{+89} \lor \neg \left(K \leq 1.6 \cdot 10^{+127}\right):\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-n\right) - \ell}\\ \end{array} \]
  5. Add Preprocessing

Alternative 12: 53.9% accurate, 1.9× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;K \leq -5 \cdot 10^{+88} \lor \neg \left(K \leq 2.6 \cdot 10^{+127}\right):\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(-n\right) - \ell} \cdot \cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right)\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= K -5e+88) (not (<= K 2.6e+127)))
   (* (cos (- M)) (exp (- (* M (- M n)) (+ n l))))
   (* (exp (- (- n) l)) (cos (* (+ m n) (* K 0.5))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((K <= -5e+88) || !(K <= 2.6e+127)) {
		tmp = cos(-M) * exp(((M * (M - n)) - (n + l)));
	} else {
		tmp = exp((-n - l)) * cos(((m + n) * (K * 0.5)));
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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 ((k <= (-5d+88)) .or. (.not. (k <= 2.6d+127))) then
        tmp = cos(-m_1) * exp(((m_1 * (m_1 - n)) - (n + l)))
    else
        tmp = exp((-n - l)) * cos(((m + n) * (k * 0.5d0)))
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((K <= -5e+88) || !(K <= 2.6e+127)) {
		tmp = Math.cos(-M) * Math.exp(((M * (M - n)) - (n + l)));
	} else {
		tmp = Math.exp((-n - l)) * Math.cos(((m + n) * (K * 0.5)));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if (K <= -5e+88) or not (K <= 2.6e+127):
		tmp = math.cos(-M) * math.exp(((M * (M - n)) - (n + l)))
	else:
		tmp = math.exp((-n - l)) * math.cos(((m + n) * (K * 0.5)))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if ((K <= -5e+88) || !(K <= 2.6e+127))
		tmp = Float64(cos(Float64(-M)) * exp(Float64(Float64(M * Float64(M - n)) - Float64(n + l))));
	else
		tmp = Float64(exp(Float64(Float64(-n) - l)) * cos(Float64(Float64(m + n) * Float64(K * 0.5))));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if ((K <= -5e+88) || ~((K <= 2.6e+127)))
		tmp = cos(-M) * exp(((M * (M - n)) - (n + l)));
	else
		tmp = exp((-n - l)) * cos(((m + n) * (K * 0.5)));
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := If[Or[LessEqual[K, -5e+88], N[Not[LessEqual[K, 2.6e+127]], $MachinePrecision]], N[(N[Cos[(-M)], $MachinePrecision] * N[Exp[N[(N[(M * N[(M - n), $MachinePrecision]), $MachinePrecision] - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Exp[N[((-n) - l), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(m + n), $MachinePrecision] * N[(K * 0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;K \leq -5 \cdot 10^{+88} \lor \neg \left(K \leq 2.6 \cdot 10^{+127}\right):\\
\;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if K < -4.99999999999999997e88 or 2.6000000000000002e127 < K

    1. Initial program 30.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity30.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -4.99999999999999997e88 < K < 2.6000000000000002e127

    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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity96.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right) \cdot e^{-\left(\ell + n\right)}} \]
    12. Step-by-step derivation
      1. *-commutative48.5%

        \[\leadsto \color{blue}{e^{-\left(\ell + n\right)} \cdot \cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right)} \]
      2. +-commutative48.5%

        \[\leadsto e^{-\color{blue}{\left(n + \ell\right)}} \cdot \cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right) \]
      3. distribute-neg-in48.5%

        \[\leadsto e^{\color{blue}{\left(-n\right) + \left(-\ell\right)}} \cdot \cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right) \]
      4. unsub-neg48.5%

        \[\leadsto e^{\color{blue}{\left(-n\right) - \ell}} \cdot \cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right) \]
      5. associate-*r*48.5%

        \[\leadsto e^{\left(-n\right) - \ell} \cdot \cos \color{blue}{\left(\left(0.5 \cdot K\right) \cdot \left(m + n\right)\right)} \]
    13. Simplified48.5%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;K \leq -5 \cdot 10^{+88} \lor \neg \left(K \leq 2.6 \cdot 10^{+127}\right):\\ \;\;\;\;\cos \left(-M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{\left(-n\right) - \ell} \cdot \cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 13: 47.9% accurate, 2.0× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ e^{\left(-n\right) - \ell} \cdot \cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (* (exp (- (- n) l)) (cos (* (+ m n) (* K 0.5)))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	return exp((-n - l)) * cos(((m + n) * (K * 0.5)));
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
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((-n - l)) * cos(((m + n) * (k * 0.5d0)))
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	return Math.exp((-n - l)) * Math.cos(((m + n) * (K * 0.5)));
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.exp((-n - l)) * math.cos(((m + n) * (K * 0.5)))
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return Float64(exp(Float64(Float64(-n) - l)) * cos(Float64(Float64(m + n) * Float64(K * 0.5))))
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	tmp = exp((-n - l)) * cos(((m + n) * (K * 0.5)));
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := N[(N[Exp[N[((-n) - l), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(m + n), $MachinePrecision] * N[(K * 0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
e^{\left(-n\right) - \ell} \cdot \cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right)
\end{array}
Derivation
  1. Initial program 72.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. Add Preprocessing
  3. Step-by-step derivation
    1. *-un-lft-identity72.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right) \cdot e^{-\left(\ell + n\right)}} \]
  12. Step-by-step derivation
    1. *-commutative37.2%

      \[\leadsto \color{blue}{e^{-\left(\ell + n\right)} \cdot \cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right)} \]
    2. +-commutative37.2%

      \[\leadsto e^{-\color{blue}{\left(n + \ell\right)}} \cdot \cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right) \]
    3. distribute-neg-in37.2%

      \[\leadsto e^{\color{blue}{\left(-n\right) + \left(-\ell\right)}} \cdot \cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right) \]
    4. unsub-neg37.2%

      \[\leadsto e^{\color{blue}{\left(-n\right) - \ell}} \cdot \cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right)\right) \]
    5. associate-*r*37.2%

      \[\leadsto e^{\left(-n\right) - \ell} \cdot \cos \color{blue}{\left(\left(0.5 \cdot K\right) \cdot \left(m + n\right)\right)} \]
  13. Simplified37.2%

    \[\leadsto \color{blue}{e^{\left(-n\right) - \ell} \cdot \cos \left(\left(0.5 \cdot K\right) \cdot \left(m + n\right)\right)} \]
  14. Final simplification37.2%

    \[\leadsto e^{\left(-n\right) - \ell} \cdot \cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \]
  15. Add Preprocessing

Reproduce

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