Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.6% → 96.4%
Time: 23.0s
Alternatives: 14
Speedup: 1.3×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 14 alternatives:

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

Initial Program: 75.6% accurate, 1.0× speedup?

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

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

Alternative 1: 96.4% accurate, 0.7× speedup?

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

\\
\cos M \cdot e^{{\left(\sqrt[3]{\left|m - n\right| - \left({\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2} + \ell\right)}\right)}^{3}}
\end{array}
Derivation
  1. Initial program 78.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 96.1%

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

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

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

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

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

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

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

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

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

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

Alternative 2: 96.4% accurate, 0.7× speedup?

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

\\
\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - e^{\log \left({\left(\left(m + n\right) \cdot 0.5 - M\right)}^{2}\right)}}
\end{array}
Derivation
  1. Initial program 78.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 96.1%

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

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

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

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

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

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

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

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

Alternative 3: 96.4% accurate, 1.0× speedup?

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

\\
\cos M \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}}
\end{array}
Derivation
  1. Initial program 78.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 96.1%

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

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

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

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

Alternative 4: 72.5% accurate, 1.3× speedup?

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

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

\mathbf{elif}\;m \leq -1.8 \cdot 10^{-90}:\\
\;\;\;\;\cos M \cdot e^{-{M}^{2}}\\

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


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

    1. Initial program 72.6%

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

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

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

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

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

    if -0.0018500000000000001 < m < -1.7999999999999999e-90

    1. Initial program 100.0%

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

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

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

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

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

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

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

    if -1.7999999999999999e-90 < m

    1. Initial program 79.8%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 5: 86.6% accurate, 1.3× speedup?

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

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

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


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

    1. Initial program 72.6%

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

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

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

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

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

    if -0.0018500000000000001 < m

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 6: 60.5% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := \cos M \cdot e^{-{M}^{2}}\\
t_1 := m \cdot 0.5 - M\\
\mathbf{if}\;m \leq -0.00185:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\

\mathbf{elif}\;m \leq -9 \cdot 10^{-220}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;m \leq -9.8 \cdot 10^{-247}:\\
\;\;\;\;\cos \left(\left(m + n\right) \cdot \left(0.5 \cdot K\right)\right) \cdot e^{\left(m - n\right) + \left(t\_1 \cdot \left(n + t\_1\right) - \ell\right)}\\

