Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a complex data type and an Imaginary data type in C++?

I know what imaginary and complex numbers are in the math world but what about in C++, what are data types of complex and imaginary. In addition, I see data types such as _Imaginary and _Complex. What is the difference and what are complex and imaginary data types?

like image 270
someguy Avatar asked Nov 18 '25 14:11

someguy


1 Answers

_Imaginary and _Complex are keywords in the C99 language standard used for defining imaginary and complex floating-point variable types; they are not part of the C++ language. They are not data types in and of themselves -- they modify the float, double and long double types. For example:

float _Imaginary x;      // imaginary 32-bit number
double _Complex y;       // complex 64-bit number
long double _Complex z;  // complex 80-bit number

_Imaginary values are mostly equivalent to regular real values, except when you add a real with an imaginary value, you get a _Complex value.

The header file <complex.h> defines the macros imaginary as _Imaginary and complex as _Complex, as well as I as either _Complex_I or _Imaginary_I (the imaginary unit). This is so that legacy C programs can use complex and imaginary as identifiers; new programs should use complex and imaginary instead of _Complex and _Imaginary. Note that identifiers beginning with an underscore followed by an uppercase letter are reserved by the implementation, so legacy code should never use _Complex or _Imaginary.

C++, on the other hand, does not use this, and instead has the templated types std::complex<float>, std::complex<double>, and std::complex<long double> for dealing with complex numbers. Those classes function very similarly to the C99 types but are non-interchangeable.

like image 110
Adam Rosenfield Avatar answered Nov 20 '25 03:11

Adam Rosenfield



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!