Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.7% → 96.8%
Time: 35.8s
Alternatives: 9
Speedup: 1.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 9 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.7% accurate, 1.0× speedup?

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

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

Alternative 1: 96.8% accurate, 1.0× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 63.2% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := \cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\
\mathbf{if}\;\ell \leq -3.8 \cdot 10^{+159}:\\
\;\;\;\;t_0\\

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

\mathbf{elif}\;\ell \leq 5.3 \cdot 10^{-17}:\\
\;\;\;\;t_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if l < -3.79999999999999965e159 or -1.5000000000000001e-29 < l < 5.2999999999999998e-17

    1. Initial program 66.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -3.79999999999999965e159 < l < -1.5000000000000001e-29

    1. Initial program 81.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos \left(\left(K \cdot 0.5\right) \cdot \left(m + n\right) - M\right) \cdot e^{-\ell}\right)} - 1} \]
      3. associate-*l*6.1%

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

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

        \[\leadsto e^{\mathsf{log1p}\left(\cos \left(K \cdot \color{blue}{\left(\left(n + m\right) \cdot 0.5\right)} - M\right) \cdot e^{-\ell}\right)} - 1 \]
      6. add-sqr-sqrt6.1%

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

        \[\leadsto e^{\mathsf{log1p}\left(\cos \left(K \cdot \left(\left(n + m\right) \cdot 0.5\right) - M\right) \cdot e^{\color{blue}{\sqrt{\left(-\ell\right) \cdot \left(-\ell\right)}}}\right)} - 1 \]
      8. sqr-neg6.1%

        \[\leadsto e^{\mathsf{log1p}\left(\cos \left(K \cdot \left(\left(n + m\right) \cdot 0.5\right) - M\right) \cdot e^{\sqrt{\color{blue}{\ell \cdot \ell}}}\right)} - 1 \]
      9. sqrt-unprod0.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos \left(K \cdot \left(\left(n + m\right) \cdot 0.5\right) - M\right) \cdot e^{\color{blue}{\sqrt{\ell} \cdot \sqrt{\ell}}}\right)} - 1 \]
      10. add-sqr-sqrt63.7%

        \[\leadsto e^{\mathsf{log1p}\left(\cos \left(K \cdot \left(\left(n + m\right) \cdot 0.5\right) - M\right) \cdot e^{\color{blue}{\ell}}\right)} - 1 \]
    8. Applied egg-rr63.7%

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

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

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

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

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

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

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

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

    if 5.2999999999999998e-17 < l

    1. Initial program 81.2%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{\cos \color{blue}{\left(0.5 \cdot \left(n \cdot K\right)\right)}}{e^{\ell}} \]
    6. Step-by-step derivation
      1. *-commutative87.0%

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

        \[\leadsto \frac{\cos \color{blue}{\left(\left(0.5 \cdot K\right) \cdot n\right)}}{e^{\ell}} \]
      3. *-commutative87.0%

        \[\leadsto \frac{\cos \left(\color{blue}{\left(K \cdot 0.5\right)} \cdot n\right)}{e^{\ell}} \]
      4. associate-*l*87.0%

        \[\leadsto \frac{\cos \color{blue}{\left(K \cdot \left(0.5 \cdot n\right)\right)}}{e^{\ell}} \]
    7. Simplified87.0%

      \[\leadsto \frac{\cos \color{blue}{\left(K \cdot \left(0.5 \cdot n\right)\right)}}{e^{\ell}} \]
    8. Taylor expanded in K around 0 97.2%

      \[\leadsto \color{blue}{\frac{1}{e^{\ell}}} \]
    9. Step-by-step derivation
      1. rec-exp97.2%

        \[\leadsto \color{blue}{e^{-\ell}} \]
    10. Simplified97.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq -3.8 \cdot 10^{+159}:\\ \;\;\;\;\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{elif}\;\ell \leq -1.5 \cdot 10^{-29}:\\ \;\;\;\;\cos \left(\left(K \cdot 0.5\right) \cdot \left(m + n\right) - M\right) \cdot e^{\ell}\\ \mathbf{elif}\;\ell \leq 5.3 \cdot 10^{-17}:\\ \;\;\;\;\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \]

