Maksimov and Kolovsky, Equation (32)

Percentage Accurate: 76.5% → 96.8%
Time: 5.1s
Alternatives: 7
Speedup: 1.5×

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)))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(k, m, n, m_1, l)
use fmin_fmax_functions
    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}

Local Percentage Accuracy vs ?

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

Accuracy vs Speed?

Herbie found 7 alternatives:

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

Initial Program: 76.5% 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)))));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(k, m, n, m_1, l)
use fmin_fmax_functions
    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.8% accurate, 1.5× speedup?

\[\begin{array}{l} \\ \cos M \cdot e^{\left|n - m\right| - \left({\left(0.5 \cdot \left(n + m\right) - M\right)}^{2} + \ell\right)} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (* (cos M) (exp (- (fabs (- n m)) (+ (pow (- (* 0.5 (+ n m)) M) 2.0) l)))))
double code(double K, double m, double n, double M, double l) {
	return cos(M) * exp((fabs((n - m)) - (pow(((0.5 * (n + m)) - M), 2.0) + l)));
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

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

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

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

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

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

      \[\leadsto \cos M \cdot \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
    3. lower-cos.f64N/A

      \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
    4. lower-exp.f64N/A

      \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    5. lower--.f64N/A

      \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    6. fabs-subN/A

      \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    7. lower-fabs.f64N/A

      \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    8. lower--.f64N/A

      \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
    9. +-commutativeN/A

      \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)} \]
    10. lower-+.f64N/A

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

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

Alternative 2: 75.8% accurate, 1.3× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;m \leq -54:\\ \;\;\;\;\cos M \cdot e^{\left(m \cdot m\right) \cdot -0.25}\\ \mathbf{elif}\;m \leq -3.6 \cdot 10^{-141}:\\ \;\;\;\;\cos M \cdot e^{-1 \cdot \left(M \cdot M\right)}\\ \mathbf{elif}\;m \leq 3.9 \cdot 10^{-290}:\\ \;\;\;\;\cos \left(0.5 \cdot \left(K \cdot m\right)\right) \cdot e^{-\ell}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= m -54.0)
   (* (cos M) (exp (* (* m m) -0.25)))
   (if (<= m -3.6e-141)
     (* (cos M) (exp (* -1.0 (* M M))))
     (if (<= m 3.9e-290)
       (* (cos (* 0.5 (* K m))) (exp (- l)))
       (* (cos M) (exp (* -0.25 (* n n))))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -54.0) {
		tmp = cos(M) * exp(((m * m) * -0.25));
	} else if (m <= -3.6e-141) {
		tmp = cos(M) * exp((-1.0 * (M * M)));
	} else if (m <= 3.9e-290) {
		tmp = cos((0.5 * (K * m))) * exp(-l);
	} else {
		tmp = cos(M) * exp((-0.25 * (n * n)));
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(k, m, n, m_1, l)
use fmin_fmax_functions
    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 <= (-54.0d0)) then
        tmp = cos(m_1) * exp(((m * m) * (-0.25d0)))
    else if (m <= (-3.6d-141)) then
        tmp = cos(m_1) * exp(((-1.0d0) * (m_1 * m_1)))
    else if (m <= 3.9d-290) then
        tmp = cos((0.5d0 * (k * m))) * exp(-l)
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n * n)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -54.0) {
		tmp = Math.cos(M) * Math.exp(((m * m) * -0.25));
	} else if (m <= -3.6e-141) {
		tmp = Math.cos(M) * Math.exp((-1.0 * (M * M)));
	} else if (m <= 3.9e-290) {
		tmp = Math.cos((0.5 * (K * m))) * Math.exp(-l);
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if m <= -54.0:
		tmp = math.cos(M) * math.exp(((m * m) * -0.25))
	elif m <= -3.6e-141:
		tmp = math.cos(M) * math.exp((-1.0 * (M * M)))
	elif m <= 3.9e-290:
		tmp = math.cos((0.5 * (K * m))) * math.exp(-l)
	else:
		tmp = math.cos(M) * math.exp((-0.25 * (n * n)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (m <= -54.0)
		tmp = Float64(cos(M) * exp(Float64(Float64(m * m) * -0.25)));
	elseif (m <= -3.6e-141)
		tmp = Float64(cos(M) * exp(Float64(-1.0 * Float64(M * M))));
	elseif (m <= 3.9e-290)
		tmp = Float64(cos(Float64(0.5 * Float64(K * m))) * exp(Float64(-l)));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (m <= -54.0)
		tmp = cos(M) * exp(((m * m) * -0.25));
	elseif (m <= -3.6e-141)
		tmp = cos(M) * exp((-1.0 * (M * M)));
	elseif (m <= 3.9e-290)
		tmp = cos((0.5 * (K * m))) * exp(-l);
	else
		tmp = cos(M) * exp((-0.25 * (n * n)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[m, -54.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(m * m), $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, -3.6e-141], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-1.0 * N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, 3.9e-290], N[(N[Cos[N[(0.5 * N[(K * m), $MachinePrecision]), $MachinePrecision]], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

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

\mathbf{elif}\;m \leq 3.9 \cdot 10^{-290}:\\
\;\;\;\;\cos \left(0.5 \cdot \left(K \cdot m\right)\right) \cdot e^{-\ell}\\

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


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

    1. Initial program 71.2%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{m}^{2} \cdot \color{blue}{\frac{-1}{4}}} \]
      2. lower-*.f64N/A

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{m}^{2} \cdot \color{blue}{\frac{-1}{4}}} \]
      3. unpow2N/A

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m \cdot m\right) \cdot \frac{-1}{4}} \]
      4. lower-*.f6468.9

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m \cdot m\right) \cdot -0.25} \]
    4. Applied rewrites68.9%

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

      \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\right)\right)} \cdot e^{\left(m \cdot m\right) \cdot \frac{-1}{4}} \]
    6. Step-by-step derivation
      1. cos-neg-revN/A

        \[\leadsto \cos M \cdot e^{\left(m \cdot m\right) \cdot \frac{-1}{4}} \]
      2. lift-cos.f6497.3

        \[\leadsto \cos M \cdot e^{\left(m \cdot m\right) \cdot -0.25} \]
    7. Applied rewrites97.3%

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

    if -54 < m < -3.60000000000000015e-141

    1. Initial program 81.5%

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

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

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

        \[\leadsto \cos M \cdot \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      3. lower-cos.f64N/A

        \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      4. lower-exp.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      5. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      6. fabs-subN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      7. lower-fabs.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      8. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      9. +-commutativeN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)} \]
      10. lower-+.f64N/A

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

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

      \[\leadsto \cos M \cdot e^{-1 \cdot {M}^{2}} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos M \cdot e^{-1 \cdot {M}^{2}} \]
      2. unpow2N/A

        \[\leadsto \cos M \cdot e^{-1 \cdot \left(M \cdot M\right)} \]
      3. lower-*.f6459.6

        \[\leadsto \cos M \cdot e^{-1 \cdot \left(M \cdot M\right)} \]
    7. Applied rewrites59.6%

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

    if -3.60000000000000015e-141 < m < 3.89999999999999973e-290

    1. Initial program 82.0%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      2. lower-neg.f6438.0

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{-\ell} \]
    4. Applied rewrites38.0%

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

      \[\leadsto \cos \color{blue}{\left(\frac{1}{2} \cdot \left(K \cdot m\right)\right)} \cdot e^{-\ell} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos \left(\frac{1}{2} \cdot \color{blue}{\left(K \cdot m\right)}\right) \cdot e^{-\ell} \]
      2. lower-*.f6440.5

        \[\leadsto \cos \left(0.5 \cdot \left(K \cdot \color{blue}{m}\right)\right) \cdot e^{-\ell} \]
    7. Applied rewrites40.5%

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

    if 3.89999999999999973e-290 < m

    1. Initial program 76.3%

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

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

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

        \[\leadsto \cos M \cdot \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      3. lower-cos.f64N/A

        \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      4. lower-exp.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      5. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      6. fabs-subN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      7. lower-fabs.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      8. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      9. +-commutativeN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)} \]
      10. lower-+.f64N/A

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

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

      \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot {n}^{2}} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot {n}^{2}} \]
      2. unpow2N/A

        \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot \left(n \cdot n\right)} \]
      3. lower-*.f6454.3

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)} \]
    7. Applied rewrites54.3%

      \[\leadsto \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)} \]
  3. Recombined 4 regimes into one program.
  4. Add Preprocessing

