Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.5% → 96.5%
Time: 42.8s
Alternatives: 12
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 12 alternatives:

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

Initial Program: 76.5% accurate, 1.0× speedup?

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

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

Alternative 1: 96.5% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \sqrt[3]{\mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}\right)}\\ \cos M \cdot \mathsf{fma}\left({t\_0}^{2}, t\_0, 1\right) \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0
         (cbrt
          (expm1 (- (- (fabs (- n m)) l) (pow (- (* 0.5 (+ n m)) M) 2.0))))))
   (* (cos M) (fma (pow t_0 2.0) t_0 1.0))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = cbrt(expm1(((fabs((n - m)) - l) - pow(((0.5 * (n + m)) - M), 2.0))));
	return cos(M) * fma(pow(t_0, 2.0), t_0, 1.0);
}
function code(K, m, n, M, l)
	t_0 = cbrt(expm1(Float64(Float64(abs(Float64(n - m)) - l) - (Float64(Float64(0.5 * Float64(n + m)) - M) ^ 2.0))))
	return Float64(cos(M) * fma((t_0 ^ 2.0), t_0, 1.0))
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Power[N[(Exp[N[(N[(N[Abs[N[(n - m), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision] - N[Power[N[(N[(0.5 * N[(n + m), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]] - 1), $MachinePrecision], 1/3], $MachinePrecision]}, N[(N[Cos[M], $MachinePrecision] * N[(N[Power[t$95$0, 2.0], $MachinePrecision] * t$95$0 + 1.0), $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \sqrt[3]{\mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}\right)}\\
\cos M \cdot \mathsf{fma}\left({t\_0}^{2}, t\_0, 1\right)
\end{array}
\end{array}
Derivation
  1. Initial program 77.1%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\mathsf{log1p}\left(\mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\color{blue}{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}}^{2}\right)\right)} \]
  7. Applied egg-rr96.8%

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

      \[\leadsto \cos M \cdot e^{\color{blue}{\log \left(1 + \mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)}} \]
    2. rem-exp-log96.8%

      \[\leadsto \cos M \cdot \color{blue}{\left(1 + \mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)} \]
    3. fabs-sub96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left(\color{blue}{\left|m - n\right|} - \ell\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right) \]
    4. associate--l-96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\color{blue}{\left|m - n\right| - \left(\ell + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)}\right)\right) \]
    5. fabs-sub96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\color{blue}{\left|n - m\right|} - \left(\ell + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)\right) \]
    6. +-commutative96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(\color{blue}{n + m}, 0.5, -M\right)\right)}^{2}\right)\right)\right) \]
  9. Applied egg-rr96.8%

    \[\leadsto \cos M \cdot \color{blue}{\left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right)\right)} \]
  10. Step-by-step derivation
    1. +-commutative96.8%

      \[\leadsto \cos M \cdot \color{blue}{\left(\mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right) + 1\right)} \]
    2. add-cube-cbrt96.8%

      \[\leadsto \cos M \cdot \left(\color{blue}{\left(\sqrt[3]{\mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right)} \cdot \sqrt[3]{\mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right)}\right) \cdot \sqrt[3]{\mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right)}} + 1\right) \]
    3. fma-define96.8%

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

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

    \[\leadsto \cos M \cdot \mathsf{fma}\left({\left(\sqrt[3]{\mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}\right)}\right)}^{2}, \sqrt[3]{\mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}\right)}, 1\right) \]
  13. Add Preprocessing

Alternative 2: 96.5% accurate, 0.6× speedup?

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

\\
\cos M \cdot \left(1 + {\left(\sqrt[3]{\mathsf{expm1}\left(\left|n - m\right| - \left({\left(\mathsf{fma}\left(0.5, n + m, -M\right)\right)}^{2} + \ell\right)\right)}\right)}^{3}\right)
\end{array}
Derivation
  1. Initial program 77.1%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\mathsf{log1p}\left(\mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\color{blue}{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}}^{2}\right)\right)} \]
  7. Applied egg-rr96.8%

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

      \[\leadsto \cos M \cdot e^{\color{blue}{\log \left(1 + \mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)}} \]
    2. rem-exp-log96.8%

      \[\leadsto \cos M \cdot \color{blue}{\left(1 + \mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)} \]
    3. fabs-sub96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left(\color{blue}{\left|m - n\right|} - \ell\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right) \]
    4. associate--l-96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\color{blue}{\left|m - n\right| - \left(\ell + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)}\right)\right) \]
    5. fabs-sub96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\color{blue}{\left|n - m\right|} - \left(\ell + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)\right) \]
    6. +-commutative96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(\color{blue}{n + m}, 0.5, -M\right)\right)}^{2}\right)\right)\right) \]
  9. Applied egg-rr96.8%

    \[\leadsto \cos M \cdot \color{blue}{\left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right)\right)} \]
  10. Step-by-step derivation
    1. +-commutative96.8%

      \[\leadsto \cos M \cdot \color{blue}{\left(\mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right) + 1\right)} \]
    2. add-cube-cbrt96.8%

      \[\leadsto \cos M \cdot \left(\color{blue}{\left(\sqrt[3]{\mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right)} \cdot \sqrt[3]{\mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right)}\right) \cdot \sqrt[3]{\mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right)}} + 1\right) \]
    3. fma-define96.8%

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

    \[\leadsto \cos M \cdot \color{blue}{\mathsf{fma}\left({\left(\sqrt[3]{\mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}\right)}\right)}^{2}, \sqrt[3]{\mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(0.5 \cdot \left(n + m\right) - M\right)}^{2}\right)}, 1\right)} \]
  12. Step-by-step derivation
    1. fma-undefine96.8%

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

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

    \[\leadsto \cos M \cdot \left(1 + {\left(\sqrt[3]{\mathsf{expm1}\left(\left|n - m\right| - \left({\left(\mathsf{fma}\left(0.5, n + m, -M\right)\right)}^{2} + \ell\right)\right)}\right)}^{3}\right) \]
  15. Add Preprocessing

