| Prev | Next |  |  |  |  |  | exp_eps.hpp | Headings

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