Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.5% → 96.8%
Time: 22.6s
Alternatives: 8
Speedup: 1.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 8 alternatives:

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

Initial Program: 76.5% accurate, 1.0× speedup?

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

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

Alternative 1: 96.8% accurate, 1.0× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 2: 76.4% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;m \leq -16500:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq -5.5 \cdot 10^{-83} \lor \neg \left(m \leq -3.8 \cdot 10^{-124}\right):\\ \;\;\;\;-0.5 \cdot \left(K \cdot \left(n \cdot \left(e^{\left|n - m\right| + \left(\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) - \ell\right)} \cdot \sin \left(0.5 \cdot \left(m \cdot K\right) - M\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos \left({\left(\sqrt[3]{K \cdot \left(\left(m + n\right) \cdot 0.5\right)}\right)}^{3} - M\right) \cdot e^{-\ell}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= m -16500.0)
   (* (cos M) (exp (* (pow m 2.0) -0.25)))
   (if (or (<= m -5.5e-83) (not (<= m -3.8e-124)))
     (*
      -0.5
      (*
       K
       (*
        n
        (*
         (exp
          (+ (fabs (- n m)) (- (* (- (* n 0.5) M) (- (- M (* n 0.5)) m)) l)))
         (sin (- (* 0.5 (* m K)) M))))))
     (* (cos (- (pow (cbrt (* K (* (+ m n) 0.5))) 3.0) M)) (exp (- l))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -16500.0) {
		tmp = cos(M) * exp((pow(m, 2.0) * -0.25));
	} else if ((m <= -5.5e-83) || !(m <= -3.8e-124)) {
		tmp = -0.5 * (K * (n * (exp((fabs((n - m)) + ((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) - l))) * sin(((0.5 * (m * K)) - M)))));
	} else {
		tmp = cos((pow(cbrt((K * ((m + n) * 0.5))), 3.0) - M)) * exp(-l);
	}
	return tmp;
}
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -16500.0) {
		tmp = Math.cos(M) * Math.exp((Math.pow(m, 2.0) * -0.25));
	} else if ((m <= -5.5e-83) || !(m <= -3.8e-124)) {
		tmp = -0.5 * (K * (n * (Math.exp((Math.abs((n - m)) + ((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) - l))) * Math.sin(((0.5 * (m * K)) - M)))));
	} else {
		tmp = Math.cos((Math.pow(Math.cbrt((K * ((m + n) * 0.5))), 3.0) - M)) * Math.exp(-l);
	}
	return tmp;
}
function code(K, m, n, M, l)
	tmp = 0.0
	if (m <= -16500.0)
		tmp = Float64(cos(M) * exp(Float64((m ^ 2.0) * -0.25)));
	elseif ((m <= -5.5e-83) || !(m <= -3.8e-124))
		tmp = Float64(-0.5 * Float64(K * Float64(n * Float64(exp(Float64(abs(Float64(n - m)) + Float64(Float64(Float64(Float64(n * 0.5) - M) * Float64(Float64(M - Float64(n * 0.5)) - m)) - l))) * sin(Float64(Float64(0.5 * Float64(m * K)) - M))))));
	else
		tmp = Float64(cos(Float64((cbrt(Float64(K * Float64(Float64(m + n) * 0.5))) ^ 3.0) - M)) * exp(Float64(-l)));
	end
	return tmp
