Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.1% → 96.3%
Time: 28.9s
Alternatives: 11
Speedup: 1.9×

Specification

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

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

Sampling outcomes in binary64 precision:

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 11 alternatives:

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

Initial Program: 76.1% accurate, 1.0× speedup?

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

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

Alternative 1: 96.3% accurate, 0.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left|m - n\right| - \ell\\ t_1 := e^{t\_0 - {\left(\frac{m + n}{2} - M\right)}^{2}}\\ t_2 := \left(m + n\right) \cdot 0.5\\ \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t\_1 \leq -1 \cdot 10^{-24}:\\ \;\;\;\;t\_1 \cdot \cos \left(\frac{\frac{K \cdot \left({m}^{3} + {n}^{3}\right)}{\mathsf{fma}\left(m, m, n \cdot \left(n - m\right)\right)}}{2} - M\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\left(t\_2 - M\right) \cdot \left(M - t\_2\right) + t\_0}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (- (fabs (- m n)) l))
        (t_1 (exp (- t_0 (pow (- (/ (+ m n) 2.0) M) 2.0))))
        (t_2 (* (+ m n) 0.5)))
   (if (<= (* (cos (- (/ (* K (+ m n)) 2.0) M)) t_1) -1e-24)
     (*
      t_1
      (cos
       (-
        (/ (/ (* K (+ (pow m 3.0) (pow n 3.0))) (fma m m (* n (- n m)))) 2.0)
        M)))
     (exp (+ (* (- t_2 M) (- M t_2)) t_0)))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = fabs((m - n)) - l;
	double t_1 = exp((t_0 - pow((((m + n) / 2.0) - M), 2.0)));
	double t_2 = (m + n) * 0.5;
	double tmp;
	if ((cos((((K * (m + n)) / 2.0) - M)) * t_1) <= -1e-24) {
		tmp = t_1 * cos(((((K * (pow(m, 3.0) + pow(n, 3.0))) / fma(m, m, (n * (n - m)))) / 2.0) - M));
	} else {
		tmp = exp((((t_2 - M) * (M - t_2)) + t_0));
	}
	return tmp;
}
function code(K, m, n, M, l)
	t_0 = Float64(abs(Float64(m - n)) - l)
	t_1 = exp(Float64(t_0 - (Float64(Float64(Float64(m + n) / 2.0) - M) ^ 2.0)))
	t_2 = Float64(Float64(m + n) * 0.5)
	tmp = 0.0
	if (Float64(cos(Float64(Float64(Float64(K * Float64(m + n)) / 2.0) - M)) * t_1) <= -1e-24)
		tmp = Float64(t_1 * cos(Float64(Float64(Float64(Float64(K * Float64((m ^ 3.0) + (n ^ 3.0))) / fma(m, m, Float64(n * Float64(n - m)))) / 2.0) - M)));
	else
		tmp = exp(Float64(Float64(Float64(t_2 - M) * Float64(M - t_2)) + t_0));
	end
	return tmp
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]}, Block[{t$95$1 = N[Exp[N[(t$95$0 - N[Power[N[(N[(N[(m + n), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision], 2.0], $MachinePrecision]), $MachinePrecision]], $MachinePrecision]}, Block[{t$95$2 = N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision]}, If[LessEqual[N[(N[Cos[N[(N[(N[(K * N[(m + n), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * t$95$1), $MachinePrecision], -1e-24], N[(t$95$1 * N[Cos[N[(N[(N[(N[(K * N[(N[Power[m, 3.0], $MachinePrecision] + N[Power[n, 3.0], $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / N[(m * m + N[(n * N[(n - m), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision] / 2.0), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[Exp[N[(N[(N[(t$95$2 - M), $MachinePrecision] * N[(M - t$95$2), $MachinePrecision]), $MachinePrecision] + t$95$0), $MachinePrecision]], $MachinePrecision]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \left|m - n\right| - \ell\\
t_1 := e^{t\_0 - {\left(\frac{m + n}{2} - M\right)}^{2}}\\
t_2 := \left(m + n\right) \cdot 0.5\\
\mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot t\_1 \leq -1 \cdot 10^{-24}:\\
\;\;\;\;t\_1 \cdot \cos \left(\frac{\frac{K \cdot \left({m}^{3} + {n}^{3}\right)}{\mathsf{fma}\left(m, m, n \cdot \left(n - m\right)\right)}}{2} - M\right)\\

\mathbf{else}:\\
\;\;\;\;e^{\left(t\_2 - M\right) \cdot \left(M - t\_2\right) + t\_0}\\


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.f64 l (fabs.f64 (-.f64 m n)))))) < -9.99999999999999924e-25

    1. Initial program 59.5%

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

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

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

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

        \[\leadsto \cos \left(\frac{\frac{\left({m}^{3} + {n}^{3}\right) \cdot K}{\color{blue}{\mathsf{fma}\left(m, m, n \cdot n - m \cdot n\right)}}}{2} - M\right) \cdot e^{\left(-{\left(\frac{m + n}{2} - M\right)}^{2}\right) - \left(\ell - \left|m - n\right|\right)} \]
      5. distribute-rgt-out--74.0%

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

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

    if -9.99999999999999924e-25 < (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.f64 l (fabs.f64 (-.f64 m n))))))

    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. Add Preprocessing
    3. Step-by-step derivation
      1. unpow277.8%

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;\cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \leq -1 \cdot 10^{-24}:\\ \;\;\;\;e^{\left(\left|m - n\right| - \ell\right) - {\left(\frac{m + n}{2} - M\right)}^{2}} \cdot \cos \left(\frac{\frac{K \cdot \left({m}^{3} + {n}^{3}\right)}{\mathsf{fma}\left(m, m, n \cdot \left(n - m\right)\right)}}{2} - M\right)\\ \mathbf{else}:\\ \;\;\;\;e^{\left(\left(m + n\right) \cdot 0.5 - M\right) \cdot \left(M - \left(m + n\right) \cdot 0.5\right) + \left(\left|m - n\right| - \ell\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 2: 96.6% accurate, 0.6× speedup?

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

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

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.f64 l (fabs.f64 (-.f64 m n)))))) < 1

    1. Initial program 96.7%

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

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

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

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

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

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

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

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

    if 1 < (*.f64 (cos.f64 (-.f64 (/.f64 (*.f64 K (+.f64 m n)) #s(literal 2 binary64)) M)) (exp.f64 (-.f64 (neg.f64 (pow.f64 (-.f64 (/.f64 (+.f64 m n) #s(literal 2 binary64)) M) #s(literal 2 binary64))) (-.f64 l (fabs.f64 (-.f64 m n))))))

    1. Initial program 16.1%

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

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

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

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

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

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

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

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

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

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

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

Alternative 3: 45.9% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos M \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{if}\;M \leq -58000000000:\\ \;\;\;\;\cos M \cdot e^{n \cdot M}\\ \mathbf{elif}\;M \leq -1.1 \cdot 10^{-90}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;M \leq -3.8 \cdot 10^{-140}:\\ \;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right)}\\ \mathbf{elif}\;M \leq 2.7 \cdot 10^{+82}:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;\cos \left(K \cdot \left(m \cdot 0.5\right) - M\right) \cdot e^{n \cdot \left(m \cdot 0.5 + \left(-1 - M\right)\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* (cos M) (exp (- (* M (- M n)) (+ n l))))))
   (if (<= M -58000000000.0)
     (* (cos M) (exp (* n M)))
     (if (<= M -1.1e-90)
       t_0
       (if (<= M -3.8e-140)
         (* (cos M) (exp (* n (- M (* m 0.5)))))
         (if (<= M 2.7e+82)
           t_0
           (*
            (cos (- (* K (* m 0.5)) M))
            (exp (* n (+ (* m 0.5) (- -1.0 M)))))))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = cos(M) * exp(((M * (M - n)) - (n + l)));
	double tmp;
	if (M <= -58000000000.0) {
		tmp = cos(M) * exp((n * M));
	} else if (M <= -1.1e-90) {
		tmp = t_0;
	} else if (M <= -3.8e-140) {
		tmp = cos(M) * exp((n * (M - (m * 0.5))));
	} else if (M <= 2.7e+82) {
		tmp = t_0;
	} else {
		tmp = cos(((K * (m * 0.5)) - M)) * exp((n * ((m * 0.5) + (-1.0 - M))));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: t_0
    real(8) :: tmp
    t_0 = cos(m_1) * exp(((m_1 * (m_1 - n)) - (n + l)))
    if (m_1 <= (-58000000000.0d0)) then
        tmp = cos(m_1) * exp((n * m_1))
    else if (m_1 <= (-1.1d-90)) then
        tmp = t_0
    else if (m_1 <= (-3.8d-140)) then
        tmp = cos(m_1) * exp((n * (m_1 - (m * 0.5d0))))
    else if (m_1 <= 2.7d+82) then
        tmp = t_0
    else
        tmp = cos(((k * (m * 0.5d0)) - m_1)) * exp((n * ((m * 0.5d0) + ((-1.0d0) - m_1))))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.cos(M) * Math.exp(((M * (M - n)) - (n + l)));
	double tmp;
	if (M <= -58000000000.0) {
		tmp = Math.cos(M) * Math.exp((n * M));
	} else if (M <= -1.1e-90) {
		tmp = t_0;
	} else if (M <= -3.8e-140) {
		tmp = Math.cos(M) * Math.exp((n * (M - (m * 0.5))));
	} else if (M <= 2.7e+82) {
		tmp = t_0;
	} else {
		tmp = Math.cos(((K * (m * 0.5)) - M)) * Math.exp((n * ((m * 0.5) + (-1.0 - M))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.cos(M) * math.exp(((M * (M - n)) - (n + l)))
	tmp = 0
	if M <= -58000000000.0:
		tmp = math.cos(M) * math.exp((n * M))
	elif M <= -1.1e-90:
		tmp = t_0
	elif M <= -3.8e-140:
		tmp = math.cos(M) * math.exp((n * (M - (m * 0.5))))
	elif M <= 2.7e+82:
		tmp = t_0
	else:
		tmp = math.cos(((K * (m * 0.5)) - M)) * math.exp((n * ((m * 0.5) + (-1.0 - M))))
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(cos(M) * exp(Float64(Float64(M * Float64(M - n)) - Float64(n + l))))
	tmp = 0.0
	if (M <= -58000000000.0)
		tmp = Float64(cos(M) * exp(Float64(n * M)));
	elseif (M <= -1.1e-90)
		tmp = t_0;
	elseif (M <= -3.8e-140)
		tmp = Float64(cos(M) * exp(Float64(n * Float64(M - Float64(m * 0.5)))));
	elseif (M <= 2.7e+82)
		tmp = t_0;
	else
		tmp = Float64(cos(Float64(Float64(K * Float64(m * 0.5)) - M)) * exp(Float64(n * Float64(Float64(m * 0.5) + Float64(-1.0 - M)))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = cos(M) * exp(((M * (M - n)) - (n + l)));
	tmp = 0.0;
	if (M <= -58000000000.0)
		tmp = cos(M) * exp((n * M));
	elseif (M <= -1.1e-90)
		tmp = t_0;
	elseif (M <= -3.8e-140)
		tmp = cos(M) * exp((n * (M - (m * 0.5))));
	elseif (M <= 2.7e+82)
		tmp = t_0;
	else
		tmp = cos(((K * (m * 0.5)) - M)) * exp((n * ((m * 0.5) + (-1.0 - M))));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(M * N[(M - n), $MachinePrecision]), $MachinePrecision] - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[M, -58000000000.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(n * M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[M, -1.1e-90], t$95$0, If[LessEqual[M, -3.8e-140], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(n * N[(M - N[(m * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[M, 2.7e+82], t$95$0, N[(N[Cos[N[(N[(K * N[(m * 0.5), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision] * N[Exp[N[(n * N[(N[(m * 0.5), $MachinePrecision] + N[(-1.0 - M), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos M \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\
\mathbf{if}\;M \leq -58000000000:\\
\;\;\;\;\cos M \cdot e^{n \cdot M}\\

\mathbf{elif}\;M \leq -1.1 \cdot 10^{-90}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;M \leq -3.8 \cdot 10^{-140}:\\
\;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right)}\\

\mathbf{elif}\;M \leq 2.7 \cdot 10^{+82}:\\
\;\;\;\;t\_0\\

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


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

    1. Initial program 76.7%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot \left(M - 0.5 \cdot m\right)}} \]
    10. Taylor expanded in M around inf 44.6%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot M}} \]
    12. Simplified44.6%

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

    if -5.8e10 < M < -1.09999999999999993e-90 or -3.79999999999999998e-140 < M < 2.6999999999999999e82

    1. Initial program 78.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{\left(-1 \cdot M\right) \cdot \left(n - M\right)} - \left(\ell + n\right)} \]
      3. neg-mul-153.4%

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

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

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

    if -1.09999999999999993e-90 < M < -3.79999999999999998e-140

    1. Initial program 60.0%

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out70.5%

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

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

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

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

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

    if 2.6999999999999999e82 < M

    1. Initial program 78.7%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -58000000000:\\ \;\;\;\;\cos M \cdot e^{n \cdot M}\\ \mathbf{elif}\;M \leq -1.1 \cdot 10^{-90}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{elif}\;M \leq -3.8 \cdot 10^{-140}:\\ \;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right)}\\ \mathbf{elif}\;M \leq 2.7 \cdot 10^{+82}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos \left(K \cdot \left(m \cdot 0.5\right) - M\right) \cdot e^{n \cdot \left(m \cdot 0.5 + \left(-1 - M\right)\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 4: 47.0% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;M \leq -6000000000000:\\ \;\;\;\;\cos M \cdot e^{n \cdot M}\\ \mathbf{elif}\;M \leq -9.8 \cdot 10^{-91} \lor \neg \left(M \leq -3.8 \cdot 10^{-140}\right) \land M \leq 3 \cdot 10^{+87}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= M -6000000000000.0)
   (* (cos M) (exp (* n M)))
   (if (or (<= M -9.8e-91) (and (not (<= M -3.8e-140)) (<= M 3e+87)))
     (* (cos M) (exp (- (* M (- M n)) (+ n l))))
     (* (cos M) (exp (* n (- M (* m 0.5))))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (M <= -6000000000000.0) {
		tmp = cos(M) * exp((n * M));
	} else if ((M <= -9.8e-91) || (!(M <= -3.8e-140) && (M <= 3e+87))) {
		tmp = cos(M) * exp(((M * (M - n)) - (n + l)));
	} else {
		tmp = cos(M) * exp((n * (M - (m * 0.5))));
	}
	return tmp;
}
real(8) function code(k, m, n, m_1, l)
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: tmp
    if (m_1 <= (-6000000000000.0d0)) then
        tmp = cos(m_1) * exp((n * m_1))
    else if ((m_1 <= (-9.8d-91)) .or. (.not. (m_1 <= (-3.8d-140))) .and. (m_1 <= 3d+87)) then
        tmp = cos(m_1) * exp(((m_1 * (m_1 - n)) - (n + l)))
    else
        tmp = cos(m_1) * exp((n * (m_1 - (m * 0.5d0))))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (M <= -6000000000000.0) {
		tmp = Math.cos(M) * Math.exp((n * M));
	} else if ((M <= -9.8e-91) || (!(M <= -3.8e-140) && (M <= 3e+87))) {
		tmp = Math.cos(M) * Math.exp(((M * (M - n)) - (n + l)));
	} else {
		tmp = Math.cos(M) * Math.exp((n * (M - (m * 0.5))));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if M <= -6000000000000.0:
		tmp = math.cos(M) * math.exp((n * M))
	elif (M <= -9.8e-91) or (not (M <= -3.8e-140) and (M <= 3e+87)):
		tmp = math.cos(M) * math.exp(((M * (M - n)) - (n + l)))
	else:
		tmp = math.cos(M) * math.exp((n * (M - (m * 0.5))))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (M <= -6000000000000.0)
		tmp = Float64(cos(M) * exp(Float64(n * M)));
	elseif ((M <= -9.8e-91) || (!(M <= -3.8e-140) && (M <= 3e+87)))
		tmp = Float64(cos(M) * exp(Float64(Float64(M * Float64(M - n)) - Float64(n + l))));
	else
		tmp = Float64(cos(M) * exp(Float64(n * Float64(M - Float64(m * 0.5)))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (M <= -6000000000000.0)
		tmp = cos(M) * exp((n * M));
	elseif ((M <= -9.8e-91) || (~((M <= -3.8e-140)) && (M <= 3e+87)))
		tmp = cos(M) * exp(((M * (M - n)) - (n + l)));
	else
		tmp = cos(M) * exp((n * (M - (m * 0.5))));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[M, -6000000000000.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(n * M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[Or[LessEqual[M, -9.8e-91], And[N[Not[LessEqual[M, -3.8e-140]], $MachinePrecision], LessEqual[M, 3e+87]]], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(M * N[(M - n), $MachinePrecision]), $MachinePrecision] - N[(n + l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(n * N[(M - N[(m * 0.5), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]
\begin{array}{l}

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

\mathbf{elif}\;M \leq -9.8 \cdot 10^{-91} \lor \neg \left(M \leq -3.8 \cdot 10^{-140}\right) \land M \leq 3 \cdot 10^{+87}:\\
\;\;\;\;\cos M \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\

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


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

    1. Initial program 76.7%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot \left(M - 0.5 \cdot m\right)}} \]
    10. Taylor expanded in M around inf 44.6%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot M}} \]
    12. Simplified44.6%

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

    if -6e12 < M < -9.7999999999999996e-91 or -3.79999999999999998e-140 < M < 2.9999999999999999e87

    1. Initial program 78.1%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos M \cdot e^{\color{blue}{\left(-1 \cdot M\right) \cdot \left(n - M\right)} - \left(\ell + n\right)} \]
      3. neg-mul-153.4%

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

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

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

    if -9.7999999999999996e-91 < M < -3.79999999999999998e-140 or 2.9999999999999999e87 < 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 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 0 82.5%

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

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

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

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

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

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

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

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

    \[\leadsto \begin{array}{l} \mathbf{if}\;M \leq -6000000000000:\\ \;\;\;\;\cos M \cdot e^{n \cdot M}\\ \mathbf{elif}\;M \leq -9.8 \cdot 10^{-91} \lor \neg \left(M \leq -3.8 \cdot 10^{-140}\right) \land M \leq 3 \cdot 10^{+87}:\\ \;\;\;\;\cos M \cdot e^{M \cdot \left(M - n\right) - \left(n + \ell\right)}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{n \cdot \left(M - m \cdot 0.5\right)}\\ \end{array} \]
  5. Add Preprocessing

Alternative 5: 48.6% accurate, 1.8× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos \left(K \cdot \left(m \cdot 0.5\right) - M\right)\\ \mathbf{if}\;M \leq -27000000000:\\ \;\;\;\;\cos M \cdot e^{n \cdot M}\\ \mathbf{elif}\;M \leq 10^{-58}:\\ \;\;\;\;t\_0 \cdot e^{\left(m - n\right) - \left(\ell + M \cdot \left(n - M\right)\right)}\\ \mathbf{else}:\\ \;\;\;\;t\_0 \cdot e^{\left(m - n\right) + \left(n \cdot \left(m \cdot 0.5 - M\right) - \ell\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (cos (- (* K (* m 0.5)) M))))
   (if (<= M -27000000000.0)
     (* (cos M) (exp (* n M)))
     (if (<= M 1e-58)
       (* t_0 (exp (- (- m n) (+ l (* M (- n M))))))
       (* t_0 (exp (+ (- m n) (- (* n (- (* m 0.5) M)) l))))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = cos(((K * (m * 0.5)) - M));
	double tmp;
	if (M <= -27000000000.0) {
		tmp = cos(M) * exp((n * M));
	} else if (M <= 1e-58) {
		tmp = t_0 * exp(((m - n) - (l + (M * (n - M)))));
	} else {
		tmp = t_0 * exp(((m - n) + ((n * ((m * 0.5) - M)) - 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) :: t_0
    real(8) :: tmp
    t_0 = cos(((k * (m * 0.5d0)) - m_1))
    if (m_1 <= (-27000000000.0d0)) then
        tmp = cos(m_1) * exp((n * m_1))
    else if (m_1 <= 1d-58) then
        tmp = t_0 * exp(((m - n) - (l + (m_1 * (n - m_1)))))
    else
        tmp = t_0 * exp(((m - n) + ((n * ((m * 0.5d0) - m_1)) - l)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.cos(((K * (m * 0.5)) - M));
	double tmp;
	if (M <= -27000000000.0) {
		tmp = Math.cos(M) * Math.exp((n * M));
	} else if (M <= 1e-58) {
		tmp = t_0 * Math.exp(((m - n) - (l + (M * (n - M)))));
	} else {
		tmp = t_0 * Math.exp(((m - n) + ((n * ((m * 0.5) - M)) - l)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.cos(((K * (m * 0.5)) - M))
	tmp = 0
	if M <= -27000000000.0:
		tmp = math.cos(M) * math.exp((n * M))
	elif M <= 1e-58:
		tmp = t_0 * math.exp(((m - n) - (l + (M * (n - M)))))
	else:
		tmp = t_0 * math.exp(((m - n) + ((n * ((m * 0.5) - M)) - l)))
	return tmp
function code(K, m, n, M, l)
	t_0 = cos(Float64(Float64(K * Float64(m * 0.5)) - M))
	tmp = 0.0
	if (M <= -27000000000.0)
		tmp = Float64(cos(M) * exp(Float64(n * M)));
	elseif (M <= 1e-58)
		tmp = Float64(t_0 * exp(Float64(Float64(m - n) - Float64(l + Float64(M * Float64(n - M))))));
	else
		tmp = Float64(t_0 * exp(Float64(Float64(m - n) + Float64(Float64(n * Float64(Float64(m * 0.5) - M)) - l))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = cos(((K * (m * 0.5)) - M));
	tmp = 0.0;
	if (M <= -27000000000.0)
		tmp = cos(M) * exp((n * M));
	elseif (M <= 1e-58)
		tmp = t_0 * exp(((m - n) - (l + (M * (n - M)))));
	else
		tmp = t_0 * exp(((m - n) + ((n * ((m * 0.5) - M)) - l)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[Cos[N[(N[(K * N[(m * 0.5), $MachinePrecision]), $MachinePrecision] - M), $MachinePrecision]], $MachinePrecision]}, If[LessEqual[M, -27000000000.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(n * M), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[M, 1e-58], N[(t$95$0 * N[Exp[N[(N[(m - n), $MachinePrecision] - N[(l + N[(M * N[(n - M), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], N[(t$95$0 * N[Exp[N[(N[(m - n), $MachinePrecision] + N[(N[(n * N[(N[(m * 0.5), $MachinePrecision] - M), $MachinePrecision]), $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

\mathbf{elif}\;M \leq 10^{-58}:\\
\;\;\;\;t\_0 \cdot e^{\left(m - n\right) - \left(\ell + M \cdot \left(n - M\right)\right)}\\

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


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

    1. Initial program 76.7%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot \left(M - 0.5 \cdot m\right)}} \]
    10. Taylor expanded in M around inf 44.6%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot M}} \]
    12. Simplified44.6%

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

    if -2.7e10 < M < 1e-58

    1. Initial program 77.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1e-58 < M

    1. Initial program 77.3%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 6: 46.9% accurate, 1.9× speedup?

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

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

\mathbf{elif}\;M \leq 1.35 \cdot 10^{+110}:\\
\;\;\;\;t\_0 \cdot e^{\left(m - n\right) - \left(\ell + M \cdot \left(n - M\right)\right)}\\

\mathbf{else}:\\
\;\;\;\;t\_0 \cdot e^{n \cdot \left(m \cdot 0.5 + \left(-1 - M\right)\right)}\\


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

    1. Initial program 76.7%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot \left(M - 0.5 \cdot m\right)}} \]
    10. Taylor expanded in M around inf 44.6%

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

        \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot M}} \]
    12. Simplified44.6%

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

    if -3.1e13 < M < 1.35000000000000005e110

    1. Initial program 77.0%

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    if 1.35000000000000005e110 < M

    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. Step-by-step derivation
      1. *-un-lft-identity78.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

        \[\leadsto \cos \left(K \cdot \left(m \cdot 0.5\right) - M\right) \cdot \left(e^{n \cdot \left(0.5 \cdot m - \color{blue}{\left(M + 1\right)}\right)} \cdot 1\right) \]
    13. Simplified32.0%

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

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

Alternative 7: 95.9% accurate, 1.9× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \left(m + n\right) \cdot 0.5\\ e^{\left(t\_0 - M\right) \cdot \left(M - t\_0\right) + \left(\left|m - n\right| - \ell\right)} \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* (+ m n) 0.5)))
   (exp (+ (* (- t_0 M) (- M t_0)) (- (fabs (- m n)) l)))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = (m + n) * 0.5;
	return exp((((t_0 - M) * (M - t_0)) + (fabs((m - n)) - 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
    real(8) :: t_0
    t_0 = (m + n) * 0.5d0
    code = exp((((t_0 - m_1) * (m_1 - t_0)) + (abs((m - n)) - l)))
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = (m + n) * 0.5;
	return Math.exp((((t_0 - M) * (M - t_0)) + (Math.abs((m - n)) - l)));
}
def code(K, m, n, M, l):
	t_0 = (m + n) * 0.5
	return math.exp((((t_0 - M) * (M - t_0)) + (math.fabs((m - n)) - l)))
function code(K, m, n, M, l)
	t_0 = Float64(Float64(m + n) * 0.5)
	return exp(Float64(Float64(Float64(t_0 - M) * Float64(M - t_0)) + Float64(abs(Float64(m - n)) - l)))
end
function tmp = code(K, m, n, M, l)
	t_0 = (m + n) * 0.5;
	tmp = exp((((t_0 - M) * (M - t_0)) + (abs((m - n)) - l)));
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[(m + n), $MachinePrecision] * 0.5), $MachinePrecision]}, N[Exp[N[(N[(N[(t$95$0 - M), $MachinePrecision] * N[(M - t$95$0), $MachinePrecision]), $MachinePrecision] + N[(N[Abs[N[(m - n), $MachinePrecision]], $MachinePrecision] - l), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]]
\begin{array}{l}

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 8: 36.7% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -7.5 \cdot 10^{-84}:\\
\;\;\;\;\cos M \cdot e^{n \cdot M}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -7.50000000000000026e-84

    1. Initial program 79.5%

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

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot \left(M - 0.5 \cdot m\right)}} \]
    10. Taylor expanded in M around inf 41.4%

      \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot n}} \]
    11. Step-by-step derivation
      1. *-commutative41.4%

        \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot M}} \]
    12. Simplified41.4%

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

    if -7.50000000000000026e-84 < M

    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 94.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-neg94.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. Simplified94.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 n around 0 72.2%

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

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

        \[\leadsto \cos M \cdot e^{\left(-\left(\color{blue}{\left(0.5 \cdot m - M\right) \cdot \left(0.5 \cdot m - M\right)} + n \cdot \left(0.5 \cdot m - M\right)\right)\right) - \left(\ell - \left|m - n\right|\right)} \]
      3. distribute-rgt-out77.5%

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

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

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

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

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

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

Alternative 9: 33.1% accurate, 2.0× speedup?

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

\\
\begin{array}{l}
\mathbf{if}\;M \leq -3.9 \cdot 10^{-92}:\\
\;\;\;\;\cos M \cdot e^{n \cdot M}\\

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


\end{array}
\end{array}
Derivation
  1. Split input into 2 regimes
  2. if M < -3.8999999999999997e-92

    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.8%

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

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

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

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

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

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

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

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

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

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

      \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot \left(M - 0.5 \cdot m\right)}} \]
    10. Taylor expanded in M around inf 42.1%

      \[\leadsto \cos M \cdot e^{\color{blue}{M \cdot n}} \]
    11. Step-by-step derivation
      1. *-commutative42.1%

        \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot M}} \]
    12. Simplified42.1%

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

    if -3.8999999999999997e-92 < 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. Add Preprocessing
    3. Taylor expanded in K around 0 94.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-neg94.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. Simplified94.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 n around 0 72.6%

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

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

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

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

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

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

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

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

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

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

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

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

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

Alternative 10: 30.8% accurate, 2.1× speedup?

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

\\
\cos M \cdot e^{n \cdot M}
\end{array}
Derivation
  1. Initial program 77.2%

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot \left(M - 0.5 \cdot m\right)}} \]
  10. Taylor expanded in M around inf 33.7%

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

      \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot M}} \]
  12. Simplified33.7%

    \[\leadsto \cos M \cdot e^{\color{blue}{n \cdot M}} \]
  13. Final simplification33.7%

    \[\leadsto \cos M \cdot e^{n \cdot M} \]
  14. Add Preprocessing

Alternative 11: 6.6% accurate, 3.8× speedup?

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

\\
\cos M \cdot \left(1 + n \cdot \left(M - m \cdot 0.5\right)\right)
\end{array}
Derivation
  1. Initial program 77.2%

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

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

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

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

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

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

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

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

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

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

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

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

    \[\leadsto \cos M \cdot \color{blue}{\left(1 + n \cdot \left(M - 0.5 \cdot m\right)\right)} \]
  11. Final simplification6.1%

    \[\leadsto \cos M \cdot \left(1 + n \cdot \left(M - m \cdot 0.5\right)\right) \]
  12. Add Preprocessing

Reproduce

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