Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 75.3% → 96.7%
Time: 14.8s
Alternatives: 7
Speedup: 1.0×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 7 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.3% 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.7% accurate, 1.0× speedup?

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

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

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

Alternative 2: 74.6% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
t_0 := {\left(e^{m}\right)}^{\left(m \cdot -0.25\right)}\\
t_1 := \cos M \cdot e^{M \cdot \left(-M\right)}\\
\mathbf{if}\;M \leq -7.4 \cdot 10^{-32}:\\
\;\;\;\;t_1\\

\mathbf{elif}\;M \leq 1.85 \cdot 10^{-196}:\\
\;\;\;\;t_0\\

\mathbf{elif}\;M \leq 2.4 \cdot 10^{-98}:\\
\;\;\;\;e^{-\ell}\\

\mathbf{elif}\;M \leq 3 \cdot 10^{-9}:\\
\;\;\;\;t_0\\

\mathbf{else}:\\
\;\;\;\;t_1\\


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if M < -7.4e-32 or 2.99999999999999998e-9 < M

    1. Initial program 76.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. 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)} \]
    3. 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)} \]
    4. 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)} \]
    5. Taylor expanded in M around inf 94.1%

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

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

        \[\leadsto \cos M \cdot e^{-\color{blue}{M \cdot M}} \]
      3. distribute-rgt-neg-in94.1%

        \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot \left(-M\right)}} \]
    7. Simplified94.1%

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

    if -7.4e-32 < M < 1.85000000000000005e-196 or 2.40000000000000005e-98 < M < 2.99999999999999998e-9

    1. Initial program 78.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. Taylor expanded in m around inf 46.3%

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(-M\right) \cdot e^{-0.25 \cdot {m}^{2}}} \]
    6. Step-by-step derivation
      1. cos-neg55.6%

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

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

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

      \[\leadsto \color{blue}{\cos M \cdot e^{\left(m \cdot m\right) \cdot -0.25}} \]
    8. Taylor expanded in M around 0 55.6%

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

        \[\leadsto e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
      2. unpow255.6%

        \[\leadsto e^{\color{blue}{\left(m \cdot m\right)} \cdot -0.25} \]
      3. associate-*r*55.6%

        \[\leadsto e^{\color{blue}{m \cdot \left(m \cdot -0.25\right)}} \]
      4. exp-prod55.6%

        \[\leadsto \color{blue}{{\left(e^{m}\right)}^{\left(m \cdot -0.25\right)}} \]
    10. Simplified55.6%

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

    if 1.85000000000000005e-196 < M < 2.40000000000000005e-98

    1. Initial program 88.2%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -7.4 \cdot 10^{-32}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{elif}\;M \leq 1.85 \cdot 10^{-196}:\\ \;\;\;\;{\left(e^{m}\right)}^{\left(m \cdot -0.25\right)}\\ \mathbf{elif}\;M \leq 2.4 \cdot 10^{-98}:\\ \;\;\;\;e^{-\ell}\\ \mathbf{elif}\;M \leq 3 \cdot 10^{-9}:\\ \;\;\;\;{\left(e^{m}\right)}^{\left(m \cdot -0.25\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \end{array} \]

Alternative 3: 66.3% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;m \leq -195:\\
\;\;\;\;{\left(e^{m}\right)}^{\left(m \cdot -0.25\right)}\\

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

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


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

    1. Initial program 73.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. Taylor expanded in m around inf 73.8%

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(-M\right) \cdot e^{-0.25 \cdot {m}^{2}}} \]
    6. Step-by-step derivation
      1. cos-neg96.8%

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

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

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

      \[\leadsto \color{blue}{\cos M \cdot e^{\left(m \cdot m\right) \cdot -0.25}} \]
    8. Taylor expanded in M around 0 96.8%

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

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

        \[\leadsto e^{\color{blue}{\left(m \cdot m\right)} \cdot -0.25} \]
      3. associate-*r*96.8%

        \[\leadsto e^{\color{blue}{m \cdot \left(m \cdot -0.25\right)}} \]
      4. exp-prod96.8%

        \[\leadsto \color{blue}{{\left(e^{m}\right)}^{\left(m \cdot -0.25\right)}} \]
    10. Simplified96.8%

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

    if -195 < m < -6.9999999999999998e-241

    1. Initial program 85.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. Taylor expanded in K around 0 98.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)} \]
    3. Step-by-step derivation
      1. cos-neg98.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)} \]
    4. Simplified98.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. Taylor expanded in M around inf 53.4%

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

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

        \[\leadsto \cos M \cdot e^{-\color{blue}{M \cdot M}} \]
      3. distribute-rgt-neg-in53.4%

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

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

    if -6.9999999999999998e-241 < m

    1. Initial program 77.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. Taylor expanded in K around 0 98.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)} \]
    3. Step-by-step derivation
      1. cos-neg98.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)} \]
    4. Simplified98.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. Taylor expanded in n around inf 51.9%

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{\left(n \cdot n\right)} \cdot -0.25} \]
    7. Simplified51.9%

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;m \leq -195:\\ \;\;\;\;{\left(e^{m}\right)}^{\left(m \cdot -0.25\right)}\\ \mathbf{elif}\;m \leq -7 \cdot 10^{-241}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(-M\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \]

