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
22 # include <stl/_complex.h>
25 #if !defined (_STLP_USE_NO_IOSTREAMS)
26 # ifndef _STLP_INTERNAL_ISTREAM
27 # include <stl/_istream.h>
30 # ifndef _STLP_INTERNAL_SSTREAM
31 # include <stl/_sstream.h>
34 # ifndef _STLP_STRING_IO_H
35 # include <stl/_string_io.h>
41 // Non-inline member functions.
44 void complex<_Tp>::_div(const _Tp& __z1_r, const _Tp& __z1_i,
45 const _Tp& __z2_r, const _Tp& __z2_i,
46 _Tp& __res_r, _Tp& __res_i) {
47 _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
48 _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
51 _Tp __ratio = __z2_r / __z2_i;
52 _Tp __denom = __z2_i * (1 + __ratio * __ratio);
53 __res_r = (__z1_r * __ratio + __z1_i) / __denom;
54 __res_i = (__z1_i * __ratio - __z1_r) / __denom;
57 _Tp __ratio = __z2_i / __z2_r;
58 _Tp __denom = __z2_r * (1 + __ratio * __ratio);
59 __res_r = (__z1_r + __z1_i * __ratio) / __denom;
60 __res_i = (__z1_i - __z1_r * __ratio) / __denom;
65 void complex<_Tp>::_div(const _Tp& __z1_r,
66 const _Tp& __z2_r, const _Tp& __z2_i,
67 _Tp& __res_r, _Tp& __res_i) {
68 _Tp __ar = __z2_r >= 0 ? __z2_r : -__z2_r;
69 _Tp __ai = __z2_i >= 0 ? __z2_i : -__z2_i;
72 _Tp __ratio = __z2_r / __z2_i;
73 _Tp __denom = __z2_i * (1 + __ratio * __ratio);
74 __res_r = (__z1_r * __ratio) / __denom;
75 __res_i = - __z1_r / __denom;
78 _Tp __ratio = __z2_i / __z2_r;
79 _Tp __denom = __z2_r * (1 + __ratio * __ratio);
80 __res_r = __z1_r / __denom;
81 __res_i = - (__z1_r * __ratio) / __denom;
86 #if !defined (_STLP_USE_NO_IOSTREAMS)
88 // Complex output, in the form (re,im). We use a two-step process
89 // involving stringstream so that we get the padding right.
90 template <class _Tp, class _CharT, class _Traits>
91 basic_ostream<_CharT, _Traits>& _STLP_CALL
92 operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __z) {
93 basic_ostringstream<_CharT, _Traits, allocator<_CharT> > __tmp;
94 __tmp.flags(__os.flags());
95 __tmp.imbue(__os.getloc());
96 __tmp.precision(__os.precision());
97 __tmp << '(' << __z.real() << ',' << __z.imag() << ')';
98 return __os << __tmp.str();
101 // Complex input from arbitrary streams. Note that results in some
102 // locales may be confusing, since the decimal character varies with
103 // locale and the separator between real and imaginary parts does not.
105 template <class _Tp, class _CharT, class _Traits>
106 basic_istream<_CharT, _Traits>& _STLP_CALL
107 operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __z) {
111 // typedef ctype<_CharT> _Ctype;
112 // locale __loc = __is.getloc();
113 //const _Ctype& __c_type = use_facet<_Ctype>(__loc);
114 const ctype<_CharT>& __c_type = *__STATIC_CAST(const ctype<_CharT>*, __is._M_ctype_facet());
116 const char __punct[4] = "(,)";
118 __c_type.widen(__punct, __punct + 3, __wpunct);
123 if (_Traits::eq(__c, __wpunct[0])) { // Left paren
125 if (_Traits::eq(__c, __wpunct[1])) // Comma
127 if (!_Traits::eq(__c, __wpunct[2])) // Right paren
128 __is.setstate(ios_base::failbit);
136 __z = complex<_Tp>(__re, __im);
140 #endif /* _STLP_USE_NO_IOSTREAMS */
144 #endif /* _STLP_COMPLEX_C */