IT++ Logo

misc.h

Go to the documentation of this file.
00001 
00030 #ifndef MISC_H
00031 #define MISC_H
00032 
00033 #ifndef _MSC_VER
00034 #  include <itpp/config.h>
00035 #else
00036 #  include <itpp/config_msvc.h>
00037 #endif
00038 
00039 #include <complex>
00040 #include <string>
00041 #include <limits>
00042 
00043 
00044 namespace std {
00045 
00047 
00048 #ifndef HAVE_STD_ISINF
00049 #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF)
00050   inline int isinf(double x) { return ::isinf(x); }
00051 #elif defined(FPCLASS)
00052   inline int isinf(double x)
00053   {
00054     if (::fpclass(a) == FP_NINF) return -1;
00055     else if (::fpclass(a) == FP_PINF) return 1;
00056     else return 0;
00057   }
00058 #else
00059   inline int isinf(double x)
00060   {
00061     if ((x == x) && ((x - x) != 0.0)) return (x < 0.0 ? -1 : 1);
00062     else return 0;
00063   }
00064 #endif // #if (HAVE_DECL_ISINF == 1) || defined(HAVE_ISINF)
00065 #  define HAVE_STD_ISINF 1
00066 #endif // #ifndef HAVE_STD_ISINF
00067 
00068 #ifndef HAVE_STD_ISNAN
00069 #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN)
00070   inline int isnan(double x) { return ::isnan(x); }
00071 #else
00072   inline int isnan(double x) { return ((x != x) ? 1 : 0); }
00073 #endif // #if (HAVE_DECL_ISNAN == 1) || defined(HAVE_ISNAN)
00074 #  define HAVE_STD_ISNAN 1
00075 #endif // #ifndef HAVE_STD_ISNAN
00076 
00077 #ifndef HAVE_STD_ISFINITE
00078 #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE)
00079   inline int isfinite(double x) { return ::isfinite(x); }
00080 #elif defined(HAVE_FINITE)
00081   inline int isfinite(double x) { return ::finite(x); }
00082 #else
00083   inline int isfinite(double x)
00084   {
00085     return ((!std::isnan(x) && !std::isinf(x)) ? 1 : 0);
00086   }
00087 #endif // #if (HAVE_DECL_ISFINITE == 1) || defined(HAVE_ISFINITE)
00088 #  define HAVE_STD_ISFINITE 1
00089 #endif // #ifndef HAVE_STD_ISFINITE
00090 
00092 
00094   template <class T>
00095   std::ostream& operator<<(std::ostream &os, const std::complex<T> &x)
00096   {
00097     os << x.real();
00098     ios::fmtflags saved_format = os.setf(ios::showpos);
00099     os << x.imag();
00100     os.setf(saved_format, ios::showpos);
00101     return os << 'i';
00102   }
00103 
00105   template <class T>
00106   std::istream& operator>>(std::istream &is, std::complex<T> &x)
00107   {
00108     T re, im;
00109     char c;
00110     is >> c;
00111     if (c == '(') {
00112       is >> re >> c;
00113       if (c == ',') {
00114         is >> im >> c;
00115         if (c == ')') {
00116           x = complex<T>(re, im);
00117         } else {
00118           is.setstate(ios_base::failbit);
00119         }
00120       } else if (c == ')') {
00121         x = complex<T>(re, T(0));
00122       } else {
00123         is.setstate(ios_base::failbit);
00124       }
00125     } else {
00126       is.putback(c);
00127       is >> re;
00128       if (!is.eof() && ((c = is.peek()) == '+' || c == '-')) {
00129         is >> im >> c;
00130         if (c == 'i') {
00131           x = complex<T>(re, im);
00132         } else {
00133           is.setstate(ios_base::failbit);
00134         }
00135       } else {
00136         x = complex<T>(re, T(0));
00137       }
00138     }
00139     return is;
00140   }
00141 
00142 } // namespace std
00143 
00144 
00146 namespace itpp {
00147 
00149   const double pi = 3.14159265358979323846;
00150 
00152   const double m_2pi = 2 * pi;
00153 
00155   const double eps = std::numeric_limits<double>::epsilon();
00156 
00159 
00161   inline bool is_int(double x)
00162   {
00163     double dummy;
00164     return (modf(x, &dummy) == 0.0);
00165   }
00166 
00168   inline bool is_even(int x) { return ((x&1) == 0); }
00169 
00170 
00172   std::string itpp_version();
00173 
00174 
00176   bool check_big_endianness();
00177 
00179 
00180 } //namespace itpp
00181 
00182 
00183 #endif // #ifndef MISC_H
SourceForge Logo

Generated on Sat Apr 19 11:01:24 2008 for IT++ by Doxygen 1.5.5