Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.1% → 95.0%
Time: 24.7s
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.1% accurate, 1.0× speedup?

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

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

Alternative 1: 95.0% accurate, 1.3× speedup?

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

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


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

    1. Initial program 67.9%

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out88.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|m - n\right|\right)} \]
      4. *-commutative88.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|m - n\right|\right)} \]
      5. *-commutative88.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|m - n\right|\right)} \]
    8. Simplified88.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|m - n\right|\right)} \]
    9. Taylor expanded in m around inf 100.0%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
    11. Simplified100.0%

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

    if -2.7000000000000001e49 < m

    1. Initial program 75.5%

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(0.5 \cdot n - M\right)} + m \cdot \left(0.5 \cdot n - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out87.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|m - n\right|\right)} \]
      4. *-commutative87.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|m - n\right|\right)} \]
      5. *-commutative87.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|m - n\right|\right)} \]
    8. Simplified87.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|m - n\right|\right)} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification90.0%

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -2.7 \cdot 10^{+49}:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \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|m - n\right| - \ell\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 96.6% accurate, 1.0× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \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 (- (- (fabs (- m n)) l) (pow (- (/ (+ m n) 2.0) M) 2.0)))))
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(((fabs((m - n)) - l) - pow((((m + n) / 2.0) - M), 2.0)));
}
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(((abs((m - n)) - l) - ((((m + n) / 2.0d0) - m_1) ** 2.0d0)))
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(((Math.abs((m - n)) - l) - Math.pow((((m + n) / 2.0) - M), 2.0)));
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.cos(M) * math.exp(((math.fabs((m - n)) - l) - math.pow((((m + n) / 2.0) - M), 2.0)))
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(abs(Float64(m - n)) - l) - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0))))
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(((abs((m - n)) - l) - ((((m + n) / 2.0) - M) ^ 2.0)));
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[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}
\end{array}
Derivation
  1. Initial program 73.9%

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

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

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

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

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

Alternative 3: 82.4% accurate, 1.3× speedup?

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

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

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


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

    1. Initial program 64.8%

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out81.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|m - n\right|\right)} \]
      4. *-commutative81.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|m - n\right|\right)} \]
      5. *-commutative81.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|m - n\right|\right)} \]
    8. Simplified81.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|m - n\right|\right)} \]
    9. Taylor expanded in m around inf 97.2%

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

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

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

    if -0.299999999999999989 < m < 2.29999999999999996e-86

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.29999999999999996e-86 < m

    1. Initial program 73.0%

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

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

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

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in n around 0 73.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|m - n\right|\right)} \]
    7. Step-by-step derivation
      1. +-commutative73.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|m - n\right|\right)} \]
      2. unpow273.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|m - n\right|\right)} \]
      3. distribute-rgt-out76.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|m - n\right|\right)} \]
      4. *-commutative76.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|m - n\right|\right)} \]
      5. *-commutative76.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|m - n\right|\right)} \]
    8. Simplified76.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|m - n\right|\right)} \]
    9. Taylor expanded in m around inf 60.1%

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

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

Alternative 4: 81.6% accurate, 1.3× speedup?

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < 1.16000000000000007e-14

    1. Initial program 71.5%

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

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

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

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

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

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

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

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

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

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

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

    if 1.16000000000000007e-14 < M

    1. Initial program 80.3%

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

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

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

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in 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|m - n\right|\right)} \]
    7. 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|m - n\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|m - n\right|\right)} \]
      3. distribute-rgt-out83.4%

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

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

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

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

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

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

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

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

Alternative 5: 74.6% accurate, 1.3× speedup?

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

\mathbf{elif}\;m \leq -1.52 \cdot 10^{-294}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;m \leq 1.15 \cdot 10^{-244}:\\
\;\;\;\;\cos M \cdot e^{-\ell}\\