Alternative 3: 73.8% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := \cos \left(0.5 \cdot \left(\left(n + m\right) \cdot K\right)\right) \cdot e^{\left(m - \ell\right) - n}\\
t_1 := \cos M \cdot e^{-{M}^{2}}\\
\mathbf{if}\;M \leq -27:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;M \leq 4.4 \cdot 10^{-195}:\\
\;\;\;\;t\_0\\

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

\mathbf{elif}\;M \leq 23:\\
\;\;\;\;t\_0\\

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


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

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

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

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

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

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

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

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

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

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

    if -27 < M < 4.40000000000000011e-195 or 1.99999999999999996e-137 < M < 23

    1. Initial program 79.1%

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

        \[\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. *-commutative79.1%

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

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

        \[\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)}} \]
      2. associate-+l-31.1%

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

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

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

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

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

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

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

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

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

    if 4.40000000000000011e-195 < M < 1.99999999999999996e-137

    1. Initial program 26.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 91.8%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -27:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;M \leq 4.4 \cdot 10^{-195}:\\ \;\;\;\;\cos \left(0.5 \cdot \left(\left(n + m\right) \cdot K\right)\right) \cdot e^{\left(m - \ell\right) - n}\\ \mathbf{elif}\;M \leq 2 \cdot 10^{-137}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{elif}\;M \leq 23:\\ \;\;\;\;\cos \left(0.5 \cdot \left(\left(n + m\right) \cdot K\right)\right) \cdot e^{\left(m - \ell\right) - n}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 73.7% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := \cos \left(0.5 \cdot \left(\left(n + m\right) \cdot K\right)\right) \cdot e^{\left(m - \ell\right) - n}\\
t_1 := \cos M \cdot e^{-{M}^{2}}\\
\mathbf{if}\;M \leq -45:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;M \leq 2.9 \cdot 10^{-189}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;M \leq 6.6 \cdot 10^{-103}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\

