5 #define INFINITY (__builtin_inff())
6 #define NAN (__builtin_nanf (""))
14 typedef _Tp value_type;
21 complex(const value_type &__re = value_type(), const value_type &__im = value_type())
22 : __re_(__re), __im_(__im) {}
24 complex(const complex<_Xp> &__c)
25 : __re_(__c.real()), __im_(__c.imag()) {}
27 value_type real() const { return __re_; }
28 value_type imag() const { return __im_; }
30 void real(value_type __re) { __re_ = __re; }
31 void imag(value_type __im) { __im_ = __im; }
33 complex &operator=(const value_type &__re) {
38 complex &operator+=(const value_type &__re) {
42 complex &operator-=(const value_type &__re) {
46 complex &operator*=(const value_type &__re) {
51 complex &operator/=(const value_type &__re) {
58 complex &operator=(const complex<_Xp> &__c) {
64 complex &operator+=(const complex<_Xp> &__c) {
70 complex &operator-=(const complex<_Xp> &__c) {
76 complex &operator*=(const complex<_Xp> &__c) {
77 *this = *this * complex(__c.real(), __c.imag());
81 complex &operator/=(const complex<_Xp> &__c) {
82 *this = *this / complex(__c.real(), __c.imag());
89 operator+(const complex<_Tp> &__x, const complex<_Tp> &__y) {
90 complex<_Tp> __t(__x);
97 operator+(const complex<_Tp> &__x, const _Tp &__y) {
98 complex<_Tp> __t(__x);
105 operator+(const _Tp &__x, const complex<_Tp> &__y) {
106 complex<_Tp> __t(__y);
113 operator-(const complex<_Tp> &__x, const complex<_Tp> &__y) {
114 complex<_Tp> __t(__x);
121 operator-(const complex<_Tp> &__x, const _Tp &__y) {
122 complex<_Tp> __t(__x);
129 operator-(const _Tp &__x, const complex<_Tp> &__y) {
130 complex<_Tp> __t(-__y);
137 operator*(const complex<_Tp> &__z, const complex<_Tp> &__w) {
138 _Tp __a = __z.real();
139 _Tp __b = __z.imag();
140 _Tp __c = __w.real();
141 _Tp __d = __w.imag();
142 _Tp __ac = __a * __c;
143 _Tp __bd = __b * __d;
144 _Tp __ad = __a * __d;
145 _Tp __bc = __b * __c;
146 _Tp __x = __ac - __bd;
147 _Tp __y = __ad + __bc;
148 if (std::isnan(__x) && std::isnan(__y)) {
149 bool __recalc = false;
150 if (std::isinf(__a) || std::isinf(__b)) {
151 __a = copysign(std::isinf(__a) ? _Tp(1) : _Tp(0), __a);
152 __b = copysign(std::isinf(__b) ? _Tp(1) : _Tp(0), __b);
154 __c = copysign(_Tp(0), __c);
156 __d = copysign(_Tp(0), __d);
159 if (std::isinf(__c) || std::isinf(__d)) {
160 __c = copysign(std::isinf(__c) ? _Tp(1) : _Tp(0), __c);
161 __d = copysign(std::isinf(__d) ? _Tp(1) : _Tp(0), __d);
163 __a = copysign(_Tp(0), __a);
165 __b = copysign(_Tp(0), __b);
168 if (!__recalc && (std::isinf(__ac) || std::isinf(__bd) ||
169 std::isinf(__ad) || std::isinf(__bc))) {
171 __a = copysign(_Tp(0), __a);
173 __b = copysign(_Tp(0), __b);
175 __c = copysign(_Tp(0), __c);
177 __d = copysign(_Tp(0), __d);
181 __x = _Tp(INFINITY) * (__a * __c - __b * __d);
182 __y = _Tp(INFINITY) * (__a * __d + __b * __c);
185 return complex<_Tp>(__x, __y);
190 operator*(const complex<_Tp> &__x, const _Tp &__y) {
191 complex<_Tp> __t(__x);
198 operator*(const _Tp &__x, const complex<_Tp> &__y) {
199 complex<_Tp> __t(__y);
206 operator/(const complex<_Tp> &__z, const complex<_Tp> &__w) {
208 _Tp __a = __z.real();
209 _Tp __b = __z.imag();
210 _Tp __c = __w.real();
211 _Tp __d = __w.imag();
212 _Tp __logbw = logb(fmax(fabs(__c), fabs(__d)));
213 if (std::isfinite(__logbw)) {
214 __ilogbw = static_cast<int>(__logbw);
215 __c = scalbn(__c, -__ilogbw);
216 __d = scalbn(__d, -__ilogbw);
218 _Tp __denom = __c * __c + __d * __d;
219 _Tp __x = scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
220 _Tp __y = scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
221 if (std::isnan(__x) && std::isnan(__y)) {
222 if ((__denom == _Tp(0)) && (!std::isnan(__a) || !std::isnan(__b))) {
223 __x = copysign(_Tp(INFINITY), __c) * __a;
224 __y = copysign(_Tp(INFINITY), __c) * __b;
225 } else if ((std::isinf(__a) || std::isinf(__b)) && std::isfinite(__c) && std::isfinite(__d)) {
226 __a = copysign(std::isinf(__a) ? _Tp(1) : _Tp(0), __a);
227 __b = copysign(std::isinf(__b) ? _Tp(1) : _Tp(0), __b);
228 __x = _Tp(INFINITY) * (__a * __c + __b * __d);
229 __y = _Tp(INFINITY) * (__b * __c - __a * __d);
230 } else if (std::isinf(__logbw) && __logbw > _Tp(0) && std::isfinite(__a) && std::isfinite(__b)) {
231 __c = copysign(std::isinf(__c) ? _Tp(1) : _Tp(0), __c);
232 __d = copysign(std::isinf(__d) ? _Tp(1) : _Tp(0), __d);
233 __x = _Tp(0) * (__a * __c + __b * __d);
234 __y = _Tp(0) * (__b * __c - __a * __d);
237 return complex<_Tp>(__x, __y);
242 operator/(const complex<_Tp> &__x, const _Tp &__y) {
243 return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
248 operator/(const _Tp &__x, const complex<_Tp> &__y) {
249 complex<_Tp> __t(__x);
256 operator+(const complex<_Tp> &__x) {
262 operator-(const complex<_Tp> &__x) {
263 return complex<_Tp>(-__x.real(), -__x.imag());
268 operator==(const complex<_Tp> &__x, const complex<_Tp> &__y) {
269 return __x.real() == __y.real() && __x.imag() == __y.imag();
274 operator==(const complex<_Tp> &__x, const _Tp &__y) {
275 return __x.real() == __y && __x.imag() == 0;
280 operator==(const _Tp &__x, const complex<_Tp> &__y) {
281 return __x == __y.real() && 0 == __y.imag();
286 operator!=(const complex<_Tp> &__x, const complex<_Tp> &__y) {
287 return !(__x == __y);
292 operator!=(const complex<_Tp> &__x, const _Tp &__y) {
293 return !(__x == __y);
298 operator!=(const _Tp &__x, const complex<_Tp> &__y) {
299 return !(__x == __y);
302 template <class _Tp> _Tp abs(const std::complex<_Tp> &__c);
306 template <class _Tp> _Tp arg(const std::complex<_Tp> &__c);
310 template <class _Tp> _Tp norm(const std::complex<_Tp> &__c);
314 template <class _Tp> std::complex<_Tp> conj(const std::complex<_Tp> &__c);
318 template <class _Tp> std::complex<_Tp> proj(const std::complex<_Tp> &__c);
323 complex<_Tp> polar(const _Tp &__rho, const _Tp &__theta = _Tp());
327 template <class _Tp> std::complex<_Tp> log(const std::complex<_Tp> &__x);
331 template <class _Tp> std::complex<_Tp> log10(const std::complex<_Tp> &__x);
336 std::complex<_Tp> sqrt(const std::complex<_Tp> &__x);
341 std::complex<_Tp> exp(const std::complex<_Tp> &__x);
346 std::complex<_Tp> pow(const std::complex<_Tp> &__x,
347 const std::complex<_Tp> &__y);
349 // __sqr, computes pow(x, 2)
351 template <class _Tp> std::complex<_Tp> __sqr(const std::complex<_Tp> &__x);
356 std::complex<_Tp> asinh(const std::complex<_Tp> &__x);
361 std::complex<_Tp> acosh(const std::complex<_Tp> &__x);
366 std::complex<_Tp> atanh(const std::complex<_Tp> &__x);
371 std::complex<_Tp> sinh(const std::complex<_Tp> &__x);
376 std::complex<_Tp> cosh(const std::complex<_Tp> &__x);
381 std::complex<_Tp> tanh(const std::complex<_Tp> &__x);
386 std::complex<_Tp> asin(const std::complex<_Tp> &__x);
391 std::complex<_Tp> acos(const std::complex<_Tp> &__x);
396 std::complex<_Tp> atan(const std::complex<_Tp> &__x);
401 std::complex<_Tp> sin(const std::complex<_Tp> &__x);
405 template <class _Tp> std::complex<_Tp> cos(const std::complex<_Tp> &__x);
410 std::complex<_Tp> tan(const std::complex<_Tp> &__x);