分享方式:


complex<double>

描述可儲存皆為 double 類型之物件的有序對,第一個代表複數的實部,而第二個代表虛部。

語法

template <>
class complex<double> {
public:
    constexpr complex(
    double RealVal = 0,
    double ImagVal = 0);

constexpr complex(const complex<double>& complexNum);

constexpr explicit complex(const complex<long double>& complexNum);
// rest same as class template complex
};

參數

RealVal
要建構之複數實際部分的 型 double 別值。

ImagVal
用於建構中複數虛部的 double 類型值。

complexNum
型別或 long doublefloat 別的複數,其實數和虛數部分用來初始化所建構之型 double 別的複數。

傳回值

double 類型的複數。

備註

類別樣板複雜至類型複雜類別的 double 明確特製化,只與類別範本在所定義的建構函式中不同。 從 轉換成 float 是隱含的,但從 long doubledouble 轉換成 必須是 explicitdouble 使用 explicit 指派語法排除初始與型別轉換。

如需類別範本 complex 的詳細資訊,請參閱 複雜類別 。 如需類別範本 complex 的成員清單,請參閱 。

範例

// complex_comp_dbl.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>

int main( )
{
   using namespace std;
   double pi = 3.14159265359;

   // The first constructor specifies real & imaginary parts
   complex <double> c1 ( 4.0 , 5.0 );
   cout << "Specifying initial real & imaginary parts,\n"
        << "as type double gives c1 = " << c1 << endl;

   // The second constructor initializes values of the real &
   // imaginary parts using those of complex number of type float
   complex <float> c2float ( 4.0 , 5.0 );
   complex <double> c2double ( c2float );
   cout << "Implicit conversion from type float to type double,"
        << endl << "gives c2double = " << c2double << endl;

   // The third constructor initializes values of the real &
   // imaginary parts using those of a complex number
   // of type long double
   complex <long double> c3longdouble ( 4.0 , 5.0 );
   complex <double> c3double ( c3longdouble );
   cout << "Explicit conversion from type float to type double,"
        << endl << "gives c3longdouble = " << c3longdouble << endl;

   // The modulus and argument of a complex number can be recovered
   double absc3 = abs ( c3longdouble );
   double argc3 = arg ( c3longdouble );
   cout << "The modulus of c3 is recovered from c3 using: abs ( c3 ) = "
        << absc3 << endl;
   cout << "Argument of c3 is recovered from c3 using:" << endl
        << "arg ( c3 ) = " << argc3 << " radians, which is "
        << argc3 * 180 / pi << " degrees." << endl;
}
/* Output:
Specifying initial real & imaginary parts,
as type double gives c1 = (4,5)
Implicit conversion from type float to type double,
gives c2double = (4,5)
Explicit conversion from type float to type double,
gives c3longdouble = (4,5)
The modulus of c3 is recovered from c3 using: abs ( c3 ) = 6.40312
Argument of c3 is recovered from c3 using:
arg ( c3 ) = 0.896055 radians, which is 51.3402 degrees.
*/

需求

標頭 : < 複雜>

命名空間:std

另請參閱

complex 類別
C++ 標準程式庫中的執行緒安全