Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.2% → 96.9%
Time: 17.9s
Alternatives: 7
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 7 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.2% 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.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}\right)} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (* (cos M) (exp (- (fabs (- m n)) (+ l (pow (- (* (+ m n) 0.5) 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) * 0.5) - 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) * 0.5d0) - 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) * 0.5) - 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) * 0.5) - M), 2.0))))
function code(K, m, n, M, l)
	return Float64(cos(M) * exp(Float64(abs(Float64(m - n)) - Float64(l + (Float64(Float64(Float64(m + n) * 0.5) - M) ^ 2.0)))))
end
function tmp = code(K, m, n, M, l)
	tmp = cos(M) * exp((abs((m - n)) - (l + ((((m + n) * 0.5) - M) ^ 2.0))));
end
code[K_, m_, n_, M_, l_] := N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - N[(l + N[Power[N[(N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]
\begin{array}{l}

\\
\cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}\right)}
\end{array}
Derivation
  1. Initial program 74.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.8%

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

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

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

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

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

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

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

Alternative 2: 74.3% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;n \leq 126:\\ \;\;\;\;\cos \left(0.5 \cdot \left(n \cdot K\right) - M\right) \cdot e^{\left(\left(m - n\right) - {M}^{2}\right) - \ell}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{{n}^{2} \cdot -0.25}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= n 126.0)
   (* (cos (- (* 0.5 (* n K)) M)) (exp (- (- (- m n) (pow M 2.0)) l)))
   (* (cos M) (exp (* (pow n 2.0) -0.25)))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 126.0) {
		tmp = cos(((0.5 * (n * K)) - M)) * exp((((m - n) - pow(M, 2.0)) - l));
	} else {
		tmp = cos(M) * exp((pow(n, 2.0) * -0.25));
	}
	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 (n <= 126.0d0) then
        tmp = cos(((0.5d0 * (n * k)) - m_1)) * exp((((m - n) - (m_1 ** 2.0d0)) - l))
    else
        tmp = cos(m_1) * exp(((n ** 2.0d0) * (-0.25d0)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (n <= 126.0) {
		tmp = Math.cos(((0.5 * (n * K)) - M)) * Math.exp((((m - n) - Math.pow(M, 2.0)) - l));
	} else {
		tmp = Math.cos(M) * Math.exp((Math.pow(n, 2.0) * -0.25));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if n <= 126.0:
		tmp = math.cos(((0.5 * (n * K)) - M)) * math.exp((((m - n) - math.pow(M, 2.0)) - l))
	else:
		tmp = math.cos(M) * math.exp((math.pow(n, 2.0) * -0.25))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (n <= 126.0)
		tmp = Float64(cos(Float64(Float64(0.5 * Float64(n * K)) - M)) * exp(Float64(Float64(Float64(m - n) - (M ^ 2.0)) - l)));
	else
		tmp = Float64(cos(M) * exp(Float64((n ^ 2.0) * -0.25)));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (n <= 126.0)
		tmp = cos(((0.5 * (n * K)) - M)) * exp((((m - n) - (M ^ 2.0)) - l));
	else
		tmp = cos(M) * exp(((n ^ 2.0) * -0.25));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[n, 126.0], N[(N[Cos[N[(N[(0.5 * N[(n * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(N[(m - n), $MachinePrecision] - N[Power[M, 2.0], $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[Power[n, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

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


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

    1. Initial program 77.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 m around 0 58.7%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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-out59.8%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative59.8%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative59.8%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
    5. Simplified59.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(0.5 \cdot \left(K \cdot n\right) - M\right) \cdot e^{\left(\color{blue}{\left|m - n\right|} - {M}^{2}\right) - \ell} \]
      11. rem-square-sqrt33.9%

        \[\leadsto \cos \left(0.5 \cdot \left(K \cdot n\right) - M\right) \cdot e^{\left(\left|\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right| - {M}^{2}\right) - \ell} \]
      12. fabs-sqr33.9%

        \[\leadsto \cos \left(0.5 \cdot \left(K \cdot n\right) - M\right) \cdot e^{\left(\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}} - {M}^{2}\right) - \ell} \]
      13. rem-square-sqrt67.5%

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

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

    if 126 < n

    1. Initial program 64.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 100.0%

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {n}^{2}}} \]
    7. Step-by-step derivation
      1. *-commutative98.4%

        \[\leadsto \cos M \cdot e^{\color{blue}{{n}^{2} \cdot -0.25}} \]
    8. Simplified98.4%

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

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

Alternative 3: 65.7% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;M \leq 38:\\ \;\;\;\;e^{m - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= M 38.0) (exp (- m (+ n l))) (* (cos M) (exp (- (pow M 2.0))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (M <= 38.0) {
		tmp = exp((m - (n + l)));
	} else {
		tmp = cos(M) * exp(-pow(M, 2.0));
	}
	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 (m_1 <= 38.0d0) then
        tmp = exp((m - (n + l)))
    else
        tmp = cos(m_1) * exp(-(m_1 ** 2.0d0))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (M <= 38.0) {
		tmp = Math.exp((m - (n + l)));
	} else {
		tmp = Math.cos(M) * Math.exp(-Math.pow(M, 2.0));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if M <= 38.0:
		tmp = math.exp((m - (n + l)))
	else:
		tmp = math.cos(M) * math.exp(-math.pow(M, 2.0))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (M <= 38.0)
		tmp = exp(Float64(m - Float64(n + l)));
	else
		tmp = Float64(cos(M) * exp(Float64(-(M ^ 2.0))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (M <= 38.0)
		tmp = exp((m - (n + l)));
	else
		tmp = cos(M) * exp(-(M ^ 2.0));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[M, 38.0], N[Exp[N[(m - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-N[Power[M, 2.0], $MachinePrecision])], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;M \leq 38:\\
\;\;\;\;e^{m - \left(n + \ell\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < 38

    1. Initial program 72.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 m around 0 54.3%

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. unpow254.3%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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-out55.3%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative55.3%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative55.3%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
    5. Simplified55.3%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \ell}} \]
    13. Step-by-step derivation
      1. div-exp19.4%

        \[\leadsto \color{blue}{\frac{e^{\left|m - n\right|}}{e^{\ell}}} \]
      2. sub-neg19.4%

        \[\leadsto \frac{e^{\left|\color{blue}{m + \left(-n\right)}\right|}}{e^{\ell}} \]
      3. mul-1-neg19.4%

        \[\leadsto \frac{e^{\left|m + \color{blue}{-1 \cdot n}\right|}}{e^{\ell}} \]
      4. mul-1-neg19.4%

        \[\leadsto \frac{e^{\left|m + \color{blue}{\left(-n\right)}\right|}}{e^{\ell}} \]
      5. remove-double-neg19.4%

        \[\leadsto \frac{e^{\left|\color{blue}{\left(-\left(-m\right)\right)} + \left(-n\right)\right|}}{e^{\ell}} \]
      6. mul-1-neg19.4%

        \[\leadsto \frac{e^{\left|\left(-\color{blue}{-1 \cdot m}\right) + \left(-n\right)\right|}}{e^{\ell}} \]
      7. distribute-neg-in19.4%

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

        \[\leadsto \frac{e^{\left|-\color{blue}{\left(n + -1 \cdot m\right)}\right|}}{e^{\ell}} \]
      9. exp-diff26.6%

        \[\leadsto \color{blue}{e^{\left|-\left(n + -1 \cdot m\right)\right| - \ell}} \]
      10. fabs-neg26.6%

        \[\leadsto e^{\color{blue}{\left|n + -1 \cdot m\right|} - \ell} \]
      11. mul-1-neg26.6%

        \[\leadsto e^{\left|n + \color{blue}{\left(-m\right)}\right| - \ell} \]
      12. sub-neg26.6%

        \[\leadsto e^{\left|\color{blue}{n - m}\right| - \ell} \]
      13. fabs-sub26.6%

        \[\leadsto e^{\color{blue}{\left|m - n\right|} - \ell} \]
      14. rem-square-sqrt14.3%

        \[\leadsto e^{\left|\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right| - \ell} \]
      15. fabs-sqr14.3%

        \[\leadsto e^{\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}} - \ell} \]
      16. rem-square-sqrt61.2%

        \[\leadsto e^{\color{blue}{\left(m - n\right)} - \ell} \]
    14. Simplified61.2%

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

    if 38 < M

    1. Initial program 80.7%

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot {M}^{2}}} \]
    7. Step-by-step derivation
      1. mul-1-neg100.0%

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

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

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

Alternative 4: 62.9% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;K \leq -2.8 \cdot 10^{+31} \lor \neg \left(K \leq 11000000000\right):\\ \;\;\;\;e^{m - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\left(m + n\right) \cdot \left(0.5 \cdot K\right) - M\right) \cdot e^{\left(\left(m - n\right) + M \cdot \left(m - M\right)\right) - \ell}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= K -2.8e+31) (not (<= K 11000000000.0)))
   (exp (- m (+ n l)))
   (*
    (cos (- (* (+ m n) (* 0.5 K)) M))
    (exp (- (+ (- m n) (* M (- m M))) l)))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((K <= -2.8e+31) || !(K <= 11000000000.0)) {
		tmp = exp((m - (n + l)));
	} else {
		tmp = cos((((m + n) * (0.5 * K)) - M)) * exp((((m - n) + (M * (m - M))) - 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 ((k <= (-2.8d+31)) .or. (.not. (k <= 11000000000.0d0))) then
        tmp = exp((m - (n + l)))
    else
        tmp = cos((((m + n) * (0.5d0 * k)) - m_1)) * exp((((m - n) + (m_1 * (m - m_1))) - l))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if ((K <= -2.8e+31) || !(K <= 11000000000.0)) {
		tmp = Math.exp((m - (n + l)));
	} else {
		tmp = Math.cos((((m + n) * (0.5 * K)) - M)) * Math.exp((((m - n) + (M * (m - M))) - l));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if (K <= -2.8e+31) or not (K <= 11000000000.0):
		tmp = math.exp((m - (n + l)))
	else:
		tmp = math.cos((((m + n) * (0.5 * K)) - M)) * math.exp((((m - n) + (M * (m - M))) - l))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if ((K <= -2.8e+31) || !(K <= 11000000000.0))
		tmp = exp(Float64(m - Float64(n + l)));
	else
		tmp = Float64(cos(Float64(Float64(Float64(m + n) * Float64(0.5 * K)) - M)) * exp(Float64(Float64(Float64(m - n) + Float64(M * Float64(m - M))) - l)));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if ((K <= -2.8e+31) || ~((K <= 11000000000.0)))
		tmp = exp((m - (n + l)));
	else
		tmp = cos((((m + n) * (0.5 * K)) - M)) * exp((((m - n) + (M * (m - M))) - l));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[Or[LessEqual[K, -2.8e+31], N[Not[LessEqual[K, 11000000000.0]], $MachinePrecision]], N[Exp[N[(m - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision], N[(N[Cos[N[(N[(N[(m + n), $MachinePrecision] * N[(0.5 * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(N[(m - n), $MachinePrecision] + N[(M * N[(m - M), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
\mathbf{if}\;K \leq -2.8 \cdot 10^{+31} \lor \neg \left(K \leq 11000000000\right):\\
\;\;\;\;e^{m - \left(n + \ell\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if K < -2.80000000000000017e31 or 1.1e10 < K

    1. Initial program 46.7%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. unpow236.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \ell}} \]
    13. Step-by-step derivation
      1. div-exp14.5%

        \[\leadsto \color{blue}{\frac{e^{\left|m - n\right|}}{e^{\ell}}} \]
      2. sub-neg14.5%

        \[\leadsto \frac{e^{\left|\color{blue}{m + \left(-n\right)}\right|}}{e^{\ell}} \]
      3. mul-1-neg14.5%

        \[\leadsto \frac{e^{\left|m + \color{blue}{-1 \cdot n}\right|}}{e^{\ell}} \]
      4. mul-1-neg14.5%

        \[\leadsto \frac{e^{\left|m + \color{blue}{\left(-n\right)}\right|}}{e^{\ell}} \]
      5. remove-double-neg14.5%

        \[\leadsto \frac{e^{\left|\color{blue}{\left(-\left(-m\right)\right)} + \left(-n\right)\right|}}{e^{\ell}} \]
      6. mul-1-neg14.5%

        \[\leadsto \frac{e^{\left|\left(-\color{blue}{-1 \cdot m}\right) + \left(-n\right)\right|}}{e^{\ell}} \]
      7. distribute-neg-in14.5%

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

        \[\leadsto \frac{e^{\left|-\color{blue}{\left(n + -1 \cdot m\right)}\right|}}{e^{\ell}} \]
      9. exp-diff25.3%

        \[\leadsto \color{blue}{e^{\left|-\left(n + -1 \cdot m\right)\right| - \ell}} \]
      10. fabs-neg25.3%

        \[\leadsto e^{\color{blue}{\left|n + -1 \cdot m\right|} - \ell} \]
      11. mul-1-neg25.3%

        \[\leadsto e^{\left|n + \color{blue}{\left(-m\right)}\right| - \ell} \]
      12. sub-neg25.3%

        \[\leadsto e^{\left|\color{blue}{n - m}\right| - \ell} \]
      13. fabs-sub25.3%

        \[\leadsto e^{\color{blue}{\left|m - n\right|} - \ell} \]
      14. rem-square-sqrt15.6%

        \[\leadsto e^{\left|\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right| - \ell} \]
      15. fabs-sqr15.6%

        \[\leadsto e^{\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}} - \ell} \]
      16. rem-square-sqrt58.9%

        \[\leadsto e^{\color{blue}{\left(m - n\right)} - \ell} \]
    14. Simplified58.9%

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

    if -2.80000000000000017e31 < K < 1.1e10

    1. Initial program 100.0%

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

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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-out79.3%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative79.3%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative79.3%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
    5. Simplified79.3%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, n + \left(-\color{blue}{\left(-m\right)}\right), -M\right)\right) \cdot e^{\left(\left|-\left(n + -1 \cdot m\right)\right| + -1 \cdot \left(M \cdot \left(M + -1 \cdot m\right)\right)\right) - \ell} \]
      5. remove-double-neg60.0%

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

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

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

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

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

Alternative 5: 52.3% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;\ell \leq -5.5 \cdot 10^{+222}:\\ \;\;\;\;e^{m - n} \cdot \cos \left(\left(\left(m + n\right) \cdot 0.5\right) \cdot K\right)\\ \mathbf{else}:\\ \;\;\;\;e^{m - \left(n + \ell\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= l -5.5e+222)
   (* (exp (- m n)) (cos (* (* (+ m n) 0.5) K)))
   (exp (- m (+ n l)))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (l <= -5.5e+222) {
		tmp = exp((m - n)) * cos((((m + n) * 0.5) * K));
	} else {
		tmp = exp((m - (n + 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 <= (-5.5d+222)) then
        tmp = exp((m - n)) * cos((((m + n) * 0.5d0) * k))
    else
        tmp = exp((m - (n + 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 <= -5.5e+222) {
		tmp = Math.exp((m - n)) * Math.cos((((m + n) * 0.5) * K));
	} else {
		tmp = Math.exp((m - (n + l)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if l <= -5.5e+222:
		tmp = math.exp((m - n)) * math.cos((((m + n) * 0.5) * K))
	else:
		tmp = math.exp((m - (n + l)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (l <= -5.5e+222)
		tmp = Float64(exp(Float64(m - n)) * cos(Float64(Float64(Float64(m + n) * 0.5) * K)));
	else
		tmp = exp(Float64(m - Float64(n + l)));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (l <= -5.5e+222)
		tmp = exp((m - n)) * cos((((m + n) * 0.5) * K));
	else
		tmp = exp((m - (n + l)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[l, -5.5e+222], N[(N[Exp[N[(m - n), $MachinePrecision]], $MachinePrecision] * N[Cos[N[(N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision] * K), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Exp[N[(m - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

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

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


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

    1. Initial program 81.8%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. unpow245.9%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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-out55.0%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative55.0%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative55.0%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
    5. Simplified55.0%

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

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

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

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

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

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

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

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

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

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

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

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

    if -5.4999999999999999e222 < l

    1. Initial program 74.1%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. unpow258.4%

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative59.2%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative59.2%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \ell}} \]
    13. Step-by-step derivation
      1. div-exp17.7%

        \[\leadsto \color{blue}{\frac{e^{\left|m - n\right|}}{e^{\ell}}} \]
      2. sub-neg17.7%

        \[\leadsto \frac{e^{\left|\color{blue}{m + \left(-n\right)}\right|}}{e^{\ell}} \]
      3. mul-1-neg17.7%

        \[\leadsto \frac{e^{\left|m + \color{blue}{-1 \cdot n}\right|}}{e^{\ell}} \]
      4. mul-1-neg17.7%

        \[\leadsto \frac{e^{\left|m + \color{blue}{\left(-n\right)}\right|}}{e^{\ell}} \]
      5. remove-double-neg17.7%

        \[\leadsto \frac{e^{\left|\color{blue}{\left(-\left(-m\right)\right)} + \left(-n\right)\right|}}{e^{\ell}} \]
      6. mul-1-neg17.7%

        \[\leadsto \frac{e^{\left|\left(-\color{blue}{-1 \cdot m}\right) + \left(-n\right)\right|}}{e^{\ell}} \]
      7. distribute-neg-in17.7%

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

        \[\leadsto \frac{e^{\left|-\color{blue}{\left(n + -1 \cdot m\right)}\right|}}{e^{\ell}} \]
      9. exp-diff24.8%

        \[\leadsto \color{blue}{e^{\left|-\left(n + -1 \cdot m\right)\right| - \ell}} \]
      10. fabs-neg24.8%

        \[\leadsto e^{\color{blue}{\left|n + -1 \cdot m\right|} - \ell} \]
      11. mul-1-neg24.8%

        \[\leadsto e^{\left|n + \color{blue}{\left(-m\right)}\right| - \ell} \]
      12. sub-neg24.8%

        \[\leadsto e^{\left|\color{blue}{n - m}\right| - \ell} \]
      13. fabs-sub24.8%

        \[\leadsto e^{\color{blue}{\left|m - n\right|} - \ell} \]
      14. rem-square-sqrt13.9%

        \[\leadsto e^{\left|\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right| - \ell} \]
      15. fabs-sqr13.9%

        \[\leadsto e^{\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}} - \ell} \]
      16. rem-square-sqrt58.9%

        \[\leadsto e^{\color{blue}{\left(m - n\right)} - \ell} \]
    14. Simplified58.9%

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

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

Alternative 6: 52.7% accurate, 4.0× speedup?

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

\\
e^{m - \left(n + \ell\right)}
\end{array}
Derivation
  1. Initial program 74.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 m around 0 57.8%

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. unpow257.8%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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-out59.0%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative59.0%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative59.0%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
  5. Simplified59.0%

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{e^{\left|m - n\right| - \ell}} \]
  13. Step-by-step derivation
    1. div-exp17.7%

      \[\leadsto \color{blue}{\frac{e^{\left|m - n\right|}}{e^{\ell}}} \]
    2. sub-neg17.7%

      \[\leadsto \frac{e^{\left|\color{blue}{m + \left(-n\right)}\right|}}{e^{\ell}} \]
    3. mul-1-neg17.7%

      \[\leadsto \frac{e^{\left|m + \color{blue}{-1 \cdot n}\right|}}{e^{\ell}} \]
    4. mul-1-neg17.7%

      \[\leadsto \frac{e^{\left|m + \color{blue}{\left(-n\right)}\right|}}{e^{\ell}} \]
    5. remove-double-neg17.7%

      \[\leadsto \frac{e^{\left|\color{blue}{\left(-\left(-m\right)\right)} + \left(-n\right)\right|}}{e^{\ell}} \]
    6. mul-1-neg17.7%

      \[\leadsto \frac{e^{\left|\left(-\color{blue}{-1 \cdot m}\right) + \left(-n\right)\right|}}{e^{\ell}} \]
    7. distribute-neg-in17.7%

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

      \[\leadsto \frac{e^{\left|-\color{blue}{\left(n + -1 \cdot m\right)}\right|}}{e^{\ell}} \]
    9. exp-diff24.6%

      \[\leadsto \color{blue}{e^{\left|-\left(n + -1 \cdot m\right)\right| - \ell}} \]
    10. fabs-neg24.6%

      \[\leadsto e^{\color{blue}{\left|n + -1 \cdot m\right|} - \ell} \]
    11. mul-1-neg24.6%

      \[\leadsto e^{\left|n + \color{blue}{\left(-m\right)}\right| - \ell} \]
    12. sub-neg24.6%

      \[\leadsto e^{\left|\color{blue}{n - m}\right| - \ell} \]
    13. fabs-sub24.6%

      \[\leadsto e^{\color{blue}{\left|m - n\right|} - \ell} \]
    14. rem-square-sqrt13.7%

      \[\leadsto e^{\left|\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right| - \ell} \]
    15. fabs-sqr13.7%

      \[\leadsto e^{\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}} - \ell} \]
    16. rem-square-sqrt57.2%

      \[\leadsto e^{\color{blue}{\left(m - n\right)} - \ell} \]
  14. Simplified57.2%

    \[\leadsto \color{blue}{e^{m - \left(n + \ell\right)}} \]
  15. Final simplification57.2%

    \[\leadsto e^{m - \left(n + \ell\right)} \]
  16. Add Preprocessing

Alternative 7: 36.0% 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 74.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 l around inf 28.8%

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

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

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

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

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

      \[\leadsto e^{-\ell} \cdot \cos \color{blue}{\left(\left(0.5 \cdot K\right) \cdot \left(m + n\right)\right)} \]
    3. *-lft-identity28.8%

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

      \[\leadsto e^{-\ell} \cdot \cos \left(\left(0.5 \cdot K\right) \cdot \left(m + \color{blue}{\left(--1\right)} \cdot n\right)\right) \]
    5. cancel-sign-sub-inv28.8%

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

      \[\leadsto e^{-\ell} \cdot \cos \color{blue}{\left(0.5 \cdot \left(K \cdot \left(m - -1 \cdot n\right)\right)\right)} \]
    7. *-commutative28.8%

      \[\leadsto e^{-\ell} \cdot \cos \left(0.5 \cdot \color{blue}{\left(\left(m - -1 \cdot n\right) \cdot K\right)}\right) \]
    8. sub-neg28.8%

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

      \[\leadsto e^{-\ell} \cdot \cos \left(0.5 \cdot \left(\left(m + \left(-\color{blue}{\left(-n\right)}\right)\right) \cdot K\right)\right) \]
    10. remove-double-neg28.8%

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

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

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

    \[\leadsto e^{-\ell} \cdot \color{blue}{1} \]
  10. Final simplification36.7%

    \[\leadsto e^{-\ell} \]
  11. Add Preprocessing

Reproduce

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