Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.7% → 96.6%
Time: 17.3s
Alternatives: 12
Speedup: 1.3×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 12 alternatives:

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

Initial Program: 75.7% 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, 1.0× speedup?

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

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

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

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

Alternative 2: 74.8% accurate, 1.3× speedup?

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

\\
\begin{array}{l}
t_0 := M - m \cdot 0.5\\
\mathbf{if}\;n \leq -8.5 \cdot 10^{-153}:\\
\;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\

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

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


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

    1. Initial program 76.0%

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

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

    if -8.4999999999999996e-153 < n < 56

    1. Initial program 86.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 n around 0 86.5%

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. unpow286.5%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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-out86.5%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative86.5%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative86.5%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
    5. Simplified86.5%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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 56 < n

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

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

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

Alternative 3: 67.3% accurate, 1.3× speedup?

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

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

\mathbf{elif}\;n \leq 54:\\
\;\;\;\;\cos \left(\frac{\left(m + n\right) \cdot K}{2} - M\right) \cdot e^{M \cdot \left(n - M\right) + \left(\left|m - n\right| - \ell\right)}\\

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


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

    1. Initial program 78.6%

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

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

    if -9.0000000000000003e-296 < n < 54

    1. Initial program 86.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 n around 0 86.4%

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. unpow286.4%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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-out86.4%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative86.4%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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. *-commutative86.4%

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
    5. Simplified86.4%

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \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)} \]
    6. Taylor expanded in m around 0 67.7%

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

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

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

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

    if 54 < n

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

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

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

Alternative 4: 65.5% accurate, 1.3× speedup?

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

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

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

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


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

    1. Initial program 79.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 97.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-neg97.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. Simplified97.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 57.0%

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

    if 4.7000000000000002e-157 < n < 0.00759999999999999998

    1. Initial program 89.3%

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

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

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

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

    if 0.00759999999999999998 < n

    1. Initial program 76.9%

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

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

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

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

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

Alternative 5: 76.8% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := e^{-{M}^{2}}\\ \mathbf{if}\;M \leq -27:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;M \leq 2.3 \cdot 10^{-6}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot {m}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot t\_0\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (exp (- (pow M 2.0)))))
   (if (<= M -27.0)
     t_0
     (if (<= M 2.3e-6)
       (* (cos M) (exp (* -0.25 (pow m 2.0))))
       (* (cos M) t_0)))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = exp(-pow(M, 2.0));
	double tmp;
	if (M <= -27.0) {
		tmp = t_0;
	} else if (M <= 2.3e-6) {
		tmp = cos(M) * exp((-0.25 * pow(m, 2.0)));
	} else {
		tmp = cos(M) * t_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 = exp(-(m_1 ** 2.0d0))
    if (m_1 <= (-27.0d0)) then
        tmp = t_0
    else if (m_1 <= 2.3d-6) then
        tmp = cos(m_1) * exp(((-0.25d0) * (m ** 2.0d0)))
    else
        tmp = cos(m_1) * t_0
    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(-Math.pow(M, 2.0));
	double tmp;
	if (M <= -27.0) {
		tmp = t_0;
	} else if (M <= 2.3e-6) {
		tmp = Math.cos(M) * Math.exp((-0.25 * Math.pow(m, 2.0)));
	} else {
		tmp = Math.cos(M) * t_0;
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.exp(-math.pow(M, 2.0))
	tmp = 0
	if M <= -27.0:
		tmp = t_0
	elif M <= 2.3e-6:
		tmp = math.cos(M) * math.exp((-0.25 * math.pow(m, 2.0)))
	else:
		tmp = math.cos(M) * t_0
	return tmp
function code(K, m, n, M, l)
	t_0 = exp(Float64(-(M ^ 2.0)))
	tmp = 0.0
	if (M <= -27.0)
		tmp = t_0;
	elseif (M <= 2.3e-6)
		tmp = Float64(cos(M) * exp(Float64(-0.25 * (m ^ 2.0))));
	else
		tmp = Float64(cos(M) * t_0);
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = exp(-(M ^ 2.0));
	tmp = 0.0;
	if (M <= -27.0)
		tmp = t_0;
	elseif (M <= 2.3e-6)
		tmp = cos(M) * exp((-0.25 * (m ^ 2.0)));
	else
		tmp = cos(M) * t_0;
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Exp[(-N[Power[M, 2.0], $MachinePrecision])], $MachinePrecision]}, If[LessEqual[M, -27.0], t$95$0, If[LessEqual[M, 2.3e-6], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[Power[m, 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * t$95$0), $MachinePrecision]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := e^{-{M}^{2}}\\
\mathbf{if}\;M \leq -27:\\
\;\;\;\;t\_0\\

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

\mathbf{else}:\\
\;\;\;\;\cos M \cdot t\_0\\


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

    1. Initial program 76.9%

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

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

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

    if 2.3e-6 < 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.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.5%

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

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

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

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

Alternative 6: 69.0% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;M \leq -0.006 \lor \neg \left(M \leq 0.15\right):\\ \;\;\;\;\cos M \cdot e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= M -0.006) (not (<= M 0.15)))
   (* (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 <= -0.006) || !(M <= 0.15)) {
		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 <= (-0.006d0)) .or. (.not. (m_1 <= 0.15d0))) 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 <= -0.006) || !(M <= 0.15)) {
		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 <= -0.006) or not (M <= 0.15):
		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 <= -0.006) || !(M <= 0.15))
		tmp = Float64(cos(M) * exp(Float64(-(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 <= -0.006) || ~((M <= 0.15)))
		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, -0.006], N[Not[LessEqual[M, 0.15]], $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 -0.006 \lor \neg \left(M \leq 0.15\right):\\
\;\;\;\;\cos M \cdot e^{-{M}^{2}}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -0.0060000000000000001 or 0.149999999999999994 < 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 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.7%

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

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

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

    if -0.0060000000000000001 < M < 0.149999999999999994

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

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

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

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

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

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

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

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

Alternative 7: 68.9% accurate, 2.0× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;M \leq -0.006 \lor \neg \left(M \leq 0.15\right):\\ \;\;\;\;e^{-{M}^{2}}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (or (<= M -0.006) (not (<= M 0.15)))
   (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 <= -0.006) || !(M <= 0.15)) {
		tmp = 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 <= (-0.006d0)) .or. (.not. (m_1 <= 0.15d0))) then
        tmp = 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 <= -0.006) || !(M <= 0.15)) {
		tmp = 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 <= -0.006) or not (M <= 0.15):
		tmp = 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 <= -0.006) || !(M <= 0.15))
		tmp = exp(Float64(-(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 <= -0.006) || ~((M <= 0.15)))
		tmp = 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, -0.006], N[Not[LessEqual[M, 0.15]], $MachinePrecision]], N[Exp[(-N[Power[M, 2.0], $MachinePrecision])], $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]]