Alternative 3: 75.8% accurate, 1.4× speedup?

\[\begin{array}{l} \\ \begin{array}{l} \mathbf{if}\;m \leq -54:\\ \;\;\;\;\cos M \cdot e^{\left(m \cdot m\right) \cdot -0.25}\\ \mathbf{elif}\;m \leq -3.6 \cdot 10^{-141}:\\ \;\;\;\;\cos M \cdot e^{-1 \cdot \left(M \cdot M\right)}\\ \mathbf{elif}\;m \leq 3.4 \cdot 10^{-290}:\\ \;\;\;\;\cos M \cdot e^{-\ell}\\ \mathbf{else}:\\ \;\;\;\;\cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (if (<= m -54.0)
   (* (cos M) (exp (* (* m m) -0.25)))
   (if (<= m -3.6e-141)
     (* (cos M) (exp (* -1.0 (* M M))))
     (if (<= m 3.4e-290)
       (* (cos M) (exp (- l)))
       (* (cos M) (exp (* -0.25 (* n n))))))))
double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -54.0) {
		tmp = cos(M) * exp(((m * m) * -0.25));
	} else if (m <= -3.6e-141) {
		tmp = cos(M) * exp((-1.0 * (M * M)));
	} else if (m <= 3.4e-290) {
		tmp = cos(M) * exp(-l);
	} else {
		tmp = cos(M) * exp((-0.25 * (n * n)));
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(k, m, n, m_1, l)
use fmin_fmax_functions
    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 <= (-54.0d0)) then
        tmp = cos(m_1) * exp(((m * m) * (-0.25d0)))
    else if (m <= (-3.6d-141)) then
        tmp = cos(m_1) * exp(((-1.0d0) * (m_1 * m_1)))
    else if (m <= 3.4d-290) then
        tmp = cos(m_1) * exp(-l)
    else
        tmp = cos(m_1) * exp(((-0.25d0) * (n * n)))
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double tmp;
	if (m <= -54.0) {
		tmp = Math.cos(M) * Math.exp(((m * m) * -0.25));
	} else if (m <= -3.6e-141) {
		tmp = Math.cos(M) * Math.exp((-1.0 * (M * M)));
	} else if (m <= 3.4e-290) {
		tmp = Math.cos(M) * Math.exp(-l);
	} else {
		tmp = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	}
	return tmp;
}
def code(K, m, n, M, l):
	tmp = 0
	if m <= -54.0:
		tmp = math.cos(M) * math.exp(((m * m) * -0.25))
	elif m <= -3.6e-141:
		tmp = math.cos(M) * math.exp((-1.0 * (M * M)))
	elif m <= 3.4e-290:
		tmp = math.cos(M) * math.exp(-l)
	else:
		tmp = math.cos(M) * math.exp((-0.25 * (n * n)))
	return tmp
