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