Prev Next exp_eps.hpp Headings

@(@\newcommand{\W}[1]{ \; #1 \; } \newcommand{\R}[1]{ {\rm #1} } \newcommand{\B}[1]{ {\bf #1} } \newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} } \newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} } \newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} } \newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }@)@
exp_eps: Implementation
template <class Type>
Type exp_eps(const Type &x, const Type &epsilon)
{     // abs_x = |x|
     Type abs_x = x;
     if( Type(0) > x )
          abs_x = - x;
     // initialize
     int  k    = 0;          // initial order
     Type term = 1.;         // term = |x|^k / k !
     Type sum  = term;       // initial sum
     while(term > epsilon)
     {     k         = k + 1;          // order for next term
          Type temp = term * abs_x;   // term = |x|^k / (k-1)!
          term      = temp / Type(k); // term = |x|^k / k !
          sum       = sum + term;     // sum  = 1 + ... + |x|^k / k !
     }
     // In the case where x is negative, use exp(x) = 1 / exp(-|x|)
     if( Type(0) > x )
          sum = Type(1) / sum;
     return sum;
}

Input File: introduction/exp_eps.omh