\begin{array}{l}

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -0.0060000000000000001 or 0.149999999999999994 < 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 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.7%

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

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

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

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

    if -0.0060000000000000001 < M < 0.149999999999999994

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

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

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

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

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

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

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

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

Alternative 8: 35.5% accurate, 2.1× speedup?

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

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

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

      \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\color{blue}{-\ell}} \]
  5. Simplified27.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 33.0%

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

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

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

Alternative 9: 10.3% accurate, 3.4× speedup?

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

\\
\cos \left(\frac{1}{\frac{2}{\left(m + n\right) \cdot K}} - M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot \left(0.5 + \ell \cdot -0.16666666666666666\right) + -1\right)\right)
\end{array}
Derivation
  1. Initial program 79.8%

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \cos \left(\frac{1}{\frac{2}{\left(m + n\right) \cdot K}} - M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot \left(0.5 + \ell \cdot -0.16666666666666666\right) + -1\right)\right) \]
  12. Add Preprocessing

Alternative 10: 9.6% accurate, 3.5× speedup?

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \cos \left(\frac{1}{\frac{2}{\left(m + n\right) \cdot K}} - M\right) \cdot \left(1 + \ell \cdot \left(\ell \cdot 0.5 + -1\right)\right) \]
  12. Add Preprocessing

Alternative 11: 7.2% accurate, 4.2× speedup?

\[\begin{array}{l} \\ \cos \left(-M\right) \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(Float64(-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 \left(-M\right)
\end{array}
Derivation
  1. Initial program 79.8%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos \left(\color{blue}{\left(0.5 \cdot K\right) \cdot m} - M\right) \]
  11. Simplified5.3%

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

    \[\leadsto \cos \color{blue}{\left(-1 \cdot M\right)} \]
  13. Step-by-step derivation
    1. neg-mul-15.5%

      \[\leadsto \cos \color{blue}{\left(-M\right)} \]
  14. Simplified5.5%

    \[\leadsto \cos \color{blue}{\left(-M\right)} \]
  15. Add Preprocessing

Alternative 12: 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 79.8%

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

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

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

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

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

Reproduce

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