Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.4% → 96.5%
Time: 36.8s
Alternatives: 11
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 11 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.4% 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.4× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := \left|n - m\right| - \ell\\ t_1 := e^{t_0 - {\left(\frac{m + n}{2} - M\right)}^{2}}\\ \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t_1 \leq \infty:\\ \;\;\;\;\cos \left(\frac{K}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}} - M\right) \cdot t_1\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{t_0 - {\log \left(e^{\mathsf{fma}\left(m + n, 0.5, M\right)}\right)}^{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 (- (fabs (- n m)) l))
        (t_1 (exp (- t_0 (pow (- (/ (+ m n) 2.0) M) 2.0)))))
   (if (<= (* (cos (- (/ (* K (+ m n)) 2.0) M)) t_1) INFINITY)
     (* (cos (- (/ K (+ (* -2.0 (/ m (pow n 2.0))) (* 2.0 (/ 1.0 n)))) M)) t_1)
     (* (cos M) (exp (- t_0 (pow (log (exp (fma (+ m n) 0.5 M))) 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 = fabs((n - m)) - l;
	double t_1 = exp((t_0 - pow((((m + n) / 2.0) - M), 2.0)));
	double tmp;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_1) <= ((double) INFINITY)) {
		tmp = cos(((K / ((-2.0 * (m / pow(n, 2.0))) + (2.0 * (1.0 / n)))) - M)) * t_1;
	} else {
		tmp = cos(M) * exp((t_0 - pow(log(exp(fma((m + n), 0.5, M))), 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(abs(Float64(n - m)) - l)
	t_1 = exp(Float64(t_0 - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)))
	tmp = 0.0
	if (Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * t_1) <= Inf)
		tmp = Float64(cos(Float64(Float64(K / Float64(Float64(-2.0 * Float64(m / (n ^ 2.0))) + Float64(2.0 * Float64(1.0 / n)))) - M)) * t_1);
	else
		tmp = Float64(cos(M) * exp(Float64(t_0 - (log(exp(fma(Float64(m + n), 0.5, M))) ^ 2.0))));
	end
	return 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[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, Block[{t$95$1 = N[Exp[N[(t$95$0 - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $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[(N[Cos[N[(N[(K / N[(N[(-2.0 * N[(m / N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(2.0 * N[(1.0 / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * t$95$1), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$0 - N[Power[N[Log[N[Exp[N[(N[(m + n), $MachinePrecision] * 0.5 + M), $MachinePrecision]], $MachinePrecision]], $MachinePrecision], 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|n - m\right| - \ell\\
t_1 := e^{t_0 - {\left(\frac{m + n}{2} - M\right)}^{2}}\\
\mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t_1 \leq \infty:\\
\;\;\;\;\cos \left(\frac{K}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}} - M\right) \cdot t_1\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot e^{t_0 - {\log \left(e^{\mathsf{fma}\left(m + n, 0.5, M\right)}\right)}^{2}}\\


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

    1. Initial program 95.7%

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

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

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

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

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

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

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

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

    1. Initial program 0.0%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-{\left(\mathsf{log1p}\left(\mathsf{expm1}\left(\color{blue}{\mathsf{fma}\left(m + n, 0.5, -M\right)}\right)\right)\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
    9. Applied egg-rr96.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \leq \infty:\\ \;\;\;\;\cos \left(\frac{K}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}} - M\right) \cdot e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - {\log \left(e^{\mathsf{fma}\left(m + n, 0.5, M\right)}\right)}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 96.6% accurate, 0.4× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} t_0 := e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}\\ \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t_0 \leq \infty:\\ \;\;\;\;\cos \left(\frac{K}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}} - M\right) \cdot t_0\\ \mathbf{else}:\\ \;\;\;\;t_0 \cdot \cos M\\ \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 (exp (- (- (fabs (- n m)) l) (pow (- (/ (+ m n) 2.0) M) 2.0)))))
   (if (<= (* (cos (- (/ (* K (+ m n)) 2.0) M)) t_0) INFINITY)
     (* (cos (- (/ K (+ (* -2.0 (/ m (pow n 2.0))) (* 2.0 (/ 1.0 n)))) M)) t_0)
     (* t_0 (cos M)))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double t_0 = exp(((fabs((n - m)) - l) - pow((((m + n) / 2.0) - M), 2.0)));
	double tmp;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_0) <= ((double) INFINITY)) {
		tmp = cos(((K / ((-2.0 * (m / pow(n, 2.0))) + (2.0 * (1.0 / n)))) - M)) * t_0;
	} else {
		tmp = t_0 * cos(M);
	}
	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.exp(((Math.abs((n - m)) - l) - Math.pow((((m + n) / 2.0) - M), 2.0)));
	double tmp;
	if ((Math.cos((((K * (m + n)) / 2.0) - M)) * t_0) <= Double.POSITIVE_INFINITY) {
		tmp = Math.cos(((K / ((-2.0 * (m / Math.pow(n, 2.0))) + (2.0 * (1.0 / n)))) - M)) * t_0;
	} else {
		tmp = t_0 * Math.cos(M);
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.exp(((math.fabs((n - m)) - l) - math.pow((((m + n) / 2.0) - M), 2.0)))
	tmp = 0
	if (math.cos((((K * (m + n)) / 2.0) - M)) * t_0) <= math.inf:
		tmp = math.cos(((K / ((-2.0 * (m / math.pow(n, 2.0))) + (2.0 * (1.0 / n)))) - M)) * t_0
	else:
		tmp = t_0 * math.cos(M)
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = exp(Float64(Float64(abs(Float64(n - m)) - l) - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)))
	tmp = 0.0
	if (Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * t_0) <= Inf)
		tmp = Float64(cos(Float64(Float64(K / Float64(Float64(-2.0 * Float64(m / (n ^ 2.0))) + Float64(2.0 * Float64(1.0 / n)))) - M)) * t_0);
	else
		tmp = Float64(t_0 * cos(M));
	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 = exp(((abs((n - m)) - l) - ((((m + n) / 2.0) - M) ^ 2.0)));
	tmp = 0.0;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_0) <= Inf)
		tmp = cos(((K / ((-2.0 * (m / (n ^ 2.0))) + (2.0 * (1.0 / n)))) - M)) * t_0;
	else
		tmp = t_0 * cos(M);
	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[Exp[N[(N[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * t$95$0), $MachinePrecision], Infinity], N[(N[Cos[N[(N[(K / N[(N[(-2.0 * N[(m / N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(2.0 * N[(1.0 / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * t$95$0), $MachinePrecision], N[(t$95$0 * N[Cos[M], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := e^{\left(\left|n - m\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}\\
\mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t_0 \leq \infty:\\
\;\;\;\;\cos \left(\frac{K}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}} - M\right) \cdot t_0\\

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


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

    1. Initial program 95.7%

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

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

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

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

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

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

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

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

    1. Initial program 0.0%

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

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

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

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

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

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

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

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

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

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

Alternative 3: 96.7% 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|n - m\right| - \ell\\ t_1 := e^{t_0 - {\left(\frac{m + n}{2} - M\right)}^{2}}\\ \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t_1 \leq -0.5:\\ \;\;\;\;\cos \left(\frac{K}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}} - M\right) \cdot e^{\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) + t_0}\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot \cos M\\ \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 (- n m)) l))
        (t_1 (exp (- t_0 (pow (- (/ (+ m n) 2.0) M) 2.0)))))
   (if (<= (* (cos (- (/ (* K (+ m n)) 2.0) M)) t_1) -0.5)
     (*
      (cos (- (/ K (+ (* -2.0 (/ m (pow n 2.0))) (* 2.0 (/ 1.0 n)))) M))
      (exp (+ (* (- (* n 0.5) M) (- (- M (* n 0.5)) m)) t_0)))
     (* t_1 (cos M)))))
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((n - m)) - l;
	double t_1 = exp((t_0 - pow((((m + n) / 2.0) - M), 2.0)));
	double tmp;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_1) <= -0.5) {
		tmp = cos(((K / ((-2.0 * (m / pow(n, 2.0))) + (2.0 * (1.0 / n)))) - M)) * exp(((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) + t_0));
	} else {
		tmp = t_1 * cos(M);
	}
	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 = abs((n - m)) - l
    t_1 = exp((t_0 - ((((m + n) / 2.0d0) - m_1) ** 2.0d0)))
    if ((cos((((k * (m + n)) / 2.0d0) - m_1)) * t_1) <= (-0.5d0)) then
        tmp = cos(((k / (((-2.0d0) * (m / (n ** 2.0d0))) + (2.0d0 * (1.0d0 / n)))) - m_1)) * exp(((((n * 0.5d0) - m_1) * ((m_1 - (n * 0.5d0)) - m)) + t_0))
    else
        tmp = t_1 * cos(m_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.abs((n - m)) - l;
	double t_1 = Math.exp((t_0 - Math.pow((((m + n) / 2.0) - M), 2.0)));
	double tmp;
	if ((Math.cos((((K * (m + n)) / 2.0) - M)) * t_1) <= -0.5) {
		tmp = Math.cos(((K / ((-2.0 * (m / Math.pow(n, 2.0))) + (2.0 * (1.0 / n)))) - M)) * Math.exp(((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) + t_0));
	} else {
		tmp = t_1 * Math.cos(M);
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.fabs((n - m)) - l
	t_1 = math.exp((t_0 - math.pow((((m + n) / 2.0) - M), 2.0)))
	tmp = 0
	if (math.cos((((K * (m + n)) / 2.0) - M)) * t_1) <= -0.5:
		tmp = math.cos(((K / ((-2.0 * (m / math.pow(n, 2.0))) + (2.0 * (1.0 / n)))) - M)) * math.exp(((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) + t_0))
	else:
		tmp = t_1 * math.cos(M)
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(abs(Float64(n - m)) - l)
	t_1 = exp(Float64(t_0 - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)))
	tmp = 0.0
	if (Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * t_1) <= -0.5)
		tmp = Float64(cos(Float64(Float64(K / Float64(Float64(-2.0 * Float64(m / (n ^ 2.0))) + Float64(2.0 * Float64(1.0 / n)))) - M)) * exp(Float64(Float64(Float64(Float64(n * 0.5) - M) * Float64(Float64(M - Float64(n * 0.5)) - m)) + t_0)));
	else
		tmp = Float64(t_1 * cos(M));
	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((n - m)) - l;
	t_1 = exp((t_0 - ((((m + n) / 2.0) - M) ^ 2.0)));
	tmp = 0.0;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_1) <= -0.5)
		tmp = cos(((K / ((-2.0 * (m / (n ^ 2.0))) + (2.0 * (1.0 / n)))) - M)) * exp(((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) + t_0));
	else
		tmp = t_1 * cos(M);
	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[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, Block[{t$95$1 = N[Exp[N[(t$95$0 - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $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], -0.5], N[(N[Cos[N[(N[(K / N[(N[(-2.0 * N[(m / N[Power[n, 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + N[(2.0 * N[(1.0 / n), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(N[(N[(n * 0.5), $MachinePrecision] - M), $MachinePrecision] * N[(N[(M - N[(n * 0.5), $MachinePrecision]), $MachinePrecision] - m), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(t$95$1 * N[Cos[M], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \left|n - m\right| - \ell\\
t_1 := e^{t_0 - {\left(\frac{m + n}{2} - M\right)}^{2}}\\
\mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t_1 \leq -0.5:\\
\;\;\;\;\cos \left(\frac{K}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}} - M\right) \cdot e^{\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) + t_0}\\

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


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

    1. Initial program 53.9%

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

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

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

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

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

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

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

      \[\leadsto \cos \left(\frac{K}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}} - M\right) \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|n - m\right|\right)} \]
    7. Step-by-step derivation
      1. +-commutative9.7%

        \[\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|n - m\right|\right)} \]
      2. unpow29.7%

        \[\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|n - m\right|\right)} \]
      3. distribute-rgt-out9.7%

        \[\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|n - m\right|\right)} \]
      4. *-commutative9.7%

        \[\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|n - m\right|\right)} \]
      5. *-commutative9.7%

        \[\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|n - m\right|\right)} \]
    8. Simplified38.4%

      \[\leadsto \cos \left(\frac{K}{-2 \cdot \frac{m}{{n}^{2}} + 2 \cdot \frac{1}{n}} - M\right) \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|n - m\right|\right)} \]

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

    1. Initial program 78.1%

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

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

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

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

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

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

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

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

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

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

Alternative 4: 96.3% 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|n - m\right| - \ell\\ t_1 := e^{t_0 - {\left(\frac{m + n}{2} - M\right)}^{2}}\\ \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t_1 \leq -0.5:\\ \;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{\left(m \cdot 0.5 - M\right) \cdot \left(\left(M - m \cdot 0.5\right) - n\right) + t_0}\\ \mathbf{else}:\\ \;\;\;\;t_1 \cdot \cos M\\ \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 (- n m)) l))
        (t_1 (exp (- t_0 (pow (- (/ (+ m n) 2.0) M) 2.0)))))
   (if (<= (* (cos (- (/ (* K (+ m n)) 2.0) M)) t_1) -0.5)
     (*
      (cos (* 0.5 (* K n)))
      (exp (+ (* (- (* m 0.5) M) (- (- M (* m 0.5)) n)) t_0)))
     (* t_1 (cos M)))))
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((n - m)) - l;
	double t_1 = exp((t_0 - pow((((m + n) / 2.0) - M), 2.0)));
	double tmp;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_1) <= -0.5) {
		tmp = cos((0.5 * (K * n))) * exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + t_0));
	} else {
		tmp = t_1 * cos(M);
	}
	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 = abs((n - m)) - l
    t_1 = exp((t_0 - ((((m + n) / 2.0d0) - m_1) ** 2.0d0)))
    if ((cos((((k * (m + n)) / 2.0d0) - m_1)) * t_1) <= (-0.5d0)) then
        tmp = cos((0.5d0 * (k * n))) * exp(((((m * 0.5d0) - m_1) * ((m_1 - (m * 0.5d0)) - n)) + t_0))
    else
        tmp = t_1 * cos(m_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.abs((n - m)) - l;
	double t_1 = Math.exp((t_0 - Math.pow((((m + n) / 2.0) - M), 2.0)));
	double tmp;
	if ((Math.cos((((K * (m + n)) / 2.0) - M)) * t_1) <= -0.5) {
		tmp = Math.cos((0.5 * (K * n))) * Math.exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + t_0));
	} else {
		tmp = t_1 * Math.cos(M);
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.fabs((n - m)) - l
	t_1 = math.exp((t_0 - math.pow((((m + n) / 2.0) - M), 2.0)))
	tmp = 0
	if (math.cos((((K * (m + n)) / 2.0) - M)) * t_1) <= -0.5:
		tmp = math.cos((0.5 * (K * n))) * math.exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + t_0))
	else:
		tmp = t_1 * math.cos(M)
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(abs(Float64(n - m)) - l)
	t_1 = exp(Float64(t_0 - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)))
	tmp = 0.0
	if (Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * t_1) <= -0.5)
		tmp = Float64(cos(Float64(0.5 * Float64(K * n))) * exp(Float64(Float64(Float64(Float64(m * 0.5) - M) * Float64(Float64(M - Float64(m * 0.5)) - n)) + t_0)));
	else
		tmp = Float64(t_1 * cos(M));
	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((n - m)) - l;
	t_1 = exp((t_0 - ((((m + n) / 2.0) - M) ^ 2.0)));
	tmp = 0.0;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_1) <= -0.5)
		tmp = cos((0.5 * (K * n))) * exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + t_0));
	else
		tmp = t_1 * cos(M);
	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[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, Block[{t$95$1 = N[Exp[N[(t$95$0 - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $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], -0.5], N[(N[Cos[N[(0.5 * N[(K * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(N[(N[(m * 0.5), $MachinePrecision] - M), $MachinePrecision] * N[(N[(M - N[(m * 0.5), $MachinePrecision]), $MachinePrecision] - n), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(t$95$1 * N[Cos[M], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \left|n - m\right| - \ell\\
t_1 := e^{t_0 - {\left(\frac{m + n}{2} - M\right)}^{2}}\\
\mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t_1 \leq -0.5:\\
\;\;\;\;\cos \left(0.5 \cdot \left(K \cdot n\right)\right) \cdot e^{\left(m \cdot 0.5 - M\right) \cdot \left(\left(M - m \cdot 0.5\right) - n\right) + t_0}\\

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


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

    1. Initial program 53.9%

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

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

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

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

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

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

      \[\leadsto \cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \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|n - m\right|\right)} \]
    6. Step-by-step derivation
      1. +-commutative9.7%

        \[\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|n - m\right|\right)} \]
      2. unpow29.7%

        \[\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|n - m\right|\right)} \]
      3. distribute-rgt-out9.7%

        \[\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|n - m\right|\right)} \]
      4. *-commutative9.7%

        \[\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|n - m\right|\right)} \]
      5. *-commutative9.7%

        \[\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|n - m\right|\right)} \]
    7. Simplified60.1%

      \[\leadsto \cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \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|n - m\right|\right)} \]
    8. Taylor expanded in n around inf 47.5%

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

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

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

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

    1. Initial program 78.1%

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

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

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

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

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

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

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

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

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

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

Alternative 5: 75.4% 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|n - m\right| - \ell\\ t_1 := \cos M \cdot e^{t_0 - 0.5 \cdot \left(m \cdot \left(n + m \cdot 0.5\right)\right)}\\ \mathbf{if}\;m \leq -1 \cdot 10^{+49}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;m \leq -1.2 \cdot 10^{-307}:\\ \;\;\;\;\cos M \cdot e^{t_0 - M \cdot \left(\left(m + n\right) + M\right)}\\ \mathbf{elif}\;m \leq 4.2 \cdot 10^{+17}:\\ \;\;\;\;\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{M \cdot \left(n - M\right) + t_0}\\ \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 (- (fabs (- n m)) l))
        (t_1 (* (cos M) (exp (- t_0 (* 0.5 (* m (+ n (* m 0.5)))))))))
   (if (<= m -1e+49)
     t_1
     (if (<= m -1.2e-307)
       (* (cos M) (exp (- t_0 (* M (+ (+ m n) M)))))
       (if (<= m 4.2e+17)
         (* (cos (* (+ m n) (* K 0.5))) (exp (+ (* M (- n M)) t_0)))
         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 = fabs((n - m)) - l;
	double t_1 = cos(M) * exp((t_0 - (0.5 * (m * (n + (m * 0.5))))));
	double tmp;
	if (m <= -1e+49) {
		tmp = t_1;
	} else if (m <= -1.2e-307) {
		tmp = cos(M) * exp((t_0 - (M * ((m + n) + M))));
	} else if (m <= 4.2e+17) {
		tmp = cos(((m + n) * (K * 0.5))) * exp(((M * (n - M)) + t_0));
	} 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) :: tmp
    t_0 = abs((n - m)) - l
    t_1 = cos(m_1) * exp((t_0 - (0.5d0 * (m * (n + (m * 0.5d0))))))
    if (m <= (-1d+49)) then
        tmp = t_1
    else if (m <= (-1.2d-307)) then
        tmp = cos(m_1) * exp((t_0 - (m_1 * ((m + n) + m_1))))
    else if (m <= 4.2d+17) then
        tmp = cos(((m + n) * (k * 0.5d0))) * exp(((m_1 * (n - m_1)) + t_0))
    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.abs((n - m)) - l;
	double t_1 = Math.cos(M) * Math.exp((t_0 - (0.5 * (m * (n + (m * 0.5))))));
	double tmp;
	if (m <= -1e+49) {
		tmp = t_1;
	} else if (m <= -1.2e-307) {
		tmp = Math.cos(M) * Math.exp((t_0 - (M * ((m + n) + M))));
	} else if (m <= 4.2e+17) {
		tmp = Math.cos(((m + n) * (K * 0.5))) * Math.exp(((M * (n - M)) + t_0));
	} 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.fabs((n - m)) - l
	t_1 = math.cos(M) * math.exp((t_0 - (0.5 * (m * (n + (m * 0.5))))))
	tmp = 0
	if m <= -1e+49:
		tmp = t_1
	elif m <= -1.2e-307:
		tmp = math.cos(M) * math.exp((t_0 - (M * ((m + n) + M))))
	elif m <= 4.2e+17:
		tmp = math.cos(((m + n) * (K * 0.5))) * math.exp(((M * (n - M)) + t_0))
	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 = Float64(abs(Float64(n - m)) - l)
	t_1 = Float64(cos(M) * exp(Float64(t_0 - Float64(0.5 * Float64(m * Float64(n + Float64(m * 0.5)))))))
	tmp = 0.0
	if (m <= -1e+49)
		tmp = t_1;
	elseif (m <= -1.2e-307)
		tmp = Float64(cos(M) * exp(Float64(t_0 - Float64(M * Float64(Float64(m + n) + M)))));
	elseif (m <= 4.2e+17)
		tmp = Float64(cos(Float64(Float64(m + n) * Float64(K * 0.5))) * exp(Float64(Float64(M * Float64(n - M)) + t_0)));
	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 = abs((n - m)) - l;
	t_1 = cos(M) * exp((t_0 - (0.5 * (m * (n + (m * 0.5))))));
	tmp = 0.0;
	if (m <= -1e+49)
		tmp = t_1;
	elseif (m <= -1.2e-307)
		tmp = cos(M) * exp((t_0 - (M * ((m + n) + M))));
	elseif (m <= 4.2e+17)
		tmp = cos(((m + n) * (K * 0.5))) * exp(((M * (n - M)) + t_0));
	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[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$0 - N[(0.5 * N[(m * N[(n + N[(m * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[m, -1e+49], t$95$1, If[LessEqual[m, -1.2e-307], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$0 - N[(M * N[(N[(m + n), $MachinePrecision] + M), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, 4.2e+17], N[(N[Cos[N[(N[(m + n), $MachinePrecision] * N[(K * 0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(M * N[(n - M), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$1]]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \left|n - m\right| - \ell\\
t_1 := \cos M \cdot e^{t_0 - 0.5 \cdot \left(m \cdot \left(n + m \cdot 0.5\right)\right)}\\
\mathbf{if}\;m \leq -1 \cdot 10^{+49}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;m \leq -1.2 \cdot 10^{-307}:\\
\;\;\;\;\cos M \cdot e^{t_0 - M \cdot \left(\left(m + n\right) + M\right)}\\

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

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if m < -9.99999999999999946e48 or 4.2e17 < m

    1. Initial program 69.2%

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)}} \]
    4. Add Preprocessing
    5. 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}\right) - \left(\ell - \left|n - m\right|\right)} \]
    6. Step-by-step derivation
      1. cos-neg98.1%

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

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

      \[\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|n - m\right|\right)} \]
    9. Step-by-step derivation
      1. +-commutative77.7%

        \[\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|n - m\right|\right)} \]
      2. unpow277.7%

        \[\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|n - m\right|\right)} \]
      3. distribute-rgt-out84.3%

        \[\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|n - m\right|\right)} \]
      4. *-commutative84.3%

        \[\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|n - m\right|\right)} \]
      5. *-commutative84.3%

        \[\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|n - m\right|\right)} \]
    10. Simplified84.3%

      \[\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|n - m\right|\right)} \]
    11. Taylor expanded in M around 0 84.3%

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

    if -9.99999999999999946e48 < m < -1.20000000000000009e-307

    1. Initial program 84.7%

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)}} \]
    4. Add Preprocessing
    5. 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|n - m\right|\right)} \]
    6. 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|n - m\right|\right)} \]
    7. Simplified94.2%

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

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

        \[\leadsto \cos M \cdot e^{\left(-{\left(\mathsf{log1p}\left(\mathsf{expm1}\left(\color{blue}{\left(m + n\right) \cdot \frac{1}{2}} - M\right)\right)\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
      3. metadata-eval93.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\left(-{\color{blue}{\log \left(e^{\mathsf{fma}\left(m + n, 0.5, M\right)}\right)}}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
    12. Taylor expanded in M around inf 64.1%

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

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

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

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

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

    if -1.20000000000000009e-307 < m < 4.2e17

    1. Initial program 80.2%

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

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

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

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

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

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

      \[\leadsto \cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \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|n - m\right|\right)} \]
    6. Step-by-step derivation
      1. +-commutative55.7%

        \[\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|n - m\right|\right)} \]
      2. unpow255.7%

        \[\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|n - m\right|\right)} \]
      3. distribute-rgt-out55.8%

        \[\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|n - m\right|\right)} \]
      4. *-commutative55.8%

        \[\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|n - m\right|\right)} \]
      5. *-commutative55.8%

        \[\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|n - m\right|\right)} \]
    7. Simplified59.2%

      \[\leadsto \cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \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|n - m\right|\right)} \]
    8. Step-by-step derivation
      1. frac-2neg59.2%

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(-\color{blue}{\left(n - M\right) \cdot M}\right)\right) - \left(\ell - \left|n - m\right|\right)} \]
      3. distribute-rgt-neg-in52.8%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -1 \cdot 10^{+49}:\\ \;\;\;\;\cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - 0.5 \cdot \left(m \cdot \left(n + m \cdot 0.5\right)\right)}\\ \mathbf{elif}\;m \leq -1.2 \cdot 10^{-307}:\\ \;\;\;\;\cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - M \cdot \left(\left(m + n\right) + M\right)}\\ \mathbf{elif}\;m \leq 4.2 \cdot 10^{+17}:\\ \;\;\;\;\cos \left(\left(m + n\right) \cdot \left(K \cdot 0.5\right)\right) \cdot e^{M \cdot \left(n - M\right) + \left(\left|n - m\right| - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - 0.5 \cdot \left(m \cdot \left(n + m \cdot 0.5\right)\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 62.8% 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|n - m\right| - \ell\\ t_1 := \cos M \cdot e^{M \cdot \left(n - M\right) + t_0}\\ \mathbf{if}\;M \leq -1.22 \cdot 10^{+32}:\\ \;\;\;\;t_1\\ \mathbf{elif}\;M \leq 4 \cdot 10^{-23}:\\ \;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right) + t_0}\\ \mathbf{elif}\;M \leq 4.8 \cdot 10^{+136}:\\ \;\;\;\;\cos M \cdot e^{t_0 - M \cdot \left(\left(m + n\right) + M\right)}\\ \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 (- (fabs (- n m)) l))
        (t_1 (* (cos M) (exp (+ (* M (- n M)) t_0)))))
   (if (<= M -1.22e+32)
     t_1
     (if (<= M 4e-23)
       (* (cos M) (exp (+ (* n (- M (* m 0.5))) t_0)))
       (if (<= M 4.8e+136)
         (* (cos M) (exp (- t_0 (* M (+ (+ m n) M)))))
         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 = fabs((n - m)) - l;
	double t_1 = cos(M) * exp(((M * (n - M)) + t_0));
	double tmp;
	if (M <= -1.22e+32) {
		tmp = t_1;
	} else if (M <= 4e-23) {
		tmp = cos(M) * exp(((n * (M - (m * 0.5))) + t_0));
	} else if (M <= 4.8e+136) {
		tmp = cos(M) * exp((t_0 - (M * ((m + n) + M))));
	} 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) :: tmp
    t_0 = abs((n - m)) - l
    t_1 = cos(m_1) * exp(((m_1 * (n - m_1)) + t_0))
    if (m_1 <= (-1.22d+32)) then
        tmp = t_1
    else if (m_1 <= 4d-23) then
        tmp = cos(m_1) * exp(((n * (m_1 - (m * 0.5d0))) + t_0))
    else if (m_1 <= 4.8d+136) then
        tmp = cos(m_1) * exp((t_0 - (m_1 * ((m + n) + m_1))))
    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.abs((n - m)) - l;
	double t_1 = Math.cos(M) * Math.exp(((M * (n - M)) + t_0));
	double tmp;
	if (M <= -1.22e+32) {
		tmp = t_1;
	} else if (M <= 4e-23) {
		tmp = Math.cos(M) * Math.exp(((n * (M - (m * 0.5))) + t_0));
	} else if (M <= 4.8e+136) {
		tmp = Math.cos(M) * Math.exp((t_0 - (M * ((m + n) + M))));
	} 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.fabs((n - m)) - l
	t_1 = math.cos(M) * math.exp(((M * (n - M)) + t_0))
	tmp = 0
	if M <= -1.22e+32:
		tmp = t_1
	elif M <= 4e-23:
		tmp = math.cos(M) * math.exp(((n * (M - (m * 0.5))) + t_0))
	elif M <= 4.8e+136:
		tmp = math.cos(M) * math.exp((t_0 - (M * ((m + n) + M))))
	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 = Float64(abs(Float64(n - m)) - l)
	t_1 = Float64(cos(M) * exp(Float64(Float64(M * Float64(n - M)) + t_0)))
	tmp = 0.0
	if (M <= -1.22e+32)
		tmp = t_1;
	elseif (M <= 4e-23)
		tmp = Float64(cos(M) * exp(Float64(Float64(n * Float64(M - Float64(m * 0.5))) + t_0)));
	elseif (M <= 4.8e+136)
		tmp = Float64(cos(M) * exp(Float64(t_0 - Float64(M * Float64(Float64(m + n) + M)))));
	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 = abs((n - m)) - l;
	t_1 = cos(M) * exp(((M * (n - M)) + t_0));
	tmp = 0.0;
	if (M <= -1.22e+32)
		tmp = t_1;
	elseif (M <= 4e-23)
		tmp = cos(M) * exp(((n * (M - (m * 0.5))) + t_0));
	elseif (M <= 4.8e+136)
		tmp = cos(M) * exp((t_0 - (M * ((m + n) + M))));
	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[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(M * N[(n - M), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[M, -1.22e+32], t$95$1, If[LessEqual[M, 4e-23], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(n * N[(M - N[(m * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[M, 4.8e+136], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$0 - N[(M * N[(N[(m + n), $MachinePrecision] + M), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$1]]]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \left|n - m\right| - \ell\\
t_1 := \cos M \cdot e^{M \cdot \left(n - M\right) + t_0}\\
\mathbf{if}\;M \leq -1.22 \cdot 10^{+32}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;M \leq 4 \cdot 10^{-23}:\\
\;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right) + t_0}\\

\mathbf{elif}\;M \leq 4.8 \cdot 10^{+136}:\\
\;\;\;\;\cos M \cdot e^{t_0 - M \cdot \left(\left(m + n\right) + M\right)}\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if M < -1.22000000000000002e32 or 4.8000000000000001e136 < M

    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. Step-by-step derivation
      1. associate-/l*76.5%

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

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

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

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

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

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

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

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

      \[\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|n - m\right|\right)} \]
    9. Step-by-step derivation
      1. +-commutative78.9%

        \[\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|n - m\right|\right)} \]
      2. unpow278.9%

        \[\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|n - m\right|\right)} \]
      3. distribute-rgt-out84.9%

        \[\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|n - m\right|\right)} \]
      4. *-commutative84.9%

        \[\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|n - m\right|\right)} \]
      5. *-commutative84.9%

        \[\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|n - m\right|\right)} \]
    10. Simplified84.9%

      \[\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|n - m\right|\right)} \]
    11. Taylor expanded in m around 0 80.3%

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

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

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

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

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

    if -1.22000000000000002e32 < M < 3.99999999999999984e-23

    1. Initial program 75.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. Step-by-step derivation
      1. associate-/l*76.3%

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

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

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

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

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

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

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

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

      \[\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|n - m\right|\right)} \]
    9. Step-by-step derivation
      1. +-commutative58.2%

        \[\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|n - m\right|\right)} \]
      2. unpow258.2%

        \[\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|n - m\right|\right)} \]
      3. distribute-rgt-out61.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|n - m\right|\right)} \]
      4. *-commutative61.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|n - m\right|\right)} \]
      5. *-commutative61.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|n - m\right|\right)} \]
    10. Simplified61.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|n - m\right|\right)} \]
    11. Taylor expanded in n around inf 39.2%

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

    if 3.99999999999999984e-23 < M < 4.8000000000000001e136

    1. Initial program 85.2%

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)}} \]
    4. Add Preprocessing
    5. 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|n - m\right|\right)} \]
    6. 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|n - m\right|\right)} \]
    7. Simplified100.0%

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-{\left(\mathsf{log1p}\left(\mathsf{expm1}\left(\color{blue}{\mathsf{fma}\left(m + n, 0.5, -M\right)}\right)\right)\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
    9. Applied egg-rr96.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\left(-{\color{blue}{\log \left(e^{\mathsf{fma}\left(m + n, 0.5, M\right)}\right)}}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
    12. Taylor expanded in M around inf 81.8%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -1.22 \cdot 10^{+32}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(n - M\right) + \left(\left|n - m\right| - \ell\right)}\\ \mathbf{elif}\;M \leq 4 \cdot 10^{-23}:\\ \;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right) + \left(\left|n - m\right| - \ell\right)}\\ \mathbf{elif}\;M \leq 4.8 \cdot 10^{+136}:\\ \;\;\;\;\cos M \cdot e^{\left(\left|n - m\right| - \ell\right) - M \cdot \left(\left(m + n\right) + M\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(n - M\right) + \left(\left|n - m\right| - \ell\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 76.8% 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|n - m\right| - \ell\\ \mathbf{if}\;m \leq -1 \cdot 10^{+49} \lor \neg \left(m \leq 4.2 \cdot 10^{+17}\right):\\ \;\;\;\;\cos M \cdot e^{t_0 - 0.5 \cdot \left(m \cdot \left(n + m \cdot 0.5\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{t_0 - M \cdot \left(\left(m + n\right) + M\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 (- n m)) l)))
   (if (or (<= m -1e+49) (not (<= m 4.2e+17)))
     (* (cos M) (exp (- t_0 (* 0.5 (* m (+ n (* m 0.5)))))))
     (* (cos M) (exp (- t_0 (* M (+ (+ m n) M))))))))
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((n - m)) - l;
	double tmp;
	if ((m <= -1e+49) || !(m <= 4.2e+17)) {
		tmp = cos(M) * exp((t_0 - (0.5 * (m * (n + (m * 0.5))))));
	} else {
		tmp = cos(M) * exp((t_0 - (M * ((m + n) + M))));
	}
	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 = abs((n - m)) - l
    if ((m <= (-1d+49)) .or. (.not. (m <= 4.2d+17))) then
        tmp = cos(m_1) * exp((t_0 - (0.5d0 * (m * (n + (m * 0.5d0))))))
    else
        tmp = cos(m_1) * exp((t_0 - (m_1 * ((m + n) + m_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.abs((n - m)) - l;
	double tmp;
	if ((m <= -1e+49) || !(m <= 4.2e+17)) {
		tmp = Math.cos(M) * Math.exp((t_0 - (0.5 * (m * (n + (m * 0.5))))));
	} else {
		tmp = Math.cos(M) * Math.exp((t_0 - (M * ((m + n) + M))));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.fabs((n - m)) - l
	tmp = 0
	if (m <= -1e+49) or not (m <= 4.2e+17):
		tmp = math.cos(M) * math.exp((t_0 - (0.5 * (m * (n + (m * 0.5))))))
	else:
		tmp = math.cos(M) * math.exp((t_0 - (M * ((m + n) + M))))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(abs(Float64(n - m)) - l)
	tmp = 0.0
	if ((m <= -1e+49) || !(m <= 4.2e+17))
		tmp = Float64(cos(M) * exp(Float64(t_0 - Float64(0.5 * Float64(m * Float64(n + Float64(m * 0.5)))))));
	else
		tmp = Float64(cos(M) * exp(Float64(t_0 - Float64(M * Float64(Float64(m + n) + M)))));
	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((n - m)) - l;
	tmp = 0.0;
	if ((m <= -1e+49) || ~((m <= 4.2e+17)))
		tmp = cos(M) * exp((t_0 - (0.5 * (m * (n + (m * 0.5))))));
	else
		tmp = cos(M) * exp((t_0 - (M * ((m + n) + M))));
	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[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, If[Or[LessEqual[m, -1e+49], N[Not[LessEqual[m, 4.2e+17]], $MachinePrecision]], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$0 - N[(0.5 * N[(m * N[(n + N[(m * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$0 - N[(M * N[(N[(m + n), $MachinePrecision] + M), $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|n - m\right| - \ell\\
\mathbf{if}\;m \leq -1 \cdot 10^{+49} \lor \neg \left(m \leq 4.2 \cdot 10^{+17}\right):\\
\;\;\;\;\cos M \cdot e^{t_0 - 0.5 \cdot \left(m \cdot \left(n + m \cdot 0.5\right)\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < -9.99999999999999946e48 or 4.2e17 < m

    1. Initial program 69.2%

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)}} \]
    4. Add Preprocessing
    5. 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}\right) - \left(\ell - \left|n - m\right|\right)} \]
    6. Step-by-step derivation
      1. cos-neg98.1%

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

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

      \[\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|n - m\right|\right)} \]
    9. Step-by-step derivation
      1. +-commutative77.7%

        \[\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|n - m\right|\right)} \]
      2. unpow277.7%

        \[\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|n - m\right|\right)} \]
      3. distribute-rgt-out84.3%

        \[\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|n - m\right|\right)} \]
      4. *-commutative84.3%

        \[\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|n - m\right|\right)} \]
      5. *-commutative84.3%

        \[\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|n - m\right|\right)} \]
    10. Simplified84.3%

      \[\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|n - m\right|\right)} \]
    11. Taylor expanded in M around 0 84.3%

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

    if -9.99999999999999946e48 < m < 4.2e17

    1. Initial program 82.7%

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-{\left(\mathsf{log1p}\left(\mathsf{expm1}\left(\color{blue}{\left(m + n\right) \cdot \frac{1}{2}} - M\right)\right)\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
      3. metadata-eval88.9%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-{\log \left(e^{\mathsf{fma}\left(m + n, 0.5, \color{blue}{\sqrt{\left(-M\right) \cdot \left(-M\right)}}\right)}\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
      11. sqr-neg88.9%

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

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

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

      \[\leadsto \cos M \cdot e^{\left(-{\color{blue}{\log \left(e^{\mathsf{fma}\left(m + n, 0.5, M\right)}\right)}}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
    12. Taylor expanded in M around inf 61.0%

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

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

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

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

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

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

Alternative 8: 92.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|n - m\right| - \ell\\ \mathbf{if}\;m \leq -6.5 \cdot 10^{+43}:\\ \;\;\;\;\cos M \cdot e^{\left(m \cdot 0.5 - M\right) \cdot \left(\left(M - m \cdot 0.5\right) - n\right) + t_0}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) + t_0}\\ \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 (- n m)) l)))
   (if (<= m -6.5e+43)
     (* (cos M) (exp (+ (* (- (* m 0.5) M) (- (- M (* m 0.5)) n)) t_0)))
     (* (cos M) (exp (+ (* (- (* n 0.5) M) (- (- M (* n 0.5)) m)) t_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 = fabs((n - m)) - l;
	double tmp;
	if (m <= -6.5e+43) {
		tmp = cos(M) * exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + t_0));
	} else {
		tmp = cos(M) * exp(((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) + t_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) :: tmp
    t_0 = abs((n - m)) - l
    if (m <= (-6.5d+43)) then
        tmp = cos(m_1) * exp(((((m * 0.5d0) - m_1) * ((m_1 - (m * 0.5d0)) - n)) + t_0))
    else
        tmp = cos(m_1) * exp(((((n * 0.5d0) - m_1) * ((m_1 - (n * 0.5d0)) - m)) + t_0))
    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.abs((n - m)) - l;
	double tmp;
	if (m <= -6.5e+43) {
		tmp = Math.cos(M) * Math.exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + t_0));
	} else {
		tmp = Math.cos(M) * Math.exp(((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) + t_0));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.fabs((n - m)) - l
	tmp = 0
	if m <= -6.5e+43:
		tmp = math.cos(M) * math.exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + t_0))
	else:
		tmp = math.cos(M) * math.exp(((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) + t_0))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(abs(Float64(n - m)) - l)
	tmp = 0.0
	if (m <= -6.5e+43)
		tmp = Float64(cos(M) * exp(Float64(Float64(Float64(Float64(m * 0.5) - M) * Float64(Float64(M - Float64(m * 0.5)) - n)) + t_0)));
	else
		tmp = Float64(cos(M) * exp(Float64(Float64(Float64(Float64(n * 0.5) - M) * Float64(Float64(M - Float64(n * 0.5)) - m)) + t_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 = abs((n - m)) - l;
	tmp = 0.0;
	if (m <= -6.5e+43)
		tmp = cos(M) * exp(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + t_0));
	else
		tmp = cos(M) * exp(((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) + t_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[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, If[LessEqual[m, -6.5e+43], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[(N[(m * 0.5), $MachinePrecision] - M), $MachinePrecision] * N[(N[(M - N[(m * 0.5), $MachinePrecision]), $MachinePrecision] - n), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(N[(N[(n * 0.5), $MachinePrecision] - M), $MachinePrecision] * N[(N[(M - N[(n * 0.5), $MachinePrecision]), $MachinePrecision] - m), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \left|n - m\right| - \ell\\
\mathbf{if}\;m \leq -6.5 \cdot 10^{+43}:\\
\;\;\;\;\cos M \cdot e^{\left(m \cdot 0.5 - M\right) \cdot \left(\left(M - m \cdot 0.5\right) - n\right) + t_0}\\

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


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

    1. Initial program 71.4%

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)}} \]
    4. Add Preprocessing
    5. 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|n - m\right|\right)} \]
    6. 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|n - m\right|\right)} \]
    7. Simplified100.0%

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

      \[\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|n - m\right|\right)} \]
    9. Step-by-step derivation
      1. +-commutative85.8%

        \[\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|n - m\right|\right)} \]
      2. unpow285.8%

        \[\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|n - m\right|\right)} \]
      3. distribute-rgt-out94.0%

        \[\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|n - m\right|\right)} \]
      4. *-commutative94.0%

        \[\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|n - m\right|\right)} \]
      5. *-commutative94.0%

        \[\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|n - m\right|\right)} \]
    10. Simplified94.0%

      \[\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|n - m\right|\right)} \]

    if -6.4999999999999998e43 < m

    1. Initial program 78.3%

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

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

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

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

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

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

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

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

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

      \[\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|n - m\right|\right)} \]
    9. Step-by-step derivation
      1. +-commutative77.1%

        \[\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|n - m\right|\right)} \]
      2. unpow277.1%

        \[\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|n - m\right|\right)} \]
      3. distribute-rgt-out82.4%

        \[\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|n - m\right|\right)} \]
      4. *-commutative82.4%

        \[\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|n - m\right|\right)} \]
      5. *-commutative82.4%

        \[\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|n - m\right|\right)} \]
    10. Simplified82.4%

      \[\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|n - m\right|\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification84.6%

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

Alternative 9: 79.1% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \cos M \cdot e^{\left(m \cdot 0.5 - M\right) \cdot \left(\left(M - m \cdot 0.5\right) - n\right) + \left(\left|n - m\right| - \ell\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
 (*
  (cos M)
  (exp (+ (* (- (* m 0.5) M) (- (- M (* m 0.5)) n)) (- (fabs (- n m)) 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(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + (fabs((n - m)) - 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(((((m * 0.5d0) - m_1) * ((m_1 - (m * 0.5d0)) - n)) + (abs((n - m)) - 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(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + (Math.abs((n - m)) - 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(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + (math.fabs((n - m)) - 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(Float64(m * 0.5) - M) * Float64(Float64(M - Float64(m * 0.5)) - n)) + Float64(abs(Float64(n - m)) - 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(((((m * 0.5) - M) * ((M - (m * 0.5)) - n)) + (abs((n - m)) - 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 * 0.5), $MachinePrecision] - M), $MachinePrecision] * N[(N[(M - N[(m * 0.5), $MachinePrecision]), $MachinePrecision] - n), $MachinePrecision]), $MachinePrecision] + N[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\cos M \cdot e^{\left(m \cdot 0.5 - M\right) \cdot \left(\left(M - m \cdot 0.5\right) - n\right) + \left(\left|n - m\right| - \ell\right)}
\end{array}
Derivation
  1. Initial program 77.0%

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
  8. Taylor expanded in n around 0 66.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|n - m\right|\right)} \]
  9. Step-by-step derivation
    1. +-commutative66.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|n - m\right|\right)} \]
    2. unpow266.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|n - m\right|\right)} \]
    3. distribute-rgt-out70.8%

      \[\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|n - m\right|\right)} \]
    4. *-commutative70.8%

      \[\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|n - m\right|\right)} \]
    5. *-commutative70.8%

      \[\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|n - m\right|\right)} \]
  10. Simplified70.8%

    \[\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|n - m\right|\right)} \]
  11. Final simplification70.8%

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

