Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.7% → 96.6%
Time: 25.2s
Alternatives: 7
Speedup: 1.3×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 7 alternatives:

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

Initial Program: 75.7% accurate, 1.0× speedup?

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

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

Alternative 1: 96.6% accurate, 0.7× speedup?

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

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

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

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

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

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

      \[\leadsto \cos \left({\left(\sqrt[3]{\color{blue}{\mathsf{fma}\left(K, \frac{m + n}{2}, -M\right)}}\right)}^{3}\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    5. div-inv76.7%

      \[\leadsto \cos \left({\left(\sqrt[3]{\mathsf{fma}\left(K, \color{blue}{\left(m + n\right) \cdot \frac{1}{2}}, -M\right)}\right)}^{3}\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    6. metadata-eval76.7%

      \[\leadsto \cos \left({\left(\sqrt[3]{\mathsf{fma}\left(K, \left(m + n\right) \cdot \color{blue}{0.5}, -M\right)}\right)}^{3}\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
  4. Applied egg-rr76.7%

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

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

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

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

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

Alternative 2: 96.8% accurate, 1.0× speedup?

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

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

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

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

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

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

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

Alternative 3: 85.9% accurate, 1.3× speedup?

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

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

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


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

    1. Initial program 64.7%

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

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

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

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

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

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

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

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

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

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

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

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

    if -2.20000000000000016e56 < m

    1. Initial program 79.6%

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 4: 78.3% accurate, 1.3× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -4.9e-5 or 30.5 < M

    1. Initial program 78.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -4.9e-5 < M < 30.5

    1. Initial program 74.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\left(-\color{blue}{\left(-M\right) \cdot \left(n - M\right)}\right) - \left(\ell - \left|m - n\right|\right)} \]
    12. Step-by-step derivation
      1. distribute-rgt-neg-in36.4%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{\mathsf{fma}\left(-M, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)}} \]
      3. add-sqr-sqrt16.5%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(\color{blue}{\sqrt{-M} \cdot \sqrt{-M}}, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)} \]
      4. sqrt-unprod36.4%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(\color{blue}{\sqrt{\left(-M\right) \cdot \left(-M\right)}}, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)} \]
      5. sqr-neg36.4%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(\sqrt{\color{blue}{M \cdot M}}, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)} \]
      6. sqrt-unprod19.9%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(\color{blue}{\sqrt{M} \cdot \sqrt{M}}, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)} \]
      7. add-sqr-sqrt36.4%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(\color{blue}{M}, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)} \]
      8. sub-neg36.4%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, -\color{blue}{\left(n + \left(-M\right)\right)}, -\left(\ell - \left|m - n\right|\right)\right)} \]
      9. distribute-neg-in36.4%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, \color{blue}{\left(-n\right) + \left(-\left(-M\right)\right)}, -\left(\ell - \left|m - n\right|\right)\right)} \]
      10. remove-double-neg36.4%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, \left(-n\right) + \color{blue}{M}, -\left(\ell - \left|m - n\right|\right)\right)} \]
      11. add-sqr-sqrt21.6%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, \left(-n\right) + M, -\left(\ell - \left|\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right|\right)\right)} \]
      12. fabs-sqr21.6%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, \left(-n\right) + M, -\left(\ell - \color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right)\right)} \]
      13. add-sqr-sqrt58.8%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, \left(-n\right) + M, -\left(\ell - \color{blue}{\left(m - n\right)}\right)\right)} \]
    13. Applied egg-rr58.8%

      \[\leadsto \cos M \cdot e^{\color{blue}{\mathsf{fma}\left(M, \left(-n\right) + M, -\left(\ell - \left(m - n\right)\right)\right)}} \]
    14. Step-by-step derivation
      1. fma-undefine58.8%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot \left(\left(-n\right) + M\right) - \left(\ell - \left(m - n\right)\right)}} \]
      3. neg-mul-158.8%

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{M \cdot \left(M - n\right) - \left(\ell + \left(-\color{blue}{\left(-1 \cdot n + m\right)}\right)\right)} \]
      11. distribute-neg-in58.8%

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

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

        \[\leadsto \cos M \cdot e^{M \cdot \left(M - n\right) - \left(\ell + \left(\color{blue}{n} + \left(-m\right)\right)\right)} \]
      14. sub-neg58.8%

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

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

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