\mathbf{elif}\;m \leq 5.2 \cdot 10^{-37}:\\
\;\;\;\;t\_0\\

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


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

    1. Initial program 64.8%

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out81.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|m - n\right|\right)} \]
      4. *-commutative81.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|m - n\right|\right)} \]
      5. *-commutative81.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|m - n\right|\right)} \]
    8. Simplified81.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|m - n\right|\right)} \]
    9. Taylor expanded in m around inf 97.2%

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

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

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

    if -0.299999999999999989 < m < -1.52000000000000007e-294 or 1.15e-244 < m < 5.19999999999999959e-37

    1. Initial program 83.8%

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    10. Step-by-step derivation
      1. mul-1-neg66.5%

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
    11. Simplified66.5%

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

    if -1.52000000000000007e-294 < m < 1.15e-244

    1. Initial program 73.6%

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - 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|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. +-commutative73.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|m - n\right|\right)} \]
      2. unpow273.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|m - n\right|\right)} \]
      3. distribute-rgt-out73.2%

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(-M\right) \cdot e^{-\ell}} \]
    10. Step-by-step derivation
      1. cos-neg73.4%

        \[\leadsto \color{blue}{\cos M} \cdot e^{-\ell} \]
    11. Simplified73.4%

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

    if 5.19999999999999959e-37 < m

    1. Initial program 68.8%

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

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

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

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in n around 0 75.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|m - n\right|\right)} \]
    7. Step-by-step derivation
      1. +-commutative75.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|m - n\right|\right)} \]
      2. unpow275.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|m - n\right|\right)} \]
      3. distribute-rgt-out78.4%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -0.3:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq -1.52 \cdot 10^{-294}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;m \leq 1.15 \cdot 10^{-244}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \mathbf{elif}\;m \leq 5.2 \cdot 10^{-37}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 81.5% accurate, 1.3× speedup?

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

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

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


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

    1. Initial program 64.8%

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out81.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|m - n\right|\right)} \]
      4. *-commutative81.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|m - n\right|\right)} \]
      5. *-commutative81.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|m - n\right|\right)} \]
    8. Simplified81.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|m - n\right|\right)} \]
    9. Taylor expanded in m around inf 97.2%

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

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

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

    if -0.299999999999999989 < m < 1.01999999999999997e-75

    1. Initial program 81.0%

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

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

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

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in n around 0 65.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|m - n\right|\right)} \]
    7. Step-by-step derivation
      1. +-commutative65.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|m - n\right|\right)} \]
      2. unpow265.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|m - n\right|\right)} \]
      3. distribute-rgt-out67.5%

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

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

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

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

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

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

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

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

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

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

    if 1.01999999999999997e-75 < m

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out76.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|m - n\right|\right)} \]
      4. *-commutative76.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|m - n\right|\right)} \]
      5. *-commutative76.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|m - n\right|\right)} \]
    8. Simplified76.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|m - n\right|\right)} \]
    9. Taylor expanded in n around inf 46.7%

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

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