Alternative 4: 73.0% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -0.000155:\\
\;\;\;\;\cos M \cdot e^{\ell}\\

\mathbf{elif}\;\ell \leq 720:\\
\;\;\;\;{\left(e^{m}\right)}^{\left(m \cdot -0.25\right)}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 3 regimes
  2. if l < -1.55e-4

    1. Initial program 82.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. Taylor expanded in l around inf 21.9%

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

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

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

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

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

      \[\leadsto \color{blue}{\cos M \cdot e^{-\ell}} \]
    8. Step-by-step derivation
      1. expm1-log1p-u17.3%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)\right)} \]
      2. expm1-udef17.3%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)} - 1} \]
      3. add-sqr-sqrt17.3%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{-\ell} \cdot \sqrt{-\ell}}}\right)} - 1 \]
      4. sqrt-unprod17.3%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\left(-\ell\right) \cdot \left(-\ell\right)}}}\right)} - 1 \]
      5. sqr-neg17.3%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\sqrt{\color{blue}{\ell \cdot \ell}}}\right)} - 1 \]
      6. sqrt-unprod0.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\ell} \cdot \sqrt{\ell}}}\right)} - 1 \]
      7. add-sqr-sqrt73.6%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\ell}}\right)} - 1 \]
    9. Applied egg-rr73.6%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)} - 1} \]
    10. Step-by-step derivation
      1. expm1-def73.6%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)\right)} \]
      2. expm1-log1p73.6%

        \[\leadsto \color{blue}{\cos M \cdot e^{\ell}} \]
    11. Simplified73.6%

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

    if -1.55e-4 < l < 720

    1. Initial program 75.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. Taylor expanded in m around inf 44.7%

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

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

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

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

      \[\leadsto \color{blue}{\cos \left(-M\right) \cdot e^{-0.25 \cdot {m}^{2}}} \]
    6. Step-by-step derivation
      1. cos-neg58.0%

        \[\leadsto \color{blue}{\cos M} \cdot e^{-0.25 \cdot {m}^{2}} \]
      2. *-commutative58.0%

        \[\leadsto \cos M \cdot e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
      3. unpow258.0%

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

      \[\leadsto \color{blue}{\cos M \cdot e^{\left(m \cdot m\right) \cdot -0.25}} \]
    8. Taylor expanded in M around 0 58.0%

      \[\leadsto \color{blue}{e^{-0.25 \cdot {m}^{2}}} \]
    9. Step-by-step derivation
      1. *-commutative58.0%

        \[\leadsto e^{\color{blue}{{m}^{2} \cdot -0.25}} \]
      2. unpow258.0%

        \[\leadsto e^{\color{blue}{\left(m \cdot m\right)} \cdot -0.25} \]
      3. associate-*r*58.0%

        \[\leadsto e^{\color{blue}{m \cdot \left(m \cdot -0.25\right)}} \]
      4. exp-prod58.0%

        \[\leadsto \color{blue}{{\left(e^{m}\right)}^{\left(m \cdot -0.25\right)}} \]
    10. Simplified58.0%

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

    if 720 < l

    1. Initial program 79.7%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq -0.000155:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{elif}\;\ell \leq 720:\\ \;\;\;\;{\left(e^{m}\right)}^{\left(m \cdot -0.25\right)}\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \]

