Share via


complex<double>

Menjelaskan objek yang menyimpan sepasang objek yang diurutkan dari kedua jenis double, yang pertama mewakili bagian nyata dari bilangan kompleks dan yang kedua mewakili bagian imajiner.

Sintaks

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
};

Parameter

RealVal
Nilai jenis double untuk bagian nyata dari bilangan kompleks yang sedang dibangun.

ImagVal
Nilai jenis double untuk bagian imajiner dari bilangan kompleks yang sedang dibangun.

complexNum
Jumlah kompleks jenis float atau jenis long double yang bagian nyata dan imajinernya digunakan untuk menginisialisasi jumlah kompleks jenis double yang sedang dibangun.

Tampilkan Nilai

Jumlah kompleks jenis double.

Keterangan

Spesialisasi eksplisit dari kompleks templat kelas ke kelas jenis double yang kompleks berbeda dari templat kelas hanya di konstruktor yang ditentukannya. Konversi dari float ke double diizinkan untuk menjadi implisit, tetapi konversi dari long double ke double harus .explicit Penggunaan explicit aturan keluar dari inisiasi dengan konversi jenis menggunakan sintaks penugasan.

Untuk informasi selengkapnya tentang templat complexkelas , lihat Kelas yang kompleks. Untuk daftar anggota templat complexkelas , lihat .

Contoh

// 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.
*/

Persyaratan

Header: <kompleks>

Namespace: std

Baca juga

Kelas kompleks
Keamanan utas di Pustaka Standar C++