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