Alternative 7: 72.3% accurate, 1.3× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;\ell \leq -0.0004:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{elif}\;\ell \leq 720:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (if (<= l -0.0004)
   (* (cos M) (exp l))
   (if (<= l 720.0)
     (* (cos M) (exp (- (pow M 2.0))))
     (* (cos M) (exp (- l))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -0.0004) {
		tmp = cos(M) * exp(l);
	} else if (l <= 720.0) {
		tmp = cos(M) * exp(-pow(M, 2.0));
	} else {
		tmp = cos(M) * exp(-l);
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (l <= (-0.0004d0)) then
        tmp = cos(m_1) * exp(l)
    else if (l <= 720.0d0) then
        tmp = cos(m_1) * exp(-(m_1 ** 2.0d0))
    else
        tmp = cos(m_1) * exp(-l)
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -0.0004) {
		tmp = Math.cos(M) * Math.exp(l);
	} else if (l <= 720.0) {
		tmp = Math.cos(M) * Math.exp(-Math.pow(M, 2.0));
	} else {
		tmp = Math.cos(M) * Math.exp(-l);
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if l <= -0.0004:
		tmp = math.cos(M) * math.exp(l)
	elif l <= 720.0:
		tmp = math.cos(M) * math.exp(-math.pow(M, 2.0))
	else:
		tmp = math.cos(M) * math.exp(-l)
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if (l <= -0.0004)
		tmp = Float64(cos(M) * exp(l));
	elseif (l <= 720.0)
		tmp = Float64(cos(M) * exp(Float64(-(M ^ 2.0))));
	else
		tmp = Float64(cos(M) * exp(Float64(-l)));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (l <= -0.0004)
		tmp = cos(M) * exp(l);
	elseif (l <= 720.0)
		tmp = cos(M) * exp(-(M ^ 2.0));
	else
		tmp = cos(M) * exp(-l);
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := If[LessEqual[l, -0.0004], N[(N[Cos[M], $MachinePrecision] * N[Exp[l], $MachinePrecision]), $MachinePrecision], If[LessEqual[l, 720.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-N[Power[M, 2.0], $MachinePrecision])], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -0.0004:\\
\;\;\;\;\cos M \cdot e^{\ell}\\

\mathbf{elif}\;\ell \leq 720:\\
\;\;\;\;\cos M \cdot e^{-{M}^{2}}\\

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


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

    1. Initial program 67.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -4.00000000000000019e-4 < l < 720

    1. Initial program 76.6%

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

      \[\leadsto \color{blue}{\cos M} \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. Taylor expanded in n around 0 67.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|m - n\right|\right)} \]
    7. Step-by-step derivation
      1. +-commutative67.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|m - n\right|\right)} \]
      2. unpow267.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|m - n\right|\right)} \]
      3. distribute-rgt-out72.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|m - n\right|\right)} \]
      4. *-commutative72.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|m - n\right|\right)} \]
      5. *-commutative72.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|m - n\right|\right)} \]
    8. Simplified72.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|m - n\right|\right)} \]
    9. Taylor expanded in M around inf 62.8%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
    11. Simplified62.8%

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

    if 720 < l

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - 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|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. +-commutative77.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|m - n\right|\right)} \]
      2. unpow277.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|m - n\right|\right)} \]
      3. distribute-rgt-out81.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|m - n\right|\right)} \]
      4. *-commutative81.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|m - n\right|\right)} \]
      5. *-commutative81.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|m - n\right|\right)} \]
    5. Simplified66.3%

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(-M\right) \cdot e^{-\ell}} \]
    10. Step-by-step derivation
      1. cos-neg100.0%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq -0.0004:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{elif}\;\ell \leq 720:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 64.3% accurate, 1.9× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;\ell \leq -0.0004:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{elif}\;\ell \leq 1.15 \cdot 10^{-5}:\\ \;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (if (<= l -0.0004)
   (* (cos M) (exp l))
   (if (<= l 1.15e-5)
     (* (cos M) (exp (* n (- M (* m 0.5)))))
     (* (cos M) (exp (- l))))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -0.0004) {
		tmp = cos(M) * exp(l);
	} else if (l <= 1.15e-5) {
		tmp = cos(M) * exp((n * (M - (m * 0.5))));
	} else {
		tmp = cos(M) * exp(-l);
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (l <= (-0.0004d0)) then
        tmp = cos(m_1) * exp(l)
    else if (l <= 1.15d-5) then
        tmp = cos(m_1) * exp((n * (m_1 - (m * 0.5d0))))
    else
        tmp = cos(m_1) * exp(-l)
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -0.0004) {
		tmp = Math.cos(M) * Math.exp(l);
	} else if (l <= 1.15e-5) {
		tmp = Math.cos(M) * Math.exp((n * (M - (m * 0.5))));
	} else {
		tmp = Math.cos(M) * Math.exp(-l);
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if l <= -0.0004:
		tmp = math.cos(M) * math.exp(l)
	elif l <= 1.15e-5:
		tmp = math.cos(M) * math.exp((n * (M - (m * 0.5))))
	else:
		tmp = math.cos(M) * math.exp(-l)
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if (l <= -0.0004)
		tmp = Float64(cos(M) * exp(l));
	elseif (l <= 1.15e-5)
		tmp = Float64(cos(M) * exp(Float64(n * Float64(M - Float64(m * 0.5)))));
	else
		tmp = Float64(cos(M) * exp(Float64(-l)));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (l <= -0.0004)
		tmp = cos(M) * exp(l);
	elseif (l <= 1.15e-5)
		tmp = cos(M) * exp((n * (M - (m * 0.5))));
	else
		tmp = cos(M) * exp(-l);
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := If[LessEqual[l, -0.0004], N[(N[Cos[M], $MachinePrecision] * N[Exp[l], $MachinePrecision]), $MachinePrecision], If[LessEqual[l, 1.15e-5], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(n * N[(M - N[(m * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -0.0004:\\
\;\;\;\;\cos M \cdot e^{\ell}\\

\mathbf{elif}\;\ell \leq 1.15 \cdot 10^{-5}:\\
\;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right)}\\

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


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

    1. Initial program 67.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -4.00000000000000019e-4 < l < 1.15e-5

    1. Initial program 76.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. Add Preprocessing
    3. 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|m - n\right|\right)} \]
    4. 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|m - n\right|\right)} \]
    5. Simplified96.0%

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out71.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|m - n\right|\right)} \]
      4. *-commutative71.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|m - n\right|\right)} \]
      5. *-commutative71.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|m - n\right|\right)} \]
    8. Simplified71.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|m - n\right|\right)} \]
    9. Taylor expanded in n around inf 45.2%

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

    if 1.15e-5 < l

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-\ell} \]
    11. Simplified98.5%

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

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