function code(K, m, n, M, l)
	tmp = 0.0
	if (m <= -54.0)
		tmp = Float64(cos(M) * exp(Float64(Float64(m * m) * -0.25)));
	elseif (m <= -3.6e-141)
		tmp = Float64(cos(M) * exp(Float64(-1.0 * Float64(M * M))));
	elseif (m <= 3.4e-290)
		tmp = Float64(cos(M) * exp(Float64(-l)));
	else
		tmp = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))));
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	tmp = 0.0;
	if (m <= -54.0)
		tmp = cos(M) * exp(((m * m) * -0.25));
	elseif (m <= -3.6e-141)
		tmp = cos(M) * exp((-1.0 * (M * M)));
	elseif (m <= 3.4e-290)
		tmp = cos(M) * exp(-l);
	else
		tmp = cos(M) * exp((-0.25 * (n * n)));
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := If[LessEqual[m, -54.0], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(N[(m * m), $MachinePrecision] * -0.25), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, -3.6e-141], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-1.0 * N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision], If[LessEqual[m, 3.4e-290], N[(N[Cos[M], $MachinePrecision] * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision], N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]]]]
\begin{array}{l}

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

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

\mathbf{elif}\;m \leq 3.4 \cdot 10^{-290}:\\
\;\;\;\;\cos M \cdot e^{-\ell}\\

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


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

    1. Initial program 71.2%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{m}^{2} \cdot \color{blue}{\frac{-1}{4}}} \]
      2. lower-*.f64N/A

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{{m}^{2} \cdot \color{blue}{\frac{-1}{4}}} \]
      3. unpow2N/A

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m \cdot m\right) \cdot \frac{-1}{4}} \]
      4. lower-*.f6468.9

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\left(m \cdot m\right) \cdot -0.25} \]
    4. Applied rewrites68.9%

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

      \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\right)\right)} \cdot e^{\left(m \cdot m\right) \cdot \frac{-1}{4}} \]
    6. Step-by-step derivation
      1. cos-neg-revN/A

        \[\leadsto \cos M \cdot e^{\left(m \cdot m\right) \cdot \frac{-1}{4}} \]
      2. lift-cos.f6497.3

        \[\leadsto \cos M \cdot e^{\left(m \cdot m\right) \cdot -0.25} \]
    7. Applied rewrites97.3%

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

    if -54 < m < -3.60000000000000015e-141

    1. Initial program 81.5%

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

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

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

        \[\leadsto \cos M \cdot \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      3. lower-cos.f64N/A

        \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      4. lower-exp.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      5. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      6. fabs-subN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      7. lower-fabs.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      8. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      9. +-commutativeN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)} \]
      10. lower-+.f64N/A

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

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

      \[\leadsto \cos M \cdot e^{-1 \cdot {M}^{2}} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos M \cdot e^{-1 \cdot {M}^{2}} \]
      2. unpow2N/A

        \[\leadsto \cos M \cdot e^{-1 \cdot \left(M \cdot M\right)} \]
      3. lower-*.f6459.6

        \[\leadsto \cos M \cdot e^{-1 \cdot \left(M \cdot M\right)} \]
    7. Applied rewrites59.6%

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

    if -3.60000000000000015e-141 < m < 3.39999999999999984e-290

    1. Initial program 82.0%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      2. lower-neg.f6438.0

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{-\ell} \]
    4. Applied rewrites38.0%

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

      \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\right)\right)} \cdot e^{-\ell} \]
    6. Step-by-step derivation
      1. cos-neg-revN/A

        \[\leadsto \cos M \cdot e^{-\ell} \]
      2. lift-cos.f6441.2

        \[\leadsto \cos M \cdot e^{-\ell} \]
    7. Applied rewrites41.2%

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

    if 3.39999999999999984e-290 < m

    1. Initial program 76.3%

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

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

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

        \[\leadsto \cos M \cdot \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      3. lower-cos.f64N/A

        \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      4. lower-exp.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      5. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      6. fabs-subN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      7. lower-fabs.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      8. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      9. +-commutativeN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)} \]
      10. lower-+.f64N/A

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

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

      \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot {n}^{2}} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot {n}^{2}} \]
      2. unpow2N/A

        \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot \left(n \cdot n\right)} \]
      3. lower-*.f6454.3

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)} \]
    7. Applied rewrites54.3%

      \[\leadsto \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)} \]
  3. Recombined 4 regimes into one program.
  4. Add Preprocessing

Alternative 4: 69.6% accurate, 1.2× speedup?

\[\begin{array}{l} \\ \begin{array}{l} t_0 := \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ t_1 := \cos M \cdot e^{-1 \cdot \left(M \cdot M\right)}\\ \mathbf{if}\;M \leq -0.4:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;M \leq -4.1 \cdot 10^{-80}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;M \leq -7.5 \cdot 10^{-220}:\\ \;\;\;\;1 \cdot e^{-\ell}\\ \mathbf{elif}\;M \leq 27:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
(FPCore (K m n M l)
 :precision binary64
 (let* ((t_0 (* (cos M) (exp (* -0.25 (* n n)))))
        (t_1 (* (cos M) (exp (* -1.0 (* M M))))))
   (if (<= M -0.4)
     t_1
     (if (<= M -4.1e-80)
       t_0
       (if (<= M -7.5e-220) (* 1.0 (exp (- l))) (if (<= M 27.0) t_0 t_1))))))
double code(double K, double m, double n, double M, double l) {
	double t_0 = cos(M) * exp((-0.25 * (n * n)));
	double t_1 = cos(M) * exp((-1.0 * (M * M)));
	double tmp;
	if (M <= -0.4) {
		tmp = t_1;
	} else if (M <= -4.1e-80) {
		tmp = t_0;
	} else if (M <= -7.5e-220) {
		tmp = 1.0 * exp(-l);
	} else if (M <= 27.0) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
module fmin_fmax_functions
    implicit none
    private
    public fmax
    public fmin

    interface fmax
        module procedure fmax88
        module procedure fmax44
        module procedure fmax84
        module procedure fmax48
    end interface
    interface fmin
        module procedure fmin88
        module procedure fmin44
        module procedure fmin84
        module procedure fmin48
    end interface
contains
    real(8) function fmax88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(4) function fmax44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, max(x, y), y /= y), x /= x)
    end function
    real(8) function fmax84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmax48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
    end function
    real(8) function fmin88(x, y) result (res)
        real(8), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(4) function fmin44(x, y) result (res)
        real(4), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(y, merge(x, min(x, y), y /= y), x /= x)
    end function
    real(8) function fmin84(x, y) result(res)
        real(8), intent (in) :: x
        real(4), intent (in) :: y
        res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
    end function
    real(8) function fmin48(x, y) result(res)
        real(4), intent (in) :: x
        real(8), intent (in) :: y
        res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
    end function
