Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.6% → 96.0%
Time: 19.9s
Alternatives: 9
Speedup: 1.4×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 9 alternatives:

AlternativeAccuracySpeedup
The accuracy (vertical axis) and speed (horizontal axis) of each alternatives. Up and to the right is better. The red square shows the initial program, and each blue circle shows an alternative.The line shows the best available speed-accuracy tradeoffs.

Initial Program: 75.6% 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.0% accurate, 1.4× speedup?

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

\\
\cos M \cdot e^{\left(-\ell\right) - {\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}}
\end{array}
Derivation
  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. Taylor expanded in K around 0 95.9%

    \[\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)}} \]
  3. Step-by-step derivation
    1. cos-neg95.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 76.5% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -4.6 \cdot 10^{+39} \lor \neg \left(M \leq 720\right):\\
\;\;\;\;\cos M \cdot e^{-{M}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -4.60000000000000024e39 or 720 < M

    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. 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)}} \]
    3. 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^{\left|m - n\right| - \left(\ell + {\color{blue}{\left(0.5 \cdot \left(m + n\right) + \left(-M\right)\right)}}^{2}\right)} \]
      3. +-commutative99.1%

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
    7. Simplified99.1%

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

    if -4.60000000000000024e39 < M < 720

    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. Applied egg-rr17.2%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -4.6 \cdot 10^{+39} \lor \neg \left(M \leq 720\right):\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{{M}^{2} + \left(\left(m - \ell\right) - n\right)}\\ \end{array} \]

Alternative 3: 66.3% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
t_0 := {m}^{2} \cdot 0.25\\
t_1 := \cos M \cdot e^{-{M}^{2}}\\
\mathbf{if}\;M \leq -4.6 \cdot 10^{+39}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;M \leq -1 \cdot 10^{-88}:\\
\;\;\;\;e^{\left(t_0 + m \cdot \left(n \cdot 0.5 + 1\right)\right) - \left(\ell + n\right)}\\

\mathbf{elif}\;M \leq 4.3 \cdot 10^{-144}:\\
\;\;\;\;e^{\left(m + 0.25 \cdot {n}^{2}\right) - \left(\ell + n\right)}\\