Alternative 9: 48.8% accurate, 2.0× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \begin{array}{l} \mathbf{if}\;\ell \leq -5 \cdot 10^{-227}:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l)
 :precision binary64
 (if (<= l -5e-227) (* (cos M) (exp l)) (* (cos M) (exp (- l)))))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -5e-227) {
		tmp = cos(M) * exp(l);
	} else {
		tmp = cos(M) * exp(-l);
	}
	return tmp;
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (l <= (-5d-227)) then
        tmp = cos(m_1) * exp(l)
    else
        tmp = cos(m_1) * exp(-l)
    end if
    code = tmp
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -5e-227) {
		tmp = Math.cos(M) * Math.exp(l);
	} else {
		tmp = Math.cos(M) * Math.exp(-l);
	}
	return tmp;
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	tmp = 0
	if l <= -5e-227:
		tmp = math.cos(M) * math.exp(l)
	else:
		tmp = math.cos(M) * math.exp(-l)
	return tmp
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	tmp = 0.0
	if (l <= -5e-227)
		tmp = Float64(cos(M) * exp(l));
	else
		tmp = Float64(cos(M) * exp(Float64(-l)));
	end
	return tmp
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (l <= -5e-227)
		tmp = cos(M) * exp(l);
	else
		tmp = cos(M) * exp(-l);
	end
	tmp_2 = tmp;
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := If[LessEqual[l, -5e-227], N[(N[Cos[M], $MachinePrecision] * N[Exp[l], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -5 \cdot 10^{-227}:\\
\;\;\;\;\cos M \cdot e^{\ell}\\

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


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

    1. Initial program 68.2%

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out69.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|m - n\right|\right)} \]
      4. *-commutative69.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|m - n\right|\right)} \]
      5. *-commutative69.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|m - n\right|\right)} \]
    5. Simplified56.2%

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(-M\right) \cdot e^{\ell}} \]
    12. Step-by-step derivation
      1. cos-neg48.9%

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

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

    if -4.99999999999999961e-227 < l

    1. Initial program 78.6%

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - 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|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. +-commutative72.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|m - n\right|\right)} \]
      2. unpow272.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|m - n\right|\right)} \]
      3. distribute-rgt-out77.2%

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(-M\right) \cdot e^{-\ell}} \]
    10. Step-by-step derivation
      1. cos-neg52.7%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq -5 \cdot 10^{-227}:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \]
  5. Add Preprocessing

Alternative 10: 24.7% accurate, 2.1× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \cos M \cdot e^{\ell} \end{array} \]
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
(FPCore (K m n M l) :precision binary64 (* (cos M) (exp 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(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(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(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(l)
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return Float64(cos(M) * exp(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(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[l], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\cos M \cdot e^{\ell}
\end{array}
Derivation
  1. Initial program 73.9%

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

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

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

      \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
    3. distribute-rgt-out73.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|m - n\right|\right)} \]
    4. *-commutative73.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|m - n\right|\right)} \]
    5. *-commutative73.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|m - n\right|\right)} \]
  5. Simplified61.6%

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos M \cdot e^{\ell}} \]
  14. Final simplification26.0%

    \[\leadsto \cos M \cdot e^{\ell} \]
  15. Add Preprocessing

Alternative 11: 7.1% accurate, 4.2× speedup?

\[\begin{array}{l} [K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\ \\ \cos \left(-M\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)))
assert(K < m && m < n && n < M && M < l);
double code(double K, double m, double n, double M, double l) {
	return cos(-M);
}
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = cos(-m_1)
end function
assert K < m && m < n && n < M && M < l;
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(-M);
}
[K, m, n, M, l] = sort([K, m, n, M, l])
def code(K, m, n, M, l):
	return math.cos(-M)
