Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.1% → 96.9%
Time: 33.6s
Alternatives: 10
Speedup: 1.9×

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 10 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.1% accurate, 1.0× speedup?

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

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

Alternative 1: 96.9% accurate, 1.0× speedup?

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

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

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

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

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

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

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

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

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

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

Alternative 2: 96.3% accurate, 1.4× speedup?

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

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

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

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

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

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

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

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

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

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

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

Alternative 3: 85.8% accurate, 1.4× speedup?

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

\\
\begin{array}{l}
t_0 := \left|m - n\right|\\
\mathbf{if}\;m \leq -3.25 \cdot 10^{+106}:\\
\;\;\;\;e^{t\_0 - 0.25 \cdot {m}^{2}}\\

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


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

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

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

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

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

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

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

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

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

      \[\leadsto 1 \cdot e^{\left|m - n\right| - \color{blue}{0.25 \cdot {m}^{2}}} \]

    if -3.2500000000000001e106 < m

    1. Initial program 76.4%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 4: 71.9% accurate, 1.9× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -0.86 \lor \neg \left(M \leq 6.5 \cdot 10^{-31}\right):\\
\;\;\;\;e^{\left|m - n\right| + \left(\left(\left(n + m \cdot 0.5\right) - M\right) \cdot \left(M - m \cdot 0.5\right) - \ell\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -0.859999999999999987 or 6.49999999999999967e-31 < M

    1. Initial program 75.0%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto 1 \cdot e^{\left|m - n\right| - \left(\ell + \left(\left(m \cdot 0.5 - M\right) \cdot n + \color{blue}{\left(m \cdot 0.5 - M\right) \cdot \left(m \cdot 0.5 - M\right)}\right)\right)} \]
      5. distribute-lft-in90.0%

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

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

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

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

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

    if -0.859999999999999987 < M < 6.49999999999999967e-31

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\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) - \left(\left(\ell - m\right) + n\right)} \]
      3. distribute-rgt-out40.0%

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 37.8% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;M \leq 1.22 \cdot 10^{+69}:\\
\;\;\;\;\cos \left(\left(n \cdot 0.5\right) \cdot K - M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\

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


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

    1. Initial program 69.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. Applied egg-rr2.3%

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\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) - \left(\left(\ell - m\right) + n\right)} \]
      3. distribute-rgt-out6.4%

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

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

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

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

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

    if -8.6e39 < M < 1.22e69

    1. Initial program 73.9%

      \[\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. Applied egg-rr23.0%

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\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) - \left(\left(\ell - m\right) + n\right)} \]
      3. distribute-rgt-out36.2%

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

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

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

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

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

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

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

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

        \[\leadsto \cos \color{blue}{\left(\mathsf{fma}\left(0.5, K \cdot n, -M\right)\right)} \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      2. *-lft-identity44.7%

        \[\leadsto \cos \left(\mathsf{fma}\left(0.5, \color{blue}{1 \cdot \left(K \cdot n\right)}, -M\right)\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      3. pow-base-144.7%

        \[\leadsto \cos \left(\mathsf{fma}\left(0.5, \color{blue}{{1}^{0.3333333333333333}} \cdot \left(K \cdot n\right), -M\right)\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      4. fma-neg44.7%

        \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left({1}^{0.3333333333333333} \cdot \left(K \cdot n\right)\right) - M\right)} \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      5. pow-base-144.7%

        \[\leadsto \cos \left(0.5 \cdot \left(\color{blue}{1} \cdot \left(K \cdot n\right)\right) - M\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      6. *-lft-identity44.7%

        \[\leadsto \cos \left(0.5 \cdot \color{blue}{\left(K \cdot n\right)} - M\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      7. associate-*r*44.7%

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

        \[\leadsto \cos \left(\color{blue}{\left(K \cdot 0.5\right)} \cdot n - M\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      9. associate-*l*44.7%

        \[\leadsto \cos \left(\color{blue}{K \cdot \left(0.5 \cdot n\right)} - M\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      10. associate-*r*44.7%

        \[\leadsto \cos \left(K \cdot \left(0.5 \cdot n\right) - M\right) \cdot e^{\color{blue}{\left(-1 \cdot M\right) \cdot \left(n - M\right)} - \left(\ell + n\right)} \]
      11. neg-mul-144.7%

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

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

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

    if 1.22e69 < M

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\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) - \left(\left(\ell - m\right) + n\right)} \]
      3. distribute-rgt-out4.6%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -8.6 \cdot 10^{+39}:\\ \;\;\;\;\cos \left(\left(\left(m + n\right) \cdot 0.5\right) \cdot K - M\right) \cdot e^{-\ell}\\ \mathbf{elif}\;M \leq 1.22 \cdot 10^{+69}:\\ \;\;\;\;\cos \left(\left(n \cdot 0.5\right) \cdot K - M\right) \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\left(\left(m + n\right) \cdot 0.5\right) \cdot K - M\right) \cdot e^{n \cdot \left(m \cdot 0.5 + \left(-1 - M\right)\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 6: 41.7% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;M \leq 6 \cdot 10^{+74}:\\
\;\;\;\;\cos \left(\left(n \cdot 0.5\right) \cdot K - M\right) \cdot e^{M \cdot \left(M - n\right) + \left(\left(m - \ell\right) - n\right)}\\

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


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

    1. Initial program 69.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. Applied egg-rr2.3%

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\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) - \left(\left(\ell - m\right) + n\right)} \]
      3. distribute-rgt-out6.4%

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

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

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

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

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

    if -1.85e43 < M < 6e74

    1. Initial program 73.9%

      \[\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. Applied egg-rr23.0%

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\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) - \left(\left(\ell - m\right) + n\right)} \]
      3. distribute-rgt-out36.2%

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

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

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

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

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

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

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

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

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

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

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

    if 6e74 < M

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\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) - \left(\left(\ell - m\right) + n\right)} \]
      3. distribute-rgt-out4.6%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -1.85 \cdot 10^{+43}:\\ \;\;\;\;\cos \left(\left(\left(m + n\right) \cdot 0.5\right) \cdot K - M\right) \cdot e^{-\ell}\\ \mathbf{elif}\;M \leq 6 \cdot 10^{+74}:\\ \;\;\;\;\cos \left(\left(n \cdot 0.5\right) \cdot K - M\right) \cdot e^{M \cdot \left(M - n\right) + \left(\left(m - \ell\right) - n\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(\left(\left(m + n\right) \cdot 0.5\right) \cdot K - M\right) \cdot e^{n \cdot \left(m \cdot 0.5 + \left(-1 - M\right)\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 84.8% accurate, 1.9× speedup?

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

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

\mathbf{else}:\\
\;\;\;\;e^{t\_0 - \left(\ell + t\_1 \cdot \left(m + t\_1\right)\right)}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if n < 2.0999999999999998e93

    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 93.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.0999999999999998e93 < n

    1. Initial program 70.5%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 8: 37.7% accurate, 1.9× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if l < 3.20000000000000003e-34

    1. Initial program 73.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. Applied egg-rr11.0%

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\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) - \left(\left(\ell - m\right) + n\right)} \]
      3. distribute-rgt-out20.2%

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

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

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

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

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

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

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

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

        \[\leadsto \cos \color{blue}{\left(\mathsf{fma}\left(0.5, K \cdot n, -M\right)\right)} \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      2. *-lft-identity22.9%

        \[\leadsto \cos \left(\mathsf{fma}\left(0.5, \color{blue}{1 \cdot \left(K \cdot n\right)}, -M\right)\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      3. pow-base-122.9%

        \[\leadsto \cos \left(\mathsf{fma}\left(0.5, \color{blue}{{1}^{0.3333333333333333}} \cdot \left(K \cdot n\right), -M\right)\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      4. fma-neg22.9%

        \[\leadsto \cos \color{blue}{\left(0.5 \cdot \left({1}^{0.3333333333333333} \cdot \left(K \cdot n\right)\right) - M\right)} \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      5. pow-base-122.9%

        \[\leadsto \cos \left(0.5 \cdot \left(\color{blue}{1} \cdot \left(K \cdot n\right)\right) - M\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      6. *-lft-identity22.9%

        \[\leadsto \cos \left(0.5 \cdot \color{blue}{\left(K \cdot n\right)} - M\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      7. associate-*r*22.9%

        \[\leadsto \cos \left(\color{blue}{\left(0.5 \cdot K\right) \cdot n} - M\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      8. *-commutative22.9%

        \[\leadsto \cos \left(\color{blue}{\left(K \cdot 0.5\right)} \cdot n - M\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      9. associate-*l*22.9%

        \[\leadsto \cos \left(\color{blue}{K \cdot \left(0.5 \cdot n\right)} - M\right) \cdot e^{-1 \cdot \left(M \cdot \left(n - M\right)\right) - \left(\ell + n\right)} \]
      10. associate-*r*22.9%

        \[\leadsto \cos \left(K \cdot \left(0.5 \cdot n\right) - M\right) \cdot e^{\color{blue}{\left(-1 \cdot M\right) \cdot \left(n - M\right)} - \left(\ell + n\right)} \]
      11. neg-mul-122.9%

        \[\leadsto \cos \left(K \cdot \left(0.5 \cdot n\right) - M\right) \cdot e^{\color{blue}{\left(-M\right)} \cdot \left(n - M\right) - \left(\ell + n\right)} \]
      12. +-commutative22.9%

        \[\leadsto \cos \left(K \cdot \left(0.5 \cdot n\right) - M\right) \cdot e^{\left(-M\right) \cdot \left(n - M\right) - \color{blue}{\left(n + \ell\right)}} \]
    14. Simplified22.9%

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

    if 3.20000000000000003e-34 < l

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\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) - \left(\left(\ell - m\right) + n\right)} \]
      3. distribute-rgt-out27.7%

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

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

      \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\color{blue}{-1 \cdot \ell}} \]
    10. Step-by-step derivation
      1. mul-1-neg77.0%

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

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

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

Alternative 9: 30.1% accurate, 2.0× speedup?

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

\\
\cos \left(\left(\left(m + n\right) \cdot 0.5\right) \cdot K - M\right) \cdot e^{-\ell}
\end{array}
Derivation
  1. Initial program 75.7%

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos \left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right) \cdot e^{\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) - \left(\left(\ell - m\right) + n\right)} \]
    3. distribute-rgt-out22.1%

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

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

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

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

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

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

Alternative 10: 24.2% accurate, 2.1× speedup?

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

\\
e^{\left|m - n\right| - \ell}
\end{array}
Derivation
  1. Initial program 75.7%

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

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

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

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

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

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

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

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

    \[\leadsto 1 \cdot e^{\left|m - n\right| - \color{blue}{\ell}} \]
  8. Final simplification20.6%

    \[\leadsto e^{\left|m - n\right| - \ell} \]
  9. Add Preprocessing

Reproduce

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