Alternative 5: 48.0% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -9.2 \cdot 10^{+23}:\\
\;\;\;\;\cos M \cdot e^{M \cdot n}\\

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

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


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

    1. Initial program 76.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -9.2000000000000002e23 < M < 3.8999999999999999e117

    1. Initial program 77.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\left(-\color{blue}{\left(-M\right) \cdot \left(n - M\right)}\right) - \left(\ell - \left|m - n\right|\right)} \]
    12. Step-by-step derivation
      1. distribute-rgt-neg-in43.8%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{\mathsf{fma}\left(-M, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)}} \]
      3. add-sqr-sqrt15.8%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(\color{blue}{\sqrt{-M} \cdot \sqrt{-M}}, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)} \]
      4. sqrt-unprod35.8%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(\color{blue}{\sqrt{\left(-M\right) \cdot \left(-M\right)}}, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)} \]
      5. sqr-neg35.8%

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

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(\color{blue}{\sqrt{M} \cdot \sqrt{M}}, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)} \]
      7. add-sqr-sqrt35.2%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(\color{blue}{M}, -\left(n - M\right), -\left(\ell - \left|m - n\right|\right)\right)} \]
      8. sub-neg35.2%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, -\color{blue}{\left(n + \left(-M\right)\right)}, -\left(\ell - \left|m - n\right|\right)\right)} \]
      9. distribute-neg-in35.2%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, \color{blue}{\left(-n\right) + \left(-\left(-M\right)\right)}, -\left(\ell - \left|m - n\right|\right)\right)} \]
      10. remove-double-neg35.2%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, \left(-n\right) + \color{blue}{M}, -\left(\ell - \left|m - n\right|\right)\right)} \]
      11. add-sqr-sqrt20.8%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, \left(-n\right) + M, -\left(\ell - \left|\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right|\right)\right)} \]
      12. fabs-sqr20.8%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, \left(-n\right) + M, -\left(\ell - \color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right)\right)} \]
      13. add-sqr-sqrt54.8%

        \[\leadsto \cos M \cdot e^{\mathsf{fma}\left(M, \left(-n\right) + M, -\left(\ell - \color{blue}{\left(m - n\right)}\right)\right)} \]
    13. Applied egg-rr54.8%

      \[\leadsto \cos M \cdot e^{\color{blue}{\mathsf{fma}\left(M, \left(-n\right) + M, -\left(\ell - \left(m - n\right)\right)\right)}} \]
    14. Step-by-step derivation
      1. fma-undefine54.8%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot \left(\left(-n\right) + M\right) - \left(\ell - \left(m - n\right)\right)}} \]
      3. neg-mul-154.8%

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{M \cdot \left(M - n\right) - \left(\ell + \left(-\color{blue}{\left(-1 \cdot n + m\right)}\right)\right)} \]
      11. distribute-neg-in54.8%

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

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

        \[\leadsto \cos M \cdot e^{M \cdot \left(M - n\right) - \left(\ell + \left(\color{blue}{n} + \left(-m\right)\right)\right)} \]
      14. sub-neg54.8%

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

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

    if 3.8999999999999999e117 < M

    1. Initial program 73.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 6: 47.6% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -1.65 \cdot 10^{+125} \lor \neg \left(\ell \leq 9.2 \cdot 10^{-39}\right):\\
\;\;\;\;\cos M \cdot e^{-\ell}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if l < -1.65000000000000003e125 or 9.20000000000000033e-39 < l

    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. Add Preprocessing
    3. Taylor expanded in K around 0 96.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    14. Simplified67.4%

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

    if -1.65000000000000003e125 < l < 9.20000000000000033e-39

    1. Initial program 79.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq -1.65 \cdot 10^{+125} \lor \neg \left(\ell \leq 9.2 \cdot 10^{-39}\right):\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{M \cdot n}\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 35.2% 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 76.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
  14. Simplified38.4%

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

Reproduce

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