end
code[K_, m_, n_, M_, l_] := If[LessEqual[m, -16500.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[m, -5.5e-83], N[Not[LessEqual[m, -3.8e-124]], $MachinePrecision]], N[(-0.5 * N[(K * N[(n * N[(N[Exp[N[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] + N[(N[(N[(N[(n * 0.5), $MachinePrecision] - M), $MachinePrecision] * N[(N[(M - N[(n * 0.5), $MachinePrecision]), $MachinePrecision] - m), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(N[(0.5 * N[(m * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Cos[N[(N[Power[N[Power[N[(K * N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision]), $MachinePrecision], 1/3], $MachinePrecision], 3.0], $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;m \leq -5.5 \cdot 10^{-83} \lor \neg \left(m \leq -3.8 \cdot 10^{-124}\right):\\
\;\;\;\;-0.5 \cdot \left(K \cdot \left(n \cdot \left(e^{\left|n - m\right| + \left(\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) - \ell\right)} \cdot \sin \left(0.5 \cdot \left(m \cdot K\right) - M\right)\right)\right)\right)\\

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


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

    1. Initial program 71.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -16500 < m < -5.49999999999999964e-83 or -3.80000000000000012e-124 < m

    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. Step-by-step derivation
      1. associate-/l*76.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto -0.5 \cdot \left(K \cdot \left(n \cdot \left(e^{\left|n - m\right| - \left(\ell + \color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(\left(0.5 \cdot n - M\right) + m\right)}\right)} \cdot \sin \left(0.5 \cdot \left(K \cdot m\right) - M\right)\right)\right)\right) \]
    9. Simplified76.0%

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

    if -5.49999999999999964e-83 < m < -3.80000000000000012e-124

    1. Initial program 74.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. Step-by-step derivation
      1. associate-/l*74.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -16500:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq -5.5 \cdot 10^{-83} \lor \neg \left(m \leq -3.8 \cdot 10^{-124}\right):\\ \;\;\;\;-0.5 \cdot \left(K \cdot \left(n \cdot \left(e^{\left|n - m\right| + \left(\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) - \ell\right)} \cdot \sin \left(0.5 \cdot \left(m \cdot K\right) - M\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos \left({\left(\sqrt[3]{K \cdot \left(\left(m + n\right) \cdot 0.5\right)}\right)}^{3} - M\right) \cdot e^{-\ell}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 76.3% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;m \leq -108000:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq -5.5 \cdot 10^{-83} \lor \neg \left(m \leq -3.8 \cdot 10^{-124}\right):\\ \;\;\;\;-0.5 \cdot \left(K \cdot \left(n \cdot \left(e^{\left|n - m\right| + \left(\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) - \ell\right)} \cdot \sin \left(0.5 \cdot \left(m \cdot K\right) - M\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell} \cdot \cos \left(0.5 \cdot \left(n \cdot K\right)\right)\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= m -108000.0)
   (* (cos M) (exp (* (pow m 2.0) -0.25)))
   (if (or (<= m -5.5e-83) (not (<= m -3.8e-124)))
     (*
      -0.5
      (*
       K
       (*
        n
        (*
         (exp
          (+ (fabs (- n m)) (- (* (- (* n 0.5) M) (- (- M (* n 0.5)) m)) l)))
         (sin (- (* 0.5 (* m K)) M))))))
     (* (exp (- l)) (cos (* 0.5 (* n K)))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -108000.0) {
		tmp = cos(M) * exp((pow(m, 2.0) * -0.25));
	} else if ((m <= -5.5e-83) || !(m <= -3.8e-124)) {
		tmp = -0.5 * (K * (n * (exp((fabs((n - m)) + ((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) - l))) * sin(((0.5 * (m * K)) - M)))));
	} else {
		tmp = exp(-l) * cos((0.5 * (n * K)));
	}
	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 <= (-108000.0d0)) then
        tmp = cos(m_1) * exp(((m ** 2.0d0) * (-0.25d0)))
    else if ((m <= (-5.5d-83)) .or. (.not. (m <= (-3.8d-124)))) then
        tmp = (-0.5d0) * (k * (n * (exp((abs((n - m)) + ((((n * 0.5d0) - m_1) * ((m_1 - (n * 0.5d0)) - m)) - l))) * sin(((0.5d0 * (m * k)) - m_1)))))
    else
        tmp = exp(-l) * cos((0.5d0 * (n * k)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -108000.0) {
		tmp = Math.cos(M) * Math.exp((Math.pow(m, 2.0) * -0.25));
	} else if ((m <= -5.5e-83) || !(m <= -3.8e-124)) {
		tmp = -0.5 * (K * (n * (Math.exp((Math.abs((n - m)) + ((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) - l))) * Math.sin(((0.5 * (m * K)) - M)))));
	} else {
		tmp = Math.exp(-l) * Math.cos((0.5 * (n * K)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if m <= -108000.0:
		tmp = math.cos(M) * math.exp((math.pow(m, 2.0) * -0.25))
	elif (m <= -5.5e-83) or not (m <= -3.8e-124):
		tmp = -0.5 * (K * (n * (math.exp((math.fabs((n - m)) + ((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) - l))) * math.sin(((0.5 * (m * K)) - M)))))
	else:
		tmp = math.exp(-l) * math.cos((0.5 * (n * K)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (m <= -108000.0)
		tmp = Float64(cos(M) * exp(Float64((m ^ 2.0) * -0.25)));
	elseif ((m <= -5.5e-83) || !(m <= -3.8e-124))
		tmp = Float64(-0.5 * Float64(K * Float64(n * Float64(exp(Float64(abs(Float64(n - m)) + Float64(Float64(Float64(Float64(n * 0.5) - M) * Float64(Float64(M - Float64(n * 0.5)) - m)) - l))) * sin(Float64(Float64(0.5 * Float64(m * K)) - M))))));
	else
		tmp = Float64(exp(Float64(-l)) * cos(Float64(0.5 * Float64(n * K))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (m <= -108000.0)
		tmp = cos(M) * exp(((m ^ 2.0) * -0.25));
	elseif ((m <= -5.5e-83) || ~((m <= -3.8e-124)))
		tmp = -0.5 * (K * (n * (exp((abs((n - m)) + ((((n * 0.5) - M) * ((M - (n * 0.5)) - m)) - l))) * sin(((0.5 * (m * K)) - M)))));
	else
		tmp = exp(-l) * cos((0.5 * (n * K)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[m, -108000.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[m, -5.5e-83], N[Not[LessEqual[m, -3.8e-124]], $MachinePrecision]], N[(-0.5 * N[(K * N[(n * N[(N[Exp[N[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] + N[(N[(N[(N[(n * 0.5), $MachinePrecision] - M), $MachinePrecision] * N[(N[(M - N[(n * 0.5), $MachinePrecision]), $MachinePrecision] - m), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Sin[N[(N[(0.5 * N[(m * K), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision], N[(N[Exp[(-l)], $MachinePrecision] * N[Cos[N[(0.5 * N[(n * K), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;m \leq -5.5 \cdot 10^{-83} \lor \neg \left(m \leq -3.8 \cdot 10^{-124}\right):\\
\;\;\;\;-0.5 \cdot \left(K \cdot \left(n \cdot \left(e^{\left|n - m\right| + \left(\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) - \ell\right)} \cdot \sin \left(0.5 \cdot \left(m \cdot K\right) - M\right)\right)\right)\right)\\

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


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

    1. Initial program 71.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -108000 < m < -5.49999999999999964e-83 or -3.80000000000000012e-124 < m

    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. Step-by-step derivation
      1. associate-/l*76.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto -0.5 \cdot \left(K \cdot \left(n \cdot \left(e^{\left|n - m\right| - \left(\ell + \color{blue}{\left(0.5 \cdot n - M\right) \cdot \left(\left(0.5 \cdot n - M\right) + m\right)}\right)} \cdot \sin \left(0.5 \cdot \left(K \cdot m\right) - M\right)\right)\right)\right) \]
    9. Simplified76.0%

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

    if -5.49999999999999964e-83 < m < -3.80000000000000012e-124

    1. Initial program 74.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. Step-by-step derivation
      1. associate-/l*74.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -108000:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq -5.5 \cdot 10^{-83} \lor \neg \left(m \leq -3.8 \cdot 10^{-124}\right):\\ \;\;\;\;-0.5 \cdot \left(K \cdot \left(n \cdot \left(e^{\left|n - m\right| + \left(\left(n \cdot 0.5 - M\right) \cdot \left(\left(M - n \cdot 0.5\right) - m\right) - \ell\right)} \cdot \sin \left(0.5 \cdot \left(m \cdot K\right) - M\right)\right)\right)\right)\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell} \cdot \cos \left(0.5 \cdot \left(n \cdot K\right)\right)\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 72.3% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos M \cdot e^{-{M}^{2}}\\ t_1 := \cos M \cdot e^{{m}^{2} \cdot -0.25}\\ t_2 := e^{-\ell}\\ \mathbf{if}\;m \leq -14500:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;m \leq -1.7 \cdot 10^{-30}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;m \leq -8.2 \cdot 10^{-194}:\\ \;\;\;\;\cos M \cdot t\_2\\ \mathbf{elif}\;m \leq 3.5 \cdot 10^{-221}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;m \leq 55:\\ \;\;\;\;t\_2 \cdot \cos \left(K \cdot \left(m \cdot 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* (cos M) (exp (- (pow M 2.0)))))
        (t_1 (* (cos M) (exp (* (pow m 2.0) -0.25))))
        (t_2 (exp (- l))))
   (if (<= m -14500.0)
     t_1
     (if (<= m -1.7e-30)
       t_0
       (if (<= m -8.2e-194)
         (* (cos M) t_2)
         (if (<= m 3.5e-221)
           t_0
           (if (<= m 55.0) (* t_2 (cos (* K (* m 0.5)))) t_1)))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = cos(M) * exp(-pow(M, 2.0));
	double t_1 = cos(M) * exp((pow(m, 2.0) * -0.25));
	double t_2 = exp(-l);
	double tmp;
	if (m <= -14500.0) {
		tmp = t_1;
	} else if (m <= -1.7e-30) {
		tmp = t_0;
	} else if (m <= -8.2e-194) {
		tmp = cos(M) * t_2;
	} else if (m <= 3.5e-221) {
		tmp = t_0;
	} else if (m <= 55.0) {
		tmp = t_2 * cos((K * (m * 0.5)));
	} 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) :: t_2
    real(8) :: tmp
    t_0 = cos(m_1) * exp(-(m_1 ** 2.0d0))
    t_1 = cos(m_1) * exp(((m ** 2.0d0) * (-0.25d0)))
    t_2 = exp(-l)
    if (m <= (-14500.0d0)) then
        tmp = t_1
    else if (m <= (-1.7d-30)) then
        tmp = t_0
    else if (m <= (-8.2d-194)) then
        tmp = cos(m_1) * t_2
    else if (m <= 3.5d-221) then
        tmp = t_0
    else if (m <= 55.0d0) then
        tmp = t_2 * cos((k * (m * 0.5d0)))
    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.cos(M) * Math.exp(-Math.pow(M, 2.0));
	double t_1 = Math.cos(M) * Math.exp((Math.pow(m, 2.0) * -0.25));
	double t_2 = Math.exp(-l);
	double tmp;
	if (m <= -14500.0) {
		tmp = t_1;
	} else if (m <= -1.7e-30) {
		tmp = t_0;
	} else if (m <= -8.2e-194) {
		tmp = Math.cos(M) * t_2;
	} else if (m <= 3.5e-221) {
		tmp = t_0;
	} else if (m <= 55.0) {
		tmp = t_2 * Math.cos((K * (m * 0.5)));
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.cos(M) * math.exp(-math.pow(M, 2.0))
	t_1 = math.cos(M) * math.exp((math.pow(m, 2.0) * -0.25))
	t_2 = math.exp(-l)
	tmp = 0
	if m <= -14500.0:
		tmp = t_1
	elif m <= -1.7e-30:
		tmp = t_0
	elif m <= -8.2e-194:
		tmp = math.cos(M) * t_2
	elif m <= 3.5e-221:
		tmp = t_0
	elif m <= 55.0:
		tmp = t_2 * math.cos((K * (m * 0.5)))
	else:
		tmp = t_1
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(cos(M) * exp(Float64(-(M ^ 2.0))))
	t_1 = Float64(cos(M) * exp(Float64((m ^ 2.0) * -0.25)))
	t_2 = exp(Float64(-l))
	tmp = 0.0
	if (m <= -14500.0)
		tmp = t_1;
	elseif (m <= -1.7e-30)
		tmp = t_0;
	elseif (m <= -8.2e-194)
		tmp = Float64(cos(M) * t_2);
	elseif (m <= 3.5e-221)
		tmp = t_0;
	elseif (m <= 55.0)
		tmp = Float64(t_2 * cos(Float64(K * Float64(m * 0.5))));
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = cos(M) * exp(-(M ^ 2.0));
	t_1 = cos(M) * exp(((m ^ 2.0) * -0.25));
	t_2 = exp(-l);
	tmp = 0.0;
	if (m <= -14500.0)
		tmp = t_1;
	elseif (m <= -1.7e-30)
		tmp = t_0;
	elseif (m <= -8.2e-194)
		tmp = cos(M) * t_2;
	elseif (m <= 3.5e-221)
		tmp = t_0;
	elseif (m <= 55.0)
		tmp = t_2 * cos((K * (m * 0.5)));
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[Cos[M], $MachinePrecision] * N[Exp[(-N[Power[M, 2.0], $MachinePrecision])], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[Power[m, 2.0], $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$2 = N[Exp[(-l)], $MachinePrecision]}, If[LessEqual[m, -14500.0], t$95$1, If[LessEqual[m, -1.7e-30], t$95$0, If[LessEqual[m, -8.2e-194], N[(N[Cos[M], $MachinePrecision] * t$95$2), $MachinePrecision], If[LessEqual[m, 3.5e-221], t$95$0, If[LessEqual[m, 55.0], N[(t$95$2 * N[Cos[N[(K * N[(m * 0.5), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], t$95$1]]]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos M \cdot e^{-{M}^{2}}\\
t_1 := \cos M \cdot e^{{m}^{2} \cdot -0.25}\\
t_2 := e^{-\ell}\\
\mathbf{if}\;m \leq -14500:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;m \leq -1.7 \cdot 10^{-30}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;m \leq -8.2 \cdot 10^{-194}:\\
\;\;\;\;\cos M \cdot t\_2\\

\mathbf{elif}\;m \leq 3.5 \cdot 10^{-221}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;m \leq 55:\\
\;\;\;\;t\_2 \cdot \cos \left(K \cdot \left(m \cdot 0.5\right)\right)\\

\mathbf{else}:\\
\;\;\;\;t\_1\\


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

    1. Initial program 68.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -14500 < m < -1.7000000000000001e-30 or -8.2000000000000005e-194 < m < 3.4999999999999999e-221

    1. Initial program 90.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. Step-by-step derivation
      1. associate-/l*90.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
    10. Simplified59.3%

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

    if -1.7000000000000001e-30 < m < -8.2000000000000005e-194

    1. Initial program 86.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. Step-by-step derivation
      1. associate-/l*86.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-\ell} \]
    10. Simplified54.1%

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

    if 3.4999999999999999e-221 < m < 55

    1. Initial program 62.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left(K \cdot m\right)\right)} \cdot e^{-\ell} \]
    9. Step-by-step derivation
      1. *-commutative59.3%

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

        \[\leadsto \cos \color{blue}{\left(\left(0.5 \cdot m\right) \cdot K\right)} \cdot e^{-\ell} \]
    10. Simplified59.3%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -14500:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq -1.7 \cdot 10^{-30}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;m \leq -8.2 \cdot 10^{-194}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \mathbf{elif}\;m \leq 3.5 \cdot 10^{-221}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;m \leq 55:\\ \;\;\;\;e^{-\ell} \cdot \cos \left(K \cdot \left(m \cdot 0.5\right)\right)\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 64.0% accurate, 1.3× speedup?

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

\mathbf{elif}\;m \leq -1.15 \cdot 10^{-30}:\\
\;\;\;\;t\_0\\

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

\mathbf{elif}\;m \leq 4.6 \cdot 10^{-222}:\\
\;\;\;\;t\_0\\

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


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

    1. Initial program 71.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -14500 < m < -1.14999999999999992e-30 or -5.20000000000000003e-194 < m < 4.6000000000000003e-222

    1. Initial program 90.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. Step-by-step derivation
      1. associate-/l*90.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{-{M}^{2}}} \]
    10. Simplified59.3%

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

    if -1.14999999999999992e-30 < m < -5.20000000000000003e-194

    1. Initial program 86.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. Step-by-step derivation
      1. associate-/l*86.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-\ell} \]
    10. Simplified54.1%

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

    if 4.6000000000000003e-222 < m

    1. Initial program 64.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -14500:\\ \;\;\;\;\cos M \cdot e^{{m}^{2} \cdot -0.25}\\ \mathbf{elif}\;m \leq -1.15 \cdot 10^{-30}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;m \leq -5.2 \cdot 10^{-194}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \mathbf{elif}\;m \leq 4.6 \cdot 10^{-222}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 69.1% accurate, 1.3× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -9.5000000000000007e-9 or 6.79999999999999982 < M

    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. Step-by-step derivation
      1. associate-/l*77.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -9.5000000000000007e-9 < M < 6.79999999999999982

    1. Initial program 74.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -9.5 \cdot 10^{-9} \lor \neg \left(M \leq 6.8\right):\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 35.9% accurate, 2.1× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 8: 7.3% accurate, 4.2× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot m\right) - M\right)} \]
  9. Step-by-step derivation
    1. *-commutative7.5%

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

      \[\leadsto \cos \left(\color{blue}{\left(0.5 \cdot m\right) \cdot K} - M\right) \]
  10. Simplified7.5%

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

    \[\leadsto \color{blue}{\cos \left(-M\right)} \]
  12. Step-by-step derivation
    1. cos-neg7.8%

      \[\leadsto \color{blue}{\cos M} \]
  13. Simplified7.8%

    \[\leadsto \color{blue}{\cos M} \]
  14. Final simplification7.8%

    \[\leadsto \cos M \]
  15. Add Preprocessing

Reproduce

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