\mathbf{elif}\;M \leq 27:\\
\;\;\;\;e^{t_0 - \left(\ell + n\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if M < -4.60000000000000024e39 or 27 < M

    1. Initial program 72.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. 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)}} \]
    3. 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^{\left|m - n\right| - \left(\ell + {\color{blue}{\left(0.5 \cdot \left(m + n\right) + \left(-M\right)\right)}}^{2}\right)} \]
      3. +-commutative99.1%

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

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

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

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

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

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

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

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

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

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

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

    if -4.60000000000000024e39 < M < -9.99999999999999934e-89

    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. Applied egg-rr18.8%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \color{blue}{\left(n + \ell\right)}} \]
    10. Simplified23.7%

      \[\leadsto \color{blue}{e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(n + \ell\right)}} \]
    11. Taylor expanded in m around inf 56.5%

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

    if -9.99999999999999934e-89 < M < 4.2999999999999999e-144

    1. Initial program 76.5%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Applied egg-rr14.2%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \color{blue}{\left(n + \ell\right)}} \]
    10. Simplified15.9%

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

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

    if 4.2999999999999999e-144 < M < 27

    1. Initial program 80.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. Applied egg-rr27.7%

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(n + \ell\right)}} \]
    11. Taylor expanded in m around inf 40.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -4.6 \cdot 10^{+39}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;M \leq -1 \cdot 10^{-88}:\\ \;\;\;\;e^{\left({m}^{2} \cdot 0.25 + m \cdot \left(n \cdot 0.5 + 1\right)\right) - \left(\ell + n\right)}\\ \mathbf{elif}\;M \leq 4.3 \cdot 10^{-144}:\\ \;\;\;\;e^{\left(m + 0.25 \cdot {n}^{2}\right) - \left(\ell + n\right)}\\ \mathbf{elif}\;M \leq 27:\\ \;\;\;\;e^{{m}^{2} \cdot 0.25 - \left(\ell + n\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \end{array} \]

Alternative 4: 68.1% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;n \leq -4.5 \cdot 10^{-87}:\\
\;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\

\mathbf{elif}\;n \leq 9.5 \cdot 10^{-264}:\\
\;\;\;\;e^{\left(m + 0.25 \cdot {n}^{2}\right) - \left(\ell + n\right)}\\

\mathbf{elif}\;n \leq 54:\\
\;\;\;\;\cos M \cdot e^{\left(-\ell\right) - {M}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if n < -4.49999999999999958e-87

    1. Initial program 70.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. Taylor expanded in K around 0 98.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)}} \]
    3. Step-by-step derivation
      1. cos-neg98.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-neg98.1%

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

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

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

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

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

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

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

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

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

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

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

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

    if -4.49999999999999958e-87 < n < 9.50000000000000012e-264

    1. Initial program 85.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. Applied egg-rr21.4%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \color{blue}{e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(\ell + n\right)}} \]
    9. Step-by-step derivation
      1. +-commutative29.0%

        \[\leadsto e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \color{blue}{\left(n + \ell\right)}} \]
    10. Simplified29.0%

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

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

    if 9.50000000000000012e-264 < n < 54

    1. Initial program 84.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. Taylor expanded in K around 0 92.9%

      \[\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)}} \]
    3. Step-by-step derivation
      1. cos-neg92.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 54 < n

    1. Initial program 66.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. Taylor expanded in K around 0 98.5%

      \[\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)}} \]
    3. Step-by-step derivation
      1. cos-neg98.5%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{{n}^{2} \cdot -0.25}} \]
    7. Simplified97.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -4.5 \cdot 10^{-87}:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;n \leq 9.5 \cdot 10^{-264}:\\ \;\;\;\;e^{\left(m + 0.25 \cdot {n}^{2}\right) - \left(\ell + n\right)}\\ \mathbf{elif}\;n \leq 54:\\ \;\;\;\;\cos M \cdot e^{\left(-\ell\right) - {M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \]

Alternative 5: 75.2% accurate, 1.4× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < -55 or 54 < m

    1. Initial program 67.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. Taylor expanded in K around 0 97.7%

      \[\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)}} \]
    3. Step-by-step derivation
      1. cos-neg97.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -55 < m < 54

    1. Initial program 82.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. Applied egg-rr21.7%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \color{blue}{\left(n + \ell\right)}} \]
    10. Simplified25.8%

      \[\leadsto \color{blue}{e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(n + \ell\right)}} \]
    11. Taylor expanded in m around inf 48.2%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -55 \lor \neg \left(m \leq 54\right):\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{else}:\\ \;\;\;\;e^{{m}^{2} \cdot 0.25 - \left(\ell + n\right)}\\ \end{array} \]

Alternative 6: 63.4% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;n \leq -6 \cdot 10^{-91}:\\
\;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\

