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
22#  include <stl/_complex.h>
23#endif
24
25#if !defined (_STLP_USE_NO_IOSTREAMS)
26#  ifndef _STLP_INTERNAL_ISTREAM
27#    include <stl/_istream.h>
28#  endif
29
30#  ifndef _STLP_INTERNAL_SSTREAM
31#    include <stl/_sstream.h>
32#  endif
33
34#  ifndef _STLP_STRING_IO_H
35#    include <stl/_string_io.h>
36#  endif
37#endif
38
39_STLP_BEGIN_NAMESPACE
40
41// Non-inline member functions.
42
43template <class _Tp>
44void 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;
49
50  if (__ar <= __ai) {
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;
55  }
56  else {
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;
61  }
62}
63
64template <class _Tp>
65void 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;
70
71  if (__ar <= __ai) {
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;
76  }
77  else {
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;
82  }
83}
84
85// I/O.
86#if !defined (_STLP_USE_NO_IOSTREAMS)
87
88// Complex output, in the form (re,im).  We use a two-step process
89// involving stringstream so that we get the padding right.
90template <class _Tp, class _CharT, class _Traits>
91basic_ostream<_CharT, _Traits>& _STLP_CALL
92operator<<(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();
99}
100
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.
104
105template <class _Tp, class _CharT, class _Traits>
106basic_istream<_CharT, _Traits>& _STLP_CALL
107operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __z) {
108  _Tp  __re = 0;
109  _Tp  __im = 0;
110
111  const ctype<_CharT>& __c_type = *__is._M_ctype_facet();
112
113  const char __punct[4] = "(,)";
114  _CharT __wpunct[3];
115  __c_type.widen(__punct, __punct + 3, __wpunct);
116
117  _CharT __c;
118
119  __is >> __c;
120  if (_Traits::eq(__c, __wpunct[0])) {  // Left paren
121    __is >> __re >> __c;
122    if (_Traits::eq(__c, __wpunct[1]))  // Comma
123      __is >> __im >> __c;
124    if (!_Traits::eq(__c, __wpunct[2])) // Right paren
125      __is.setstate(ios_base::failbit);
126  }
127  else {
128    __is.putback(__c);
129    __is >> __re;
130  }
131
132  if (__is)
133    __z = complex<_Tp>(__re, __im);
134  return __is;
135}
136
137#endif /* _STLP_USE_NO_IOSTREAMS */
138
139_STLP_END_NAMESPACE
140
141#endif /* _STLP_COMPLEX_C */
142
143// Local Variables:
144// mode:C++
145// End:
146