\mathbf{elif}\;M \leq 23:\\
\;\;\;\;t\_0\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if M < -45 or 23 < M

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

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

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

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

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

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

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

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

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

    if -45 < M < 2.9e-189 or 6.59999999999999979e-103 < M < 23

    1. Initial program 78.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. Step-by-step derivation
      1. *-un-lft-identity78.8%

        \[\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. *-commutative78.8%

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

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

        \[\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)}} \]
      2. associate-+l-30.8%

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

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

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

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

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

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

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

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

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

    if 2.9e-189 < M < 6.59999999999999979e-103

    1. Initial program 37.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 93.0%

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -45:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;M \leq 2.9 \cdot 10^{-189}:\\ \;\;\;\;\cos \left(0.5 \cdot \left(\left(n + m\right) \cdot K\right)\right) \cdot e^{\left(m - \ell\right) - n}\\ \mathbf{elif}\;M \leq 6.6 \cdot 10^{-103}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {n}^{2}}\\ \mathbf{elif}\;M \leq 23:\\ \;\;\;\;\cos \left(0.5 \cdot \left(\left(n + m\right) \cdot K\right)\right) \cdot e^{\left(m - \ell\right) - n}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 96.5% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := 0.5 \cdot \left(n + m\right) - M\\
\cos M \cdot \left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + t\_0 \cdot t\_0\right)\right)\right)
\end{array}
\end{array}
Derivation
  1. Initial program 77.1%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\mathsf{log1p}\left(\mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\color{blue}{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}}^{2}\right)\right)} \]
  7. Applied egg-rr96.8%

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

      \[\leadsto \cos M \cdot e^{\color{blue}{\log \left(1 + \mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)}} \]
    2. rem-exp-log96.8%

      \[\leadsto \cos M \cdot \color{blue}{\left(1 + \mathsf{expm1}\left(\left(\left|n - m\right| - \ell\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)} \]
    3. fabs-sub96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left(\color{blue}{\left|m - n\right|} - \ell\right) - {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right) \]
    4. associate--l-96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\color{blue}{\left|m - n\right| - \left(\ell + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)}\right)\right) \]
    5. fabs-sub96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\color{blue}{\left|n - m\right|} - \left(\ell + {\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2}\right)\right)\right) \]
    6. +-commutative96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(\color{blue}{n + m}, 0.5, -M\right)\right)}^{2}\right)\right)\right) \]
  9. Applied egg-rr96.8%

    \[\leadsto \cos M \cdot \color{blue}{\left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(n + m, 0.5, -M\right)\right)}^{2}\right)\right)\right)} \]
  10. Step-by-step derivation
    1. +-commutative96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + {\left(\mathsf{fma}\left(\color{blue}{m + n}, 0.5, -M\right)\right)}^{2}\right)\right)\right) \]
    2. fma-neg96.8%

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

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

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

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + \left(0.5 \cdot \color{blue}{\left(n + m\right)} - M\right) \cdot \left(\left(m + n\right) \cdot 0.5 - M\right)\right)\right)\right) \]
    6. *-commutative96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + \left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(\color{blue}{0.5 \cdot \left(m + n\right)} - M\right)\right)\right)\right) \]
    7. +-commutative96.8%

      \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + \left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(0.5 \cdot \color{blue}{\left(n + m\right)} - M\right)\right)\right)\right) \]
  11. Applied egg-rr96.8%

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

    \[\leadsto \cos M \cdot \left(1 + \mathsf{expm1}\left(\left|n - m\right| - \left(\ell + \left(0.5 \cdot \left(n + m\right) - M\right) \cdot \left(0.5 \cdot \left(n + m\right) - M\right)\right)\right)\right) \]
  13. Add Preprocessing

Alternative 6: 76.8% accurate, 1.3× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -1.40000000000000007e62 or 40 < M

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

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

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

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

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

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

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

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

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

    if -1.40000000000000007e62 < M < 40

    1. Initial program 74.0%

      \[\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    2. Add Preprocessing
    3. Step-by-step derivation
      1. *-un-lft-identity74.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. *-commutative74.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-rr28.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-identity28.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)}} \]
      2. associate-+l-28.7%

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

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

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

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

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

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

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

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

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

Alternative 7: 73.7% accurate, 1.3× speedup?

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

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

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


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

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

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

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

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

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

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

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

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

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

    if -27 < M < 23

    1. Initial program 74.3%

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

        \[\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. *-commutative74.3%

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

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

        \[\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)}} \]
      2. associate-+l-28.5%

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

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

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

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

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

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

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

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

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

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

