STK++ 1.0
STK_Funct_expm1.cpp
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004  Serge Iovleff
00003 
00004     This program is free software; you can redistribute it and/or modify
00005     it under the terms of the GNU Lesser General Public License as
00006     published by the Free Software Foundation; either version 2 of the
00007     License, or (at your option) any later version.
00008 
00009     This program is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this program; if not, write to the
00016     Free Software Foundation, Inc.,
00017     59 Temple Place,
00018     Suite 330,
00019     Boston, MA 02111-1307
00020     USA
00021 
00022     Contact : Serge.Iovleff@stkpp.org
00023 */
00024 
00025 /*
00026  * Project:  Analysis
00027  * Purpose:  implementation of exp(x)-1
00028  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00029  **/
00030 
00031 
00032 #include <cmath>
00033 // for Integer and Real
00034 #include "../../STKernel/include/STK_Integer.h"
00035 #include "../../STKernel/include/STK_Real.h"
00036 #include "../../STKernel/include/STK_Misc.h"
00037 
00038 #include "../include/STK_Funct_util.h"
00039 // for _LN2_
00040 #include "../include/STK_Const_Math.h"
00041 
00042 namespace STK
00043 {
00044 namespace Funct
00045 {
00054 Real expm1(Real const& x)
00055 {
00056   // small values of x
00057   if (abs(x) < Const::_LN2_)
00058   {
00059     // a + 1 != 1 -> use compute Taylor serie else use first order
00060     // Taylor expansion
00061     if (abs(x) > Arithmetic<Real>::epsilon())
00062     {
00063       // Compute the taylor series S = x + (1/2!) x^2 + (1/3!) x^3 + ...
00064       Real term = x, sum =x, n =1.;
00065       do
00066       {
00067         sum += (term *= x/(++n));
00068       }
00069       while (abs(term) > abs(sum) * Arithmetic<Real>::epsilon()) ;
00070       
00071       return sum ;
00072     }
00073     else return (x / 2. + 1.) * x;
00074   }
00075   else return exp(double(x))-1.;
00076 }
00077 
00078 } // namespace Funct
00079 
00080 } // namespace STK