Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1092 lines
27 KiB

  1. // complex standard header
  2. #pragma once
  3. #ifndef _COMPLEX_
  4. #define _COMPLEX_
  5. #include <ymath.h>
  6. #include <cmath>
  7. #include <sstream>
  8. #pragma pack(push,8)
  9. #pragma warning(push,3)
  10. #pragma warning(disable: 4244)
  11. _STD_BEGIN
  12. #define __STD_COMPLEX /* signal presence of complex classes */
  13. // TEMPLATE CLASS _Ctraits
  14. template<class _Ty>
  15. class _Ctraits
  16. { // complex traits for _Ty
  17. public:
  18. static _Ty __cdecl _Cosh(_Ty _Left, _Ty _Right)
  19. { // return cosh(_Left) * _Right
  20. return (::_Cosh((double)_Left, (double)_Right));
  21. }
  22. static short __cdecl _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent)
  23. { // compute exp(*_Pleft) * _Right * 2 ^ _Exponent
  24. double _Tmp = (double)*_Pleft;
  25. short _Ans = ::_Exp(&_Tmp, (double)_Right, _Exponent);
  26. *_Pleft = (_Ty)_Tmp;
  27. return (_Ans);
  28. }
  29. static _Ty __cdecl _Infv(_Ty)
  30. { // return infinity
  31. return (::_Inf._Double);
  32. }
  33. static bool __cdecl _Isinf(_Ty _Left)
  34. { // test for infinity
  35. double _Tmp = (double)_Left;
  36. return (::_Dtest(&_Tmp) == _INFCODE);
  37. }
  38. static bool __cdecl _Isnan(_Ty _Left)
  39. { // test for NaN
  40. double _Tmp = (double)_Left;
  41. return (::_Dtest(&_Tmp) == _NANCODE);
  42. }
  43. static _Ty __cdecl _Nanv(_Ty)
  44. { // return NaN
  45. return (::_Nan._Double);
  46. }
  47. static _Ty __cdecl _Sinh(_Ty _Left, _Ty _Right)
  48. { // return sinh(_Left) * _Right
  49. return (::_Sinh((double)_Left, (double)_Right));
  50. }
  51. static _Ty __cdecl atan2(_Ty _Yval, _Ty _Xval)
  52. { // return atan(_Yval / _Xval)
  53. return (::atan2((double)_Yval, (double)_Xval));
  54. }
  55. static _Ty __cdecl cos(_Ty _Left)
  56. { // return cos(_Left)
  57. return (::cos((double)_Left));
  58. }
  59. static _Ty __cdecl exp(_Ty _Left)
  60. { // return exp(_Left)
  61. return (::exp((double)_Left));
  62. }
  63. static _Ty __cdecl ldexp(_Ty _Left, int _Exponent)
  64. { // return _Left * 2 ^ _Exponent
  65. return (::ldexp((double)_Left, _Exponent));
  66. }
  67. static _Ty __cdecl log(_Ty _Left)
  68. { // return log(_Left)
  69. return (::log((double)_Left));
  70. }
  71. static _Ty __cdecl pow(_Ty _Left, _Ty _Right)
  72. { // return _Left ^ _Right
  73. return (::pow((double)_Left, (double)_Right));
  74. }
  75. static _Ty __cdecl sin(_Ty _Left)
  76. { // return sin(_Left)
  77. return (::sin((double)_Left));
  78. }
  79. static _Ty __cdecl sqrt(_Ty _Left)
  80. { // return sqrt(_Left)
  81. return (::sqrt((double)_Left));
  82. }
  83. static _Ty __cdecl tan(_Ty _Left)
  84. { // return tan(_Left)
  85. return (::tan((double)_Left));
  86. }
  87. };
  88. // CLASS _Ctraits<long double>
  89. template<> class _CRTIMP2 _Ctraits<long double>
  90. { // complex traits for long double
  91. public:
  92. typedef long double _Ty;
  93. static _Ty __cdecl _Cosh(_Ty _Left, _Ty _Right)
  94. { // return cosh(_Left) * _Right
  95. return (::_LCosh(_Left, _Right));
  96. }
  97. static short __cdecl _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent)
  98. { // compute exp(*_Pleft) * _Right * 2 ^ _Exponent
  99. return (::_LExp(_Pleft, _Right, _Exponent));
  100. }
  101. static _Ty __cdecl _Infv(_Ty)
  102. { // return infinity
  103. return (::_LInf._Long_double);
  104. }
  105. static bool __cdecl _Isinf(_Ty _Left)
  106. { // test for infinity
  107. return (::_LDtest(&_Left) == _INFCODE);
  108. }
  109. static bool __cdecl _Isnan(_Ty _Left)
  110. { // test for NaN
  111. return (::_LDtest(&_Left) == _NANCODE);
  112. }
  113. static _Ty __cdecl _Nanv(_Ty)
  114. { // return NaN
  115. return (::_LNan._Long_double);
  116. }
  117. static _Ty __cdecl _Sinh(_Ty _Left, _Ty _Right)
  118. { // return sinh(_Left) * _Right
  119. return (::_LSinh(_Left, _Right));
  120. }
  121. static _Ty __cdecl atan2(_Ty _Yval, _Ty _Xval)
  122. { // return atan(_Yval / _Xval)
  123. return (::atan2l(_Yval, _Xval));
  124. }
  125. static _Ty __cdecl cos(_Ty _Left)
  126. { // return cos(_Left)
  127. return (::cosl(_Left));
  128. }
  129. static _Ty __cdecl exp(_Ty _Left)
  130. { // return exp(_Left)
  131. return (::expl(_Left));
  132. }
  133. static _Ty __cdecl ldexp(_Ty _Left, int _Exponent)
  134. { // return _Left * 2 ^ _Exponent
  135. return (::ldexpl(_Left, _Exponent));
  136. }
  137. static _Ty __cdecl log(_Ty _Left)
  138. { // return log(_Left)
  139. return (::logl(_Left));
  140. }
  141. static _Ty __cdecl pow(_Ty _Left, _Ty _Right)
  142. { // return _Left ^ _Right
  143. return (::powl(_Left, _Right));
  144. }
  145. static _Ty __cdecl sin(_Ty _Left)
  146. { // return sin(_Left)
  147. return (::sinl(_Left));
  148. }
  149. static _Ty __cdecl sqrt(_Ty _Left)
  150. { // return sqrt(_Left)
  151. return (::sqrtl(_Left));
  152. }
  153. static _Ty __cdecl tan(_Ty _Left)
  154. { // return tan(_Left)
  155. return (::tanl(_Left));
  156. }
  157. };
  158. // CLASS _Ctraits<double>
  159. template<> class _CRTIMP2 _Ctraits<double>
  160. { // complex traits for double
  161. public:
  162. typedef double _Ty;
  163. static _Ty __cdecl _Cosh(_Ty _Left, _Ty _Right)
  164. { // return cosh(_Left) * _Right
  165. return (::_Cosh(_Left, _Right));
  166. }
  167. static short __cdecl _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent)
  168. { // compute exp(*_Pleft) * _Right * 2 ^ _Exponent
  169. return (::_Exp(_Pleft, _Right, _Exponent));
  170. }
  171. static _Ty __cdecl _Infv(_Ty)
  172. { // return infinity
  173. return (::_Inf._Double);
  174. }
  175. static bool __cdecl _Isinf(_Ty _Left)
  176. { // test for infinity
  177. return (::_Dtest(&_Left) == _INFCODE);
  178. }
  179. static bool __cdecl _Isnan(_Ty _Left)
  180. { // test for NaN
  181. return (::_Dtest(&_Left) == _NANCODE);
  182. }
  183. static _Ty __cdecl _Nanv(_Ty)
  184. { // return NaN
  185. return (::_Nan._Double);
  186. }
  187. static _Ty __cdecl _Sinh(_Ty _Left, _Ty _Right)
  188. { // return sinh(_Left) * _Right
  189. return (::_Sinh(_Left, _Right));
  190. }
  191. static _Ty __cdecl atan2(_Ty _Yval, _Ty _Xval)
  192. { // return atan(_Yval / _Xval)
  193. return (::atan2(_Yval, _Xval));
  194. }
  195. static _Ty __cdecl cos(_Ty _Left)
  196. { // return cos(_Left)
  197. return (::cos(_Left));
  198. }
  199. static _Ty __cdecl exp(_Ty _Left)
  200. { // return exp(_Left)
  201. return (::exp(_Left));
  202. }
  203. static _Ty __cdecl ldexp(_Ty _Left, int _Exponent)
  204. { // return _Left * 2 ^ _Exponent
  205. return (::ldexp(_Left, _Exponent));
  206. }
  207. static _Ty __cdecl log(_Ty _Left)
  208. { // return log(_Left)
  209. return (::log(_Left));
  210. }
  211. static _Ty __cdecl pow(_Ty _Left, _Ty _Right)
  212. { // return _Left ^ _Right
  213. return (::pow(_Left, _Right));
  214. }
  215. static _Ty __cdecl sin(_Ty _Left)
  216. { // return sin(_Left)
  217. return (::sin(_Left));
  218. }
  219. static _Ty __cdecl sqrt(_Ty _Left)
  220. { // return sqrt(_Left)
  221. return (::sqrt(_Left));
  222. }
  223. static _Ty __cdecl tan(_Ty _Left)
  224. { // return tan(_Left)
  225. return (::tan(_Left));
  226. }
  227. };
  228. // CLASS _Ctraits<float>
  229. template<> class _CRTIMP2 _Ctraits<float>
  230. { // complex traits for float
  231. public:
  232. typedef float _Ty;
  233. static _Ty __cdecl _Cosh(_Ty _Left, _Ty _Right)
  234. { // return cosh(_Left) * _Right
  235. return (::_FCosh(_Left, _Right));
  236. }
  237. static short __cdecl _Exp(_Ty *_Pleft, _Ty _Right, short _Exponent)
  238. { // compute exp(*_Pleft) * _Right * 2 ^ _Exponent
  239. return (::_FExp(_Pleft, _Right, _Exponent));
  240. }
  241. static _Ty __cdecl _Infv(_Ty)
  242. { // return infinity
  243. return (::_FInf._Float);
  244. }
  245. static bool __cdecl _Isinf(_Ty _Left)
  246. { // test for infinity
  247. return (::_FDtest(&_Left) == _INFCODE);
  248. }
  249. static bool __cdecl _Isnan(_Ty _Left)
  250. { // test for NaN
  251. return (::_FDtest(&_Left) == _NANCODE);
  252. }
  253. static _Ty __cdecl _Nanv(_Ty)
  254. { // return NaN
  255. return (::_FNan._Float);
  256. }
  257. static _Ty __cdecl _Sinh(_Ty _Left, _Ty _Right)
  258. { // return sinh(_Left) * _Right
  259. return (::_FSinh(_Left, _Right));
  260. }
  261. static _Ty __cdecl atan2(_Ty _Yval, _Ty _Xval)
  262. { // return atan(_Yval / _Xval)
  263. return (::atan2f(_Yval, _Xval));
  264. }
  265. static _Ty __cdecl cos(_Ty _Left)
  266. { // return cos(_Left)
  267. return (::cosf(_Left));
  268. }
  269. static _Ty __cdecl exp(_Ty _Left)
  270. { // return exp(_Left)
  271. return (::expf(_Left));
  272. }
  273. static _Ty __cdecl ldexp(_Ty _Left, int _Exponent)
  274. { // return _Left * 2 ^ _Exponent
  275. return (::ldexpf(_Left, _Exponent));
  276. }
  277. static _Ty __cdecl log(_Ty _Left)
  278. { // return log(_Left)
  279. return (::logf(_Left));
  280. }
  281. static _Ty __cdecl pow(_Ty _Left, _Ty _Right)
  282. { // return _Left ^ _Right
  283. return (::powf(_Left, _Right));
  284. }
  285. static _Ty __cdecl sin(_Ty _Left)
  286. { // return sin(_Left)
  287. return (::sinf(_Left));
  288. }
  289. static _Ty __cdecl sqrt(_Ty _Left)
  290. { // return sqrt(_Left)
  291. return (::sqrtf(_Left));
  292. }
  293. static _Ty __cdecl tan(_Ty _Left)
  294. { // return tan(_Left)
  295. return (::tanf(_Left));
  296. }
  297. };
  298. template<class _Ty>
  299. class complex;
  300. template<> class _CRTIMP2 complex<float>;
  301. template<> class _CRTIMP2 complex<double>;
  302. template<> class _CRTIMP2 complex<long double>;
  303. // TEMPLATE CLASS _Complex_base
  304. template<class _Ty>
  305. class _Complex_base
  306. { // base for all complex types
  307. public:
  308. typedef _Ctraits<_Ty> _Myctraits;
  309. typedef _Complex_base<_Ty> _Myt;
  310. typedef _Ty value_type;
  311. _Complex_base(const _Ty& _Realval, const _Ty& _Imagval)
  312. : _Real(_Realval), _Imag(_Imagval)
  313. { // construct from components of same type
  314. }
  315. _Ty real(const _Ty& _Right)
  316. { // set real component
  317. return (_Real = _Right);
  318. }
  319. _Ty imag(const _Ty& _Right)
  320. { // set imaginary component
  321. return (_Imag = _Right);
  322. }
  323. _Ty real() const
  324. { // return real component
  325. return (_Real);
  326. }
  327. _Ty imag() const
  328. { // return imaginary component
  329. return (_Imag);
  330. }
  331. protected:
  332. template<class _Other> inline
  333. void _Add(const complex<_Other>& _Right)
  334. { // add other complex
  335. _Real = _Real + (_Ty)_Right.real();
  336. _Imag = _Imag + (_Ty)_Right.imag();
  337. }
  338. template<class _Other> inline
  339. void _Sub(const complex<_Other>& _Right)
  340. { // subtract other complex
  341. _Real = _Real - (_Ty)_Right.real();
  342. _Imag = _Imag - (_Ty)_Right.imag();
  343. }
  344. template<class _Other> inline
  345. void _Mul(const complex<_Other>& _Right)
  346. { // multiply by other complex
  347. _Ty _Rightreal = (_Ty)_Right.real();
  348. _Ty _Rightimag = (_Ty)_Right.imag();
  349. _Ty _Tmp = _Real * _Rightreal - _Imag * _Rightimag;
  350. _Imag = _Real * _Rightimag + _Imag * _Rightreal;
  351. _Real = _Tmp;
  352. }
  353. template<class _Other> inline
  354. void _Div(const complex<_Other>& _Right)
  355. { // divide by other complex
  356. typedef _Ctraits<_Ty> _Myctraits;
  357. _Ty _Rightreal = (_Ty)_Right.real();
  358. _Ty _Rightimag = (_Ty)_Right.imag();
  359. if (_Myctraits::_Isnan(_Rightreal) || _Myctraits::_Isnan(_Rightimag))
  360. { // set NaN result
  361. _Real = _Myctraits::_Nanv(_Rightreal);
  362. _Imag = _Real;
  363. }
  364. else if ((_Rightimag < 0 ? -_Rightimag : +_Rightimag)
  365. < (_Rightreal < 0 ? -_Rightreal : +_Rightreal))
  366. { // |_Right.imag()| < |_Right.real()|
  367. _Ty _Wr = _Rightimag / _Rightreal;
  368. _Ty _Wd = _Rightreal + _Wr * _Rightimag;
  369. if (_Myctraits::_Isnan(_Wd) || _Wd == 0)
  370. { // set NaN result
  371. _Real = _Myctraits::_Nanv(_Rightreal);
  372. _Imag = _Real;
  373. }
  374. else
  375. { // compute representable result
  376. _Ty _Tmp = (_Real + _Imag * _Wr) / _Wd;
  377. _Imag = (_Imag - _Real * _Wr) / _Wd;
  378. _Real = _Tmp;
  379. }
  380. }
  381. else if (_Rightimag == 0)
  382. { // set NaN result
  383. _Real = _Myctraits::_Nanv(_Rightreal);
  384. _Imag = _Real;
  385. }
  386. else
  387. { // 0 < |_Right.real()| <= |_Right.imag()|
  388. _Ty _Wr = _Rightreal / _Rightimag;
  389. _Ty _Wd = _Rightimag + _Wr * _Rightreal;
  390. if (_Myctraits::_Isnan(_Wd) || _Wd == 0)
  391. { // set NaN result
  392. _Real = _Myctraits::_Nanv(_Rightreal);
  393. _Imag = _Real;
  394. }
  395. else
  396. { // compute representable result
  397. _Ty _Tmp = (_Real * _Wr + _Imag) / _Wd;
  398. _Imag = (_Imag * _Wr - _Real) / _Wd;
  399. _Real = _Tmp;
  400. }
  401. }
  402. }
  403. _Ty _Real; // the real component
  404. _Ty _Imag; // the imaginary component
  405. };
  406. // CLASS complex<float>
  407. template<> class _CRTIMP2 complex<float>
  408. : public _Complex_base<float>
  409. { // complex with float components
  410. public:
  411. typedef float _Ty;
  412. typedef complex<_Ty> _Myt;
  413. explicit complex(const complex<double>&); // defined below
  414. explicit complex(const complex<long double>&); // defined below
  415. complex(const _Ty& _Realval = 0, const _Ty& _Imagval = 0)
  416. : _Complex_base<_Ty>(_Realval, _Imagval)
  417. { // construct from float components
  418. }
  419. complex<_Ty>& operator=(const _Ty& _Right)
  420. { // assign real
  421. _Real = _Right;
  422. _Imag = 0;
  423. return (*this);
  424. }
  425. _Myt& operator+=(const _Ty& _Right)
  426. { // add real
  427. _Real = _Real + _Right;
  428. return (*this);
  429. }
  430. _Myt& operator-=(const _Ty& _Right)
  431. { // subtract real
  432. _Real = _Real - _Right;
  433. return (*this);
  434. }
  435. _Myt& operator*=(const _Ty& _Right)
  436. { // multiply by real
  437. _Real = _Real * _Right;
  438. _Imag = _Imag * _Right;
  439. return (*this);
  440. }
  441. _Myt& operator/=(const _Ty& _Right)
  442. { // divide by real
  443. _Real = _Real / _Right;
  444. _Imag = _Imag / _Right;
  445. return (*this);
  446. }
  447. _Myt& operator+=(const _Myt& _Right)
  448. { // add other complex
  449. _Add(_Right);
  450. return (*this);
  451. }
  452. _Myt& operator-=(const _Myt& _Right)
  453. { // subtract other complex
  454. _Sub(_Right);
  455. return (*this);
  456. }
  457. _Myt& operator*=(const _Myt& _Right)
  458. { // multiply by other complex
  459. _Mul(_Right);
  460. return (*this);
  461. }
  462. _Myt& operator/=(const _Myt& _Right)
  463. { // divide by other complex
  464. _Div(_Right);
  465. return (*this);
  466. }
  467. template<class _Other> inline
  468. _Myt& operator+=(const complex<_Other>& _Right)
  469. { // add other complex
  470. _Add(_Right);
  471. return (*this);
  472. }
  473. template<class _Other> inline
  474. _Myt& operator-=(const complex<_Other>& _Right)
  475. { // subtract other complex
  476. _Sub(_Right);
  477. return (*this);
  478. }
  479. template<class _Other> inline
  480. _Myt& operator*=(const complex<_Other>& _Right)
  481. { // multiply by other complex
  482. _Mul(_Right);
  483. return (*this);
  484. }
  485. template<class _Other> inline
  486. _Myt& operator/=(const complex<_Other>& _Right)
  487. { // divide by other complex
  488. _Div(_Right);
  489. return (*this);
  490. }
  491. };
  492. // CLASS complex<double>
  493. template<> class _CRTIMP2 complex<double>
  494. : public _Complex_base<double>
  495. { // complex with double components
  496. public:
  497. typedef double _Ty;
  498. typedef complex<_Ty> _Myt;
  499. complex(const complex<float>&); // defined below
  500. explicit complex(const complex<long double>&); // defined below
  501. complex(const _Ty& _Realval = 0, const _Ty& _Imagval = 0)
  502. : _Complex_base<_Ty>(_Realval, _Imagval)
  503. { // construct from double components
  504. }
  505. complex<_Ty>& operator=(const _Ty& _Right)
  506. { // assign real
  507. _Real = _Right;
  508. _Imag = 0;
  509. return (*this);
  510. }
  511. _Myt& operator+=(const _Ty& _Right)
  512. { // add real
  513. _Real = _Real + _Right;
  514. return (*this);
  515. }
  516. _Myt& operator-=(const _Ty& _Right)
  517. { // subtract real
  518. _Real = _Real - _Right;
  519. return (*this);
  520. }
  521. _Myt& operator*=(const _Ty& _Right)
  522. { // multiply by real
  523. _Real = _Real * _Right;
  524. _Imag = _Imag * _Right;
  525. return (*this);
  526. }
  527. _Myt& operator/=(const _Ty& _Right)
  528. { // divide by real
  529. _Real = _Real / _Right;
  530. _Imag = _Imag / _Right;
  531. return (*this);
  532. }
  533. _Myt& operator+=(const _Myt& _Right)
  534. { // add other complex
  535. _Add(_Right);
  536. return (*this);
  537. }
  538. _Myt& operator-=(const _Myt& _Right)
  539. { // subtract other complex
  540. _Sub(_Right);
  541. return (*this);
  542. }
  543. _Myt& operator*=(const _Myt& _Right)
  544. { // multiply by other complex
  545. _Mul(_Right);
  546. return (*this);
  547. }
  548. _Myt& operator/=(const _Myt& _Right)
  549. { // divide by other complex
  550. _Div(_Right);
  551. return (*this);
  552. }
  553. template<class _Other> inline
  554. _Myt& operator+=(const complex<_Other>& _Right)
  555. { // add other complex
  556. _Add(_Right);
  557. return (*this);
  558. }
  559. template<class _Other> inline
  560. _Myt& operator-=(const complex<_Other>& _Right)
  561. { // subtract other complex
  562. _Sub(_Right);
  563. return (*this);
  564. }
  565. template<class _Other> inline
  566. _Myt& operator*=(const complex<_Other>& _Right)
  567. { // multiply by other complex
  568. _Mul(_Right);
  569. return (*this);
  570. }
  571. template<class _Other> inline
  572. _Myt& operator/=(const complex<_Other>& _Right)
  573. { // divide by other complex
  574. _Div(_Right);
  575. return (*this);
  576. }
  577. };
  578. // CLASS complex<long double>
  579. template<> class _CRTIMP2 complex<long double>
  580. : public _Complex_base<long double>
  581. { // complex with long double components
  582. public:
  583. typedef long double _Ty;
  584. typedef complex<_Ty> _Myt;
  585. complex(const complex<float>&); // defined below
  586. complex(const complex<double>&); // defined below
  587. complex(const _Ty& _Realval = 0, const _Ty& _Imagval = 0)
  588. : _Complex_base<_Ty>(_Realval, _Imagval)
  589. { // construct from long double components
  590. }
  591. complex<_Ty>& operator=(const _Ty& _Right)
  592. { // assign real
  593. _Real = _Right;
  594. _Imag = 0;
  595. return (*this);
  596. }
  597. _Myt& operator+=(const _Ty& _Right)
  598. { // add real
  599. _Real = _Real + _Right;
  600. return (*this);
  601. }
  602. _Myt& operator-=(const _Ty& _Right)
  603. { // subtract real
  604. _Real = _Real - _Right;
  605. return (*this);
  606. }
  607. _Myt& operator*=(const _Ty& _Right)
  608. { // multiply by real
  609. _Real = _Real * _Right;
  610. _Imag = _Imag * _Right;
  611. return (*this);
  612. }
  613. _Myt& operator/=(const _Ty& _Right)
  614. { // divide by real
  615. _Real = _Real / _Right;
  616. _Imag = _Imag / _Right;
  617. return (*this);
  618. }
  619. _Myt& operator+=(const _Myt& _Right)
  620. { // add other complex
  621. _Add(_Right);
  622. return (*this);
  623. }
  624. _Myt& operator-=(const _Myt& _Right)
  625. { // subtract other complex
  626. _Sub(_Right);
  627. return (*this);
  628. }
  629. _Myt& operator*=(const _Myt& _Right)
  630. { // multiply by other complex
  631. _Mul(_Right);
  632. return (*this);
  633. }
  634. _Myt& operator/=(const _Myt& _Right)
  635. { // divide by other complex
  636. _Div(_Right);
  637. return (*this);
  638. }
  639. template<class _Other> inline
  640. _Myt& operator+=(const complex<_Other>& _Right)
  641. { // add other complex
  642. _Add(_Right);
  643. return (*this);
  644. }
  645. template<class _Other> inline
  646. _Myt& operator-=(const complex<_Other>& _Right)
  647. { // subtract other complex
  648. _Sub(_Right);
  649. return (*this);
  650. }
  651. template<class _Other> inline
  652. _Myt& operator*=(const complex<_Other>& _Right)
  653. { // multiply by other complex
  654. _Mul(_Right);
  655. return (*this);
  656. }
  657. template<class _Other> inline
  658. _Myt& operator/=(const complex<_Other>& _Right)
  659. { // divide by other complex
  660. _Div(_Right);
  661. return (*this);
  662. }
  663. };
  664. // CONSTRUCTORS FOR complex SPECIALIZATIONS
  665. _TEMPLATE_STAT inline
  666. complex<float>::complex(const complex<double>& _Right)
  667. : _Complex_base<float>((_Ty)_Right.real(), (_Ty)_Right.imag())
  668. { // construct complex<float> from complex<double>
  669. }
  670. _TEMPLATE_STAT inline
  671. complex<float>::complex(const complex<long double>& _Right)
  672. : _Complex_base<float>((_Ty)_Right.real(), (_Ty)_Right.imag())
  673. { // construct complex<float> from complex<long double>
  674. }
  675. _TEMPLATE_STAT inline
  676. complex<double>::complex(const complex<float>& _Right)
  677. : _Complex_base<double>((_Ty)_Right.real(), (_Ty)_Right.imag())
  678. { // construct complex<double> from complex<float>
  679. }
  680. _TEMPLATE_STAT inline
  681. complex<double>::complex(const complex<long double>& _Right)
  682. : _Complex_base<double>((_Ty)_Right.real(), (_Ty)_Right.imag())
  683. { // construct complex<double> from complex<long double>
  684. }
  685. _TEMPLATE_STAT inline
  686. complex<long double>::complex(const complex<float>& _Right)
  687. : _Complex_base<long double>((_Ty)_Right.real(), (_Ty)_Right.imag())
  688. { // construct complex<long double> from complex<float>
  689. }
  690. _TEMPLATE_STAT inline
  691. complex<long double>::complex(const complex<double>& _Right)
  692. : _Complex_base<long double>((_Ty)_Right.real(), (_Ty)_Right.imag())
  693. { // construct complex<long double> from complex<double>
  694. }
  695. // TEMPLATE CLASS complex
  696. template<class _Ty>
  697. class complex
  698. : public _Complex_base<_Ty>
  699. { // complex with _Ty components
  700. public:
  701. typedef complex<_Ty> _Myt;
  702. typedef _Complex_base<_Ty> _Mybase;
  703. complex(const _Ty& _Realval = 0, const _Ty& _Imagval = 0)
  704. : _Mybase(_Realval, _Imagval)
  705. { // construct from components of same type
  706. }
  707. _Myt& operator=(const _Ty& _Right)
  708. { // assign real
  709. this->_Real = _Right;
  710. this->_Imag = 0;
  711. return (*this);
  712. }
  713. template<class _Other>
  714. complex(const complex<_Other>& _Right)
  715. : _Mybase((_Ty)_Right.real(), (_Ty)_Right.imag())
  716. { // construct from other complex type
  717. }
  718. template<class _Other>
  719. _Myt& operator=(const complex<_Other>& _Right)
  720. { // assign other complex type
  721. this->_Real = (_Ty)_Right.real();
  722. this->_Imag = (_Ty)_Right.imag();
  723. return (*this);
  724. }
  725. _Myt& operator+=(const _Ty& _Right)
  726. { // add real
  727. this->_Real = this->_Real + _Right;
  728. return (*this);
  729. }
  730. _Myt& operator-=(const _Ty& _Right)
  731. { // subtract real
  732. this->_Real = this->_Real - _Right;
  733. return (*this);
  734. }
  735. _Myt& operator*=(const _Ty& _Right)
  736. { // multiply by real
  737. this->_Real = this->_Real * _Right;
  738. this->_Imag = this->_Imag * _Right;
  739. return (*this);
  740. }
  741. _Myt& operator/=(const _Ty& _Right)
  742. { // divide by real
  743. this->_Real = this->_Real / _Right;
  744. this->_Imag = this->_Imag / _Right;
  745. return (*this);
  746. }
  747. _Myt& operator+=(const _Myt& _Right)
  748. { // add other complex
  749. _Add(_Right);
  750. return (*this);
  751. }
  752. _Myt& operator-=(const _Myt& _Right)
  753. { // subtract other complex
  754. _Sub(_Right);
  755. return (*this);
  756. }
  757. _Myt& operator*=(const _Myt& _Right)
  758. { // multiply by other complex
  759. _Mul(_Right);
  760. return (*this);
  761. }
  762. _Myt& operator/=(const _Myt& _Right)
  763. { // divide by other complex
  764. _Div(_Right);
  765. return (*this);
  766. }
  767. template<class _Other> inline
  768. _Myt& operator+=(const complex<_Other>& _Right)
  769. { // add other complex
  770. this->_Add(_Right);
  771. return (*this);
  772. }
  773. template<class _Other> inline
  774. _Myt& operator-=(const complex<_Other>& _Right)
  775. { // subtract other complex
  776. this->_Sub(_Right);
  777. return (*this);
  778. }
  779. template<class _Other> inline
  780. _Myt& operator*=(const complex<_Other>& _Right)
  781. { // multiply by other complex
  782. this->_Mul(_Right);
  783. return (*this);
  784. }
  785. template<class _Other> inline
  786. _Myt& operator/=(const complex<_Other>& _Right)
  787. { // divide by other complex
  788. this->_Div(_Right);
  789. return (*this);
  790. }
  791. };
  792. #define _CMPLX(T) complex<T >
  793. #define _CTR(T) _Ctraits<T >
  794. #define _TMPLT(T) template<class T >
  795. #include <xcomplex> /* define all complex template functions */
  796. // TEMPLATE FUNCTION operator>>
  797. template<class _Ty,
  798. class _Elem,
  799. class _Tr> inline
  800. basic_istream<_Elem, _Tr>& __cdecl operator>>(
  801. basic_istream<_Elem, _Tr>& _Istr, complex<_Ty>& _Right)
  802. { // extract a complex<_Ty>
  803. typedef complex<_Ty> _Myt;
  804. _Elem _Ch;
  805. long double _Real, _Imag = 0;
  806. if (_Istr >> _Ch && _Ch != '(')
  807. { // no leading '(', treat as real only
  808. _Istr.putback(_Ch);
  809. _Istr >> _Real;
  810. _Imag = 0;
  811. }
  812. else if (_Istr >> _Real >> _Ch && _Ch != ',')
  813. if (_Ch == ')')
  814. _Imag = 0; // (real)
  815. else
  816. { // no trailing ')' after real, treat as bad field
  817. _Istr.putback(_Ch);
  818. _Istr.setstate(ios_base::failbit);
  819. }
  820. else if (_Istr >> _Imag >> _Ch && _Ch != ')')
  821. { // no imag or trailing ')', treat as bad field
  822. _Istr.putback(_Ch);
  823. _Istr.setstate(ios_base::failbit);
  824. }
  825. if (!_Istr.fail())
  826. { // store valid result
  827. _Ty _Tyreal((_Ty)_Real), _Tyimag((_Ty)_Imag);
  828. _Right = _Myt(_Tyreal, _Tyimag);
  829. }
  830. return (_Istr);
  831. }
  832. // TEMPLATE FUNCTION operator<<
  833. template<class _Ty,
  834. class _Elem,
  835. class _Tr> inline
  836. basic_ostream<_Elem, _Tr>& __cdecl operator<<(
  837. basic_ostream<_Elem, _Tr>& _Ostr, const complex<_Ty>& _Right)
  838. { // insert a complex<_Ty>
  839. basic_ostringstream<_Elem, _Tr, allocator<_Elem> > _Sstr;
  840. _Sstr.flags(_Ostr.flags());
  841. _Sstr.imbue(_Ostr.getloc());
  842. _Sstr.precision(_Ostr.precision());
  843. _Sstr << '(' << real(_Right) << ',' << imag(_Right) << ')';
  844. basic_string<_Elem, _Tr, allocator<_Elem> > _Str = _Sstr.str();
  845. return (_Ostr << _Str.c_str());
  846. }
  847. #ifdef _DLL_CPPLIB
  848. #ifndef CRTDLL2
  849. template _CRTIMP2 basic_istream<char, char_traits<char> >&
  850. __cdecl operator>>(basic_istream<char, char_traits<char> >&,
  851. complex<float>&);
  852. template _CRTIMP2 basic_ostream<char, char_traits<char> >&
  853. __cdecl operator<<(basic_ostream<char, char_traits<char> >&,
  854. const complex<float>&);
  855. template _CRTIMP2 basic_istream<wchar_t, char_traits<wchar_t> >&
  856. __cdecl operator>>(basic_istream<wchar_t, char_traits<wchar_t> >&,
  857. complex<float>&);
  858. template _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >&
  859. __cdecl operator<<(basic_ostream<wchar_t, char_traits<wchar_t> >&,
  860. const complex<float>&);
  861. #ifdef _CRTBLD_NATIVE_WCHAR_T
  862. template _CRTIMP2 basic_istream<unsigned short, char_traits<unsigned short> >&
  863. __cdecl operator>>(basic_istream<unsigned short, char_traits<unsigned short> >&,
  864. complex<float>&);
  865. template _CRTIMP2 basic_ostream<unsigned short, char_traits<unsigned short> >&
  866. __cdecl operator<<(basic_ostream<unsigned short, char_traits<unsigned short> >&,
  867. const complex<float>&);
  868. #endif
  869. template _CRTIMP2 basic_istream<char, char_traits<char> >&
  870. __cdecl operator>>(basic_istream<char, char_traits<char> >&,
  871. complex<double>&);
  872. template _CRTIMP2 basic_ostream<char, char_traits<char> >&
  873. __cdecl operator<<(basic_ostream<char, char_traits<char> >&,
  874. const complex<double>&);
  875. template _CRTIMP2 basic_istream<wchar_t, char_traits<wchar_t> >&
  876. __cdecl operator>>(basic_istream<wchar_t, char_traits<wchar_t> >&,
  877. complex<double>&);
  878. template _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >&
  879. __cdecl operator<<(basic_ostream<wchar_t, char_traits<wchar_t> >&,
  880. const complex<double>&);
  881. #ifdef _CRTBLD_NATIVE_WCHAR_T
  882. template _CRTIMP2 basic_istream<unsigned short, char_traits<unsigned short> >&
  883. __cdecl operator>>(basic_istream<unsigned short, char_traits<unsigned short> >&,
  884. complex<double>&);
  885. template _CRTIMP2 basic_ostream<unsigned short, char_traits<unsigned short> >&
  886. __cdecl operator<<(basic_ostream<unsigned short, char_traits<unsigned short> >&,
  887. const complex<double>&);
  888. #endif
  889. template _CRTIMP2 basic_istream<char, char_traits<char> >&
  890. __cdecl operator>>(basic_istream<char, char_traits<char> >&,
  891. complex<long double>&);
  892. template _CRTIMP2 basic_ostream<char, char_traits<char> >&
  893. __cdecl operator<<(basic_ostream<char, char_traits<char> >&,
  894. const complex<long double>&);
  895. template _CRTIMP2 basic_istream<wchar_t, char_traits<wchar_t> >&
  896. __cdecl operator>>(basic_istream<wchar_t, char_traits<wchar_t> >&,
  897. complex<long double>&);
  898. template _CRTIMP2 basic_ostream<wchar_t, char_traits<wchar_t> >&
  899. __cdecl operator<<(basic_ostream<wchar_t, char_traits<wchar_t> >&,
  900. const complex<long double>&);
  901. #ifdef _CRTBLD_NATIVE_WCHAR_T
  902. template _CRTIMP2 basic_istream<unsigned short, char_traits<unsigned short> >&
  903. __cdecl operator>>(basic_istream<unsigned short, char_traits<unsigned short> >&,
  904. complex<long double>&);
  905. template _CRTIMP2 basic_ostream<unsigned short, char_traits<unsigned short> >&
  906. __cdecl operator<<(basic_ostream<unsigned short, char_traits<unsigned short> >&,
  907. const complex<long double>&);
  908. #endif
  909. #endif // CRTDLL2
  910. #endif // _DLL_CPPLIB
  911. _STD_END
  912. #pragma warning(default: 4244)
  913. #pragma warning(pop)
  914. #pragma pack(pop)
  915. #endif /* _COMPLEX_ */
  916. /*
  917. * Copyright (c) 1992-2001 by P.J. Plauger. ALL RIGHTS RESERVED.
  918. * Consult your license regarding permissions and restrictions.
  919. V3.10:0009 */