Alternative 3: 75.2% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := \left|m - n\right| - \ell\\
\mathbf{if}\;M \leq -2.1 \cdot 10^{+125} \lor \neg \left(M \leq 3.1 \cdot 10^{+61}\right):\\
\;\;\;\;\cos M \cdot e^{t_0 - M \cdot M}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -2.1000000000000001e125 or 3.0999999999999999e61 < M

    1. Initial program 73.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -2.1000000000000001e125 < M < 3.0999999999999999e61

    1. Initial program 71.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -2.1 \cdot 10^{+125} \lor \neg \left(M \leq 3.1 \cdot 10^{+61}\right):\\ \;\;\;\;\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - M \cdot M}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - 0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \]

Alternative 4: 71.9% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := \left|m - n\right|\\
\mathbf{if}\;n \leq 8 \cdot 10^{+60}:\\
\;\;\;\;\frac{\cos M}{e^{\ell + \left(\left(m \cdot m\right) \cdot 0.25 - t_0\right)}}\\

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


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

    1. Initial program 77.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \frac{\cos M}{\frac{e^{\ell + 0.25 \cdot {m}^{2}}}{e^{\color{blue}{\left|m - n\right|}}}} \]
      4. sub-neg21.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 7.9999999999999996e60 < n

    1. Initial program 56.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 44.6% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -13:\\
\;\;\;\;\cos \left(\left(K \cdot 0.5\right) \cdot \left(m + n\right) - M\right) \cdot e^{\ell}\\

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


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

    1. Initial program 73.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(\left(K \cdot 0.5\right) \cdot \left(m + n\right) - M\right) \cdot \color{blue}{e^{-\ell}} \]
    6. Applied egg-rr19.5%

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

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

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos \left(\left(K \cdot 0.5\right) \cdot \left(m + n\right) - M\right) \cdot e^{-\ell}\right)} - 1} \]
      3. associate-*l*13.7%

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

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

        \[\leadsto e^{\mathsf{log1p}\left(\cos \left(K \cdot \color{blue}{\left(\left(n + m\right) \cdot 0.5\right)} - M\right) \cdot e^{-\ell}\right)} - 1 \]
      6. add-sqr-sqrt13.7%

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

        \[\leadsto e^{\mathsf{log1p}\left(\cos \left(K \cdot \left(\left(n + m\right) \cdot 0.5\right) - M\right) \cdot e^{\color{blue}{\sqrt{\left(-\ell\right) \cdot \left(-\ell\right)}}}\right)} - 1 \]
      8. sqr-neg13.7%

        \[\leadsto e^{\mathsf{log1p}\left(\cos \left(K \cdot \left(\left(n + m\right) \cdot 0.5\right) - M\right) \cdot e^{\sqrt{\color{blue}{\ell \cdot \ell}}}\right)} - 1 \]
      9. sqrt-unprod0.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos \left(K \cdot \left(\left(n + m\right) \cdot 0.5\right) - M\right) \cdot e^{\color{blue}{\sqrt{\ell} \cdot \sqrt{\ell}}}\right)} - 1 \]
      10. add-sqr-sqrt53.7%

        \[\leadsto e^{\mathsf{log1p}\left(\cos \left(K \cdot \left(\left(n + m\right) \cdot 0.5\right) - M\right) \cdot e^{\color{blue}{\ell}}\right)} - 1 \]
    8. Applied egg-rr53.7%

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

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

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

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

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

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

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

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

    if -13 < l

    1. Initial program 72.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(\left(K \cdot 0.5\right) \cdot \left(m + n\right) - M\right) \cdot \color{blue}{e^{-\ell}} \]
    6. Applied egg-rr35.6%

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

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

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

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

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

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