K, m, n, M, l = sort([K, m, n, M, l])
function code(K, m, n, M, l)
	return cos(Float64(-M))
end
K, m, n, M, l = num2cell(sort([K, m, n, M, l])){:}
function tmp = code(K, m, n, M, l)
	tmp = cos(-M);
end
NOTE: K, m, n, M, and l should be sorted in increasing order before calling this function.
code[K_, m_, n_, M_, l_] := N[Cos[(-M)], $MachinePrecision]
\begin{array}{l}
[K, m, n, M, l] = \mathsf{sort}([K, m, n, M, l])\\
\\
\cos \left(-M\right)
\end{array}
Derivation
  1. Initial program 73.9%

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

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

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

      \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
    3. distribute-rgt-out73.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|m - n\right|\right)} \]
    4. *-commutative73.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|m - n\right|\right)} \]
    5. *-commutative73.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|m - n\right|\right)} \]
  5. Simplified61.6%

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

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

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

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

    \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot \left(m + n\right)\right) - M\right)} \]
  10. Step-by-step derivation
    1. associate-*r*6.9%

      \[\leadsto \cos \left(\color{blue}{\left(0.5 \cdot K\right) \cdot \left(m + n\right)} - M\right) \]
    2. fma-neg6.9%

      \[\leadsto \cos \color{blue}{\left(\mathsf{fma}\left(0.5 \cdot K, m + n, -M\right)\right)} \]
    3. +-commutative6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, \color{blue}{n + m}, -M\right)\right) \]
    4. *-lft-identity6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, n + \color{blue}{1 \cdot m}, -M\right)\right) \]
    5. metadata-eval6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, n + \color{blue}{\left(--1\right)} \cdot m, -M\right)\right) \]
    6. cancel-sign-sub-inv6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, \color{blue}{n - -1 \cdot m}, -M\right)\right) \]
    7. fma-neg6.9%

      \[\leadsto \cos \color{blue}{\left(\left(0.5 \cdot K\right) \cdot \left(n - -1 \cdot m\right) - M\right)} \]
    8. associate-*r*6.9%

      \[\leadsto \cos \left(\color{blue}{0.5 \cdot \left(K \cdot \left(n - -1 \cdot m\right)\right)} - M\right) \]
    9. associate-*r*6.9%

      \[\leadsto \cos \left(\color{blue}{\left(0.5 \cdot K\right) \cdot \left(n - -1 \cdot m\right)} - M\right) \]
    10. fma-neg6.9%

      \[\leadsto \cos \color{blue}{\left(\mathsf{fma}\left(0.5 \cdot K, n - -1 \cdot m, -M\right)\right)} \]
    11. cancel-sign-sub-inv6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, \color{blue}{n + \left(--1\right) \cdot m}, -M\right)\right) \]
    12. metadata-eval6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, n + \color{blue}{1} \cdot m, -M\right)\right) \]
    13. *-lft-identity6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, n + \color{blue}{m}, -M\right)\right) \]
    14. +-commutative6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, \color{blue}{m + n}, -M\right)\right) \]
    15. *-lft-identity6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, m + \color{blue}{1 \cdot n}, -M\right)\right) \]
    16. metadata-eval6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, m + \color{blue}{\left(--1\right)} \cdot n, -M\right)\right) \]
    17. cancel-sign-sub-inv6.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, \color{blue}{m - -1 \cdot n}, -M\right)\right) \]
    18. fma-neg6.9%

      \[\leadsto \cos \color{blue}{\left(\left(0.5 \cdot K\right) \cdot \left(m - -1 \cdot n\right) - M\right)} \]
  11. Simplified6.9%

    \[\leadsto \color{blue}{\cos \left(\left(K \cdot 0.5\right) \cdot \left(n + m\right) - M\right)} \]
  12. Taylor expanded in K around 0 7.1%

    \[\leadsto \cos \color{blue}{\left(-1 \cdot M\right)} \]
  13. Step-by-step derivation
    1. neg-mul-17.1%

      \[\leadsto \cos \color{blue}{\left(-M\right)} \]
  14. Simplified7.1%

    \[\leadsto \cos \color{blue}{\left(-M\right)} \]
  15. Final simplification7.1%

    \[\leadsto \cos \left(-M\right) \]
  16. Add Preprocessing

Reproduce

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