\mathbf{elif}\;m \leq 8.5 \cdot 10^{-198}:\\
\;\;\;\;t\_0\\

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


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

    1. Initial program 72.6%

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

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

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

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

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

    if -0.0018500000000000001 < m < -8.99999999999999934e-220 or -9.8e-247 < m < 8.4999999999999994e-198

    1. Initial program 91.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 94.7%

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

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

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

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

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

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

    if -8.99999999999999934e-220 < m < -9.8e-247

    1. Initial program 100.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 8.4999999999999994e-198 < m

    1. Initial program 73.3%

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -0.00185:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{elif}\;m \leq -9 \cdot 10^{-220}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;m \leq -9.8 \cdot 10^{-247}:\\ \;\;\;\;\cos \left(\left(m + n\right) \cdot \left(0.5 \cdot K\right)\right) \cdot e^{\left(m - n\right) + \left(\left(m \cdot 0.5 - M\right) \cdot \left(n + \left(m \cdot 0.5 - M\right)\right) - \ell\right)}\\ \mathbf{elif}\;m \leq 8.5 \cdot 10^{-198}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{m \cdot \left(M - n \cdot 0.5\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 7: 69.7% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := n \cdot 0.5 - M\\
t_1 := m \cdot 0.5 - M\\
t_2 := \cos M \cdot e^{-{M}^{2}}\\
\mathbf{if}\;M \leq -26.5:\\
\;\;\;\;t\_2\\

\mathbf{elif}\;M \leq -1.02 \cdot 10^{-270}:\\
\;\;\;\;\cos M \cdot e^{t\_0 \cdot \left(m + t\_0\right) + \left(\left(m - n\right) - \ell\right)}\\

\mathbf{elif}\;M \leq 1.6 \cdot 10^{-15}:\\
\;\;\;\;e^{\left(m - n\right) + \left(t\_1 \cdot \left(n + t\_1\right) - \ell\right)} \cdot \cos \left(-M\right)\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if M < -26.5 or 1.6e-15 < M

    1. Initial program 83.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(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

    if -26.5 < M < -1.02e-270

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(n \cdot 0.5 - M\right) \cdot \left(\left(n \cdot 0.5 - M\right) + m\right) - \left(\ell - \color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right)} \]
      11. add-sqr-sqrt23.2%

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

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

    if -1.02e-270 < M < 1.6e-15

    1. Initial program 71.2%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -26.5:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;M \leq -1.02 \cdot 10^{-270}:\\ \;\;\;\;\cos M \cdot e^{\left(n \cdot 0.5 - M\right) \cdot \left(m + \left(n \cdot 0.5 - M\right)\right) + \left(\left(m - n\right) - \ell\right)}\\ \mathbf{elif}\;M \leq 1.6 \cdot 10^{-15}:\\ \;\;\;\;e^{\left(m - n\right) + \left(\left(m \cdot 0.5 - M\right) \cdot \left(n + \left(m \cdot 0.5 - M\right)\right) - \ell\right)} \cdot \cos \left(-M\right)\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 64.7% accurate, 1.3× speedup?

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

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

\mathbf{elif}\;n \leq 2.9 \cdot 10^{-6}:\\
\;\;\;\;\cos M \cdot e^{-{M}^{2}}\\

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


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

    1. Initial program 79.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.4%

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

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

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

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

    if 3.0500000000000001e-232 < n < 2.9000000000000002e-6

    1. Initial program 80.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 94.5%

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

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

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

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

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

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

    if 2.9000000000000002e-6 < n

    1. Initial program 74.7%

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;n \leq 3.05 \cdot 10^{-232}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{elif}\;n \leq 2.9 \cdot 10^{-6}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 9: 54.9% accurate, 1.8× speedup?

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

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

\mathbf{elif}\;\ell \leq 2.5 \cdot 10^{-7}:\\
\;\;\;\;\cos M \cdot e^{m \cdot \left(M - n \cdot 0.5\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if l < -4.7e228

    1. Initial program 92.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. Step-by-step derivation
      1. *-un-lft-identity92.9%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if -4.7e228 < l < 2.49999999999999989e-7

    1. Initial program 78.3%

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.49999999999999989e-7 < l

    1. Initial program 75.4%

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    9. Taylor expanded in M around 0 98.4%

      \[\leadsto \color{blue}{e^{-\ell}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification59.9%

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

Alternative 10: 55.2% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;\ell \leq 2.5 \cdot 10^{-7}:\\
\;\;\;\;\cos M \cdot e^{m \cdot \left(M - n \cdot 0.5\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if l < -1.80000000000000001e248

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{{\left(\sqrt[3]{\left(\left(n \cdot 0.5 - M\right) \cdot \left(-\color{blue}{\left(m + \left(n \cdot 0.5 - M\right)\right)}\right) - \ell\right) + \left|m - n\right|}\right)}^{3}} \]
    10. Applied egg-rr82.0%

      \[\leadsto \cos M \cdot e^{\color{blue}{{\left(\sqrt[3]{\left(\left(n \cdot 0.5 - M\right) \cdot \left(-\left(m + \left(n \cdot 0.5 - M\right)\right)\right) - \ell\right) + \left|m - n\right|}\right)}^{3}}} \]
    11. Step-by-step derivation
      1. rem-cube-cbrt82.0%

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(n \cdot 0.5 - M\right) \cdot \left(\left(n \cdot 0.5 - M\right) + m\right) - \left(\ell - \color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right)} \]
      11. add-sqr-sqrt46.2%

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

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

    if -1.80000000000000001e248 < l < 2.49999999999999989e-7

    1. Initial program 78.6%

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.49999999999999989e-7 < l

    1. Initial program 75.4%

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    9. Taylor expanded in M around 0 98.4%

      \[\leadsto \color{blue}{e^{-\ell}} \]
  3. Recombined 3 regimes into one program.
  4. Final simplification58.4%

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

Alternative 11: 54.8% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq 2.5 \cdot 10^{-7}:\\
\;\;\;\;\cos M \cdot e^{m \cdot \left(M - n \cdot 0.5\right)}\\

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


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

    1. Initial program 79.3%

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.49999999999999989e-7 < l

    1. Initial program 75.4%

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
    9. Taylor expanded in M around 0 98.4%

      \[\leadsto \color{blue}{e^{-\ell}} \]
  3. Recombined 2 regimes into one program.
  4. Final simplification57.7%

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

Alternative 12: 35.5% accurate, 2.1× speedup?

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

\\
\cos M \cdot e^{-\ell}
\end{array}
Derivation
  1. Initial program 78.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 96.1%

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

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

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

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

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

    \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
  9. Final simplification32.5%

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

Alternative 13: 35.2% accurate, 4.2× speedup?

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

\\
e^{-\ell}
\end{array}
Derivation
  1. Initial program 78.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 96.1%

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

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

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

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

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

    \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
  9. Taylor expanded in M around 0 32.5%

    \[\leadsto \color{blue}{e^{-\ell}} \]
  10. Final simplification32.5%

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

Alternative 14: 7.2% accurate, 4.2× speedup?

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

\\
\cos M
\end{array}
Derivation
  1. Initial program 78.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 96.1%

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

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

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

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

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

    \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
  9. Taylor expanded in l around 0 6.9%

    \[\leadsto \color{blue}{\cos M} \]
  10. Final simplification6.9%

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

Reproduce

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