Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.0% → 96.6%
Time: 18.7s
Alternatives: 11
Speedup: 2.0×

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

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

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

Alternative 1: 96.6% accurate, 0.7× speedup?

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

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

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

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

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

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

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

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

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

Alternative 2: 96.7% accurate, 1.0× speedup?

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

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

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

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

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

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

Alternative 3: 94.6% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -1.5 \cdot 10^{+20} \lor \neg \left(M \leq 1.06 \cdot 10^{+77}\right):\\
\;\;\;\;e^{-{M}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -1.5e20 or 1.06000000000000003e77 < 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(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
    4. Step-by-step derivation
      1. cos-neg100.0%

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

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

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

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

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

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

    if -1.5e20 < M < 1.06000000000000003e77

    1. Initial program 67.9%

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

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

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

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

      \[\leadsto \color{blue}{e^{\left|m - n\right| - \left(\ell + 0.25 \cdot {\left(m + n\right)}^{2}\right)}} \]
    7. Step-by-step derivation
      1. associate--r+95.0%

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

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

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

Alternative 4: 61.7% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := n \cdot 0.5 - M\\
\mathbf{if}\;n \leq -2.15 \cdot 10^{-162}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(m \cdot m\right)}\\

\mathbf{elif}\;n \leq 3 \cdot 10^{-218}:\\
\;\;\;\;\cos \left(\frac{\left(m + n\right) \cdot K}{2} - M\right) \cdot e^{\left(m - n\right) + \left(t\_0 \cdot \left(m + t\_0\right) - \ell\right)}\\

\mathbf{elif}\;n \leq 54:\\
\;\;\;\;e^{-{M}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 4 regimes
  2. if n < -2.14999999999999998e-162

    1. Initial program 67.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 99.1%

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

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

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

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

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \color{blue}{\left(m \cdot m\right)}} \]
    8. Applied egg-rr58.2%

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

    if -2.14999999999999998e-162 < n < 2.9999999999999998e-218

    1. Initial program 89.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. sub-neg89.5%

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{\left(\mathsf{fma}\left(m + n, \color{blue}{0.5}, -M\right)\right)}^{2} + \left(-\left(\ell - \left|m - n\right|\right)\right)} \]
      10. add-sqr-sqrt7.2%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(-\left(\ell - \left|\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right|\right)\right)} \]
      11. fabs-sqr7.2%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(-\left(\ell - \color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right)\right)} \]
      12. add-sqr-sqrt29.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 2.9999999999999998e-218 < n < 54

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

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

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

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

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

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

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

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

    if 54 < n

    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. Taylor expanded in K around 0 98.4%

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

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

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

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

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

Alternative 5: 75.9% accurate, 1.4× speedup?

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

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

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

\mathbf{elif}\;M \leq 4 \cdot 10^{-105}:\\
\;\;\;\;\cos \left(\frac{\left(m + n\right) \cdot K}{2} - M\right) \cdot e^{\left(m - n\right) + \left(t\_1 \cdot \left(n + t\_1\right) - \ell\right)}\\

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

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


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

    1. Initial program 81.8%

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

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

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

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

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

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

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

    if -26 < M < 3.4500000000000001e-189 or 3.99999999999999986e-105 < M < 27

    1. Initial program 62.2%

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    7. Taylor expanded in M around 0 67.7%

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

    if 3.4500000000000001e-189 < M < 3.99999999999999986e-105

    1. Initial program 86.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. sub-neg86.2%

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{\left(\mathsf{fma}\left(m + n, \color{blue}{0.5}, -M\right)\right)}^{2} + \left(-\left(\ell - \left|m - n\right|\right)\right)} \]
      10. add-sqr-sqrt28.0%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(-\left(\ell - \left|\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right|\right)\right)} \]
      11. fabs-sqr28.0%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(-\left(\ell - \color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right)\right)} \]
      12. add-sqr-sqrt48.9%

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(-\left(\ell - \left(m - n\right)\right)\right)}} \]
    5. Step-by-step derivation
      1. unsub-neg48.9%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{\left(m - n\right) + \left({\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} - \ell\right)}} \]
      4. fmm-undef48.9%

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

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

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

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

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

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

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

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

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

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

    if 27 < 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 K around 0 98.4%

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

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

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

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

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

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

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

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

Alternative 6: 76.0% accurate, 1.7× speedup?

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

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

\mathbf{elif}\;M \leq 8.2 \cdot 10^{-188}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;M \leq 7.5 \cdot 10^{-108}:\\
\;\;\;\;\cos \left(\frac{\left(m + n\right) \cdot K}{2} - M\right) \cdot e^{\left(m - n\right) + \left(t\_1 \cdot \left(n + t\_1\right) - \ell\right)}\\

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

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


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

    1. Initial program 78.7%

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

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

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

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

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

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

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

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

    if -26 < M < 8.19999999999999965e-188 or 7.4999999999999993e-108 < M < 27

    1. Initial program 62.2%

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    7. Taylor expanded in M around 0 67.7%

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

    if 8.19999999999999965e-188 < M < 7.4999999999999993e-108

    1. Initial program 86.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. sub-neg86.2%

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{\left(\mathsf{fma}\left(m + n, \color{blue}{0.5}, -M\right)\right)}^{2} + \left(-\left(\ell - \left|m - n\right|\right)\right)} \]
      10. add-sqr-sqrt28.0%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(-\left(\ell - \left|\color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right|\right)\right)} \]
      11. fabs-sqr28.0%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(-\left(\ell - \color{blue}{\sqrt{m - n} \cdot \sqrt{m - n}}\right)\right)} \]
      12. add-sqr-sqrt48.9%

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{{\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} + \left(-\left(\ell - \left(m - n\right)\right)\right)}} \]
    5. Step-by-step derivation
      1. unsub-neg48.9%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{\left(m - n\right) + \left({\left(\mathsf{fma}\left(m + n, 0.5, -M\right)\right)}^{2} - \ell\right)}} \]
      4. fmm-undef48.9%

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

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

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

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

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

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

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

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

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

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

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