Alternative 5: 48.7% accurate, 2.1× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq 1.55 \cdot 10^{-12}:\\
\;\;\;\;\cos M \cdot e^{\ell}\\

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


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

    1. Initial program 77.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. Taylor expanded in l around inf 14.3%

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

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

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

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

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

      \[\leadsto \color{blue}{\cos M \cdot e^{-\ell}} \]
    8. Step-by-step derivation
      1. expm1-log1p-u12.9%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)\right)} \]
      2. expm1-udef12.9%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)} - 1} \]
      3. add-sqr-sqrt10.4%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{-\ell} \cdot \sqrt{-\ell}}}\right)} - 1 \]
      4. sqrt-unprod12.9%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\left(-\ell\right) \cdot \left(-\ell\right)}}}\right)} - 1 \]
      5. sqr-neg12.9%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\sqrt{\color{blue}{\ell \cdot \ell}}}\right)} - 1 \]
      6. sqrt-unprod2.5%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\ell} \cdot \sqrt{\ell}}}\right)} - 1 \]
      7. add-sqr-sqrt33.2%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\ell}}\right)} - 1 \]
    9. Applied egg-rr33.2%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)} - 1} \]
    10. Step-by-step derivation
      1. expm1-def33.2%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)\right)} \]
      2. expm1-log1p33.2%

        \[\leadsto \color{blue}{\cos M \cdot e^{\ell}} \]
    11. Simplified33.2%

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

    if 1.5500000000000001e-12 < l

    1. Initial program 80.0%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq 1.55 \cdot 10^{-12}:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{else}:\\ \;\;\;\;e^{-\ell}\\ \end{array} \]

Alternative 6: 48.7% accurate, 2.1× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;\ell \leq -5 \cdot 10^{-266}:\\
\;\;\;\;\cos M \cdot e^{\ell}\\

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


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

    1. Initial program 78.3%

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

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

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-\ell} \]
    7. Simplified17.9%

      \[\leadsto \color{blue}{\cos M \cdot e^{-\ell}} \]
    8. Step-by-step derivation
      1. expm1-log1p-u15.2%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)\right)} \]
      2. expm1-udef15.2%

        \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{-\ell}\right)} - 1} \]
      3. add-sqr-sqrt15.2%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{-\ell} \cdot \sqrt{-\ell}}}\right)} - 1 \]
      4. sqrt-unprod15.2%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\left(-\ell\right) \cdot \left(-\ell\right)}}}\right)} - 1 \]
      5. sqr-neg15.2%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\sqrt{\color{blue}{\ell \cdot \ell}}}\right)} - 1 \]
      6. sqrt-unprod0.0%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\sqrt{\ell} \cdot \sqrt{\ell}}}\right)} - 1 \]
      7. add-sqr-sqrt46.9%

        \[\leadsto e^{\mathsf{log1p}\left(\cos M \cdot e^{\color{blue}{\ell}}\right)} - 1 \]
    9. Applied egg-rr46.9%

      \[\leadsto \color{blue}{e^{\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)} - 1} \]
    10. Step-by-step derivation
      1. expm1-def46.9%

        \[\leadsto \color{blue}{\mathsf{expm1}\left(\mathsf{log1p}\left(\cos M \cdot e^{\ell}\right)\right)} \]
      2. expm1-log1p46.9%

        \[\leadsto \color{blue}{\cos M \cdot e^{\ell}} \]
    11. Simplified46.9%

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

    if -4.99999999999999992e-266 < l

    1. Initial program 78.2%

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

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

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

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

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

        \[\leadsto \color{blue}{\cos M} \cdot e^{-\ell} \]
    7. Simplified54.7%

      \[\leadsto \color{blue}{\cos M \cdot e^{-\ell}} \]
    8. Taylor expanded in l around -inf 54.7%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{-\ell}} \]
      2. exp-neg54.7%

        \[\leadsto \cos M \cdot \color{blue}{\frac{1}{e^{\ell}}} \]
      3. associate-*r/54.7%

        \[\leadsto \color{blue}{\frac{\cos M \cdot 1}{e^{\ell}}} \]
      4. *-rgt-identity54.7%

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\ell \leq -5 \cdot 10^{-266}:\\ \;\;\;\;\cos M \cdot e^{\ell}\\ \mathbf{else}:\\ \;\;\;\;\frac{\cos M}{e^{\ell}}\\ \end{array} \]

Alternative 7: 34.9% accurate, 4.2× speedup?

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

\\
e^{-\ell}
\end{array}
Derivation
  1. Initial program 78.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. Taylor expanded in l around inf 31.9%

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

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

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

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

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

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

    \[\leadsto \color{blue}{e^{-\ell}} \]
  9. Final simplification36.4%

    \[\leadsto e^{-\ell} \]

Reproduce

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