Alternative 10: 56.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|n - m\right| - \ell\\ \mathbf{if}\;\ell \leq -8 \cdot 10^{-20}:\\ \;\;\;\;\cos M \cdot e^{t_0 - M \cdot \left(\left(m + n\right) + M\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(n - M\right) + t_0}\\ \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 (- n m)) l)))
   (if (<= l -8e-20)
     (* (cos M) (exp (- t_0 (* M (+ (+ m n) M)))))
     (* (cos M) (exp (+ (* M (- n M)) t_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 = fabs((n - m)) - l;
	double tmp;
	if (l <= -8e-20) {
		tmp = cos(M) * exp((t_0 - (M * ((m + n) + M))));
	} else {
		tmp = cos(M) * exp(((M * (n - M)) + t_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) :: tmp
    t_0 = abs((n - m)) - l
    if (l <= (-8d-20)) then
        tmp = cos(m_1) * exp((t_0 - (m_1 * ((m + n) + m_1))))
    else
        tmp = cos(m_1) * exp(((m_1 * (n - m_1)) + t_0))
    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.abs((n - m)) - l;
	double tmp;
	if (l <= -8e-20) {
		tmp = Math.cos(M) * Math.exp((t_0 - (M * ((m + n) + M))));
	} else {
		tmp = Math.cos(M) * Math.exp(((M * (n - M)) + t_0));
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	t_0 = math.fabs((n - m)) - l
	tmp = 0
	if l <= -8e-20:
		tmp = math.cos(M) * math.exp((t_0 - (M * ((m + n) + M))))
	else:
		tmp = math.cos(M) * math.exp(((M * (n - M)) + t_0))
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	t_0 = Float64(abs(Float64(n - m)) - l)
	tmp = 0.0
	if (l <= -8e-20)
		tmp = Float64(cos(M) * exp(Float64(t_0 - Float64(M * Float64(Float64(m + n) + M)))));
	else
		tmp = Float64(cos(M) * exp(Float64(Float64(M * Float64(n - M)) + t_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 = abs((n - m)) - l;
	tmp = 0.0;
	if (l <= -8e-20)
		tmp = cos(M) * exp((t_0 - (M * ((m + n) + M))));
	else
		tmp = cos(M) * exp(((M * (n - M)) + t_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[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, If[LessEqual[l, -8e-20], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(t$95$0 - N[(M * N[(N[(m + n), $MachinePrecision] + M), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(M * N[(n - M), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
t_0 := \left|n - m\right| - \ell\\
\mathbf{if}\;\ell \leq -8 \cdot 10^{-20}:\\
\;\;\;\;\cos M \cdot e^{t_0 - M \cdot \left(\left(m + n\right) + M\right)}\\

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


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

    1. Initial program 75.0%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-{\left(\mathsf{log1p}\left(\mathsf{expm1}\left(\color{blue}{\mathsf{fma}\left(m + n, 0.5, -M\right)}\right)\right)\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
    9. Applied egg-rr78.4%

      \[\leadsto \cos M \cdot e^{\left(-{\color{blue}{\left(\mathsf{log1p}\left(\mathsf{expm1}\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)\right)\right)}}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
    10. Step-by-step derivation
      1. log1p-expm1-u84.4%

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-{\log \left(e^{\mathsf{fma}\left(m + n, 0.5, \color{blue}{\sqrt{\left(-M\right) \cdot \left(-M\right)}}\right)}\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
      11. sqr-neg78.4%

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

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

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

      \[\leadsto \cos M \cdot e^{\left(-{\color{blue}{\log \left(e^{\mathsf{fma}\left(m + n, 0.5, M\right)}\right)}}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
    12. Taylor expanded in M around inf 42.8%

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

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

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

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

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

    if -7.99999999999999956e-20 < l

    1. Initial program 77.7%

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(\frac{K}{\frac{2}{m + n}} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)}} \]
    4. Add Preprocessing
    5. 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}\right) - \left(\ell - \left|n - m\right|\right)} \]
    6. Step-by-step derivation
      1. cos-neg97.0%

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

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

      \[\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|n - m\right|\right)} \]
    9. Step-by-step derivation
      1. +-commutative68.2%

        \[\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|n - m\right|\right)} \]
      2. unpow268.2%

        \[\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|n - m\right|\right)} \]
      3. distribute-rgt-out72.9%

        \[\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|n - m\right|\right)} \]
      4. *-commutative72.9%

        \[\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|n - m\right|\right)} \]
      5. *-commutative72.9%

        \[\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|n - m\right|\right)} \]
    10. Simplified72.9%

      \[\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|n - m\right|\right)} \]
    11. Taylor expanded in m around 0 55.0%

      \[\leadsto \cos M \cdot e^{\left(-\color{blue}{-1 \cdot \left(M \cdot \left(n - M\right)\right)}\right) - \left(\ell - \left|n - m\right|\right)} \]
    12. Step-by-step derivation
      1. mul-1-neg55.0%

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

        \[\leadsto \cos M \cdot e^{\left(-\left(-\color{blue}{\left(n - M\right) \cdot M}\right)\right) - \left(\ell - \left|n - m\right|\right)} \]
      3. distribute-rgt-neg-in55.0%

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

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

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

Alternative 11: 56.4% accurate, 1.4× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \cos M \cdot e^{M \cdot \left(n - M\right) + \left(\left|n - m\right| - \ell\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
 (* (cos M) (exp (+ (* M (- n M)) (- (fabs (- n m)) 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(((M * (n - M)) + (fabs((n - m)) - 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(((m_1 * (n - m_1)) + (abs((n - m)) - 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(((M * (n - M)) + (Math.abs((n - m)) - 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(((M * (n - M)) + (math.fabs((n - m)) - 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(M * Float64(n - M)) + Float64(abs(Float64(n - m)) - 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(((M * (n - M)) + (abs((n - m)) - 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[(M * N[(n - M), $MachinePrecision]), $MachinePrecision] + N[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\cos M \cdot e^{M \cdot \left(n - M\right) + \left(\left|n - m\right| - \ell\right)}
\end{array}
Derivation
  1. Initial program 77.0%

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|n - m\right|\right)} \]
  8. Taylor expanded in n around 0 66.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|n - m\right|\right)} \]
  9. Step-by-step derivation
    1. +-commutative66.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|n - m\right|\right)} \]
    2. unpow266.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|n - m\right|\right)} \]
    3. distribute-rgt-out70.8%

      \[\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|n - m\right|\right)} \]
    4. *-commutative70.8%

      \[\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|n - m\right|\right)} \]
    5. *-commutative70.8%

      \[\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|n - m\right|\right)} \]
  10. Simplified70.8%

    \[\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|n - m\right|\right)} \]
  11. Taylor expanded in m around 0 50.4%

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

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

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

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

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

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

Reproduce

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