Alternative 6: 35.8% accurate, 2.1× speedup?

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

\\
\cos M \cdot e^{-\ell}
\end{array}
Derivation
  1. Initial program 72.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos M \cdot e^{-\ell}} \]
  10. Final simplification36.2%

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

Alternative 7: 35.8% accurate, 2.1× speedup?

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

\\
\frac{\cos M}{e^{\ell}}
\end{array}
Derivation
  1. Initial program 72.5%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
  7. Simplified36.2%

    \[\leadsto \color{blue}{\frac{\cos M}{e^{\ell}}} \]
  8. Final simplification36.2%

    \[\leadsto \frac{\cos M}{e^{\ell}} \]

Alternative 8: 35.4% accurate, 4.2× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \frac{\cos \color{blue}{\left(\left(0.5 \cdot K\right) \cdot n\right)}}{e^{\ell}} \]
    3. *-commutative33.6%

      \[\leadsto \frac{\cos \left(\color{blue}{\left(K \cdot 0.5\right)} \cdot n\right)}{e^{\ell}} \]
    4. associate-*l*33.6%

      \[\leadsto \frac{\cos \color{blue}{\left(K \cdot \left(0.5 \cdot n\right)\right)}}{e^{\ell}} \]
  7. Simplified33.6%

    \[\leadsto \frac{\cos \color{blue}{\left(K \cdot \left(0.5 \cdot n\right)\right)}}{e^{\ell}} \]
  8. Taylor expanded in K around 0 35.4%

    \[\leadsto \color{blue}{\frac{1}{e^{\ell}}} \]
  9. Step-by-step derivation
    1. rec-exp35.4%

      \[\leadsto \color{blue}{e^{-\ell}} \]
  10. Simplified35.4%

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

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

Alternative 9: 7.1% accurate, 4.2× speedup?

\[\begin{array}{l} \\ \cos M \end{array} \]
(FPCore (K m n M l) :precision binary64 (cos M))
double code(double K, double m, double n, double M, double l) {
	return cos(M);
}
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
public static double code(double K, double m, double n, double M, double l) {
	return Math.cos(M);
}
def code(K, m, n, M, l):
	return math.cos(M)
function code(K, m, n, M, l)
	return cos(M)
end
function tmp = code(K, m, n, M, l)
	tmp = cos(M);
end
code[K_, m_, n_, M_, l_] := N[Cos[M], $MachinePrecision]
\begin{array}{l}

\\
\cos M
\end{array}
Derivation
  1. Initial program 72.5%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos \left(\color{blue}{\left(K \cdot 0.5\right)} \cdot \left(n + m\right) - M\right) \]
    3. +-commutative5.4%

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

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

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

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

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

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

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

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

      \[\leadsto \cos \left(\left(K \cdot 0.5\right) \cdot \left(n + \left(-\color{blue}{\left(-m\right)}\right)\right) - M\right) \]
    12. remove-double-neg5.4%

      \[\leadsto \cos \left(\left(K \cdot 0.5\right) \cdot \left(n + \color{blue}{m}\right) - M\right) \]
    13. +-commutative5.4%

      \[\leadsto \cos \left(\left(K \cdot 0.5\right) \cdot \color{blue}{\left(m + n\right)} - M\right) \]
    14. associate-*l*5.4%

      \[\leadsto \cos \left(\color{blue}{K \cdot \left(0.5 \cdot \left(m + n\right)\right)} - M\right) \]
    15. +-commutative5.4%

      \[\leadsto \cos \left(K \cdot \left(0.5 \cdot \color{blue}{\left(n + m\right)}\right) - M\right) \]
  7. Simplified5.4%

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

    \[\leadsto \color{blue}{\cos \left(-M\right)} \]
  9. Step-by-step derivation
    1. cos-neg6.2%

      \[\leadsto \color{blue}{\cos M} \]
  10. Simplified6.2%

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

    \[\leadsto \cos M \]

Reproduce

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