Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.3% → 96.9%
Time: 23.9s
Alternatives: 7
Speedup: 1.3×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 7 alternatives:

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

Initial Program: 76.3% accurate, 1.0× speedup?

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

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

Alternative 1: 96.9% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := \left(m + n\right) \cdot 0.5 - M\\
\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - t\_0 \cdot t\_0}
\end{array}
\end{array}
Derivation
  1. Initial program 75.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 95.9%

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

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

    \[\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. Step-by-step derivation
    1. unpow295.9%

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

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

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

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

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

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

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

Alternative 2: 87.8% accurate, 1.3× speedup?

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

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

\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|m - n\right| - \ell\right)}\\


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

    1. Initial program 64.4%

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

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

    if -185 < m

    1. Initial program 78.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 94.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-neg94.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. Simplified94.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 m around 0 79.6%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -185:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \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|m - n\right| - \ell\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 73.7% accurate, 1.3× speedup?

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

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

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


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

    1. Initial program 79.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 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 M around inf 98.3%

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

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

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

    if -2.1000000000000001e24 < M < 27

    1. Initial program 71.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{m - \left(n + \ell\right)} \cdot \cos \left(K \cdot \left(0.5 \cdot m\right) + \color{blue}{K \cdot \left(n \cdot 0.5\right)}\right) \]
      10. distribute-lft-in51.4%

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

        \[\leadsto e^{m - \left(n + \ell\right)} \cdot \cos \left(K \cdot \left(0.5 \cdot m + \color{blue}{0.5 \cdot n}\right)\right) \]
      12. distribute-lft-in51.4%

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

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

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

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

Alternative 4: 51.3% accurate, 1.9× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;e^{-\ell}\\


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

    1. Initial program 76.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. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity76.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 720 < l

    1. Initial program 72.4%

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 45.6% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;m \leq -6.5 \cdot 10^{-9}:\\
\;\;\;\;\cos \left(\frac{\left(m + n\right) \cdot K}{2} - M\right) \cdot e^{m}\\

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


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

    1. Initial program 65.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. Step-by-step derivation
      1. *-un-lft-identity65.0%

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

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

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

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

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

    if -6.5000000000000003e-9 < m

    1. Initial program 78.5%

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

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

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

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

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

Alternative 6: 36.0% 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.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 95.9%

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

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

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

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

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

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

Alternative 7: 35.6% accurate, 4.2× speedup?

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

\\
e^{-\ell}
\end{array}
Derivation
  1. Initial program 75.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. Step-by-step derivation
    1. add-exp-log40.5%

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

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

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

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

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

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

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

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

    \[\leadsto \color{blue}{1} \cdot e^{-\ell} \]
  12. Final simplification34.3%

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

Reproduce

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