Alternative 7: 77.5% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -26 \lor \neg \left(M \leq 26.5\right):\\
\;\;\;\;e^{-{M}^{2}}\\

\mathbf{else}:\\
\;\;\;\;e^{-0.25 \cdot {m}^{2}}\\


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

    1. Initial program 78.7%

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

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

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

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

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

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

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

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

    if -26 < M < 26.5

    1. Initial program 67.6%

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    7. Taylor expanded in M around 0 62.1%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -26 \lor \neg \left(M \leq 26.5\right):\\ \;\;\;\;e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;e^{-0.25 \cdot {m}^{2}}\\ \end{array} \]
  5. Add Preprocessing

Alternative 8: 69.4% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;m \leq -11 \lor \neg \left(m \leq 0.000105\right):\\
\;\;\;\;e^{-0.25 \cdot {m}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if m < -11 or 1.05e-4 < m

    1. Initial program 63.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 99.2%

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
    7. Taylor expanded in M around 0 96.5%

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

    if -11 < m < 1.05e-4

    1. Initial program 84.9%

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(-M\right) \cdot e^{-\ell}} \]
    7. Step-by-step derivation
      1. cos-neg41.0%

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

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

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

Alternative 9: 53.6% accurate, 2.1× speedup?

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

\\
e^{-0.25 \cdot {m}^{2}}
\end{array}
Derivation
  1. Initial program 73.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 97.5%

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

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

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

    \[\leadsto \cos M \cdot e^{\color{blue}{-0.25 \cdot {m}^{2}}} \]
  7. Taylor expanded in M around 0 55.9%

    \[\leadsto \color{blue}{e^{-0.25 \cdot {m}^{2}}} \]
  8. Add Preprocessing

Alternative 10: 7.2% accurate, 4.2× speedup?

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

\\
\cos M
\end{array}
Derivation
  1. Initial program 73.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 l around inf 24.9%

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

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

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

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

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

      \[\leadsto \cos \left(\color{blue}{\left(\left(m + n\right) \cdot K\right) \cdot 0.5} - M\right) \]
    3. *-commutative4.7%

      \[\leadsto \cos \left(\color{blue}{\left(K \cdot \left(m + n\right)\right)} \cdot 0.5 - M\right) \]
    4. associate-*r*4.7%

      \[\leadsto \cos \left(\color{blue}{K \cdot \left(\left(m + n\right) \cdot 0.5\right)} - M\right) \]
    5. fmm-undef4.7%

      \[\leadsto \cos \color{blue}{\left(\mathsf{fma}\left(K, \left(m + n\right) \cdot 0.5, -M\right)\right)} \]
    6. fmm-undef4.7%

      \[\leadsto \cos \color{blue}{\left(K \cdot \left(\left(m + n\right) \cdot 0.5\right) - M\right)} \]
    7. associate-*r*4.7%

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

      \[\leadsto \cos \left(\color{blue}{\left(\left(m + n\right) \cdot K\right)} \cdot 0.5 - M\right) \]
    9. *-commutative4.7%

      \[\leadsto \cos \left(\color{blue}{0.5 \cdot \left(\left(m + n\right) \cdot K\right)} - M\right) \]
    10. *-commutative4.7%

      \[\leadsto \cos \left(0.5 \cdot \color{blue}{\left(K \cdot \left(m + n\right)\right)} - M\right) \]
    11. associate-*r*4.7%

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

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

    \[\leadsto \color{blue}{\cos \left(-M\right)} \]
  10. Step-by-step derivation
    1. cos-neg5.6%

      \[\leadsto \color{blue}{\cos M} \]
  11. Simplified5.6%

    \[\leadsto \color{blue}{\cos M} \]
  12. Add Preprocessing

Alternative 11: 7.2% accurate, 425.0× speedup?

\[\begin{array}{l} \\ 1 \end{array} \]
(FPCore (K m n M l) :precision binary64 1.0)
double code(double K, double m, double n, double M, double l) {
	return 1.0;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    code = 1.0d0
end function
public static double code(double K, double m, double n, double M, double l) {
	return 1.0;
}
def code(K, m, n, M, l):
	return 1.0
function code(K, m, n, M, l)
	return 1.0
end
function tmp = code(K, m, n, M, l)
	tmp = 1.0;
end
code[K_, m_, n_, M_, l_] := 1.0
\begin{array}{l}

\\
1
\end{array}
Derivation
  1. Initial program 73.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 97.5%

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

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

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

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

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

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

    \[\leadsto \color{blue}{1} \]
  10. Add Preprocessing

Reproduce

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