home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _complex.c < prev    next >
C/C++ Source or Header  |  2002-02-02  |  5KB  |  170 lines

  1. /*
  2.  * Copyright (c) 1999
  3.  * Silicon Graphics Computer Systems, Inc.
  4.  *
  5.  * Copyright (c) 1999 
  6.  * Boris Fomitchev
  7.  *
  8.  * This material is provided "as is", with absolutely no warranty expressed
  9.  * or implied. Any use is at your own risk.
  10.  *
  11.  * Permission to use or copy this software for any purpose is hereby granted 
  12.  * without fee, provided the above notices are retained on all copies.
  13.  * Permission to modify the code and to distribute modified code is granted,
  14.  * provided the above notices are retained, and a notice that the code was
  15.  * modified is included with the above copyright notice.
  16.  *
  17.  */ 
  18. #ifndef _STLP_COMPLEX_C
  19. #define _STLP_COMPLEX_C
  20.  
  21. # ifndef _STLP_internal_complex_h
  22. #  include <stl/_complex.h>
  23. # endif
  24.  
  25. #include <istream>
  26.  
  27. #ifdef _STLP_USE_NEW_IOSTREAMS
  28. # include <sstream>
  29. #endif
  30.  
  31. _STLP_BEGIN_NAMESPACE
  32.  
  33. // Non-inline member functions.
  34.  
  35. template <class _Tp>
  36. void complex<_Tp>::_div(const _Tp& __z1_r, const _Tp& __z1_i,
  37.                         const _Tp& __z2_r, const _Tp& __z2_i,
  38.                         _Tp& __res_r, _Tp& __res_i) {
  39.   _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
  40.   _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
  41.  
  42.   if (__ar <= __ai) {
  43.     _Tp __ratio = __z2_r / __z2_i;
  44.     _Tp __denom = __z2_i * (1 + __ratio * __ratio);
  45.     __res_r = (__z1_r * __ratio + __z1_i) / __denom;
  46.     __res_i = (__z1_i * __ratio - __z1_r) / __denom;
  47.   }
  48.   else {
  49.     _Tp __ratio = __z2_i / __z2_r;
  50.     _Tp __denom = __z2_r * (1 + __ratio * __ratio);
  51.     __res_r = (__z1_r + __z1_i * __ratio) / __denom;
  52.     __res_i = (__z1_i - __z1_r * __ratio) / __denom;
  53.   }
  54. }
  55.  
  56. template <class _Tp>
  57. void complex<_Tp>::_div(const _Tp& __z1_r,
  58.                         const _Tp& __z2_r, const _Tp& __z2_i,
  59.                         _Tp& __res_r, _Tp& __res_i) {
  60.   _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
  61.   _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
  62.  
  63.   if (__ar <= __ai) {
  64.     _Tp __ratio = __z2_r / __z2_i;
  65.     _Tp __denom = __z2_i * (1 + __ratio * __ratio);
  66.     __res_r = (__z1_r * __ratio) / __denom;
  67.     __res_i = - __z1_r / __denom;
  68.   }
  69.   else {
  70.     _Tp __ratio = __z2_i / __z2_r;
  71.     _Tp __denom = __z2_r * (1 + __ratio * __ratio);
  72.     __res_r = __z1_r / __denom;
  73.     __res_i = - (__z1_r * __ratio) / __denom;
  74.   }
  75. }
  76.  
  77. // I/O.
  78.  
  79. #ifdef _STLP_USE_NEW_IOSTREAMS
  80.  
  81. // Complex output, in the form (re,im).  We use a two-step process 
  82. // involving stringstream so that we get the padding right.  
  83. template <class _Tp, class _CharT, class _Traits>
  84. basic_ostream<_CharT, _Traits>& _STLP_CALL
  85. operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __z)
  86. {
  87.   basic_ostringstream<_CharT, _Traits, allocator<_CharT> > __tmp;
  88.   __tmp.flags(__os.flags());
  89.   __tmp.imbue(__os.getloc());
  90.   __tmp.precision(__os.precision());
  91.   __tmp << '(' << __z.real() << ',' << __z.imag() << ')';
  92.   return __os << __tmp.str();
  93. }
  94.  
  95. // Complex input from arbitrary streams.  Note that results in some
  96. // locales may be confusing, since the decimal character varies with
  97. // locale and the separator between real and imaginary parts does not.
  98.  
  99. template <class _Tp, class _CharT, class _Traits>
  100. basic_istream<_CharT, _Traits>& _STLP_CALL
  101. operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __z)
  102. {
  103.   _Tp  __re = 0;
  104.   _Tp  __im = 0;
  105.  
  106.   // typedef ctype<_CharT> _Ctype;
  107.   //  locale __loc = __is.getloc();
  108.   //const _Ctype&  __c_type  = use_facet<_Ctype>(__loc);
  109.   const ctype<_CharT>& __c_type = *(const ctype<_CharT>*)__is._M_ctype_facet();
  110.  
  111.   char   __punct[4] = "(,)";
  112.   _CharT __wpunct[3];
  113.   __c_type.widen(__punct, __punct + 3, __wpunct);
  114.  
  115.   _CharT __c;
  116.  
  117.   __is >> __c;
  118.   if (_Traits::eq(__c, __wpunct[0])) {  // Left paren
  119.     __is >> __re >> __c;
  120.     if (_Traits::eq(__c, __wpunct[1]))  // Comma
  121.       __is >> __im >> __c;
  122.     if (!_Traits::eq(__c, __wpunct[2])) // Right paren
  123.       __is.setstate(ios_base::failbit);
  124.   }
  125.   else {
  126.     __is.putback(__c);
  127.     __is >> __re;
  128.   }
  129.  
  130.   if (__is)
  131.     __z = complex<_Tp>(__re, __im);
  132.   return __is;
  133. }
  134.  
  135.  
  136. #else /* _STLP_USE_NEW_IOSTREAMS */
  137.  
  138. template <class _Tp>
  139. ostream& _STLP_CALL operator<<(ostream& s, const complex<_Tp>& __z)
  140. {
  141.   return s << "( " << __z._M_re <<", " << __z._M_im <<")";
  142. }
  143.  
  144. template <class _Tp>
  145. istream& _STLP_CALL operator>>(istream& s, complex<_Tp>& a)
  146. {
  147.   _Tp re = 0, im = 0;
  148.   char     c = 0;
  149.  
  150.   s >> c;
  151.   if (c == '(') {
  152.     s >> re >> c;
  153.     if (c == ',') s >> im >> c;
  154.     if (c != ')') s.clear(ios::badbit);
  155.   }
  156.   else {
  157.     s.putback(c);
  158.     s >> re;
  159.   }
  160.  
  161.   if (s) a = complex<_Tp>(re, im);
  162.   return s;
  163. }
  164.  
  165. #endif /* _STLP_USE_NEW_IOSTREAMS */
  166.  
  167. _STLP_END_NAMESPACE
  168.  
  169. #endif /* _STLP_COMPLEX_C */
  170.