Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.1% → 96.9%
Time: 20.2s
Alternatives: 9
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 9 alternatives:

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

Initial Program: 76.1% accurate, 1.0× speedup?

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

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

Alternative 1: 96.9% accurate, 1.0× speedup?

\[\begin{array}{l} \\ \cos M \cdot e^{\left(\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 78.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 96.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-neg96.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. Simplified96.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. Final simplification96.7%

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

Alternative 2: 65.4% accurate, 1.3× speedup?

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

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

\mathbf{elif}\;m \leq -2.6 \cdot 10^{-46}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;m \leq -3.2 \cdot 10^{-56}:\\
\;\;\;\;\frac{\cos M}{e^{\ell}}\\

\mathbf{elif}\;m \leq 3.1 \cdot 10^{-173}:\\
\;\;\;\;t\_0\\

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


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

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

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

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

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

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

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

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

    if -55 < m < -2.6000000000000002e-46 or -3.19999999999999986e-56 < m < 3.10000000000000005e-173

    1. Initial program 81.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 95.6%

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

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

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

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

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

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

    if -2.6000000000000002e-46 < m < -3.19999999999999986e-56

    1. Initial program 100.0%

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

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

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

      \[\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 35.2%

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

        \[\leadsto \cos \left(-M\right) \cdot \color{blue}{\frac{1}{e^{\ell}}} \]
      2. associate-*r/35.2%

        \[\leadsto \color{blue}{\frac{\cos \left(-M\right) \cdot 1}{e^{\ell}}} \]
      3. *-rgt-identity35.2%

        \[\leadsto \frac{\color{blue}{\cos \left(-M\right)}}{e^{\ell}} \]
      4. cos-neg35.2%

        \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
    8. Simplified35.2%

      \[\leadsto \color{blue}{\frac{\cos M}{e^{\ell}}} \]

    if 3.10000000000000005e-173 < m

    1. Initial program 76.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 96.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-neg96.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. Simplified96.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 56.8%

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -55:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{elif}\;m \leq -2.6 \cdot 10^{-46}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{elif}\;m \leq -3.2 \cdot 10^{-56}:\\ \;\;\;\;\frac{\cos M}{e^{\ell}}\\ \mathbf{elif}\;m \leq 3.1 \cdot 10^{-173}:\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{{n}^{2} \cdot -0.25}\\ \end{array} \]
  5. Add Preprocessing

Alternative 3: 87.9% accurate, 1.3× speedup?

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

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

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


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

    1. Initial program 80.4%

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

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

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

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

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

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

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

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

    if 5.00000000000000027e31 < n

    1. Initial program 69.6%

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

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

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

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

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

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

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

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

Alternative 4: 68.9% accurate, 1.3× speedup?

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

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

\mathbf{elif}\;m \leq 5 \cdot 10^{-173}:\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(n - M\right) + \left(\left|n - m\right| - \ell\right)}\\

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


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

    1. Initial program 72.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 98.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-neg98.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. Simplified98.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.4%

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

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

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

    if -3.14999999999999987e25 < m < 5.0000000000000002e-173

    1. Initial program 82.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 5.0000000000000002e-173 < m

    1. Initial program 76.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 96.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-neg96.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. Simplified96.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 56.8%

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

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

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

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

Alternative 5: 77.3% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;M \leq -26 \lor \neg \left(M \leq 27\right):\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot 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 27.0)))
   (* (cos M) (exp (- (pow M 2.0))))
   (* (cos M) (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 <= 27.0)) {
		tmp = cos(M) * exp(-pow(M, 2.0));
	} else {
		tmp = cos(M) * 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 <= 27.0d0))) then
        tmp = cos(m_1) * exp(-(m_1 ** 2.0d0))
    else
        tmp = cos(m_1) * 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 <= 27.0)) {
		tmp = Math.cos(M) * Math.exp(-Math.pow(M, 2.0));
	} else {
		tmp = Math.cos(M) * 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 <= 27.0):
		tmp = math.cos(M) * math.exp(-math.pow(M, 2.0))
	else:
		tmp = math.cos(M) * 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 <= 27.0))
		tmp = Float64(cos(M) * exp(Float64(-(M ^ 2.0))));
	else
		tmp = Float64(cos(M) * 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 <= 27.0)))
		tmp = cos(M) * exp(-(M ^ 2.0));
	else
		tmp = cos(M) * 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, 27.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[(-0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

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


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

    1. Initial program 80.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.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-neg98.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. Simplified98.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 95.6%

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

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

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

    if -26 < M < 27

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
    8. Simplified51.6%

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

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

Alternative 6: 70.8% accurate, 1.3× speedup?

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

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


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

    1. Initial program 80.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.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-neg98.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. Simplified98.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 95.6%

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

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

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

    if -26 < M < 27

    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 l around inf 36.8%

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

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

      \[\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 42.7%

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

        \[\leadsto \cos \left(-M\right) \cdot \color{blue}{\frac{1}{e^{\ell}}} \]
      2. associate-*r/42.7%

        \[\leadsto \color{blue}{\frac{\cos \left(-M\right) \cdot 1}{e^{\ell}}} \]
      3. *-rgt-identity42.7%

        \[\leadsto \frac{\color{blue}{\cos \left(-M\right)}}{e^{\ell}} \]
      4. cos-neg42.7%

        \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
    8. Simplified42.7%

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

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

Alternative 7: 44.7% accurate, 2.0× speedup?

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

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

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


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

    1. Initial program 85.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 l around inf 26.7%

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

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

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

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

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

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

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

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

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

        \[\leadsto {\left(\cos \left(\mathsf{fma}\left(K, \left(m + n\right) \cdot \color{blue}{0.5}, -M\right)\right) \cdot e^{-\ell}\right)}^{1} \]
      8. add-sqr-sqrt26.7%

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

        \[\leadsto {\left(\cos \left(\mathsf{fma}\left(K, \left(m + n\right) \cdot 0.5, -M\right)\right) \cdot e^{\color{blue}{\sqrt{\left(-\ell\right) \cdot \left(-\ell\right)}}}\right)}^{1} \]
      10. sqr-neg26.7%

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto e^{\ell} \cdot \cos \left(\mathsf{fma}\left(0.5 \cdot K, n + \color{blue}{\left(-\left(-m\right)\right)}, -M\right)\right) \]
      10. sub-neg60.4%

        \[\leadsto e^{\ell} \cdot \cos \left(\mathsf{fma}\left(0.5 \cdot K, \color{blue}{n - \left(-m\right)}, -M\right)\right) \]
      11. neg-mul-160.4%

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

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

        \[\leadsto e^{\ell} \cdot \cos \left(\color{blue}{\left(K \cdot 0.5\right)} \cdot \left(n - -1 \cdot m\right) - M\right) \]
      14. sub-neg60.4%

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

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

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

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

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

    if -680 < l

    1. Initial program 75.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 32.1%

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

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

      \[\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 39.9%

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

        \[\leadsto \cos \left(-M\right) \cdot \color{blue}{\frac{1}{e^{\ell}}} \]
      2. associate-*r/39.9%

        \[\leadsto \color{blue}{\frac{\cos \left(-M\right) \cdot 1}{e^{\ell}}} \]
      3. *-rgt-identity39.9%

        \[\leadsto \frac{\color{blue}{\cos \left(-M\right)}}{e^{\ell}} \]
      4. cos-neg39.9%

        \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
    8. Simplified39.9%

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

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

Alternative 8: 36.0% accurate, 2.1× speedup?

\[\begin{array}{l} \\ \frac{\cos M}{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(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}

\\
\frac{\cos M}{e^{\ell}}
\end{array}
Derivation
  1. Initial program 78.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 l around inf 30.6%

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

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

    \[\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 35.9%

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

      \[\leadsto \cos \left(-M\right) \cdot \color{blue}{\frac{1}{e^{\ell}}} \]
    2. associate-*r/35.9%

      \[\leadsto \color{blue}{\frac{\cos \left(-M\right) \cdot 1}{e^{\ell}}} \]
    3. *-rgt-identity35.9%

      \[\leadsto \frac{\color{blue}{\cos \left(-M\right)}}{e^{\ell}} \]
    4. cos-neg35.9%

      \[\leadsto \frac{\color{blue}{\cos M}}{e^{\ell}} \]
  8. Simplified35.9%

    \[\leadsto \color{blue}{\frac{\cos M}{e^{\ell}}} \]
  9. Final simplification35.9%

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

Alternative 9: 7.1% accurate, 4.2× speedup?

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

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

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

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

    \[\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 5.9%

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

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

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

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, \color{blue}{n + m}, -M\right)\right) \]
    4. remove-double-neg5.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, n + \color{blue}{\left(-\left(-m\right)\right)}, -M\right)\right) \]
    5. sub-neg5.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, \color{blue}{n - \left(-m\right)}, -M\right)\right) \]
    6. neg-mul-15.9%

      \[\leadsto \cos \left(\mathsf{fma}\left(0.5 \cdot K, n - \color{blue}{-1 \cdot m}, -M\right)\right) \]
    7. fma-neg5.9%

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

      \[\leadsto \cos \left(\color{blue}{\left(K \cdot 0.5\right)} \cdot \left(n - -1 \cdot m\right) - M\right) \]
    9. sub-neg5.9%

      \[\leadsto \cos \left(\left(K \cdot 0.5\right) \cdot \color{blue}{\left(n + \left(--1 \cdot m\right)\right)} - M\right) \]
    10. neg-mul-15.9%

      \[\leadsto \cos \left(\left(K \cdot 0.5\right) \cdot \left(n + \left(-\color{blue}{\left(-m\right)}\right)\right) - M\right) \]
    11. remove-double-neg5.9%

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

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

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

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

      \[\leadsto \color{blue}{\cos M} \]
  11. Simplified6.4%

    \[\leadsto \color{blue}{\cos M} \]
  12. Final simplification6.4%

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

Reproduce

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