end module

real(8) function code(k, m, n, m_1, l)
use fmin_fmax_functions
    real(8), intent (in) :: k
    real(8), intent (in) :: m
    real(8), intent (in) :: n
    real(8), intent (in) :: m_1
    real(8), intent (in) :: l
    real(8) :: t_0
    real(8) :: t_1
    real(8) :: tmp
    t_0 = cos(m_1) * exp(((-0.25d0) * (n * n)))
    t_1 = cos(m_1) * exp(((-1.0d0) * (m_1 * m_1)))
    if (m_1 <= (-0.4d0)) then
        tmp = t_1
    else if (m_1 <= (-4.1d-80)) then
        tmp = t_0
    else if (m_1 <= (-7.5d-220)) then
        tmp = 1.0d0 * exp(-l)
    else if (m_1 <= 27.0d0) then
        tmp = t_0
    else
        tmp = t_1
    end if
    code = tmp
end function
public static double code(double K, double m, double n, double M, double l) {
	double t_0 = Math.cos(M) * Math.exp((-0.25 * (n * n)));
	double t_1 = Math.cos(M) * Math.exp((-1.0 * (M * M)));
	double tmp;
	if (M <= -0.4) {
		tmp = t_1;
	} else if (M <= -4.1e-80) {
		tmp = t_0;
	} else if (M <= -7.5e-220) {
		tmp = 1.0 * Math.exp(-l);
	} else if (M <= 27.0) {
		tmp = t_0;
	} else {
		tmp = t_1;
	}
	return tmp;
}
def code(K, m, n, M, l):
	t_0 = math.cos(M) * math.exp((-0.25 * (n * n)))
	t_1 = math.cos(M) * math.exp((-1.0 * (M * M)))
	tmp = 0
	if M <= -0.4:
		tmp = t_1
	elif M <= -4.1e-80:
		tmp = t_0
	elif M <= -7.5e-220:
		tmp = 1.0 * math.exp(-l)
	elif M <= 27.0:
		tmp = t_0
	else:
		tmp = t_1
	return tmp
function code(K, m, n, M, l)
	t_0 = Float64(cos(M) * exp(Float64(-0.25 * Float64(n * n))))
	t_1 = Float64(cos(M) * exp(Float64(-1.0 * Float64(M * M))))
	tmp = 0.0
	if (M <= -0.4)
		tmp = t_1;
	elseif (M <= -4.1e-80)
		tmp = t_0;
	elseif (M <= -7.5e-220)
		tmp = Float64(1.0 * exp(Float64(-l)));
	elseif (M <= 27.0)
		tmp = t_0;
	else
		tmp = t_1;
	end
	return tmp
end
function tmp_2 = code(K, m, n, M, l)
	t_0 = cos(M) * exp((-0.25 * (n * n)));
	t_1 = cos(M) * exp((-1.0 * (M * M)));
	tmp = 0.0;
	if (M <= -0.4)
		tmp = t_1;
	elseif (M <= -4.1e-80)
		tmp = t_0;
	elseif (M <= -7.5e-220)
		tmp = 1.0 * exp(-l);
	elseif (M <= 27.0)
		tmp = t_0;
	else
		tmp = t_1;
	end
	tmp_2 = tmp;
end
code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-1.0 * N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[M, -0.4], t$95$1, If[LessEqual[M, -4.1e-80], t$95$0, If[LessEqual[M, -7.5e-220], N[(1.0 * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision], If[LessEqual[M, 27.0], t$95$0, t$95$1]]]]]]
\begin{array}{l}

\\
\begin{array}{l}
t_0 := \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\
t_1 := \cos M \cdot e^{-1 \cdot \left(M \cdot M\right)}\\
\mathbf{if}\;M \leq -0.4:\\
\;\;\;\;t\_1\\

\mathbf{elif}\;M \leq -4.1 \cdot 10^{-80}:\\
\;\;\;\;t\_0\\

