/home/ntakagi/work/STLport-5.1.5/stlport/stl/_complex.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 #ifndef _STLP_COMPLEX_C
00019 #define _STLP_COMPLEX_C
00020 
00021 #ifndef _STLP_INTERNAL_COMPLEX
00022 #  include <stl/_complex.h>
00023 #endif
00024 
00025 #if !defined (_STLP_USE_NO_IOSTREAMS)
00026 #  ifndef _STLP_INTERNAL_ISTREAM
00027 #    include <stl/_istream.h>
00028 #  endif
00029 
00030 #  ifndef _STLP_INTERNAL_SSTREAM
00031 #    include <stl/_sstream.h>
00032 #  endif
00033 
00034 #  ifndef _STLP_STRING_IO_H
00035 #    include <stl/_string_io.h>
00036 #  endif
00037 #endif
00038 
00039 _STLP_BEGIN_NAMESPACE
00040 
00041 // Non-inline member functions.
00042 
00043 template <class _Tp>
00044 void complex<_Tp>::_div(const _Tp& __z1_r, const _Tp& __z1_i,
00045                         const _Tp& __z2_r, const _Tp& __z2_i,
00046                         _Tp& __res_r, _Tp& __res_i) {
00047   _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
00048   _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
00049 
00050   if (__ar <= __ai) {
00051     _Tp __ratio = __z2_r / __z2_i;
00052     _Tp __denom = __z2_i * (1 + __ratio * __ratio);
00053     __res_r = (__z1_r * __ratio + __z1_i) / __denom;
00054     __res_i = (__z1_i * __ratio - __z1_r) / __denom;
00055   }
00056   else {
00057     _Tp __ratio = __z2_i / __z2_r;
00058     _Tp __denom = __z2_r * (1 + __ratio * __ratio);
00059     __res_r = (__z1_r + __z1_i * __ratio) / __denom;
00060     __res_i = (__z1_i - __z1_r * __ratio) / __denom;
00061   }
00062 }
00063 
00064 template <class _Tp>
00065 void complex<_Tp>::_div(const _Tp& __z1_r,
00066                         const _Tp& __z2_r, const _Tp& __z2_i,
00067                         _Tp& __res_r, _Tp& __res_i) {
00068   _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
00069   _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
00070 
00071   if (__ar <= __ai) {
00072     _Tp __ratio = __z2_r / __z2_i;
00073     _Tp __denom = __z2_i * (1 + __ratio * __ratio);
00074     __res_r = (__z1_r * __ratio) / __denom;
00075     __res_i = - __z1_r / __denom;
00076   }
00077   else {
00078     _Tp __ratio = __z2_i / __z2_r;
00079     _Tp __denom = __z2_r * (1 + __ratio * __ratio);
00080     __res_r = __z1_r / __denom;
00081     __res_i = - (__z1_r * __ratio) / __denom;
00082   }
00083 }
00084 
00085 // I/O.
00086 #if !defined (_STLP_USE_NO_IOSTREAMS)
00087 
00088 // Complex output, in the form (re,im).  We use a two-step process
00089 // involving stringstream so that we get the padding right.
00090 template <class _Tp, class _CharT, class _Traits>
00091 basic_ostream<_CharT, _Traits>& _STLP_CALL
00092 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __z) {
00093   basic_ostringstream<_CharT, _Traits, allocator<_CharT> > __tmp;
00094   __tmp.flags(__os.flags());
00095   __tmp.imbue(__os.getloc());
00096   __tmp.precision(__os.precision());
00097   __tmp << '(' << __z.real() << ',' << __z.imag() << ')';
00098   return __os << __tmp.str();
00099 }
00100 
00101 // Complex input from arbitrary streams.  Note that results in some
00102 // locales may be confusing, since the decimal character varies with
00103 // locale and the separator between real and imaginary parts does not.
00104 
00105 template <class _Tp, class _CharT, class _Traits>
00106 basic_istream<_CharT, _Traits>& _STLP_CALL
00107 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __z) {
00108   _Tp  __re = 0;
00109   _Tp  __im = 0;
00110 
00111   // typedef ctype<_CharT> _Ctype;
00112   //  locale __loc = __is.getloc();
00113   //const _Ctype&  __c_type  = use_facet<_Ctype>(__loc);
00114   const ctype<_CharT>& __c_type = *__STATIC_CAST(const ctype<_CharT>*, __is._M_ctype_facet());
00115 
00116   const char __punct[4] = "(,)";
00117   _CharT __wpunct[3];
00118   __c_type.widen(__punct, __punct + 3, __wpunct);
00119 
00120   _CharT __c;
00121 
00122   __is >> __c;
00123   if (_Traits::eq(__c, __wpunct[0])) {  // Left paren
00124     __is >> __re >> __c;
00125     if (_Traits::eq(__c, __wpunct[1]))  // Comma
00126       __is >> __im >> __c;
00127     if (!_Traits::eq(__c, __wpunct[2])) // Right paren
00128       __is.setstate(ios_base::failbit);
00129   }
00130   else {
00131     __is.putback(__c);
00132     __is >> __re;
00133   }
00134 
00135   if (__is)
00136     __z = complex<_Tp>(__re, __im);
00137   return __is;
00138 }
00139 
00140 #endif /* _STLP_USE_NO_IOSTREAMS */
00141 
00142 _STLP_END_NAMESPACE
00143 
00144 #endif /* _STLP_COMPLEX_C */
00145 
00146 // Local Variables:
00147 // mode:C++
00148 // End:



Generated on Mon Mar 10 15:32:20 2008 by  doxygen 1.5.1