Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.3% → 95.8%
Time: 17.3s
Alternatives: 8
Speedup: 2.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 8 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: 76.3% 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: 95.8% accurate, 1.9× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;m \leq -8 \cdot 10^{+15}:\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left(\left(n - m\right) + \left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right)\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 (<= m -8e+15)
   (exp (* (pow m 2.0) -0.25))
   (*
    (cos M)
    (exp (- (+ (- n m) (* (- (* n 0.5) M) (- (- M (* n 0.5)) m))) 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 (m <= -8e+15) {
		tmp = exp((pow(m, 2.0) * -0.25));
	} else {
		tmp = cos(M) * exp((((n - m) + (((n * 0.5) - M) * ((M - (n * 0.5)) - m))) - 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 (m <= (-8d+15)) then
        tmp = exp(((m ** 2.0d0) * (-0.25d0)))
    else
        tmp = cos(m_1) * exp((((n - m) + (((n * 0.5d0) - m_1) * ((m_1 - (n * 0.5d0)) - m))) - 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 (m <= -8e+15) {
		tmp = Math.exp((Math.pow(m, 2.0) * -0.25));
	} else {
		tmp = Math.cos(M) * Math.exp((((n - m) + (((n * 0.5) - M) * ((M - (n * 0.5)) - m))) - l));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if m <= -8e+15:
		tmp = math.exp((math.pow(m, 2.0) * -0.25))
	else:
		tmp = math.cos(M) * math.exp((((n - m) + (((n * 0.5) - M) * ((M - (n * 0.5)) - m))) - 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 (m <= -8e+15)
		tmp = exp(Float64((m ^ 2.0) * -0.25));
	else
		tmp = Float64(cos(M) * exp(Float64(Float64(Float64(n - m) + Float64(Float64(Float64(n * 0.5) - M) * Float64(Float64(M - Float64(n * 0.5)) - m))) - 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 (m <= -8e+15)
		tmp = exp(((m ^ 2.0) * -0.25));
	else
		tmp = cos(M) * exp((((n - m) + (((n * 0.5) - M) * ((M - (n * 0.5)) - m))) - 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[LessEqual[m, -8e+15], N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[(n - m), $MachinePrecision] + N[(N[(N[(n * 0.5), $MachinePrecision] - M), $MachinePrecision] * N[(N[(M - N[(n * 0.5), $MachinePrecision]), $MachinePrecision] - m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;m \leq -8 \cdot 10^{+15}:\\
\;\;\;\;e^{{m}^{2} \cdot -0.25}\\

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


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

    1. Initial program 71.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. Simplified73.6%

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

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

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

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

      \[\leadsto \cos M \cdot e^{-\color{blue}{0.25 \cdot {m}^{2}}} \]
    7. Taylor expanded in M around 0 98.1%

      \[\leadsto \color{blue}{e^{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. distribute-lft-neg-in98.1%

        \[\leadsto e^{\color{blue}{\left(-0.25\right) \cdot {m}^{2}}} \]
      2. metadata-eval98.1%

        \[\leadsto e^{\color{blue}{-0.25} \cdot {m}^{2}} \]
      3. *-commutative98.1%

        \[\leadsto e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
    9. Simplified98.1%

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

    if -8e15 < m

    1. Initial program 77.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. Simplified77.8%

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, \color{blue}{0.5}, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left|n - m\right|\right)}\right) \]
      6. add-sqr-sqrt20.2%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right|\right)}\right) \]
      7. fabs-sqr20.2%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right)}\right) \]
      8. add-sqr-sqrt70.2%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \color{blue}{\left(n - m\right)}\right)}\right) \]
    7. Applied egg-rr70.2%

      \[\leadsto \cos M \cdot \color{blue}{\left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left(n - m\right)\right)}\right)} \]
    8. Step-by-step derivation
      1. prod-exp95.8%

        \[\leadsto \cos M \cdot \color{blue}{e^{\left(-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right) + \left(-\left(\ell - \left(n - m\right)\right)\right)}} \]
      2. distribute-neg-in95.8%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\left({\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(\ell - \left(n - m\right)\right)\right)}} \]
      3. +-commutative95.8%

        \[\leadsto \cos M \cdot e^{-\color{blue}{\left(\left(\ell - \left(n - m\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)}} \]
      4. sub-neg95.8%

        \[\leadsto \cos M \cdot e^{-\left(\color{blue}{\left(\ell + \left(-\left(n - m\right)\right)\right)} + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      5. sub-neg95.8%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\color{blue}{\left(n + \left(-m\right)\right)}\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      6. mul-1-neg95.8%

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

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\color{blue}{\left(-1 \cdot m + n\right)}\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      8. distribute-neg-in95.8%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \color{blue}{\left(\left(--1 \cdot m\right) + \left(-n\right)\right)}\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      9. mul-1-neg95.8%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(\left(-\color{blue}{\left(-m\right)}\right) + \left(-n\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      10. remove-double-neg95.8%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(\color{blue}{m} + \left(-n\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      11. sub-neg95.8%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \color{blue}{\left(m - n\right)}\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      12. associate-+l+95.8%

        \[\leadsto \cos M \cdot e^{-\color{blue}{\left(\ell + \left(\left(m - n\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)}} \]
      13. fma-neg95.8%

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{-\left(\ell + \left(\left(m - n\right) + \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)\right)} \]
      3. distribute-rgt-out91.4%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -8 \cdot 10^{+15}:\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left(\left(n - m\right) + \left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right)\right) - \ell}\\ \end{array} \]

Alternative 2: 96.6% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \cos M \cdot e^{\left(\left(n - m\right) - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}\right) - \ell} \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
 (* (cos M) (exp (- (- (- n m) (pow (- (* (+ m n) 0.5) M) 2.0)) l))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	return cos(M) * exp((((n - m) - pow((((m + n) * 0.5) - M), 2.0)) - l));
}
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 = cos(m_1) * exp((((n - m) - ((((m + n) * 0.5d0) - m_1) ** 2.0d0)) - l))
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.cos(M) * Math.exp((((n - m) - Math.pow((((m + n) * 0.5) - M), 2.0)) - l));
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.cos(M) * math.exp((((n - m) - math.pow((((m + n) * 0.5) - M), 2.0)) - l))
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return Float64(cos(M) * exp(Float64(Float64(Float64(n - m) - (Float64(Float64(Float64(m + n) * 0.5) - M) ^ 2.0)) - l)))
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	tmp = cos(M) * exp((((n - m) - ((((m + n) * 0.5) - M) ^ 2.0)) - l));
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[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[(n - m), $MachinePrecision] - N[Power[N[(N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\cos M \cdot e^{\left(\left(n - m\right) - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}\right) - \ell}
\end{array}
Derivation
  1. Initial program 76.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. Simplified76.9%

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, \color{blue}{0.5}, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left|n - m\right|\right)}\right) \]
    6. add-sqr-sqrt18.0%

      \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right|\right)}\right) \]
    7. fabs-sqr18.0%

      \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right)}\right) \]
    8. add-sqr-sqrt59.5%

      \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \color{blue}{\left(n - m\right)}\right)}\right) \]
  7. Applied egg-rr59.5%

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-\left({\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(\ell - \left(n - m\right)\right)\right)}} \]
    3. +-commutative96.3%

      \[\leadsto \cos M \cdot e^{-\color{blue}{\left(\left(\ell - \left(n - m\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)}} \]
    4. sub-neg96.3%

      \[\leadsto \cos M \cdot e^{-\left(\color{blue}{\left(\ell + \left(-\left(n - m\right)\right)\right)} + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
    5. sub-neg96.3%

      \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\color{blue}{\left(n + \left(-m\right)\right)}\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
    6. mul-1-neg96.3%

      \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\left(n + \color{blue}{-1 \cdot m}\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
    7. +-commutative96.3%

      \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\color{blue}{\left(-1 \cdot m + n\right)}\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
    8. distribute-neg-in96.3%

      \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \color{blue}{\left(\left(--1 \cdot m\right) + \left(-n\right)\right)}\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
    9. mul-1-neg96.3%

      \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(\left(-\color{blue}{\left(-m\right)}\right) + \left(-n\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
    10. remove-double-neg96.3%

      \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(\color{blue}{m} + \left(-n\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
    11. sub-neg96.3%

      \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \color{blue}{\left(m - n\right)}\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
    12. associate-+l+96.3%

      \[\leadsto \cos M \cdot e^{-\color{blue}{\left(\ell + \left(\left(m - n\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)}} \]
    13. fma-neg96.3%

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

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

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

    \[\leadsto \cos M \cdot \color{blue}{e^{-\left(\ell + \left(\left(m - n\right) + {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}\right)\right)}} \]
  10. Final simplification96.3%

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

Alternative 3: 88.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}\;m \leq -96000000000000:\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq 8.5 \cdot 10^{-218}:\\ \;\;\;\;\cos M \cdot e^{\left(M \cdot \left(m - M\right) - m\right) - \ell}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{n - \left(\left(n \cdot 0.5\right) \cdot \left(m + n \cdot 0.5\right) + \left(m + \ell\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 (<= m -96000000000000.0)
   (exp (* (pow m 2.0) -0.25))
   (if (<= m 8.5e-218)
     (* (cos M) (exp (- (- (* M (- m M)) m) l)))
     (* (cos M) (exp (- n (+ (* (* n 0.5) (+ m (* n 0.5))) (+ m 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 (m <= -96000000000000.0) {
		tmp = exp((pow(m, 2.0) * -0.25));
	} else if (m <= 8.5e-218) {
		tmp = cos(M) * exp((((M * (m - M)) - m) - l));
	} else {
		tmp = cos(M) * exp((n - (((n * 0.5) * (m + (n * 0.5))) + (m + 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 (m <= (-96000000000000.0d0)) then
        tmp = exp(((m ** 2.0d0) * (-0.25d0)))
    else if (m <= 8.5d-218) then
        tmp = cos(m_1) * exp((((m_1 * (m - m_1)) - m) - l))
    else
        tmp = cos(m_1) * exp((n - (((n * 0.5d0) * (m + (n * 0.5d0))) + (m + 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 (m <= -96000000000000.0) {
		tmp = Math.exp((Math.pow(m, 2.0) * -0.25));
	} else if (m <= 8.5e-218) {
		tmp = Math.cos(M) * Math.exp((((M * (m - M)) - m) - l));
	} else {
		tmp = Math.cos(M) * Math.exp((n - (((n * 0.5) * (m + (n * 0.5))) + (m + l))));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if m <= -96000000000000.0:
		tmp = math.exp((math.pow(m, 2.0) * -0.25))
	elif m <= 8.5e-218:
		tmp = math.cos(M) * math.exp((((M * (m - M)) - m) - l))
	else:
		tmp = math.cos(M) * math.exp((n - (((n * 0.5) * (m + (n * 0.5))) + (m + 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 (m <= -96000000000000.0)
		tmp = exp(Float64((m ^ 2.0) * -0.25));
	elseif (m <= 8.5e-218)
		tmp = Float64(cos(M) * exp(Float64(Float64(Float64(M * Float64(m - M)) - m) - l)));
	else
		tmp = Float64(cos(M) * exp(Float64(n - Float64(Float64(Float64(n * 0.5) * Float64(m + Float64(n * 0.5))) + Float64(m + 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 (m <= -96000000000000.0)
		tmp = exp(((m ^ 2.0) * -0.25));
	elseif (m <= 8.5e-218)
		tmp = cos(M) * exp((((M * (m - M)) - m) - l));
	else
		tmp = cos(M) * exp((n - (((n * 0.5) * (m + (n * 0.5))) + (m + 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[LessEqual[m, -96000000000000.0], N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision], If[LessEqual[m, 8.5e-218], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[(M * N[(m - M), $MachinePrecision]), $MachinePrecision] - m), $MachinePrecision] - l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(n - N[(N[(N[(n * 0.5), $MachinePrecision] * N[(m + N[(n * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(m + l), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;m \leq -96000000000000:\\
\;\;\;\;e^{{m}^{2} \cdot -0.25}\\

\mathbf{elif}\;m \leq 8.5 \cdot 10^{-218}:\\
\;\;\;\;\cos M \cdot e^{\left(M \cdot \left(m - M\right) - m\right) - \ell}\\

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


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

    1. Initial program 70.4%

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{-\color{blue}{0.25 \cdot {m}^{2}}} \]
    7. Taylor expanded in M around 0 98.2%

      \[\leadsto \color{blue}{e^{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. distribute-lft-neg-in98.2%

        \[\leadsto e^{\color{blue}{\left(-0.25\right) \cdot {m}^{2}}} \]
      2. metadata-eval98.2%

        \[\leadsto e^{\color{blue}{-0.25} \cdot {m}^{2}} \]
      3. *-commutative98.2%

        \[\leadsto e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
    9. Simplified98.2%

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

    if -9.6e13 < m < 8.5000000000000004e-218

    1. Initial program 81.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. Simplified81.8%

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, \color{blue}{0.5}, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left|n - m\right|\right)}\right) \]
      6. add-sqr-sqrt33.3%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right|\right)}\right) \]
      7. fabs-sqr33.3%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right)}\right) \]
      8. add-sqr-sqrt68.7%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \color{blue}{\left(n - m\right)}\right)}\right) \]
    7. Applied egg-rr68.7%

      \[\leadsto \cos M \cdot \color{blue}{\left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left(n - m\right)\right)}\right)} \]
    8. Step-by-step derivation
      1. prod-exp97.0%

        \[\leadsto \cos M \cdot \color{blue}{e^{\left(-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right) + \left(-\left(\ell - \left(n - m\right)\right)\right)}} \]
      2. distribute-neg-in97.0%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\left({\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(\ell - \left(n - m\right)\right)\right)}} \]
      3. +-commutative97.0%

        \[\leadsto \cos M \cdot e^{-\color{blue}{\left(\left(\ell - \left(n - m\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)}} \]
      4. sub-neg97.0%

        \[\leadsto \cos M \cdot e^{-\left(\color{blue}{\left(\ell + \left(-\left(n - m\right)\right)\right)} + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      5. sub-neg97.0%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\color{blue}{\left(n + \left(-m\right)\right)}\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      6. mul-1-neg97.0%

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

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\color{blue}{\left(-1 \cdot m + n\right)}\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      8. distribute-neg-in97.0%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \color{blue}{\left(\left(--1 \cdot m\right) + \left(-n\right)\right)}\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      9. mul-1-neg97.0%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(\left(-\color{blue}{\left(-m\right)}\right) + \left(-n\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      10. remove-double-neg97.0%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(\color{blue}{m} + \left(-n\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      11. sub-neg97.0%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \color{blue}{\left(m - n\right)}\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      12. associate-+l+97.0%

        \[\leadsto \cos M \cdot e^{-\color{blue}{\left(\ell + \left(\left(m - n\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)}} \]
      13. fma-neg97.0%

        \[\leadsto \cos M \cdot e^{-\left(\ell + \left(\left(m - n\right) + {\color{blue}{\left(\left(m + n\right) \cdot 0.5 - M\right)}}^{2}\right)\right)} \]
      14. *-commutative97.0%

        \[\leadsto \cos M \cdot e^{-\left(\ell + \left(\left(m - n\right) + {\left(\color{blue}{0.5 \cdot \left(m + n\right)} - M\right)}^{2}\right)\right)} \]
      15. *-commutative97.0%

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

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

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

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

        \[\leadsto \cos M \cdot e^{-\left(\ell + \left(\left(m - n\right) + \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)\right)} \]
      3. distribute-rgt-out97.0%

        \[\leadsto \cos M \cdot e^{-\left(\ell + \left(\left(m - n\right) + \color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(\left(0.5 \cdot n - M\right) + m\right)}\right)\right)} \]
    12. Simplified97.0%

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

      \[\leadsto \cos M \cdot \color{blue}{e^{-\left(\ell + \left(m + -1 \cdot \left(M \cdot \left(m - M\right)\right)\right)\right)}} \]
    14. Step-by-step derivation
      1. distribute-neg-in81.2%

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\ell\right) - \color{blue}{\left(m - M \cdot \left(m - M\right)\right)}} \]
    15. Simplified81.2%

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

    if 8.5000000000000004e-218 < 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. Simplified74.6%

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, \color{blue}{0.5}, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left|n - m\right|\right)}\right) \]
      6. add-sqr-sqrt7.8%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right|\right)}\right) \]
      7. fabs-sqr7.8%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right)}\right) \]
      8. add-sqr-sqrt71.3%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \color{blue}{\left(n - m\right)}\right)}\right) \]
    7. Applied egg-rr71.3%

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

        \[\leadsto \cos M \cdot \color{blue}{e^{\left(-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right) + \left(-\left(\ell - \left(n - m\right)\right)\right)}} \]
      2. distribute-neg-in94.6%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\left({\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(\ell - \left(n - m\right)\right)\right)}} \]
      3. +-commutative94.6%

        \[\leadsto \cos M \cdot e^{-\color{blue}{\left(\left(\ell - \left(n - m\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)}} \]
      4. sub-neg94.6%

        \[\leadsto \cos M \cdot e^{-\left(\color{blue}{\left(\ell + \left(-\left(n - m\right)\right)\right)} + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      5. sub-neg94.6%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\color{blue}{\left(n + \left(-m\right)\right)}\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      6. mul-1-neg94.6%

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

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\color{blue}{\left(-1 \cdot m + n\right)}\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      8. distribute-neg-in94.6%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \color{blue}{\left(\left(--1 \cdot m\right) + \left(-n\right)\right)}\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      9. mul-1-neg94.6%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(\left(-\color{blue}{\left(-m\right)}\right) + \left(-n\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      10. remove-double-neg94.6%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(\color{blue}{m} + \left(-n\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      11. sub-neg94.6%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \color{blue}{\left(m - n\right)}\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      12. associate-+l+94.6%

        \[\leadsto \cos M \cdot e^{-\color{blue}{\left(\ell + \left(\left(m - n\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)}} \]
      13. fma-neg94.6%

        \[\leadsto \cos M \cdot e^{-\left(\ell + \left(\left(m - n\right) + {\color{blue}{\left(\left(m + n\right) \cdot 0.5 - M\right)}}^{2}\right)\right)} \]
      14. *-commutative94.6%

        \[\leadsto \cos M \cdot e^{-\left(\ell + \left(\left(m - n\right) + {\left(\color{blue}{0.5 \cdot \left(m + n\right)} - M\right)}^{2}\right)\right)} \]
      15. *-commutative94.6%

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

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

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

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

        \[\leadsto \cos M \cdot e^{-\left(\ell + \left(\left(m - n\right) + \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)\right)} \]
      3. distribute-rgt-out86.0%

        \[\leadsto \cos M \cdot e^{-\left(\ell + \left(\left(m - n\right) + \color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(\left(0.5 \cdot n - M\right) + m\right)}\right)\right)} \]
    12. Simplified86.0%

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

      \[\leadsto \cos M \cdot \color{blue}{e^{n - \left(\ell + \left(m + 0.5 \cdot \left(n \cdot \left(m + 0.5 \cdot n\right)\right)\right)\right)}} \]
    14. Step-by-step derivation
      1. associate-+r+76.5%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -96000000000000:\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq 8.5 \cdot 10^{-218}:\\ \;\;\;\;\cos M \cdot e^{\left(M \cdot \left(m - M\right) - m\right) - \ell}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{n - \left(\left(n \cdot 0.5\right) \cdot \left(m + n \cdot 0.5\right) + \left(m + \ell\right)\right)}\\ \end{array} \]

Alternative 4: 68.2% accurate, 2.0× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;m \leq -96000000000000 \lor \neg \left(m \leq 5.2 \cdot 10^{-30}\right):\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(K \cdot \left(m \cdot 0.5\right) - M\right) \cdot e^{-\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 (<= m -96000000000000.0) (not (<= m 5.2e-30)))
   (exp (* (pow m 2.0) -0.25))
   (* (cos (- (* K (* m 0.5)) M)) (exp (- 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 ((m <= -96000000000000.0) || !(m <= 5.2e-30)) {
		tmp = exp((pow(m, 2.0) * -0.25));
	} else {
		tmp = cos(((K * (m * 0.5)) - M)) * exp(-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 ((m <= (-96000000000000.0d0)) .or. (.not. (m <= 5.2d-30))) then
        tmp = exp(((m ** 2.0d0) * (-0.25d0)))
    else
        tmp = cos(((k * (m * 0.5d0)) - m_1)) * exp(-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 ((m <= -96000000000000.0) || !(m <= 5.2e-30)) {
		tmp = Math.exp((Math.pow(m, 2.0) * -0.25));
	} else {
		tmp = Math.cos(((K * (m * 0.5)) - M)) * Math.exp(-l);
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if (m <= -96000000000000.0) or not (m <= 5.2e-30):
		tmp = math.exp((math.pow(m, 2.0) * -0.25))
	else:
		tmp = math.cos(((K * (m * 0.5)) - M)) * math.exp(-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 ((m <= -96000000000000.0) || !(m <= 5.2e-30))
		tmp = exp(Float64((m ^ 2.0) * -0.25));
	else
		tmp = Float64(cos(Float64(Float64(K * Float64(m * 0.5)) - M)) * exp(Float64(-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 ((m <= -96000000000000.0) || ~((m <= 5.2e-30)))
		tmp = exp(((m ^ 2.0) * -0.25));
	else
		tmp = cos(((K * (m * 0.5)) - M)) * exp(-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[m, -96000000000000.0], N[Not[LessEqual[m, 5.2e-30]], $MachinePrecision]], N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision], N[(N[Cos[N[(N[(K * N[(m * 0.5), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;m \leq -96000000000000 \lor \neg \left(m \leq 5.2 \cdot 10^{-30}\right):\\
\;\;\;\;e^{{m}^{2} \cdot -0.25}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < -9.6e13 or 5.19999999999999973e-30 < m

    1. Initial program 70.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. Simplified70.9%

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

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

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

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

      \[\leadsto \cos M \cdot e^{-\color{blue}{0.25 \cdot {m}^{2}}} \]
    7. Taylor expanded in M around 0 89.4%

      \[\leadsto \color{blue}{e^{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. distribute-lft-neg-in89.4%

        \[\leadsto e^{\color{blue}{\left(-0.25\right) \cdot {m}^{2}}} \]
      2. metadata-eval89.4%

        \[\leadsto e^{\color{blue}{-0.25} \cdot {m}^{2}} \]
      3. *-commutative89.4%

        \[\leadsto e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
    9. Simplified89.4%

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

    if -9.6e13 < m < 5.19999999999999973e-30

    1. Initial program 82.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. Simplified82.6%

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -96000000000000 \lor \neg \left(m \leq 5.2 \cdot 10^{-30}\right):\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(K \cdot \left(m \cdot 0.5\right) - M\right) \cdot e^{-\ell}\\ \end{array} \]

Alternative 5: 85.2% accurate, 2.0× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;m \leq -96000000000000:\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left(M \cdot \left(m - M\right) - m\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 (<= m -96000000000000.0)
   (exp (* (pow m 2.0) -0.25))
   (* (cos M) (exp (- (- (* M (- m M)) m) 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 (m <= -96000000000000.0) {
		tmp = exp((pow(m, 2.0) * -0.25));
	} else {
		tmp = cos(M) * exp((((M * (m - M)) - m) - 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 (m <= (-96000000000000.0d0)) then
        tmp = exp(((m ** 2.0d0) * (-0.25d0)))
    else
        tmp = cos(m_1) * exp((((m_1 * (m - m_1)) - m) - 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 (m <= -96000000000000.0) {
		tmp = Math.exp((Math.pow(m, 2.0) * -0.25));
	} else {
		tmp = Math.cos(M) * Math.exp((((M * (m - M)) - m) - l));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if m <= -96000000000000.0:
		tmp = math.exp((math.pow(m, 2.0) * -0.25))
	else:
		tmp = math.cos(M) * math.exp((((M * (m - M)) - m) - 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 (m <= -96000000000000.0)
		tmp = exp(Float64((m ^ 2.0) * -0.25));
	else
		tmp = Float64(cos(M) * exp(Float64(Float64(Float64(M * Float64(m - M)) - m) - 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 (m <= -96000000000000.0)
		tmp = exp(((m ^ 2.0) * -0.25));
	else
		tmp = cos(M) * exp((((M * (m - M)) - m) - 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[LessEqual[m, -96000000000000.0], N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[(M * N[(m - M), $MachinePrecision]), $MachinePrecision] - m), $MachinePrecision] - l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;m \leq -96000000000000:\\
\;\;\;\;e^{{m}^{2} \cdot -0.25}\\

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


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

    1. Initial program 70.4%

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{-\color{blue}{0.25 \cdot {m}^{2}}} \]
    7. Taylor expanded in M around 0 98.2%

      \[\leadsto \color{blue}{e^{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. distribute-lft-neg-in98.2%

        \[\leadsto e^{\color{blue}{\left(-0.25\right) \cdot {m}^{2}}} \]
      2. metadata-eval98.2%

        \[\leadsto e^{\color{blue}{-0.25} \cdot {m}^{2}} \]
      3. *-commutative98.2%

        \[\leadsto e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
    9. Simplified98.2%

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

    if -9.6e13 < m

    1. Initial program 78.1%

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, \color{blue}{0.5}, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left|n - m\right|\right)}\right) \]
      6. add-sqr-sqrt20.3%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left|\color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right|\right)}\right) \]
      7. fabs-sqr20.3%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \color{blue}{\sqrt{n - m} \cdot \sqrt{n - m}}\right)}\right) \]
      8. add-sqr-sqrt70.0%

        \[\leadsto \cos M \cdot \left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \color{blue}{\left(n - m\right)}\right)}\right) \]
    7. Applied egg-rr70.0%

      \[\leadsto \cos M \cdot \color{blue}{\left(e^{-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}} \cdot e^{-\left(\ell - \left(n - m\right)\right)}\right)} \]
    8. Step-by-step derivation
      1. prod-exp95.8%

        \[\leadsto \cos M \cdot \color{blue}{e^{\left(-{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right) + \left(-\left(\ell - \left(n - m\right)\right)\right)}} \]
      2. distribute-neg-in95.8%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\left({\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(\ell - \left(n - m\right)\right)\right)}} \]
      3. +-commutative95.8%

        \[\leadsto \cos M \cdot e^{-\color{blue}{\left(\left(\ell - \left(n - m\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)}} \]
      4. sub-neg95.8%

        \[\leadsto \cos M \cdot e^{-\left(\color{blue}{\left(\ell + \left(-\left(n - m\right)\right)\right)} + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      5. sub-neg95.8%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\color{blue}{\left(n + \left(-m\right)\right)}\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      6. mul-1-neg95.8%

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

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(-\color{blue}{\left(-1 \cdot m + n\right)}\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      8. distribute-neg-in95.8%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \color{blue}{\left(\left(--1 \cdot m\right) + \left(-n\right)\right)}\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      9. mul-1-neg95.8%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(\left(-\color{blue}{\left(-m\right)}\right) + \left(-n\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      10. remove-double-neg95.8%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \left(\color{blue}{m} + \left(-n\right)\right)\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      11. sub-neg95.8%

        \[\leadsto \cos M \cdot e^{-\left(\left(\ell + \color{blue}{\left(m - n\right)}\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)} \]
      12. associate-+l+95.8%

        \[\leadsto \cos M \cdot e^{-\color{blue}{\left(\ell + \left(\left(m - n\right) + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)}} \]
      13. fma-neg95.8%

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{-\left(\ell + \left(\left(m - n\right) + \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)\right)} \]
      3. distribute-rgt-out91.4%

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

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

      \[\leadsto \cos M \cdot \color{blue}{e^{-\left(\ell + \left(m + -1 \cdot \left(M \cdot \left(m - M\right)\right)\right)\right)}} \]
    14. Step-by-step derivation
      1. distribute-neg-in81.3%

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\ell\right) - \color{blue}{\left(m - M \cdot \left(m - M\right)\right)}} \]
    15. Simplified81.3%

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

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

Alternative 6: 68.2% accurate, 2.0× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;m \leq -96000000000000 \lor \neg \left(m \leq 5.2 \cdot 10^{-30}\right):\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\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 (<= m -96000000000000.0) (not (<= m 5.2e-30)))
   (exp (* (pow m 2.0) -0.25))
   (* (cos M) (exp (- 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 ((m <= -96000000000000.0) || !(m <= 5.2e-30)) {
		tmp = exp((pow(m, 2.0) * -0.25));
	} else {
		tmp = cos(M) * exp(-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 ((m <= (-96000000000000.0d0)) .or. (.not. (m <= 5.2d-30))) then
        tmp = exp(((m ** 2.0d0) * (-0.25d0)))
    else
        tmp = cos(m_1) * exp(-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 ((m <= -96000000000000.0) || !(m <= 5.2e-30)) {
		tmp = Math.exp((Math.pow(m, 2.0) * -0.25));
	} else {
		tmp = Math.cos(M) * Math.exp(-l);
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if (m <= -96000000000000.0) or not (m <= 5.2e-30):
		tmp = math.exp((math.pow(m, 2.0) * -0.25))
	else:
		tmp = math.cos(M) * math.exp(-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 ((m <= -96000000000000.0) || !(m <= 5.2e-30))
		tmp = exp(Float64((m ^ 2.0) * -0.25));
	else
		tmp = Float64(cos(M) * exp(Float64(-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 ((m <= -96000000000000.0) || ~((m <= 5.2e-30)))
		tmp = exp(((m ^ 2.0) * -0.25));
	else
		tmp = cos(M) * exp(-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[m, -96000000000000.0], N[Not[LessEqual[m, 5.2e-30]], $MachinePrecision]], N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;m \leq -96000000000000 \lor \neg \left(m \leq 5.2 \cdot 10^{-30}\right):\\
\;\;\;\;e^{{m}^{2} \cdot -0.25}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < -9.6e13 or 5.19999999999999973e-30 < m

    1. Initial program 70.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. Simplified70.9%

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

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

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

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

      \[\leadsto \cos M \cdot e^{-\color{blue}{0.25 \cdot {m}^{2}}} \]
    7. Taylor expanded in M around 0 89.4%

      \[\leadsto \color{blue}{e^{-0.25 \cdot {m}^{2}}} \]
    8. Step-by-step derivation
      1. distribute-lft-neg-in89.4%

        \[\leadsto e^{\color{blue}{\left(-0.25\right) \cdot {m}^{2}}} \]
      2. metadata-eval89.4%

        \[\leadsto e^{\color{blue}{-0.25} \cdot {m}^{2}} \]
      3. *-commutative89.4%

        \[\leadsto e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
    9. Simplified89.4%

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

    if -9.6e13 < m < 5.19999999999999973e-30

    1. Initial program 82.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. Simplified82.6%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -96000000000000 \lor \neg \left(m \leq 5.2 \cdot 10^{-30}\right):\\ \;\;\;\;e^{{m}^{2} \cdot -0.25}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \]

Alternative 7: 53.2% accurate, 2.1× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ e^{{m}^{2} \cdot -0.25} \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 (* (pow m 2.0) -0.25)))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	return exp((pow(m, 2.0) * -0.25));
}
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(((m ** 2.0d0) * (-0.25d0)))
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.pow(m, 2.0) * -0.25));
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.exp((math.pow(m, 2.0) * -0.25))
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return exp(Float64((m ^ 2.0) * -0.25))
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	tmp = exp(((m ^ 2.0) * -0.25));
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[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
e^{{m}^{2} \cdot -0.25}
\end{array}
Derivation
  1. Initial program 76.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. Simplified76.9%

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

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

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

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

    \[\leadsto \cos M \cdot e^{-\color{blue}{0.25 \cdot {m}^{2}}} \]
  7. Taylor expanded in M around 0 49.2%

    \[\leadsto \color{blue}{e^{-0.25 \cdot {m}^{2}}} \]
  8. Step-by-step derivation
    1. distribute-lft-neg-in49.2%

      \[\leadsto e^{\color{blue}{\left(-0.25\right) \cdot {m}^{2}}} \]
    2. metadata-eval49.2%

      \[\leadsto e^{\color{blue}{-0.25} \cdot {m}^{2}} \]
    3. *-commutative49.2%

      \[\leadsto e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
  9. Simplified49.2%

    \[\leadsto \color{blue}{e^{{m}^{2} \cdot -0.25}} \]
  10. Final simplification49.2%

    \[\leadsto e^{{m}^{2} \cdot -0.25} \]

Alternative 8: 7.2% accurate, 4.2× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \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 (cos M))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	return 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 = 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.cos(M);
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.cos(M)
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return cos(M)
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	tmp = 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[Cos[M], $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\cos M
\end{array}
Derivation
  1. Initial program 76.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. Simplified76.9%

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

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

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

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

    \[\leadsto \cos M \cdot e^{-\color{blue}{0.25 \cdot {m}^{2}}} \]
  7. Taylor expanded in m around 0 6.8%

    \[\leadsto \color{blue}{\cos M} \]
  8. Final simplification6.8%

    \[\leadsto \cos M \]

Reproduce

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