Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.2% → 96.9%
Time: 22.0s
Alternatives: 7
Speedup: 1.3×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 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. sub-neg96.8%

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

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

Alternative 2: 78.1% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -9500000000 \lor \neg \left(M \leq 27\right):\\
\;\;\;\;\cos M \cdot e^{-{M}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -9.5e9 or 27 < M

    1. Initial program 78.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 99.1%

      \[\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-neg99.1%

        \[\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. sub-neg99.1%

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

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

        \[\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}}} \]
      5. *-commutative99.1%

        \[\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}} \]
      6. associate--r+99.1%

        \[\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. Simplified99.1%

      \[\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 97.5%

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

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

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

    if -9.5e9 < M < 27

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

      \[\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. +-commutative50.6%

        \[\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. unpow250.6%

        \[\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-out50.7%

        \[\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. *-commutative50.7%

        \[\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. *-commutative50.7%

        \[\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. Simplified50.7%

      \[\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 28.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+28.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*28.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-128.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-sub28.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. Simplified28.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. Step-by-step derivation
      1. associate-+l-28.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 3: 48.0% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(m + n\right) \cdot K\\ \mathbf{if}\;M \leq -41000000000:\\ \;\;\;\;\cos \left(0.5 \cdot t\_0\right) \cdot e^{m - \left(n + \ell\right)}\\ \mathbf{elif}\;M \leq 1.08 \cdot 10^{+96}:\\ \;\;\;\;\cos M \cdot e^{\left(m - n\right) + \left(M \cdot \left(M + m\right) - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\frac{t\_0}{2} - M\right) \cdot e^{m \cdot \left(M - n \cdot 0.5\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* (+ m n) K)))
   (if (<= M -41000000000.0)
     (* (cos (* 0.5 t_0)) (exp (- m (+ n l))))
     (if (<= M 1.08e+96)
       (* (cos M) (exp (+ (- m n) (- (* M (+ M m)) l))))
       (* (cos (- (/ t_0 2.0) M)) (exp (* m (- M (* n 0.5)))))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = (m + n) * K;
	double tmp;
	if (M <= -41000000000.0) {
		tmp = cos((0.5 * t_0)) * exp((m - (n + l)));
	} else if (M <= 1.08e+96) {
		tmp = cos(M) * exp(((m - n) + ((M * (M + m)) - l)));
	} else {
		tmp = cos(((t_0 / 2.0) - M)) * exp((m * (M - (n * 0.5))));
	}
	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 = (m + n) * k
    if (m_1 <= (-41000000000.0d0)) then
        tmp = cos((0.5d0 * t_0)) * exp((m - (n + l)))
    else if (m_1 <= 1.08d+96) then
        tmp = cos(m_1) * exp(((m - n) + ((m_1 * (m_1 + m)) - l)))
    else
        tmp = cos(((t_0 / 2.0d0) - m_1)) * exp((m * (m_1 - (n * 0.5d0))))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = (m + n) * K;
	double tmp;
	if (M <= -41000000000.0) {
		tmp = Math.cos((0.5 * t_0)) * Math.exp((m - (n + l)));
	} else if (M <= 1.08e+96) {
		tmp = Math.cos(M) * Math.exp(((m - n) + ((M * (M + m)) - l)));
	} else {
		tmp = Math.cos(((t_0 / 2.0) - M)) * Math.exp((m * (M - (n * 0.5))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = (m + n) * K
	tmp = 0
	if M <= -41000000000.0:
		tmp = math.cos((0.5 * t_0)) * math.exp((m - (n + l)))
	elif M <= 1.08e+96:
		tmp = math.cos(M) * math.exp(((m - n) + ((M * (M + m)) - l)))
	else:
		tmp = math.cos(((t_0 / 2.0) - M)) * math.exp((m * (M - (n * 0.5))))
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(Float64(m + n) * K)
	tmp = 0.0
	if (M <= -41000000000.0)
		tmp = Float64(cos(Float64(0.5 * t_0)) * exp(Float64(m - Float64(n + l))));
	elseif (M <= 1.08e+96)
		tmp = Float64(cos(M) * exp(Float64(Float64(m - n) + Float64(Float64(M * Float64(M + m)) - l))));
	else
		tmp = Float64(cos(Float64(Float64(t_0 / 2.0) - M)) * exp(Float64(m * Float64(M - Float64(n * 0.5)))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = (m + n) * K;
	tmp = 0.0;
	if (M <= -41000000000.0)
		tmp = cos((0.5 * t_0)) * exp((m - (n + l)));
	elseif (M <= 1.08e+96)
		tmp = cos(M) * exp(((m - n) + ((M * (M + m)) - l)));
	else
		tmp = cos(((t_0 / 2.0) - M)) * exp((m * (M - (n * 0.5))));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[(m + n), $MachinePrecision] * K), $MachinePrecision]}, If[LessEqual[M, -41000000000.0], N[(N[Cos[N[(0.5 * t$95$0), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(m - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[M, 1.08e+96], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(m - n), $MachinePrecision] + N[(N[(M * N[(M + m), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[N[(N[(t$95$0 / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(m * N[(M - N[(n * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if M < -4.1e10

    1. Initial program 76.3%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in m around 0 62.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. +-commutative62.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. unpow262.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-out66.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. *-commutative66.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. *-commutative66.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. Simplified66.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 62.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+62.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*62.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-162.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-sub62.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. Simplified62.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. Step-by-step derivation
      1. associate-+l-62.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -4.1e10 < M < 1.08e96

    1. Initial program 72.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 52.5%

      \[\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. +-commutative52.5%

        \[\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. unpow252.5%

        \[\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-out52.5%

        \[\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. *-commutative52.5%

        \[\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. *-commutative52.5%

        \[\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. Simplified52.5%

      \[\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 32.4%

      \[\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+32.4%

        \[\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*32.4%

        \[\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-132.4%

        \[\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-sub32.4%

        \[\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. Simplified32.4%

      \[\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. Step-by-step derivation
      1. associate-+l-32.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.08e96 < M

    1. Initial program 78.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 70.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. +-commutative70.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. unpow270.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-out73.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. *-commutative73.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. *-commutative73.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. Simplified73.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 m around inf 32.6%

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

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

Alternative 4: 48.0% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -780000000000 \lor \neg \left(M \leq 2.6 \cdot 10^{+95}\right):\\
\;\;\;\;\cos \left(0.5 \cdot \left(\left(m + n\right) \cdot K\right)\right) \cdot e^{m - \left(n + \ell\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -7.8e11 or 2.5999999999999999e95 < M

    1. Initial program 77.0%

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

      \[\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. +-commutative66.1%

        \[\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. unpow266.1%

        \[\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-out69.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. *-commutative69.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. *-commutative69.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. Simplified69.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 66.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+66.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*66.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-166.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-sub66.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. Simplified66.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. Step-by-step derivation
      1. associate-+l-66.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -7.8e11 < M < 2.5999999999999999e95

    1. Initial program 72.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 52.5%

      \[\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. +-commutative52.5%

        \[\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. unpow252.5%

        \[\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-out52.5%

        \[\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. *-commutative52.5%

        \[\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. *-commutative52.5%

        \[\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. Simplified52.5%

      \[\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 32.4%

      \[\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+32.4%

        \[\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*32.4%

        \[\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-132.4%

        \[\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-sub32.4%

        \[\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. Simplified32.4%

      \[\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. Step-by-step derivation
      1. associate-+l-32.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 35.5% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{-\ell}\\ \mathbf{if}\;\ell \leq -2.5:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;\ell \leq 1.95 \cdot 10^{-150}:\\ \;\;\;\;t\_0 \cdot \left(-0.125 \cdot {\left(\left(m + n\right) \cdot K\right)}^{2}\right)\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (exp (- l))))
   (if (<= l -2.5)
     t_0
     (if (<= l 1.95e-150)
       (* t_0 (* -0.125 (pow (* (+ m n) K) 2.0)))
       (* (cos M) t_0)))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = exp(-l);
	double tmp;
	if (l <= -2.5) {
		tmp = t_0;
	} else if (l <= 1.95e-150) {
		tmp = t_0 * (-0.125 * pow(((m + n) * K), 2.0));
	} else {
		tmp = cos(M) * t_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) :: t_0
    real(8) :: tmp
    t_0 = exp(-l)
    if (l <= (-2.5d0)) then
        tmp = t_0
    else if (l <= 1.95d-150) then
        tmp = t_0 * ((-0.125d0) * (((m + n) * k) ** 2.0d0))
    else
        tmp = cos(m_1) * t_0
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.exp(-l);
	double tmp;
	if (l <= -2.5) {
		tmp = t_0;
	} else if (l <= 1.95e-150) {
		tmp = t_0 * (-0.125 * Math.pow(((m + n) * K), 2.0));
	} else {
		tmp = Math.cos(M) * t_0;
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.exp(-l)
	tmp = 0
	if l <= -2.5:
		tmp = t_0
	elif l <= 1.95e-150:
		tmp = t_0 * (-0.125 * math.pow(((m + n) * K), 2.0))
	else:
		tmp = math.cos(M) * t_0
	return tmp
function code(K, m, n, M, l)
	t_0 = exp(Float64(-l))
	tmp = 0.0
	if (l <= -2.5)
		tmp = t_0;
	elseif (l <= 1.95e-150)
		tmp = Float64(t_0 * Float64(-0.125 * (Float64(Float64(m + n) * K) ^ 2.0)));
	else
		tmp = Float64(cos(M) * t_0);
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = exp(-l);
	tmp = 0.0;
	if (l <= -2.5)
		tmp = t_0;
	elseif (l <= 1.95e-150)
		tmp = t_0 * (-0.125 * (((m + n) * K) ^ 2.0));
	else
		tmp = cos(M) * t_0;
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Exp[(-l)], $MachinePrecision]}, If[LessEqual[l, -2.5], t$95$0, If[LessEqual[l, 1.95e-150], N[(t$95$0 * N[(-0.125 * N[Power[N[(N[(m + n), $MachinePrecision] * K), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * t$95$0), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{-\ell}\\
\mathbf{if}\;\ell \leq -2.5:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;\ell \leq 1.95 \cdot 10^{-150}:\\
\;\;\;\;t\_0 \cdot \left(-0.125 \cdot {\left(\left(m + n\right) \cdot K\right)}^{2}\right)\\

\mathbf{else}:\\
\;\;\;\;\cos M \cdot t\_0\\


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

    1. Initial program 75.0%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Taylor expanded in l around inf 18.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-neg18.8%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
    5. Simplified18.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 18.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. *-commutative18.8%

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

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

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

        \[\leadsto e^{-\ell} \cdot \cos \left(\left(0.5 \cdot K\right) \cdot \left(m + \left(-\color{blue}{-1 \cdot n}\right)\right)\right) \]
      5. sub-neg18.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*18.8%

        \[\leadsto e^{-\ell} \cdot \cos \color{blue}{\left(0.5 \cdot \left(K \cdot \left(m - -1 \cdot n\right)\right)\right)} \]
      7. *-commutative18.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-neg18.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-neg18.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-neg18.8%

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

        \[\leadsto e^{-\ell} \cdot \cos \left(0.5 \cdot \left(\color{blue}{\left(n + m\right)} \cdot K\right)\right) \]
    8. Simplified18.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 20.6%

      \[\leadsto e^{-\ell} \cdot \color{blue}{\left(1 + -0.125 \cdot \left({K}^{2} \cdot {\left(m + n\right)}^{2}\right)\right)} \]
    10. Step-by-step derivation
      1. *-commutative20.6%

        \[\leadsto e^{-\ell} \cdot \left(1 + -0.125 \cdot \color{blue}{\left({\left(m + n\right)}^{2} \cdot {K}^{2}\right)}\right) \]
    11. Simplified20.6%

      \[\leadsto e^{-\ell} \cdot \color{blue}{\left(1 + -0.125 \cdot \left({\left(m + n\right)}^{2} \cdot {K}^{2}\right)\right)} \]
    12. Taylor expanded in K around 0 20.8%

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

    if -2.5 < l < 1.9500000000000001e-150

    1. Initial program 73.6%

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

      \[\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-neg9.7%

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

      \[\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 9.7%

      \[\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. *-commutative9.7%

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

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

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

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

        \[\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*9.7%

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

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

        \[\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-neg9.7%

        \[\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-neg9.7%

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

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

      \[\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 8.0%

      \[\leadsto e^{-\ell} \cdot \color{blue}{\left(1 + -0.125 \cdot \left({K}^{2} \cdot {\left(m + n\right)}^{2}\right)\right)} \]
    10. Step-by-step derivation
      1. *-commutative8.0%

        \[\leadsto e^{-\ell} \cdot \left(1 + -0.125 \cdot \color{blue}{\left({\left(m + n\right)}^{2} \cdot {K}^{2}\right)}\right) \]
    11. Simplified8.0%

      \[\leadsto e^{-\ell} \cdot \color{blue}{\left(1 + -0.125 \cdot \left({\left(m + n\right)}^{2} \cdot {K}^{2}\right)\right)} \]
    12. Taylor expanded in K around inf 21.5%

      \[\leadsto e^{-\ell} \cdot \color{blue}{\left(-0.125 \cdot \left({K}^{2} \cdot {\left(m + n\right)}^{2}\right)\right)} \]
    13. Step-by-step derivation
      1. unpow221.5%

        \[\leadsto e^{-\ell} \cdot \left(-0.125 \cdot \left(\color{blue}{\left(K \cdot K\right)} \cdot {\left(m + n\right)}^{2}\right)\right) \]
      2. unpow221.5%

        \[\leadsto e^{-\ell} \cdot \left(-0.125 \cdot \left(\left(K \cdot K\right) \cdot \color{blue}{\left(\left(m + n\right) \cdot \left(m + n\right)\right)}\right)\right) \]
      3. swap-sqr19.2%

        \[\leadsto e^{-\ell} \cdot \left(-0.125 \cdot \color{blue}{\left(\left(K \cdot \left(m + n\right)\right) \cdot \left(K \cdot \left(m + n\right)\right)\right)}\right) \]
      4. unpow219.2%

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

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

    if 1.9500000000000001e-150 < l

    1. Initial program 74.8%

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

      \[\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-neg97.6%

        \[\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. sub-neg97.6%

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

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

        \[\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}}} \]
      5. *-commutative97.6%

        \[\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}} \]
      6. associate--r+97.6%

        \[\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. Simplified97.6%

      \[\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 l around inf 68.3%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    7. Step-by-step derivation
      1. neg-mul-168.3%

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    8. Simplified68.3%

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

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

Alternative 6: 51.9% accurate, 1.9× speedup?

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

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

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


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

    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 55.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. +-commutative55.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. unpow255.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-out57.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. *-commutative57.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. *-commutative57.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. Simplified57.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 40.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+40.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*40.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-140.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-sub40.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. Simplified40.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. Step-by-step derivation
      1. associate-+l-40.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.6200000000000001 < l

    1. Initial program 74.3%

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

      \[\leadsto \color{blue}{\cos \left(-M\right) \cdot e^{\left|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. sub-neg100.0%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(0.5 \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      4. 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}}} \]
      5. *-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}} \]
      6. 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 l around inf 98.6%

      \[\leadsto \cos M \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    7. Step-by-step derivation
      1. neg-mul-198.6%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq 1.62:\\ \;\;\;\;\cos M \cdot e^{\left(m - n\right) + \left(M \cdot \left(M + m\right) - \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \]
  5. 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. remove-double-neg28.8%

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

      \[\leadsto e^{-\ell} \cdot \cos \left(\left(0.5 \cdot K\right) \cdot \left(m + \left(-\color{blue}{-1 \cdot n}\right)\right)\right) \]
    5. sub-neg28.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 18.2%

    \[\leadsto e^{-\ell} \cdot \color{blue}{\left(1 + -0.125 \cdot \left({K}^{2} \cdot {\left(m + n\right)}^{2}\right)\right)} \]
  10. Step-by-step derivation
    1. *-commutative18.2%

      \[\leadsto e^{-\ell} \cdot \left(1 + -0.125 \cdot \color{blue}{\left({\left(m + n\right)}^{2} \cdot {K}^{2}\right)}\right) \]
  11. Simplified18.2%

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

    \[\leadsto \color{blue}{e^{-\ell}} \]
  13. 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)))))))