3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
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.
18 #ifndef _STLP_COMPLEX_C
19 #define _STLP_COMPLEX_C
21 # ifndef _STLP_internal_complex_h
22 # include <stl/_complex.h>
27 #ifdef _STLP_USE_NEW_IOSTREAMS
33 // Non-inline member functions.
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;
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;
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;
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;
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;
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;
79 #ifdef _STLP_USE_NEW_IOSTREAMS
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)
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();
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.
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)
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();
111 char __punct[4] = "(,)";
113 __c_type.widen(__punct, __punct + 3, __wpunct);
118 if (_Traits::eq(__c, __wpunct[0])) { // Left paren
120 if (_Traits::eq(__c, __wpunct[1])) // Comma
122 if (!_Traits::eq(__c, __wpunct[2])) // Right paren
123 __is.setstate(ios_base::failbit);
131 __z = complex<_Tp>(__re, __im);
136 #else /* _STLP_USE_NEW_IOSTREAMS */
139 ostream& _STLP_CALL operator<<(ostream& s, const complex<_Tp>& __z)
141 return s << "( " << __z._M_re <<", " << __z._M_im <<")";
145 istream& _STLP_CALL operator>>(istream& s, complex<_Tp>& a)
153 if (c == ',') s >> im >> c;
154 if (c != ')') s.clear(ios::badbit);
161 if (s) a = complex<_Tp>(re, im);
165 #endif /* _STLP_USE_NEW_IOSTREAMS */
169 #endif /* _STLP_COMPLEX_C */