complex 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP_COMPLEX
  10. #define _LIBCPP_COMPLEX
  11. /*
  12. complex synopsis
  13. namespace std
  14. {
  15. template<class T>
  16. class complex
  17. {
  18. public:
  19. typedef T value_type;
  20. complex(const T& re = T(), const T& im = T()); // constexpr in C++14
  21. complex(const complex&); // constexpr in C++14
  22. template<class X> complex(const complex<X>&); // constexpr in C++14
  23. T real() const; // constexpr in C++14
  24. T imag() const; // constexpr in C++14
  25. void real(T); // constexpr in C++20
  26. void imag(T); // constexpr in C++20
  27. complex<T>& operator= (const T&); // constexpr in C++20
  28. complex<T>& operator+=(const T&); // constexpr in C++20
  29. complex<T>& operator-=(const T&); // constexpr in C++20
  30. complex<T>& operator*=(const T&); // constexpr in C++20
  31. complex<T>& operator/=(const T&); // constexpr in C++20
  32. complex& operator=(const complex&); // constexpr in C++20
  33. template<class X> complex<T>& operator= (const complex<X>&); // constexpr in C++20
  34. template<class X> complex<T>& operator+=(const complex<X>&); // constexpr in C++20
  35. template<class X> complex<T>& operator-=(const complex<X>&); // constexpr in C++20
  36. template<class X> complex<T>& operator*=(const complex<X>&); // constexpr in C++20
  37. template<class X> complex<T>& operator/=(const complex<X>&); // constexpr in C++20
  38. };
  39. template<>
  40. class complex<float>
  41. {
  42. public:
  43. typedef float value_type;
  44. constexpr complex(float re = 0.0f, float im = 0.0f);
  45. explicit constexpr complex(const complex<double>&);
  46. explicit constexpr complex(const complex<long double>&);
  47. constexpr float real() const;
  48. void real(float); // constexpr in C++20
  49. constexpr float imag() const;
  50. void imag(float); // constexpr in C++20
  51. complex<float>& operator= (float); // constexpr in C++20
  52. complex<float>& operator+=(float); // constexpr in C++20
  53. complex<float>& operator-=(float); // constexpr in C++20
  54. complex<float>& operator*=(float); // constexpr in C++20
  55. complex<float>& operator/=(float); // constexpr in C++20
  56. complex<float>& operator=(const complex<float>&); // constexpr in C++20
  57. template<class X> complex<float>& operator= (const complex<X>&); // constexpr in C++20
  58. template<class X> complex<float>& operator+=(const complex<X>&); // constexpr in C++20
  59. template<class X> complex<float>& operator-=(const complex<X>&); // constexpr in C++20
  60. template<class X> complex<float>& operator*=(const complex<X>&); // constexpr in C++20
  61. template<class X> complex<float>& operator/=(const complex<X>&); // constexpr in C++20
  62. };
  63. template<>
  64. class complex<double>
  65. {
  66. public:
  67. typedef double value_type;
  68. constexpr complex(double re = 0.0, double im = 0.0);
  69. constexpr complex(const complex<float>&);
  70. explicit constexpr complex(const complex<long double>&);
  71. constexpr double real() const;
  72. void real(double); // constexpr in C++20
  73. constexpr double imag() const;
  74. void imag(double); // constexpr in C++20
  75. complex<double>& operator= (double); // constexpr in C++20
  76. complex<double>& operator+=(double); // constexpr in C++20
  77. complex<double>& operator-=(double); // constexpr in C++20
  78. complex<double>& operator*=(double); // constexpr in C++20
  79. complex<double>& operator/=(double); // constexpr in C++20
  80. complex<double>& operator=(const complex<double>&); // constexpr in C++20
  81. template<class X> complex<double>& operator= (const complex<X>&); // constexpr in C++20
  82. template<class X> complex<double>& operator+=(const complex<X>&); // constexpr in C++20
  83. template<class X> complex<double>& operator-=(const complex<X>&); // constexpr in C++20
  84. template<class X> complex<double>& operator*=(const complex<X>&); // constexpr in C++20
  85. template<class X> complex<double>& operator/=(const complex<X>&); // constexpr in C++20
  86. };
  87. template<>
  88. class complex<long double>
  89. {
  90. public:
  91. typedef long double value_type;
  92. constexpr complex(long double re = 0.0L, long double im = 0.0L);
  93. constexpr complex(const complex<float>&);
  94. constexpr complex(const complex<double>&);
  95. constexpr long double real() const;
  96. void real(long double); // constexpr in C++20
  97. constexpr long double imag() const;
  98. void imag(long double); // constexpr in C++20
  99. complex<long double>& operator=(const complex<long double>&); // constexpr in C++20
  100. complex<long double>& operator= (long double); // constexpr in C++20
  101. complex<long double>& operator+=(long double); // constexpr in C++20
  102. complex<long double>& operator-=(long double); // constexpr in C++20
  103. complex<long double>& operator*=(long double); // constexpr in C++20
  104. complex<long double>& operator/=(long double); // constexpr in C++20
  105. template<class X> complex<long double>& operator= (const complex<X>&); // constexpr in C++20
  106. template<class X> complex<long double>& operator+=(const complex<X>&); // constexpr in C++20
  107. template<class X> complex<long double>& operator-=(const complex<X>&); // constexpr in C++20
  108. template<class X> complex<long double>& operator*=(const complex<X>&); // constexpr in C++20
  109. template<class X> complex<long double>& operator/=(const complex<X>&); // constexpr in C++20
  110. };
  111. // 26.3.6 operators:
  112. template<class T> complex<T> operator+(const complex<T>&, const complex<T>&); // constexpr in C++20
  113. template<class T> complex<T> operator+(const complex<T>&, const T&); // constexpr in C++20
  114. template<class T> complex<T> operator+(const T&, const complex<T>&); // constexpr in C++20
  115. template<class T> complex<T> operator-(const complex<T>&, const complex<T>&); // constexpr in C++20
  116. template<class T> complex<T> operator-(const complex<T>&, const T&); // constexpr in C++20
  117. template<class T> complex<T> operator-(const T&, const complex<T>&); // constexpr in C++20
  118. template<class T> complex<T> operator*(const complex<T>&, const complex<T>&); // constexpr in C++20
  119. template<class T> complex<T> operator*(const complex<T>&, const T&); // constexpr in C++20
  120. template<class T> complex<T> operator*(const T&, const complex<T>&); // constexpr in C++20
  121. template<class T> complex<T> operator/(const complex<T>&, const complex<T>&); // constexpr in C++20
  122. template<class T> complex<T> operator/(const complex<T>&, const T&); // constexpr in C++20
  123. template<class T> complex<T> operator/(const T&, const complex<T>&); // constexpr in C++20
  124. template<class T> complex<T> operator+(const complex<T>&); // constexpr in C++20
  125. template<class T> complex<T> operator-(const complex<T>&); // constexpr in C++20
  126. template<class T> bool operator==(const complex<T>&, const complex<T>&); // constexpr in C++14
  127. template<class T> bool operator==(const complex<T>&, const T&); // constexpr in C++14
  128. template<class T> bool operator==(const T&, const complex<T>&); // constexpr in C++14, removed in C++20
  129. template<class T> bool operator!=(const complex<T>&, const complex<T>&); // constexpr in C++14, removed in C++20
  130. template<class T> bool operator!=(const complex<T>&, const T&); // constexpr in C++14, removed in C++20
  131. template<class T> bool operator!=(const T&, const complex<T>&); // constexpr in C++14, removed in C++20
  132. template<class T, class charT, class traits>
  133. basic_istream<charT, traits>&
  134. operator>>(basic_istream<charT, traits>&, complex<T>&);
  135. template<class T, class charT, class traits>
  136. basic_ostream<charT, traits>&
  137. operator<<(basic_ostream<charT, traits>&, const complex<T>&);
  138. // 26.3.7 values:
  139. template<class T> T real(const complex<T>&); // constexpr in C++14
  140. long double real(long double); // constexpr in C++14
  141. double real(double); // constexpr in C++14
  142. template<Integral T> double real(T); // constexpr in C++14
  143. float real(float); // constexpr in C++14
  144. template<class T> T imag(const complex<T>&); // constexpr in C++14
  145. long double imag(long double); // constexpr in C++14
  146. double imag(double); // constexpr in C++14
  147. template<Integral T> double imag(T); // constexpr in C++14
  148. float imag(float); // constexpr in C++14
  149. template<class T> T abs(const complex<T>&);
  150. template<class T> T arg(const complex<T>&);
  151. long double arg(long double);
  152. double arg(double);
  153. template<Integral T> double arg(T);
  154. float arg(float);
  155. template<class T> T norm(const complex<T>&); // constexpr in C++20
  156. long double norm(long double); // constexpr in C++20
  157. double norm(double); // constexpr in C++20
  158. template<Integral T> double norm(T); // constexpr in C++20
  159. float norm(float); // constexpr in C++20
  160. template<class T> complex<T> conj(const complex<T>&); // constexpr in C++20
  161. complex<long double> conj(long double); // constexpr in C++20
  162. complex<double> conj(double); // constexpr in C++20
  163. template<Integral T> complex<double> conj(T); // constexpr in C++20
  164. complex<float> conj(float); // constexpr in C++20
  165. template<class T> complex<T> proj(const complex<T>&);
  166. complex<long double> proj(long double);
  167. complex<double> proj(double);
  168. template<Integral T> complex<double> proj(T);
  169. complex<float> proj(float);
  170. template<class T> complex<T> polar(const T&, const T& = T());
  171. // 26.3.8 transcendentals:
  172. template<class T> complex<T> acos(const complex<T>&);
  173. template<class T> complex<T> asin(const complex<T>&);
  174. template<class T> complex<T> atan(const complex<T>&);
  175. template<class T> complex<T> acosh(const complex<T>&);
  176. template<class T> complex<T> asinh(const complex<T>&);
  177. template<class T> complex<T> atanh(const complex<T>&);
  178. template<class T> complex<T> cos (const complex<T>&);
  179. template<class T> complex<T> cosh (const complex<T>&);
  180. template<class T> complex<T> exp (const complex<T>&);
  181. template<class T> complex<T> log (const complex<T>&);
  182. template<class T> complex<T> log10(const complex<T>&);
  183. template<class T> complex<T> pow(const complex<T>&, const T&);
  184. template<class T> complex<T> pow(const complex<T>&, const complex<T>&);
  185. template<class T> complex<T> pow(const T&, const complex<T>&);
  186. template<class T> complex<T> sin (const complex<T>&);
  187. template<class T> complex<T> sinh (const complex<T>&);
  188. template<class T> complex<T> sqrt (const complex<T>&);
  189. template<class T> complex<T> tan (const complex<T>&);
  190. template<class T> complex<T> tanh (const complex<T>&);
  191. } // std
  192. */
  193. #include <__assert> // all public C++ headers provide the assertion handler
  194. #include <__config>
  195. #include <cmath>
  196. #include <iosfwd>
  197. #include <stdexcept>
  198. #include <version>
  199. #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
  200. # include <sstream> // for std::basic_ostringstream
  201. #endif
  202. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  203. # pragma GCC system_header
  204. #endif
  205. _LIBCPP_BEGIN_NAMESPACE_STD
  206. template<class _Tp> class _LIBCPP_TEMPLATE_VIS complex;
  207. template<class _Tp> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator*(const complex<_Tp>& __z, const complex<_Tp>& __w);
  208. template<class _Tp> _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);
  209. template<class _Tp>
  210. class _LIBCPP_TEMPLATE_VIS complex
  211. {
  212. public:
  213. typedef _Tp value_type;
  214. private:
  215. value_type __re_;
  216. value_type __im_;
  217. public:
  218. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  219. complex(const value_type& __re = value_type(), const value_type& __im = value_type())
  220. : __re_(__re), __im_(__im) {}
  221. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  222. complex(const complex<_Xp>& __c)
  223. : __re_(__c.real()), __im_(__c.imag()) {}
  224. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type real() const {return __re_;}
  225. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14 value_type imag() const {return __im_;}
  226. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
  227. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
  228. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const value_type& __re)
  229. {__re_ = __re; __im_ = value_type(); return *this;}
  230. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const value_type& __re) {__re_ += __re; return *this;}
  231. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const value_type& __re) {__re_ -= __re; return *this;}
  232. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const value_type& __re) {__re_ *= __re; __im_ *= __re; return *this;}
  233. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const value_type& __re) {__re_ /= __re; __im_ /= __re; return *this;}
  234. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
  235. {
  236. __re_ = __c.real();
  237. __im_ = __c.imag();
  238. return *this;
  239. }
  240. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
  241. {
  242. __re_ += __c.real();
  243. __im_ += __c.imag();
  244. return *this;
  245. }
  246. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
  247. {
  248. __re_ -= __c.real();
  249. __im_ -= __c.imag();
  250. return *this;
  251. }
  252. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
  253. {
  254. *this = *this * complex(__c.real(), __c.imag());
  255. return *this;
  256. }
  257. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
  258. {
  259. *this = *this / complex(__c.real(), __c.imag());
  260. return *this;
  261. }
  262. };
  263. template<> class _LIBCPP_TEMPLATE_VIS complex<double>;
  264. template<> class _LIBCPP_TEMPLATE_VIS complex<long double>;
  265. template<>
  266. class _LIBCPP_TEMPLATE_VIS complex<float>
  267. {
  268. float __re_;
  269. float __im_;
  270. public:
  271. typedef float value_type;
  272. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(float __re = 0.0f, float __im = 0.0f)
  273. : __re_(__re), __im_(__im) {}
  274. _LIBCPP_INLINE_VISIBILITY
  275. explicit _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
  276. _LIBCPP_INLINE_VISIBILITY
  277. explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
  278. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float real() const {return __re_;}
  279. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR float imag() const {return __im_;}
  280. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
  281. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
  282. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (float __re)
  283. {__re_ = __re; __im_ = value_type(); return *this;}
  284. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(float __re) {__re_ += __re; return *this;}
  285. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(float __re) {__re_ -= __re; return *this;}
  286. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(float __re) {__re_ *= __re; __im_ *= __re; return *this;}
  287. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(float __re) {__re_ /= __re; __im_ /= __re; return *this;}
  288. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
  289. {
  290. __re_ = __c.real();
  291. __im_ = __c.imag();
  292. return *this;
  293. }
  294. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
  295. {
  296. __re_ += __c.real();
  297. __im_ += __c.imag();
  298. return *this;
  299. }
  300. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
  301. {
  302. __re_ -= __c.real();
  303. __im_ -= __c.imag();
  304. return *this;
  305. }
  306. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
  307. {
  308. *this = *this * complex(__c.real(), __c.imag());
  309. return *this;
  310. }
  311. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
  312. {
  313. *this = *this / complex(__c.real(), __c.imag());
  314. return *this;
  315. }
  316. };
  317. template<>
  318. class _LIBCPP_TEMPLATE_VIS complex<double>
  319. {
  320. double __re_;
  321. double __im_;
  322. public:
  323. typedef double value_type;
  324. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(double __re = 0.0, double __im = 0.0)
  325. : __re_(__re), __im_(__im) {}
  326. _LIBCPP_INLINE_VISIBILITY
  327. _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
  328. _LIBCPP_INLINE_VISIBILITY
  329. explicit _LIBCPP_CONSTEXPR complex(const complex<long double>& __c);
  330. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double real() const {return __re_;}
  331. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR double imag() const {return __im_;}
  332. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
  333. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
  334. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (double __re)
  335. {__re_ = __re; __im_ = value_type(); return *this;}
  336. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(double __re) {__re_ += __re; return *this;}
  337. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(double __re) {__re_ -= __re; return *this;}
  338. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(double __re) {__re_ *= __re; __im_ *= __re; return *this;}
  339. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(double __re) {__re_ /= __re; __im_ /= __re; return *this;}
  340. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
  341. {
  342. __re_ = __c.real();
  343. __im_ = __c.imag();
  344. return *this;
  345. }
  346. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
  347. {
  348. __re_ += __c.real();
  349. __im_ += __c.imag();
  350. return *this;
  351. }
  352. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
  353. {
  354. __re_ -= __c.real();
  355. __im_ -= __c.imag();
  356. return *this;
  357. }
  358. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
  359. {
  360. *this = *this * complex(__c.real(), __c.imag());
  361. return *this;
  362. }
  363. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
  364. {
  365. *this = *this / complex(__c.real(), __c.imag());
  366. return *this;
  367. }
  368. };
  369. template<>
  370. class _LIBCPP_TEMPLATE_VIS complex<long double>
  371. {
  372. long double __re_;
  373. long double __im_;
  374. public:
  375. typedef long double value_type;
  376. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR complex(long double __re = 0.0L, long double __im = 0.0L)
  377. : __re_(__re), __im_(__im) {}
  378. _LIBCPP_INLINE_VISIBILITY
  379. _LIBCPP_CONSTEXPR complex(const complex<float>& __c);
  380. _LIBCPP_INLINE_VISIBILITY
  381. _LIBCPP_CONSTEXPR complex(const complex<double>& __c);
  382. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double real() const {return __re_;}
  383. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR long double imag() const {return __im_;}
  384. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void real(value_type __re) {__re_ = __re;}
  385. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 void imag(value_type __im) {__im_ = __im;}
  386. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (long double __re)
  387. {__re_ = __re; __im_ = value_type(); return *this;}
  388. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(long double __re) {__re_ += __re; return *this;}
  389. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(long double __re) {__re_ -= __re; return *this;}
  390. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(long double __re) {__re_ *= __re; __im_ *= __re; return *this;}
  391. _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(long double __re) {__re_ /= __re; __im_ /= __re; return *this;}
  392. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator= (const complex<_Xp>& __c)
  393. {
  394. __re_ = __c.real();
  395. __im_ = __c.imag();
  396. return *this;
  397. }
  398. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator+=(const complex<_Xp>& __c)
  399. {
  400. __re_ += __c.real();
  401. __im_ += __c.imag();
  402. return *this;
  403. }
  404. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator-=(const complex<_Xp>& __c)
  405. {
  406. __re_ -= __c.real();
  407. __im_ -= __c.imag();
  408. return *this;
  409. }
  410. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator*=(const complex<_Xp>& __c)
  411. {
  412. *this = *this * complex(__c.real(), __c.imag());
  413. return *this;
  414. }
  415. template<class _Xp> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20 complex& operator/=(const complex<_Xp>& __c)
  416. {
  417. *this = *this / complex(__c.real(), __c.imag());
  418. return *this;
  419. }
  420. };
  421. inline
  422. _LIBCPP_CONSTEXPR
  423. complex<float>::complex(const complex<double>& __c)
  424. : __re_(__c.real()), __im_(__c.imag()) {}
  425. inline
  426. _LIBCPP_CONSTEXPR
  427. complex<float>::complex(const complex<long double>& __c)
  428. : __re_(__c.real()), __im_(__c.imag()) {}
  429. inline
  430. _LIBCPP_CONSTEXPR
  431. complex<double>::complex(const complex<float>& __c)
  432. : __re_(__c.real()), __im_(__c.imag()) {}
  433. inline
  434. _LIBCPP_CONSTEXPR
  435. complex<double>::complex(const complex<long double>& __c)
  436. : __re_(__c.real()), __im_(__c.imag()) {}
  437. inline
  438. _LIBCPP_CONSTEXPR
  439. complex<long double>::complex(const complex<float>& __c)
  440. : __re_(__c.real()), __im_(__c.imag()) {}
  441. inline
  442. _LIBCPP_CONSTEXPR
  443. complex<long double>::complex(const complex<double>& __c)
  444. : __re_(__c.real()), __im_(__c.imag()) {}
  445. // 26.3.6 operators:
  446. template<class _Tp>
  447. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  448. complex<_Tp>
  449. operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
  450. {
  451. complex<_Tp> __t(__x);
  452. __t += __y;
  453. return __t;
  454. }
  455. template<class _Tp>
  456. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  457. complex<_Tp>
  458. operator+(const complex<_Tp>& __x, const _Tp& __y)
  459. {
  460. complex<_Tp> __t(__x);
  461. __t += __y;
  462. return __t;
  463. }
  464. template<class _Tp>
  465. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  466. complex<_Tp>
  467. operator+(const _Tp& __x, const complex<_Tp>& __y)
  468. {
  469. complex<_Tp> __t(__y);
  470. __t += __x;
  471. return __t;
  472. }
  473. template<class _Tp>
  474. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  475. complex<_Tp>
  476. operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
  477. {
  478. complex<_Tp> __t(__x);
  479. __t -= __y;
  480. return __t;
  481. }
  482. template<class _Tp>
  483. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  484. complex<_Tp>
  485. operator-(const complex<_Tp>& __x, const _Tp& __y)
  486. {
  487. complex<_Tp> __t(__x);
  488. __t -= __y;
  489. return __t;
  490. }
  491. template<class _Tp>
  492. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  493. complex<_Tp>
  494. operator-(const _Tp& __x, const complex<_Tp>& __y)
  495. {
  496. complex<_Tp> __t(-__y);
  497. __t += __x;
  498. return __t;
  499. }
  500. template<class _Tp>
  501. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
  502. operator*(const complex<_Tp>& __z, const complex<_Tp>& __w)
  503. {
  504. _Tp __a = __z.real();
  505. _Tp __b = __z.imag();
  506. _Tp __c = __w.real();
  507. _Tp __d = __w.imag();
  508. // Avoid floating point operations that are invalid during constant evaluation
  509. if (__libcpp_is_constant_evaluated()) {
  510. bool __z_zero = __a == _Tp(0) && __b == _Tp(0);
  511. bool __w_zero = __c == _Tp(0) && __d == _Tp(0);
  512. bool __z_inf = std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b);
  513. bool __w_inf = std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d);
  514. bool __z_nan = !__z_inf && (
  515. (std::__constexpr_isnan(__a) && std::__constexpr_isnan(__b))
  516. || (std::__constexpr_isnan(__a) && __b == _Tp(0))
  517. || (__a == _Tp(0) && std::__constexpr_isnan(__b))
  518. );
  519. bool __w_nan = !__w_inf && (
  520. (std::__constexpr_isnan(__c) && std::__constexpr_isnan(__d))
  521. || (std::__constexpr_isnan(__c) && __d == _Tp(0))
  522. || (__c == _Tp(0) && std::__constexpr_isnan(__d))
  523. );
  524. if (__z_nan || __w_nan) {
  525. return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
  526. }
  527. if (__z_inf || __w_inf) {
  528. if (__z_zero || __w_zero) {
  529. return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
  530. }
  531. return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
  532. }
  533. bool __z_nonzero_nan = !__z_inf && !__z_nan && (std::__constexpr_isnan(__a) || std::__constexpr_isnan(__b));
  534. bool __w_nonzero_nan = !__w_inf && !__w_nan && (std::__constexpr_isnan(__c) || std::__constexpr_isnan(__d));
  535. if (__z_nonzero_nan || __w_nonzero_nan) {
  536. return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
  537. }
  538. }
  539. _Tp __ac = __a * __c;
  540. _Tp __bd = __b * __d;
  541. _Tp __ad = __a * __d;
  542. _Tp __bc = __b * __c;
  543. _Tp __x = __ac - __bd;
  544. _Tp __y = __ad + __bc;
  545. if (std::__constexpr_isnan(__x) && std::__constexpr_isnan(__y))
  546. {
  547. bool __recalc = false;
  548. if (std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b))
  549. {
  550. __a = std::__constexpr_copysign(std::__constexpr_isinf(__a) ? _Tp(1) : _Tp(0), __a);
  551. __b = std::__constexpr_copysign(std::__constexpr_isinf(__b) ? _Tp(1) : _Tp(0), __b);
  552. if (std::__constexpr_isnan(__c))
  553. __c = std::__constexpr_copysign(_Tp(0), __c);
  554. if (std::__constexpr_isnan(__d))
  555. __d = std::__constexpr_copysign(_Tp(0), __d);
  556. __recalc = true;
  557. }
  558. if (std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d))
  559. {
  560. __c = std::__constexpr_copysign(std::__constexpr_isinf(__c) ? _Tp(1) : _Tp(0), __c);
  561. __d = std::__constexpr_copysign(std::__constexpr_isinf(__d) ? _Tp(1) : _Tp(0), __d);
  562. if (std::__constexpr_isnan(__a))
  563. __a = std::__constexpr_copysign(_Tp(0), __a);
  564. if (std::__constexpr_isnan(__b))
  565. __b = std::__constexpr_copysign(_Tp(0), __b);
  566. __recalc = true;
  567. }
  568. if (!__recalc && (std::__constexpr_isinf(__ac) || std::__constexpr_isinf(__bd) ||
  569. std::__constexpr_isinf(__ad) || std::__constexpr_isinf(__bc)))
  570. {
  571. if (std::__constexpr_isnan(__a))
  572. __a = std::__constexpr_copysign(_Tp(0), __a);
  573. if (std::__constexpr_isnan(__b))
  574. __b = std::__constexpr_copysign(_Tp(0), __b);
  575. if (std::__constexpr_isnan(__c))
  576. __c = std::__constexpr_copysign(_Tp(0), __c);
  577. if (std::__constexpr_isnan(__d))
  578. __d = std::__constexpr_copysign(_Tp(0), __d);
  579. __recalc = true;
  580. }
  581. if (__recalc)
  582. {
  583. __x = _Tp(INFINITY) * (__a * __c - __b * __d);
  584. __y = _Tp(INFINITY) * (__a * __d + __b * __c);
  585. }
  586. }
  587. return complex<_Tp>(__x, __y);
  588. }
  589. template<class _Tp>
  590. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  591. complex<_Tp>
  592. operator*(const complex<_Tp>& __x, const _Tp& __y)
  593. {
  594. complex<_Tp> __t(__x);
  595. __t *= __y;
  596. return __t;
  597. }
  598. template<class _Tp>
  599. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  600. complex<_Tp>
  601. operator*(const _Tp& __x, const complex<_Tp>& __y)
  602. {
  603. complex<_Tp> __t(__y);
  604. __t *= __x;
  605. return __t;
  606. }
  607. template<class _Tp>
  608. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 complex<_Tp>
  609. operator/(const complex<_Tp>& __z, const complex<_Tp>& __w)
  610. {
  611. int __ilogbw = 0;
  612. _Tp __a = __z.real();
  613. _Tp __b = __z.imag();
  614. _Tp __c = __w.real();
  615. _Tp __d = __w.imag();
  616. _Tp __logbw = std::__constexpr_logb(std::__constexpr_fmax(std::__constexpr_fabs(__c), std::__constexpr_fabs(__d)));
  617. if (std::__constexpr_isfinite(__logbw))
  618. {
  619. __ilogbw = static_cast<int>(__logbw);
  620. __c = std::__constexpr_scalbn(__c, -__ilogbw);
  621. __d = std::__constexpr_scalbn(__d, -__ilogbw);
  622. }
  623. // Avoid floating point operations that are invalid during constant evaluation
  624. if (__libcpp_is_constant_evaluated()) {
  625. bool __z_zero = __a == _Tp(0) && __b == _Tp(0);
  626. bool __w_zero = __c == _Tp(0) && __d == _Tp(0);
  627. bool __z_inf = std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b);
  628. bool __w_inf = std::__constexpr_isinf(__c) || std::__constexpr_isinf(__d);
  629. bool __z_nan = !__z_inf && (
  630. (std::__constexpr_isnan(__a) && std::__constexpr_isnan(__b))
  631. || (std::__constexpr_isnan(__a) && __b == _Tp(0))
  632. || (__a == _Tp(0) && std::__constexpr_isnan(__b))
  633. );
  634. bool __w_nan = !__w_inf && (
  635. (std::__constexpr_isnan(__c) && std::__constexpr_isnan(__d))
  636. || (std::__constexpr_isnan(__c) && __d == _Tp(0))
  637. || (__c == _Tp(0) && std::__constexpr_isnan(__d))
  638. );
  639. if ((__z_nan || __w_nan) || (__z_inf && __w_inf)) {
  640. return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
  641. }
  642. bool __z_nonzero_nan = !__z_inf && !__z_nan && (std::__constexpr_isnan(__a) || std::__constexpr_isnan(__b));
  643. bool __w_nonzero_nan = !__w_inf && !__w_nan && (std::__constexpr_isnan(__c) || std::__constexpr_isnan(__d));
  644. if (__z_nonzero_nan || __w_nonzero_nan) {
  645. if (__w_zero) {
  646. return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
  647. }
  648. return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
  649. }
  650. if (__w_inf) {
  651. return complex<_Tp>(_Tp(0), _Tp(0));
  652. }
  653. if (__z_inf) {
  654. return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
  655. }
  656. if (__w_zero) {
  657. if (__z_zero) {
  658. return complex<_Tp>(_Tp(numeric_limits<_Tp>::quiet_NaN()), _Tp(0));
  659. }
  660. return complex<_Tp>(_Tp(numeric_limits<_Tp>::infinity()), _Tp(numeric_limits<_Tp>::infinity()));
  661. }
  662. }
  663. _Tp __denom = __c * __c + __d * __d;
  664. _Tp __x = std::__constexpr_scalbn((__a * __c + __b * __d) / __denom, -__ilogbw);
  665. _Tp __y = std::__constexpr_scalbn((__b * __c - __a * __d) / __denom, -__ilogbw);
  666. if (std::__constexpr_isnan(__x) && std::__constexpr_isnan(__y))
  667. {
  668. if ((__denom == _Tp(0)) && (!std::__constexpr_isnan(__a) || !std::__constexpr_isnan(__b)))
  669. {
  670. __x = std::__constexpr_copysign(_Tp(INFINITY), __c) * __a;
  671. __y = std::__constexpr_copysign(_Tp(INFINITY), __c) * __b;
  672. } else if ((std::__constexpr_isinf(__a) || std::__constexpr_isinf(__b)) && std::__constexpr_isfinite(__c) &&
  673. std::__constexpr_isfinite(__d)) {
  674. __a = std::__constexpr_copysign(std::__constexpr_isinf(__a) ? _Tp(1) : _Tp(0), __a);
  675. __b = std::__constexpr_copysign(std::__constexpr_isinf(__b) ? _Tp(1) : _Tp(0), __b);
  676. __x = _Tp(INFINITY) * (__a * __c + __b * __d);
  677. __y = _Tp(INFINITY) * (__b * __c - __a * __d);
  678. } else if (std::__constexpr_isinf(__logbw) && __logbw > _Tp(0) && std::__constexpr_isfinite(__a) &&
  679. std::__constexpr_isfinite(__b)) {
  680. __c = std::__constexpr_copysign(std::__constexpr_isinf(__c) ? _Tp(1) : _Tp(0), __c);
  681. __d = std::__constexpr_copysign(std::__constexpr_isinf(__d) ? _Tp(1) : _Tp(0), __d);
  682. __x = _Tp(0) * (__a * __c + __b * __d);
  683. __y = _Tp(0) * (__b * __c - __a * __d);
  684. }
  685. }
  686. return complex<_Tp>(__x, __y);
  687. }
  688. template<class _Tp>
  689. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  690. complex<_Tp>
  691. operator/(const complex<_Tp>& __x, const _Tp& __y)
  692. {
  693. return complex<_Tp>(__x.real() / __y, __x.imag() / __y);
  694. }
  695. template<class _Tp>
  696. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  697. complex<_Tp>
  698. operator/(const _Tp& __x, const complex<_Tp>& __y)
  699. {
  700. complex<_Tp> __t(__x);
  701. __t /= __y;
  702. return __t;
  703. }
  704. template<class _Tp>
  705. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  706. complex<_Tp>
  707. operator+(const complex<_Tp>& __x)
  708. {
  709. return __x;
  710. }
  711. template<class _Tp>
  712. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  713. complex<_Tp>
  714. operator-(const complex<_Tp>& __x)
  715. {
  716. return complex<_Tp>(-__x.real(), -__x.imag());
  717. }
  718. template<class _Tp>
  719. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  720. bool
  721. operator==(const complex<_Tp>& __x, const complex<_Tp>& __y)
  722. {
  723. return __x.real() == __y.real() && __x.imag() == __y.imag();
  724. }
  725. template<class _Tp>
  726. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  727. bool
  728. operator==(const complex<_Tp>& __x, const _Tp& __y)
  729. {
  730. return __x.real() == __y && __x.imag() == 0;
  731. }
  732. #if _LIBCPP_STD_VER <= 17
  733. template<class _Tp>
  734. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  735. bool
  736. operator==(const _Tp& __x, const complex<_Tp>& __y)
  737. {
  738. return __x == __y.real() && 0 == __y.imag();
  739. }
  740. template<class _Tp>
  741. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  742. bool
  743. operator!=(const complex<_Tp>& __x, const complex<_Tp>& __y)
  744. {
  745. return !(__x == __y);
  746. }
  747. template<class _Tp>
  748. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  749. bool
  750. operator!=(const complex<_Tp>& __x, const _Tp& __y)
  751. {
  752. return !(__x == __y);
  753. }
  754. template<class _Tp>
  755. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  756. bool
  757. operator!=(const _Tp& __x, const complex<_Tp>& __y)
  758. {
  759. return !(__x == __y);
  760. }
  761. #endif
  762. // 26.3.7 values:
  763. template <class _Tp, bool = is_integral<_Tp>::value,
  764. bool = is_floating_point<_Tp>::value
  765. >
  766. struct __libcpp_complex_overload_traits {};
  767. // Integral Types
  768. template <class _Tp>
  769. struct __libcpp_complex_overload_traits<_Tp, true, false>
  770. {
  771. typedef double _ValueType;
  772. typedef complex<double> _ComplexType;
  773. };
  774. // Floating point types
  775. template <class _Tp>
  776. struct __libcpp_complex_overload_traits<_Tp, false, true>
  777. {
  778. typedef _Tp _ValueType;
  779. typedef complex<_Tp> _ComplexType;
  780. };
  781. // real
  782. template<class _Tp>
  783. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  784. _Tp
  785. real(const complex<_Tp>& __c)
  786. {
  787. return __c.real();
  788. }
  789. template <class _Tp>
  790. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  791. typename __libcpp_complex_overload_traits<_Tp>::_ValueType
  792. real(_Tp __re)
  793. {
  794. return __re;
  795. }
  796. // imag
  797. template<class _Tp>
  798. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  799. _Tp
  800. imag(const complex<_Tp>& __c)
  801. {
  802. return __c.imag();
  803. }
  804. template <class _Tp>
  805. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX14
  806. typename __libcpp_complex_overload_traits<_Tp>::_ValueType
  807. imag(_Tp)
  808. {
  809. return 0;
  810. }
  811. // abs
  812. template<class _Tp>
  813. inline _LIBCPP_INLINE_VISIBILITY
  814. _Tp
  815. abs(const complex<_Tp>& __c)
  816. {
  817. return std::hypot(__c.real(), __c.imag());
  818. }
  819. // arg
  820. template<class _Tp>
  821. inline _LIBCPP_INLINE_VISIBILITY
  822. _Tp
  823. arg(const complex<_Tp>& __c)
  824. {
  825. return std::atan2(__c.imag(), __c.real());
  826. }
  827. template <class _Tp, __enable_if_t<is_same<_Tp, long double>::value, int> = 0>
  828. inline _LIBCPP_INLINE_VISIBILITY
  829. long double
  830. arg(_Tp __re)
  831. {
  832. return std::atan2l(0.L, __re);
  833. }
  834. template<class _Tp, __enable_if_t<is_integral<_Tp>::value || is_same<_Tp, double>::value, int> = 0>
  835. inline _LIBCPP_INLINE_VISIBILITY
  836. double
  837. arg(_Tp __re)
  838. {
  839. return std::atan2(0., __re);
  840. }
  841. template <class _Tp, __enable_if_t<is_same<_Tp, float>::value, int> = 0>
  842. inline _LIBCPP_INLINE_VISIBILITY
  843. float
  844. arg(_Tp __re)
  845. {
  846. return std::atan2f(0.F, __re);
  847. }
  848. // norm
  849. template<class _Tp>
  850. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  851. _Tp
  852. norm(const complex<_Tp>& __c)
  853. {
  854. if (std::__constexpr_isinf(__c.real()))
  855. return std::abs(__c.real());
  856. if (std::__constexpr_isinf(__c.imag()))
  857. return std::abs(__c.imag());
  858. return __c.real() * __c.real() + __c.imag() * __c.imag();
  859. }
  860. template <class _Tp>
  861. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  862. typename __libcpp_complex_overload_traits<_Tp>::_ValueType
  863. norm(_Tp __re)
  864. {
  865. typedef typename __libcpp_complex_overload_traits<_Tp>::_ValueType _ValueType;
  866. return static_cast<_ValueType>(__re) * __re;
  867. }
  868. // conj
  869. template<class _Tp>
  870. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  871. complex<_Tp>
  872. conj(const complex<_Tp>& __c)
  873. {
  874. return complex<_Tp>(__c.real(), -__c.imag());
  875. }
  876. template <class _Tp>
  877. inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX20
  878. typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
  879. conj(_Tp __re)
  880. {
  881. typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
  882. return _ComplexType(__re);
  883. }
  884. // proj
  885. template<class _Tp>
  886. inline _LIBCPP_INLINE_VISIBILITY
  887. complex<_Tp>
  888. proj(const complex<_Tp>& __c)
  889. {
  890. complex<_Tp> __r = __c;
  891. if (std::__constexpr_isinf(__c.real()) || std::__constexpr_isinf(__c.imag()))
  892. __r = complex<_Tp>(INFINITY, std::copysign(_Tp(0), __c.imag()));
  893. return __r;
  894. }
  895. template <class _Tp, __enable_if_t<is_floating_point<_Tp>::value, int> = 0>
  896. inline _LIBCPP_INLINE_VISIBILITY
  897. typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
  898. proj(_Tp __re)
  899. {
  900. if (std::__constexpr_isinf(__re))
  901. __re = std::abs(__re);
  902. return complex<_Tp>(__re);
  903. }
  904. template <class _Tp, __enable_if_t<is_integral<_Tp>::value, int> = 0>
  905. inline _LIBCPP_INLINE_VISIBILITY
  906. typename __libcpp_complex_overload_traits<_Tp>::_ComplexType
  907. proj(_Tp __re)
  908. {
  909. typedef typename __libcpp_complex_overload_traits<_Tp>::_ComplexType _ComplexType;
  910. return _ComplexType(__re);
  911. }
  912. // polar
  913. template<class _Tp>
  914. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  915. polar(const _Tp& __rho, const _Tp& __theta = _Tp())
  916. {
  917. if (std::__constexpr_isnan(__rho) || std::signbit(__rho))
  918. return complex<_Tp>(_Tp(NAN), _Tp(NAN));
  919. if (std::__constexpr_isnan(__theta))
  920. {
  921. if (std::__constexpr_isinf(__rho))
  922. return complex<_Tp>(__rho, __theta);
  923. return complex<_Tp>(__theta, __theta);
  924. }
  925. if (std::__constexpr_isinf(__theta))
  926. {
  927. if (std::__constexpr_isinf(__rho))
  928. return complex<_Tp>(__rho, _Tp(NAN));
  929. return complex<_Tp>(_Tp(NAN), _Tp(NAN));
  930. }
  931. _Tp __x = __rho * std::cos(__theta);
  932. if (std::__constexpr_isnan(__x))
  933. __x = 0;
  934. _Tp __y = __rho * std::sin(__theta);
  935. if (std::__constexpr_isnan(__y))
  936. __y = 0;
  937. return complex<_Tp>(__x, __y);
  938. }
  939. // log
  940. template<class _Tp>
  941. inline _LIBCPP_INLINE_VISIBILITY
  942. complex<_Tp>
  943. log(const complex<_Tp>& __x)
  944. {
  945. return complex<_Tp>(std::log(std::abs(__x)), std::arg(__x));
  946. }
  947. // log10
  948. template<class _Tp>
  949. inline _LIBCPP_INLINE_VISIBILITY
  950. complex<_Tp>
  951. log10(const complex<_Tp>& __x)
  952. {
  953. return std::log(__x) / std::log(_Tp(10));
  954. }
  955. // sqrt
  956. template<class _Tp>
  957. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  958. sqrt(const complex<_Tp>& __x)
  959. {
  960. if (std::__constexpr_isinf(__x.imag()))
  961. return complex<_Tp>(_Tp(INFINITY), __x.imag());
  962. if (std::__constexpr_isinf(__x.real()))
  963. {
  964. if (__x.real() > _Tp(0))
  965. return complex<_Tp>(__x.real(), std::__constexpr_isnan(__x.imag()) ? __x.imag() : std::copysign(_Tp(0), __x.imag()));
  966. return complex<_Tp>(std::__constexpr_isnan(__x.imag()) ? __x.imag() : _Tp(0), std::copysign(__x.real(), __x.imag()));
  967. }
  968. return std::polar(std::sqrt(std::abs(__x)), std::arg(__x) / _Tp(2));
  969. }
  970. // exp
  971. template<class _Tp>
  972. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  973. exp(const complex<_Tp>& __x)
  974. {
  975. _Tp __i = __x.imag();
  976. if (__i == 0) {
  977. return complex<_Tp>(std::exp(__x.real()), std::copysign(_Tp(0), __x.imag()));
  978. }
  979. if (std::__constexpr_isinf(__x.real()))
  980. {
  981. if (__x.real() < _Tp(0))
  982. {
  983. if (!std::__constexpr_isfinite(__i))
  984. __i = _Tp(1);
  985. }
  986. else if (__i == 0 || !std::__constexpr_isfinite(__i))
  987. {
  988. if (std::__constexpr_isinf(__i))
  989. __i = _Tp(NAN);
  990. return complex<_Tp>(__x.real(), __i);
  991. }
  992. }
  993. _Tp __e = std::exp(__x.real());
  994. return complex<_Tp>(__e * std::cos(__i), __e * std::sin(__i));
  995. }
  996. // pow
  997. template<class _Tp>
  998. inline _LIBCPP_INLINE_VISIBILITY
  999. complex<_Tp>
  1000. pow(const complex<_Tp>& __x, const complex<_Tp>& __y)
  1001. {
  1002. return std::exp(__y * std::log(__x));
  1003. }
  1004. template<class _Tp, class _Up>
  1005. inline _LIBCPP_INLINE_VISIBILITY
  1006. complex<typename __promote<_Tp, _Up>::type>
  1007. pow(const complex<_Tp>& __x, const complex<_Up>& __y)
  1008. {
  1009. typedef complex<typename __promote<_Tp, _Up>::type> result_type;
  1010. return _VSTD::pow(result_type(__x), result_type(__y));
  1011. }
  1012. template<class _Tp, class _Up, __enable_if_t<is_arithmetic<_Up>::value, int> = 0>
  1013. inline _LIBCPP_INLINE_VISIBILITY
  1014. complex<typename __promote<_Tp, _Up>::type>
  1015. pow(const complex<_Tp>& __x, const _Up& __y)
  1016. {
  1017. typedef complex<typename __promote<_Tp, _Up>::type> result_type;
  1018. return _VSTD::pow(result_type(__x), result_type(__y));
  1019. }
  1020. template<class _Tp, class _Up, __enable_if_t<is_arithmetic<_Tp>::value, int> = 0>
  1021. inline _LIBCPP_INLINE_VISIBILITY
  1022. complex<typename __promote<_Tp, _Up>::type>
  1023. pow(const _Tp& __x, const complex<_Up>& __y)
  1024. {
  1025. typedef complex<typename __promote<_Tp, _Up>::type> result_type;
  1026. return _VSTD::pow(result_type(__x), result_type(__y));
  1027. }
  1028. // __sqr, computes pow(x, 2)
  1029. template<class _Tp>
  1030. inline _LIBCPP_INLINE_VISIBILITY
  1031. complex<_Tp>
  1032. __sqr(const complex<_Tp>& __x)
  1033. {
  1034. return complex<_Tp>((__x.real() - __x.imag()) * (__x.real() + __x.imag()),
  1035. _Tp(2) * __x.real() * __x.imag());
  1036. }
  1037. // asinh
  1038. template<class _Tp>
  1039. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1040. asinh(const complex<_Tp>& __x)
  1041. {
  1042. const _Tp __pi(atan2(+0., -0.));
  1043. if (std::__constexpr_isinf(__x.real()))
  1044. {
  1045. if (std::__constexpr_isnan(__x.imag()))
  1046. return __x;
  1047. if (std::__constexpr_isinf(__x.imag()))
  1048. return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
  1049. return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
  1050. }
  1051. if (std::__constexpr_isnan(__x.real()))
  1052. {
  1053. if (std::__constexpr_isinf(__x.imag()))
  1054. return complex<_Tp>(__x.imag(), __x.real());
  1055. if (__x.imag() == 0)
  1056. return __x;
  1057. return complex<_Tp>(__x.real(), __x.real());
  1058. }
  1059. if (std::__constexpr_isinf(__x.imag()))
  1060. return complex<_Tp>(std::copysign(__x.imag(), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
  1061. complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) + _Tp(1)));
  1062. return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
  1063. }
  1064. // acosh
  1065. template<class _Tp>
  1066. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1067. acosh(const complex<_Tp>& __x)
  1068. {
  1069. const _Tp __pi(atan2(+0., -0.));
  1070. if (std::__constexpr_isinf(__x.real()))
  1071. {
  1072. if (std::__constexpr_isnan(__x.imag()))
  1073. return complex<_Tp>(std::abs(__x.real()), __x.imag());
  1074. if (std::__constexpr_isinf(__x.imag()))
  1075. {
  1076. if (__x.real() > 0)
  1077. return complex<_Tp>(__x.real(), std::copysign(__pi * _Tp(0.25), __x.imag()));
  1078. else
  1079. return complex<_Tp>(-__x.real(), std::copysign(__pi * _Tp(0.75), __x.imag()));
  1080. }
  1081. if (__x.real() < 0)
  1082. return complex<_Tp>(-__x.real(), std::copysign(__pi, __x.imag()));
  1083. return complex<_Tp>(__x.real(), std::copysign(_Tp(0), __x.imag()));
  1084. }
  1085. if (std::__constexpr_isnan(__x.real()))
  1086. {
  1087. if (std::__constexpr_isinf(__x.imag()))
  1088. return complex<_Tp>(std::abs(__x.imag()), __x.real());
  1089. return complex<_Tp>(__x.real(), __x.real());
  1090. }
  1091. if (std::__constexpr_isinf(__x.imag()))
  1092. return complex<_Tp>(std::abs(__x.imag()), std::copysign(__pi/_Tp(2), __x.imag()));
  1093. complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
  1094. return complex<_Tp>(std::copysign(__z.real(), _Tp(0)), std::copysign(__z.imag(), __x.imag()));
  1095. }
  1096. // atanh
  1097. template<class _Tp>
  1098. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1099. atanh(const complex<_Tp>& __x)
  1100. {
  1101. const _Tp __pi(atan2(+0., -0.));
  1102. if (std::__constexpr_isinf(__x.imag()))
  1103. {
  1104. return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
  1105. }
  1106. if (std::__constexpr_isnan(__x.imag()))
  1107. {
  1108. if (std::__constexpr_isinf(__x.real()) || __x.real() == 0)
  1109. return complex<_Tp>(std::copysign(_Tp(0), __x.real()), __x.imag());
  1110. return complex<_Tp>(__x.imag(), __x.imag());
  1111. }
  1112. if (std::__constexpr_isnan(__x.real()))
  1113. {
  1114. return complex<_Tp>(__x.real(), __x.real());
  1115. }
  1116. if (std::__constexpr_isinf(__x.real()))
  1117. {
  1118. return complex<_Tp>(std::copysign(_Tp(0), __x.real()), std::copysign(__pi/_Tp(2), __x.imag()));
  1119. }
  1120. if (std::abs(__x.real()) == _Tp(1) && __x.imag() == _Tp(0))
  1121. {
  1122. return complex<_Tp>(std::copysign(_Tp(INFINITY), __x.real()), std::copysign(_Tp(0), __x.imag()));
  1123. }
  1124. complex<_Tp> __z = std::log((_Tp(1) + __x) / (_Tp(1) - __x)) / _Tp(2);
  1125. return complex<_Tp>(std::copysign(__z.real(), __x.real()), std::copysign(__z.imag(), __x.imag()));
  1126. }
  1127. // sinh
  1128. template<class _Tp>
  1129. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1130. sinh(const complex<_Tp>& __x)
  1131. {
  1132. if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag()))
  1133. return complex<_Tp>(__x.real(), _Tp(NAN));
  1134. if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag()))
  1135. return complex<_Tp>(__x.real(), _Tp(NAN));
  1136. if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real()))
  1137. return __x;
  1138. return complex<_Tp>(std::sinh(__x.real()) * std::cos(__x.imag()), std::cosh(__x.real()) * std::sin(__x.imag()));
  1139. }
  1140. // cosh
  1141. template<class _Tp>
  1142. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1143. cosh(const complex<_Tp>& __x)
  1144. {
  1145. if (std::__constexpr_isinf(__x.real()) && !std::__constexpr_isfinite(__x.imag()))
  1146. return complex<_Tp>(std::abs(__x.real()), _Tp(NAN));
  1147. if (__x.real() == 0 && !std::__constexpr_isfinite(__x.imag()))
  1148. return complex<_Tp>(_Tp(NAN), __x.real());
  1149. if (__x.real() == 0 && __x.imag() == 0)
  1150. return complex<_Tp>(_Tp(1), __x.imag());
  1151. if (__x.imag() == 0 && !std::__constexpr_isfinite(__x.real()))
  1152. return complex<_Tp>(std::abs(__x.real()), __x.imag());
  1153. return complex<_Tp>(std::cosh(__x.real()) * std::cos(__x.imag()), std::sinh(__x.real()) * std::sin(__x.imag()));
  1154. }
  1155. // tanh
  1156. template<class _Tp>
  1157. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1158. tanh(const complex<_Tp>& __x)
  1159. {
  1160. if (std::__constexpr_isinf(__x.real()))
  1161. {
  1162. if (!std::__constexpr_isfinite(__x.imag()))
  1163. return complex<_Tp>(std::copysign(_Tp(1), __x.real()), _Tp(0));
  1164. return complex<_Tp>(std::copysign(_Tp(1), __x.real()), std::copysign(_Tp(0), std::sin(_Tp(2) * __x.imag())));
  1165. }
  1166. if (std::__constexpr_isnan(__x.real()) && __x.imag() == 0)
  1167. return __x;
  1168. _Tp __2r(_Tp(2) * __x.real());
  1169. _Tp __2i(_Tp(2) * __x.imag());
  1170. _Tp __d(std::cosh(__2r) + std::cos(__2i));
  1171. _Tp __2rsh(std::sinh(__2r));
  1172. if (std::__constexpr_isinf(__2rsh) && std::__constexpr_isinf(__d))
  1173. return complex<_Tp>(__2rsh > _Tp(0) ? _Tp(1) : _Tp(-1),
  1174. __2i > _Tp(0) ? _Tp(0) : _Tp(-0.));
  1175. return complex<_Tp>(__2rsh/__d, std::sin(__2i)/__d);
  1176. }
  1177. // asin
  1178. template<class _Tp>
  1179. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1180. asin(const complex<_Tp>& __x)
  1181. {
  1182. complex<_Tp> __z = std::asinh(complex<_Tp>(-__x.imag(), __x.real()));
  1183. return complex<_Tp>(__z.imag(), -__z.real());
  1184. }
  1185. // acos
  1186. template<class _Tp>
  1187. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1188. acos(const complex<_Tp>& __x)
  1189. {
  1190. const _Tp __pi(atan2(+0., -0.));
  1191. if (std::__constexpr_isinf(__x.real()))
  1192. {
  1193. if (std::__constexpr_isnan(__x.imag()))
  1194. return complex<_Tp>(__x.imag(), __x.real());
  1195. if (std::__constexpr_isinf(__x.imag()))
  1196. {
  1197. if (__x.real() < _Tp(0))
  1198. return complex<_Tp>(_Tp(0.75) * __pi, -__x.imag());
  1199. return complex<_Tp>(_Tp(0.25) * __pi, -__x.imag());
  1200. }
  1201. if (__x.real() < _Tp(0))
  1202. return complex<_Tp>(__pi, std::signbit(__x.imag()) ? -__x.real() : __x.real());
  1203. return complex<_Tp>(_Tp(0), std::signbit(__x.imag()) ? __x.real() : -__x.real());
  1204. }
  1205. if (std::__constexpr_isnan(__x.real()))
  1206. {
  1207. if (std::__constexpr_isinf(__x.imag()))
  1208. return complex<_Tp>(__x.real(), -__x.imag());
  1209. return complex<_Tp>(__x.real(), __x.real());
  1210. }
  1211. if (std::__constexpr_isinf(__x.imag()))
  1212. return complex<_Tp>(__pi/_Tp(2), -__x.imag());
  1213. // Somehow isnan can be a macro, so we use __constexpr_isnan
  1214. if (__x.real() == 0 && (__x.imag() == 0 || std::__constexpr_isnan(__x.imag())))
  1215. return complex<_Tp>(__pi/_Tp(2), -__x.imag());
  1216. complex<_Tp> __z = std::log(__x + std::sqrt(std::__sqr(__x) - _Tp(1)));
  1217. if (std::signbit(__x.imag()))
  1218. return complex<_Tp>(std::abs(__z.imag()), std::abs(__z.real()));
  1219. return complex<_Tp>(std::abs(__z.imag()), -std::abs(__z.real()));
  1220. }
  1221. // atan
  1222. template<class _Tp>
  1223. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1224. atan(const complex<_Tp>& __x)
  1225. {
  1226. complex<_Tp> __z = std::atanh(complex<_Tp>(-__x.imag(), __x.real()));
  1227. return complex<_Tp>(__z.imag(), -__z.real());
  1228. }
  1229. // sin
  1230. template<class _Tp>
  1231. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1232. sin(const complex<_Tp>& __x)
  1233. {
  1234. complex<_Tp> __z = std::sinh(complex<_Tp>(-__x.imag(), __x.real()));
  1235. return complex<_Tp>(__z.imag(), -__z.real());
  1236. }
  1237. // cos
  1238. template<class _Tp>
  1239. inline _LIBCPP_INLINE_VISIBILITY
  1240. complex<_Tp>
  1241. cos(const complex<_Tp>& __x)
  1242. {
  1243. return std::cosh(complex<_Tp>(-__x.imag(), __x.real()));
  1244. }
  1245. // tan
  1246. template<class _Tp>
  1247. _LIBCPP_HIDE_FROM_ABI complex<_Tp>
  1248. tan(const complex<_Tp>& __x)
  1249. {
  1250. complex<_Tp> __z = std::tanh(complex<_Tp>(-__x.imag(), __x.real()));
  1251. return complex<_Tp>(__z.imag(), -__z.real());
  1252. }
  1253. #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
  1254. template<class _Tp, class _CharT, class _Traits>
  1255. _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
  1256. operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
  1257. {
  1258. if (__is.good())
  1259. {
  1260. std::ws(__is);
  1261. if (__is.peek() == _CharT('('))
  1262. {
  1263. __is.get();
  1264. _Tp __r;
  1265. __is >> __r;
  1266. if (!__is.fail())
  1267. {
  1268. std::ws(__is);
  1269. _CharT __c = __is.peek();
  1270. if (__c == _CharT(','))
  1271. {
  1272. __is.get();
  1273. _Tp __i;
  1274. __is >> __i;
  1275. if (!__is.fail())
  1276. {
  1277. std::ws(__is);
  1278. __c = __is.peek();
  1279. if (__c == _CharT(')'))
  1280. {
  1281. __is.get();
  1282. __x = complex<_Tp>(__r, __i);
  1283. }
  1284. else
  1285. __is.setstate(__is.failbit);
  1286. }
  1287. else
  1288. __is.setstate(__is.failbit);
  1289. }
  1290. else if (__c == _CharT(')'))
  1291. {
  1292. __is.get();
  1293. __x = complex<_Tp>(__r, _Tp(0));
  1294. }
  1295. else
  1296. __is.setstate(__is.failbit);
  1297. }
  1298. else
  1299. __is.setstate(__is.failbit);
  1300. }
  1301. else
  1302. {
  1303. _Tp __r;
  1304. __is >> __r;
  1305. if (!__is.fail())
  1306. __x = complex<_Tp>(__r, _Tp(0));
  1307. else
  1308. __is.setstate(__is.failbit);
  1309. }
  1310. }
  1311. else
  1312. __is.setstate(__is.failbit);
  1313. return __is;
  1314. }
  1315. template<class _Tp, class _CharT, class _Traits>
  1316. _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
  1317. operator<<(basic_ostream<_CharT, _Traits>& __os, const complex<_Tp>& __x)
  1318. {
  1319. basic_ostringstream<_CharT, _Traits> __s;
  1320. __s.flags(__os.flags());
  1321. __s.imbue(__os.getloc());
  1322. __s.precision(__os.precision());
  1323. __s << '(' << __x.real() << ',' << __x.imag() << ')';
  1324. return __os << __s.str();
  1325. }
  1326. #endif // !_LIBCPP_HAS_NO_LOCALIZATION
  1327. #if _LIBCPP_STD_VER >= 14
  1328. // Literal suffix for complex number literals [complex.literals]
  1329. inline namespace literals
  1330. {
  1331. inline namespace complex_literals
  1332. {
  1333. _LIBCPP_HIDE_FROM_ABI constexpr complex<long double> operator""il(long double __im)
  1334. {
  1335. return { 0.0l, __im };
  1336. }
  1337. _LIBCPP_HIDE_FROM_ABI constexpr complex<long double> operator""il(unsigned long long __im)
  1338. {
  1339. return { 0.0l, static_cast<long double>(__im) };
  1340. }
  1341. _LIBCPP_HIDE_FROM_ABI constexpr complex<double> operator""i(long double __im)
  1342. {
  1343. return { 0.0, static_cast<double>(__im) };
  1344. }
  1345. _LIBCPP_HIDE_FROM_ABI constexpr complex<double> operator""i(unsigned long long __im)
  1346. {
  1347. return { 0.0, static_cast<double>(__im) };
  1348. }
  1349. #if !defined(__CUDACC__)
  1350. _LIBCPP_HIDE_FROM_ABI constexpr complex<float> operator""if(long double __im)
  1351. {
  1352. return { 0.0f, static_cast<float>(__im) };
  1353. }
  1354. _LIBCPP_HIDE_FROM_ABI constexpr complex<float> operator""if(unsigned long long __im)
  1355. {
  1356. return { 0.0f, static_cast<float>(__im) };
  1357. }
  1358. #endif
  1359. } // namespace complex_literals
  1360. } // namespace literals
  1361. #endif
  1362. _LIBCPP_END_NAMESPACE_STD
  1363. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  1364. # include <type_traits>
  1365. #endif
  1366. #endif // _LIBCPP_COMPLEX