Alternative 8: 50.6% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(n + m\right) \cdot K\\ \mathbf{if}\;m \leq -1.2 \cdot 10^{+34}:\\ \;\;\;\;\cos \left(\frac{t\_0}{2} - M\right) \cdot e^{m - n}\\ \mathbf{elif}\;m \leq 1.95 \cdot 10^{-119}:\\ \;\;\;\;\cos \left(0.5 \cdot t\_0\right) \cdot e^{\left(m - \ell\right) - n}\\ \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 (* (+ n m) K)))
   (if (<= m -1.2e+34)
     (* (cos (- (/ t_0 2.0) M)) (exp (- m n)))
     (if (<= m 1.95e-119)
       (* (cos (* 0.5 t_0)) (exp (- (- m l) n)))
       (* (cos M) (exp (* m (- M (* n 0.5)))))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = (n + m) * K;
	double tmp;
	if (m <= -1.2e+34) {
		tmp = cos(((t_0 / 2.0) - M)) * exp((m - n));
	} else if (m <= 1.95e-119) {
		tmp = cos((0.5 * t_0)) * exp(((m - l) - n));
	} 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) :: tmp
    t_0 = (n + m) * k
    if (m <= (-1.2d+34)) then
        tmp = cos(((t_0 / 2.0d0) - m_1)) * exp((m - n))
    else if (m <= 1.95d-119) then
        tmp = cos((0.5d0 * t_0)) * exp(((m - l) - n))
    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 = (n + m) * K;
	double tmp;
	if (m <= -1.2e+34) {
		tmp = Math.cos(((t_0 / 2.0) - M)) * Math.exp((m - n));
	} else if (m <= 1.95e-119) {
		tmp = Math.cos((0.5 * t_0)) * Math.exp(((m - l) - n));
	} else {
		tmp = Math.cos(M) * Math.exp((m * (M - (n * 0.5))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = (n + m) * K
	tmp = 0
	if m <= -1.2e+34:
		tmp = math.cos(((t_0 / 2.0) - M)) * math.exp((m - n))
	elif m <= 1.95e-119:
		tmp = math.cos((0.5 * t_0)) * math.exp(((m - l) - n))
	else:
		tmp = math.cos(M) * math.exp((m * (M - (n * 0.5))))
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(Float64(n + m) * K)
	tmp = 0.0
	if (m <= -1.2e+34)
		tmp = Float64(cos(Float64(Float64(t_0 / 2.0) - M)) * exp(Float64(m - n)));
	elseif (m <= 1.95e-119)
		tmp = Float64(cos(Float64(0.5 * t_0)) * exp(Float64(Float64(m - l) - n)));
	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 = (n + m) * K;
	tmp = 0.0;
	if (m <= -1.2e+34)
		tmp = cos(((t_0 / 2.0) - M)) * exp((m - n));
	elseif (m <= 1.95e-119)
		tmp = cos((0.5 * t_0)) * exp(((m - l) - n));
	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[(n + m), $MachinePrecision] * K), $MachinePrecision]}, If[LessEqual[m, -1.2e+34], N[(N[Cos[N[(N[(t$95$0 / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(m - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, 1.95e-119], N[(N[Cos[N[(0.5 * t$95$0), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(N[(m - l), $MachinePrecision] - n), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], 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 := \left(n + m\right) \cdot K\\
\mathbf{if}\;m \leq -1.2 \cdot 10^{+34}:\\
\;\;\;\;\cos \left(\frac{t\_0}{2} - M\right) \cdot e^{m - n}\\

\mathbf{elif}\;m \leq 1.95 \cdot 10^{-119}:\\
\;\;\;\;\cos \left(0.5 \cdot t\_0\right) \cdot e^{\left(m - \ell\right) - n}\\

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


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

    1. Initial program 63.5%

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

        \[\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. *-commutative63.5%

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

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

        \[\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)}} \]
      2. associate-+l-4.8%

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

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

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

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

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

    if -1.19999999999999993e34 < m < 1.94999999999999995e-119

    1. Initial program 84.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-identity84.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. *-commutative84.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-rr28.4%

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

        \[\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)}} \]
      2. associate-+l-28.4%

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

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

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

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

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

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

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

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

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

    if 1.94999999999999995e-119 < m

    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 m around 0 57.7%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
    4. Step-by-step derivation
      1. unpow257.7%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{m \cdot \left(M - 0.5 \cdot n\right)} \]
    8. Step-by-step derivation
      1. cos-neg46.3%

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

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

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

Alternative 9: 49.8% accurate, 1.9× speedup?

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

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


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

    1. Initial program 77.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-identity77.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. *-commutative77.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-rr21.2%

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

        \[\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)}} \]
      2. associate-+l-21.2%

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

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

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

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

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

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

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

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

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

    if 1.94999999999999995e-119 < m

    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 m around 0 57.7%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
    4. Step-by-step derivation
      1. unpow257.7%

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

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

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(-M\right)} \cdot e^{m \cdot \left(M - 0.5 \cdot n\right)} \]
    8. Step-by-step derivation
      1. cos-neg46.3%

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

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

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

Alternative 10: 55.5% accurate, 2.0× speedup?

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

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

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


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

    1. Initial program 75.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 m around 0 59.4%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
    4. Step-by-step derivation
      1. unpow259.4%

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

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

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

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

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

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

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

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

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

    if 0.245 < l

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

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

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

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

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

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

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

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

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

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

Alternative 11: 36.2% accurate, 2.1× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 12: 6.9% accurate, 4.0× speedup?

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

\\
\cos \left(n \cdot \left(0.5 \cdot K\right) - M\right)
\end{array}
Derivation
  1. Initial program 77.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 m around 0 63.6%

    \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
  4. Step-by-step derivation
    1. unpow263.6%

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

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

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

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

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

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

    \[\leadsto \color{blue}{\cos \left(0.5 \cdot \left(K \cdot n\right) - M\right)} \]
  8. Step-by-step derivation
    1. associate-*r*7.8%

      \[\leadsto \cos \left(\color{blue}{\left(0.5 \cdot K\right) \cdot n} - M\right) \]
    2. *-commutative7.8%

      \[\leadsto \cos \left(\color{blue}{n \cdot \left(0.5 \cdot K\right)} - M\right) \]
  9. Simplified7.8%

    \[\leadsto \color{blue}{\cos \left(n \cdot \left(0.5 \cdot K\right) - M\right)} \]
  10. Final simplification7.8%

    \[\leadsto \cos \left(n \cdot \left(0.5 \cdot K\right) - M\right) \]
  11. Add Preprocessing

Reproduce

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