\mathbf{elif}\;n \leq 2.4 \cdot 10^{-6}:\\
\;\;\;\;e^{\left(m + 0.25 \cdot {n}^{2}\right) - \left(\ell + n\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if n < -6.0000000000000004e-91

    1. Initial program 70.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. Taylor expanded in K around 0 98.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)}} \]
    3. Step-by-step derivation
      1. cos-neg98.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-neg98.1%

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

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

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

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

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

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

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

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

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

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

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

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

    if -6.0000000000000004e-91 < n < 2.3999999999999999e-6

    1. Initial program 85.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. Applied egg-rr21.7%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \color{blue}{\left(n + \ell\right)}} \]
    10. Simplified27.2%

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

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

    if 2.3999999999999999e-6 < n

    1. Initial program 66.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. Taylor expanded in K around 0 98.5%

      \[\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)}} \]
    3. Step-by-step derivation
      1. cos-neg98.5%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{{n}^{2} \cdot -0.25}} \]
    7. Simplified97.0%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq -6 \cdot 10^{-91}:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;n \leq 2.4 \cdot 10^{-6}:\\ \;\;\;\;e^{\left(m + 0.25 \cdot {n}^{2}\right) - \left(\ell + n\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \]

Alternative 7: 44.7% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;m \leq -20000:\\
\;\;\;\;e^{\left(m + 0.25 \cdot \left(n \cdot \left(n + m \cdot 2\right)\right)\right) - \left(\ell + n\right)}\\

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


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

    1. Initial program 70.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. Applied egg-rr0.7%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \color{blue}{\left(n + \ell\right)}} \]
    10. Simplified5.9%

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

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

        \[\leadsto e^{\left(m + 0.25 \cdot \color{blue}{\left({n}^{2} + 2 \cdot \left(m \cdot n\right)\right)}\right) - \left(n + \ell\right)} \]
      2. unpow252.8%

        \[\leadsto e^{\left(m + 0.25 \cdot \left(\color{blue}{n \cdot n} + 2 \cdot \left(m \cdot n\right)\right)\right) - \left(n + \ell\right)} \]
      3. associate-*r*52.8%

        \[\leadsto e^{\left(m + 0.25 \cdot \left(n \cdot n + \color{blue}{\left(2 \cdot m\right) \cdot n}\right)\right) - \left(n + \ell\right)} \]
      4. distribute-rgt-out55.9%

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

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

    if -2e4 < m

    1. Initial program 76.4%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Applied egg-rr14.3%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \color{blue}{\left(n + \ell\right)}} \]
    10. Simplified18.9%

      \[\leadsto \color{blue}{e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \left(n + \ell\right)}} \]
    11. Taylor expanded in m around inf 35.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -20000:\\ \;\;\;\;e^{\left(m + 0.25 \cdot \left(n \cdot \left(n + m \cdot 2\right)\right)\right) - \left(\ell + n\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{{m}^{2} \cdot 0.25 - \left(\ell + n\right)}\\ \end{array} \]

Alternative 8: 46.2% accurate, 2.1× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq 0.004:\\
\;\;\;\;e^{\left(m + 0.25 \cdot \left(n \cdot \left(n + m \cdot 2\right)\right)\right) - \left(\ell + n\right)}\\

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


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

    1. Initial program 75.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. Applied egg-rr8.7%

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\left(m + 0.25 \cdot {\left(m + n\right)}^{2}\right) - \color{blue}{\left(n + \ell\right)}} \]
    10. Simplified10.2%

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

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

        \[\leadsto e^{\left(m + 0.25 \cdot \color{blue}{\left({n}^{2} + 2 \cdot \left(m \cdot n\right)\right)}\right) - \left(n + \ell\right)} \]
      2. unpow223.2%

        \[\leadsto e^{\left(m + 0.25 \cdot \left(\color{blue}{n \cdot n} + 2 \cdot \left(m \cdot n\right)\right)\right) - \left(n + \ell\right)} \]
      3. associate-*r*23.2%

        \[\leadsto e^{\left(m + 0.25 \cdot \left(n \cdot n + \color{blue}{\left(2 \cdot m\right) \cdot n}\right)\right) - \left(n + \ell\right)} \]
      4. distribute-rgt-out24.8%

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

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

    if 0.0040000000000000001 < l

    1. Initial program 71.7%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. 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)}} \]
    3. 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^{\left|m - n\right| - \left(\ell + {\color{blue}{\left(0.5 \cdot \left(m + n\right) + \left(-M\right)\right)}}^{2}\right)} \]
      3. +-commutative100.0%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\left(-\ell\right) - \color{blue}{{M}^{2}}} \]
    9. Taylor expanded in l around inf 96.8%

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

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

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

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

Alternative 9: 36.2% accurate, 3.7× speedup?

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

\\
e^{\left(m + 0.25 \cdot \left(n \cdot \left(n + m \cdot 2\right)\right)\right) - \left(\ell + n\right)}
\end{array}
Derivation
  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. Applied egg-rr10.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto e^{\left(m + 0.25 \cdot \color{blue}{\left({n}^{2} + 2 \cdot \left(m \cdot n\right)\right)}\right) - \left(n + \ell\right)} \]
    2. unpow230.0%

      \[\leadsto e^{\left(m + 0.25 \cdot \left(\color{blue}{n \cdot n} + 2 \cdot \left(m \cdot n\right)\right)\right) - \left(n + \ell\right)} \]
    3. associate-*r*30.0%

      \[\leadsto e^{\left(m + 0.25 \cdot \left(n \cdot n + \color{blue}{\left(2 \cdot m\right) \cdot n}\right)\right) - \left(n + \ell\right)} \]
    4. distribute-rgt-out31.2%

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

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

    \[\leadsto e^{\left(m + 0.25 \cdot \left(n \cdot \left(n + m \cdot 2\right)\right)\right) - \left(\ell + n\right)} \]

Reproduce

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