\mathbf{elif}\;M \leq -7.5 \cdot 10^{-220}:\\
\;\;\;\;1 \cdot e^{-\ell}\\

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

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


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

    1. Initial program 79.4%

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

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

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

        \[\leadsto \cos M \cdot \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      3. lower-cos.f64N/A

        \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      4. lower-exp.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      5. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      6. fabs-subN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      7. lower-fabs.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      8. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      9. +-commutativeN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)} \]
      10. lower-+.f64N/A

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

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

      \[\leadsto \cos M \cdot e^{-1 \cdot {M}^{2}} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos M \cdot e^{-1 \cdot {M}^{2}} \]
      2. unpow2N/A

        \[\leadsto \cos M \cdot e^{-1 \cdot \left(M \cdot M\right)} \]
      3. lower-*.f6497.1

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

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

    if -0.40000000000000002 < M < -4.0999999999999999e-80 or -7.5000000000000002e-220 < M < 27

    1. Initial program 73.6%

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

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

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

        \[\leadsto \cos M \cdot \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      3. lower-cos.f64N/A

        \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
      4. lower-exp.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      5. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      6. fabs-subN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      7. lower-fabs.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      8. lower--.f64N/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
      9. +-commutativeN/A

        \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)} \]
      10. lower-+.f64N/A

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

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

      \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot {n}^{2}} \]
    6. Step-by-step derivation
      1. lower-*.f64N/A

        \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot {n}^{2}} \]
      2. unpow2N/A

        \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot \left(n \cdot n\right)} \]
      3. lower-*.f6458.1

        \[\leadsto \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)} \]
    7. Applied rewrites58.1%

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

    if -4.0999999999999999e-80 < M < -7.5000000000000002e-220

    1. Initial program 74.3%

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

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

        \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
      2. lower-neg.f6438.8

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

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

      \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\right)\right)} \cdot e^{-\ell} \]
    6. Step-by-step derivation
      1. cos-neg-revN/A

        \[\leadsto \cos M \cdot e^{-\ell} \]
      2. lift-cos.f6443.8

        \[\leadsto \cos M \cdot e^{-\ell} \]
    7. Applied rewrites43.8%

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

      \[\leadsto 1 \cdot e^{-\ell} \]
    9. Step-by-step derivation
      1. Applied rewrites43.8%

        \[\leadsto 1 \cdot e^{-\ell} \]
    10. Recombined 3 regimes into one program.
    11. Add Preprocessing

    Alternative 5: 63.3% accurate, 1.2× speedup?

    \[\begin{array}{l} \\ \begin{array}{l} t_0 := 1 \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ t_1 := \cos M \cdot e^{-1 \cdot \left(M \cdot M\right)}\\ \mathbf{if}\;M \leq -0.4:\\ \;\;\;\;t\_1\\ \mathbf{elif}\;M \leq -4.1 \cdot 10^{-80}:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;M \leq -7.5 \cdot 10^{-220}:\\ \;\;\;\;1 \cdot e^{-\ell}\\ \mathbf{elif}\;M \leq 27:\\ \;\;\;\;t\_0\\ \mathbf{else}:\\ \;\;\;\;t\_1\\ \end{array} \end{array} \]
    (FPCore (K m n M l)
     :precision binary64
     (let* ((t_0 (* 1.0 (exp (* -0.25 (* n n)))))
            (t_1 (* (cos M) (exp (* -1.0 (* M M))))))
       (if (<= M -0.4)
         t_1
         (if (<= M -4.1e-80)
           t_0
           (if (<= M -7.5e-220) (* 1.0 (exp (- l))) (if (<= M 27.0) t_0 t_1))))))
    double code(double K, double m, double n, double M, double l) {
    	double t_0 = 1.0 * exp((-0.25 * (n * n)));
    	double t_1 = cos(M) * exp((-1.0 * (M * M)));
    	double tmp;
    	if (M <= -0.4) {
    		tmp = t_1;
    	} else if (M <= -4.1e-80) {
    		tmp = t_0;
    	} else if (M <= -7.5e-220) {
    		tmp = 1.0 * exp(-l);
    	} else if (M <= 27.0) {
    		tmp = t_0;
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    module fmin_fmax_functions
        implicit none
        private
        public fmax
        public fmin
    
        interface fmax
            module procedure fmax88
            module procedure fmax44
            module procedure fmax84
            module procedure fmax48
        end interface
        interface fmin
            module procedure fmin88
            module procedure fmin44
            module procedure fmin84
            module procedure fmin48
        end interface
    contains
        real(8) function fmax88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(4) function fmax44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, max(x, y), y /= y), x /= x)
        end function
        real(8) function fmax84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmax48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
        end function
        real(8) function fmin88(x, y) result (res)
            real(8), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(4) function fmin44(x, y) result (res)
            real(4), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(y, merge(x, min(x, y), y /= y), x /= x)
        end function
        real(8) function fmin84(x, y) result(res)
            real(8), intent (in) :: x
            real(4), intent (in) :: y
            res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
        end function
        real(8) function fmin48(x, y) result(res)
            real(4), intent (in) :: x
            real(8), intent (in) :: y
            res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
        end function
    end module
    
    real(8) function code(k, m, n, m_1, l)
    use fmin_fmax_functions
        real(8), intent (in) :: k
        real(8), intent (in) :: m
        real(8), intent (in) :: n
        real(8), intent (in) :: m_1
        real(8), intent (in) :: l
        real(8) :: t_0
        real(8) :: t_1
        real(8) :: tmp
        t_0 = 1.0d0 * exp(((-0.25d0) * (n * n)))
        t_1 = cos(m_1) * exp(((-1.0d0) * (m_1 * m_1)))
        if (m_1 <= (-0.4d0)) then
            tmp = t_1
        else if (m_1 <= (-4.1d-80)) then
            tmp = t_0
        else if (m_1 <= (-7.5d-220)) then
            tmp = 1.0d0 * exp(-l)
        else if (m_1 <= 27.0d0) then
            tmp = t_0
        else
            tmp = t_1
        end if
        code = tmp
    end function
    
    public static double code(double K, double m, double n, double M, double l) {
    	double t_0 = 1.0 * Math.exp((-0.25 * (n * n)));
    	double t_1 = Math.cos(M) * Math.exp((-1.0 * (M * M)));
    	double tmp;
    	if (M <= -0.4) {
    		tmp = t_1;
    	} else if (M <= -4.1e-80) {
    		tmp = t_0;
    	} else if (M <= -7.5e-220) {
    		tmp = 1.0 * Math.exp(-l);
    	} else if (M <= 27.0) {
    		tmp = t_0;
    	} else {
    		tmp = t_1;
    	}
    	return tmp;
    }
    
    def code(K, m, n, M, l):
    	t_0 = 1.0 * math.exp((-0.25 * (n * n)))
    	t_1 = math.cos(M) * math.exp((-1.0 * (M * M)))
    	tmp = 0
    	if M <= -0.4:
    		tmp = t_1
    	elif M <= -4.1e-80:
    		tmp = t_0
    	elif M <= -7.5e-220:
    		tmp = 1.0 * math.exp(-l)
    	elif M <= 27.0:
    		tmp = t_0
    	else:
    		tmp = t_1
    	return tmp
    
    function code(K, m, n, M, l)
    	t_0 = Float64(1.0 * exp(Float64(-0.25 * Float64(n * n))))
    	t_1 = Float64(cos(M) * exp(Float64(-1.0 * Float64(M * M))))
    	tmp = 0.0
    	if (M <= -0.4)
    		tmp = t_1;
    	elseif (M <= -4.1e-80)
    		tmp = t_0;
    	elseif (M <= -7.5e-220)
    		tmp = Float64(1.0 * exp(Float64(-l)));
    	elseif (M <= 27.0)
    		tmp = t_0;
    	else
    		tmp = t_1;
    	end
    	return tmp
    end
    
    function tmp_2 = code(K, m, n, M, l)
    	t_0 = 1.0 * exp((-0.25 * (n * n)));
    	t_1 = cos(M) * exp((-1.0 * (M * M)));
    	tmp = 0.0;
    	if (M <= -0.4)
    		tmp = t_1;
    	elseif (M <= -4.1e-80)
    		tmp = t_0;
    	elseif (M <= -7.5e-220)
    		tmp = 1.0 * exp(-l);
    	elseif (M <= 27.0)
    		tmp = t_0;
    	else
    		tmp = t_1;
    	end
    	tmp_2 = tmp;
    end
    
    code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(1.0 * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, Block[{t$95$1 = N[(N[Cos[M], $MachinePrecision] * N[Exp[N[(-1.0 * N[(M * M), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[M, -0.4], t$95$1, If[LessEqual[M, -4.1e-80], t$95$0, If[LessEqual[M, -7.5e-220], N[(1.0 * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision], If[LessEqual[M, 27.0], t$95$0, t$95$1]]]]]]
    
    \begin{array}{l}
    
    \\
    \begin{array}{l}
    t_0 := 1 \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\
    t_1 := \cos M \cdot e^{-1 \cdot \left(M \cdot M\right)}\\
    \mathbf{if}\;M \leq -0.4:\\
    \;\;\;\;t\_1\\
    
    \mathbf{elif}\;M \leq -4.1 \cdot 10^{-80}:\\
    \;\;\;\;t\_0\\
    
    \mathbf{elif}\;M \leq -7.5 \cdot 10^{-220}:\\
    \;\;\;\;1 \cdot e^{-\ell}\\
    
    \mathbf{elif}\;M \leq 27:\\
    \;\;\;\;t\_0\\
    
    \mathbf{else}:\\
    \;\;\;\;t\_1\\
    
    
    \end{array}
    \end{array}
    
    Derivation
    1. Split input into 3 regimes
    2. if M < -0.40000000000000002 or 27 < M

      1. Initial program 79.4%

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

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

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

          \[\leadsto \cos M \cdot \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
        3. lower-cos.f64N/A

          \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
        4. lower-exp.f64N/A

          \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
        5. lower--.f64N/A

          \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
        6. fabs-subN/A

          \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
        7. lower-fabs.f64N/A

          \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
        8. lower--.f64N/A

          \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
        9. +-commutativeN/A

          \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)} \]
        10. lower-+.f64N/A

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

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

        \[\leadsto \cos M \cdot e^{-1 \cdot {M}^{2}} \]
      6. Step-by-step derivation
        1. lower-*.f64N/A

          \[\leadsto \cos M \cdot e^{-1 \cdot {M}^{2}} \]
        2. unpow2N/A

          \[\leadsto \cos M \cdot e^{-1 \cdot \left(M \cdot M\right)} \]
        3. lower-*.f6497.1

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

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

      if -0.40000000000000002 < M < -4.0999999999999999e-80 or -7.5000000000000002e-220 < M < 27

      1. Initial program 73.6%

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

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

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

          \[\leadsto \cos M \cdot \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
        3. lower-cos.f64N/A

          \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
        4. lower-exp.f64N/A

          \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
        5. lower--.f64N/A

          \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
        6. fabs-subN/A

          \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
        7. lower-fabs.f64N/A

          \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
        8. lower--.f64N/A

          \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
        9. +-commutativeN/A

          \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)} \]
        10. lower-+.f64N/A

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

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

        \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot {n}^{2}} \]
      6. Step-by-step derivation
        1. lower-*.f64N/A

          \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot {n}^{2}} \]
        2. unpow2N/A

          \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot \left(n \cdot n\right)} \]
        3. lower-*.f6458.1

          \[\leadsto \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)} \]
      7. Applied rewrites58.1%

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

        \[\leadsto 1 \cdot e^{\color{blue}{\frac{-1}{4} \cdot \left(n \cdot n\right)}} \]
      9. Step-by-step derivation
        1. Applied rewrites58.1%

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

        if -4.0999999999999999e-80 < M < -7.5000000000000002e-220

        1. Initial program 74.3%

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

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

            \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
          2. lower-neg.f6438.8

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

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

          \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\right)\right)} \cdot e^{-\ell} \]
        6. Step-by-step derivation
          1. cos-neg-revN/A

            \[\leadsto \cos M \cdot e^{-\ell} \]
          2. lift-cos.f6443.8

            \[\leadsto \cos M \cdot e^{-\ell} \]
        7. Applied rewrites43.8%

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

          \[\leadsto 1 \cdot e^{-\ell} \]
        9. Step-by-step derivation
          1. Applied rewrites43.8%

            \[\leadsto 1 \cdot e^{-\ell} \]
        10. Recombined 3 regimes into one program.
        11. Add Preprocessing

        Alternative 6: 63.2% accurate, 1.8× speedup?

        \[\begin{array}{l} \\ \begin{array}{l} t_0 := 1 \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\ \mathbf{if}\;n \leq -54:\\ \;\;\;\;t\_0\\ \mathbf{elif}\;n \leq 34000:\\ \;\;\;\;1 \cdot e^{-\ell}\\ \mathbf{else}:\\ \;\;\;\;t\_0\\ \end{array} \end{array} \]
        (FPCore (K m n M l)
         :precision binary64
         (let* ((t_0 (* 1.0 (exp (* -0.25 (* n n))))))
           (if (<= n -54.0) t_0 (if (<= n 34000.0) (* 1.0 (exp (- l))) t_0))))
        double code(double K, double m, double n, double M, double l) {
        	double t_0 = 1.0 * exp((-0.25 * (n * n)));
        	double tmp;
        	if (n <= -54.0) {
        		tmp = t_0;
        	} else if (n <= 34000.0) {
        		tmp = 1.0 * exp(-l);
        	} else {
        		tmp = t_0;
        	}
        	return tmp;
        }
        
        module fmin_fmax_functions
            implicit none
            private
            public fmax
            public fmin
        
            interface fmax
                module procedure fmax88
                module procedure fmax44
                module procedure fmax84
                module procedure fmax48
            end interface
            interface fmin
                module procedure fmin88
                module procedure fmin44
                module procedure fmin84
                module procedure fmin48
            end interface
        contains
            real(8) function fmax88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(4) function fmax44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, max(x, y), y /= y), x /= x)
            end function
            real(8) function fmax84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmax48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
            end function
            real(8) function fmin88(x, y) result (res)
                real(8), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(4) function fmin44(x, y) result (res)
                real(4), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(y, merge(x, min(x, y), y /= y), x /= x)
            end function
            real(8) function fmin84(x, y) result(res)
                real(8), intent (in) :: x
                real(4), intent (in) :: y
                res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
            end function
            real(8) function fmin48(x, y) result(res)
                real(4), intent (in) :: x
                real(8), intent (in) :: y
                res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
            end function
        end module
        
        real(8) function code(k, m, n, m_1, l)
        use fmin_fmax_functions
            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 = 1.0d0 * exp(((-0.25d0) * (n * n)))
            if (n <= (-54.0d0)) then
                tmp = t_0
            else if (n <= 34000.0d0) then
                tmp = 1.0d0 * exp(-l)
            else
                tmp = t_0
            end if
            code = tmp
        end function
        
        public static double code(double K, double m, double n, double M, double l) {
        	double t_0 = 1.0 * Math.exp((-0.25 * (n * n)));
        	double tmp;
        	if (n <= -54.0) {
        		tmp = t_0;
        	} else if (n <= 34000.0) {
        		tmp = 1.0 * Math.exp(-l);
        	} else {
        		tmp = t_0;
        	}
        	return tmp;
        }
        
        def code(K, m, n, M, l):
        	t_0 = 1.0 * math.exp((-0.25 * (n * n)))
        	tmp = 0
        	if n <= -54.0:
        		tmp = t_0
        	elif n <= 34000.0:
        		tmp = 1.0 * math.exp(-l)
        	else:
        		tmp = t_0
        	return tmp
        
        function code(K, m, n, M, l)
        	t_0 = Float64(1.0 * exp(Float64(-0.25 * Float64(n * n))))
        	tmp = 0.0
        	if (n <= -54.0)
        		tmp = t_0;
        	elseif (n <= 34000.0)
        		tmp = Float64(1.0 * exp(Float64(-l)));
        	else
        		tmp = t_0;
        	end
        	return tmp
        end
        
        function tmp_2 = code(K, m, n, M, l)
        	t_0 = 1.0 * exp((-0.25 * (n * n)));
        	tmp = 0.0;
        	if (n <= -54.0)
        		tmp = t_0;
        	elseif (n <= 34000.0)
        		tmp = 1.0 * exp(-l);
        	else
        		tmp = t_0;
        	end
        	tmp_2 = tmp;
        end
        
        code[K_, m_, n_, M_, l_] := Block[{t$95$0 = N[(1.0 * N[Exp[N[(-0.25 * N[(n * n), $MachinePrecision]), $MachinePrecision]], $MachinePrecision]), $MachinePrecision]}, If[LessEqual[n, -54.0], t$95$0, If[LessEqual[n, 34000.0], N[(1.0 * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision], t$95$0]]]
        
        \begin{array}{l}
        
        \\
        \begin{array}{l}
        t_0 := 1 \cdot e^{-0.25 \cdot \left(n \cdot n\right)}\\
        \mathbf{if}\;n \leq -54:\\
        \;\;\;\;t\_0\\
        
        \mathbf{elif}\;n \leq 34000:\\
        \;\;\;\;1 \cdot e^{-\ell}\\
        
        \mathbf{else}:\\
        \;\;\;\;t\_0\\
        
        
        \end{array}
        \end{array}
        
        Derivation
        1. Split input into 2 regimes
        2. if n < -54 or 34000 < n

          1. Initial program 69.9%

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

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

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

              \[\leadsto \cos M \cdot \color{blue}{e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
            3. lower-cos.f64N/A

              \[\leadsto \cos M \cdot e^{\color{blue}{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)}} \]
            4. lower-exp.f64N/A

              \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
            5. lower--.f64N/A

              \[\leadsto \cos M \cdot e^{\left|m - n\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
            6. fabs-subN/A

              \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
            7. lower-fabs.f64N/A

              \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
            8. lower--.f64N/A

              \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left(\ell + {\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2}\right)} \]
            9. +-commutativeN/A

              \[\leadsto \cos M \cdot e^{\left|n - m\right| - \left({\left(\frac{1}{2} \cdot \left(m + n\right) - M\right)}^{2} + \ell\right)} \]
            10. lower-+.f64N/A

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

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

            \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot {n}^{2}} \]
          6. Step-by-step derivation
            1. lower-*.f64N/A

              \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot {n}^{2}} \]
            2. unpow2N/A

              \[\leadsto \cos M \cdot e^{\frac{-1}{4} \cdot \left(n \cdot n\right)} \]
            3. lower-*.f6497.4

              \[\leadsto \cos M \cdot e^{-0.25 \cdot \left(n \cdot n\right)} \]
          7. Applied rewrites97.4%

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

            \[\leadsto 1 \cdot e^{\color{blue}{\frac{-1}{4} \cdot \left(n \cdot n\right)}} \]
          9. Step-by-step derivation
            1. Applied rewrites97.4%

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

            if -54 < n < 34000

            1. Initial program 83.1%

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

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

                \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
              2. lower-neg.f6440.2

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

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

              \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\right)\right)} \cdot e^{-\ell} \]
            6. Step-by-step derivation
              1. cos-neg-revN/A

                \[\leadsto \cos M \cdot e^{-\ell} \]
              2. lift-cos.f6442.6

                \[\leadsto \cos M \cdot e^{-\ell} \]
            7. Applied rewrites42.6%

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

              \[\leadsto 1 \cdot e^{-\ell} \]
            9. Step-by-step derivation
              1. Applied rewrites41.8%

                \[\leadsto 1 \cdot e^{-\ell} \]
            10. Recombined 2 regimes into one program.
            11. Add Preprocessing

            Alternative 7: 34.9% accurate, 5.8× speedup?

            \[\begin{array}{l} \\ 1 \cdot e^{-\ell} \end{array} \]
            (FPCore (K m n M l) :precision binary64 (* 1.0 (exp (- l))))
            double code(double K, double m, double n, double M, double l) {
            	return 1.0 * exp(-l);
            }
            
            module fmin_fmax_functions
                implicit none
                private
                public fmax
                public fmin
            
                interface fmax
                    module procedure fmax88
                    module procedure fmax44
                    module procedure fmax84
                    module procedure fmax48
                end interface
                interface fmin
                    module procedure fmin88
                    module procedure fmin44
                    module procedure fmin84
                    module procedure fmin48
                end interface
            contains
                real(8) function fmax88(x, y) result (res)
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                end function
                real(4) function fmax44(x, y) result (res)
                    real(4), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(y, merge(x, max(x, y), y /= y), x /= x)
                end function
                real(8) function fmax84(x, y) result(res)
                    real(8), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(dble(y), merge(x, max(x, dble(y)), y /= y), x /= x)
                end function
                real(8) function fmax48(x, y) result(res)
                    real(4), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(dble(x), max(dble(x), y), y /= y), x /= x)
                end function
                real(8) function fmin88(x, y) result (res)
                    real(8), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                end function
                real(4) function fmin44(x, y) result (res)
                    real(4), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(y, merge(x, min(x, y), y /= y), x /= x)
                end function
                real(8) function fmin84(x, y) result(res)
                    real(8), intent (in) :: x
                    real(4), intent (in) :: y
                    res = merge(dble(y), merge(x, min(x, dble(y)), y /= y), x /= x)
                end function
                real(8) function fmin48(x, y) result(res)
                    real(4), intent (in) :: x
                    real(8), intent (in) :: y
                    res = merge(y, merge(dble(x), min(dble(x), y), y /= y), x /= x)
                end function
            end module
            
            real(8) function code(k, m, n, m_1, l)
            use fmin_fmax_functions
                real(8), intent (in) :: k
                real(8), intent (in) :: m
                real(8), intent (in) :: n
                real(8), intent (in) :: m_1
                real(8), intent (in) :: l
                code = 1.0d0 * exp(-l)
            end function
            
            public static double code(double K, double m, double n, double M, double l) {
            	return 1.0 * Math.exp(-l);
            }
            
            def code(K, m, n, M, l):
            	return 1.0 * math.exp(-l)
            
            function code(K, m, n, M, l)
            	return Float64(1.0 * exp(Float64(-l)))
            end
            
            function tmp = code(K, m, n, M, l)
            	tmp = 1.0 * exp(-l);
            end
            
            code[K_, m_, n_, M_, l_] := N[(1.0 * N[Exp[(-l)], $MachinePrecision]), $MachinePrecision]
            
            \begin{array}{l}
            
            \\
            1 \cdot e^{-\ell}
            \end{array}
            
            Derivation
            1. Initial program 76.5%

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

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

                \[\leadsto \cos \left(\frac{K \cdot \left(m + n\right)}{2} - M\right) \cdot e^{\mathsf{neg}\left(\ell\right)} \]
              2. lower-neg.f6430.6

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

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

              \[\leadsto \color{blue}{\cos \left(\mathsf{neg}\left(M\right)\right)} \cdot e^{-\ell} \]
            6. Step-by-step derivation
              1. cos-neg-revN/A

                \[\leadsto \cos M \cdot e^{-\ell} \]
              2. lift-cos.f6435.4

                \[\leadsto \cos M \cdot e^{-\ell} \]
            7. Applied rewrites35.4%

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

              \[\leadsto 1 \cdot e^{-\ell} \]
            9. Step-by-step derivation
              1. Applied rewrites34.9%

                \[\leadsto 1 \cdot e^{-\ell} \]
              2. Add Preprocessing

              Reproduce

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