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?
_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.
These are actually C datatypes, not C++ datatypes.
When creating new C standards, the committee avoids using new keywords. So, they use reserved identifiers, that begin with an underscore. Then, the standard may also provide a header file that defines a nicer macro (complex, instead of _Complex).
Since C++ is newer that C, it has included the complex datatype since it was first standardized, and doesn't need to resort to the underscored version.
not this?
and from another source:
_Imaginary
Revision as of 11:59, 3 December 2010 by PyBot (Talk | contribs)
Category
Reserved Words
Syntax
_Imaginary
Description
Reserved for future use.
Warning: C++Builder does not implement _Imaginary but does treat it as a keyword when the -An compiler flag is set. Do not use _Imaginary as an identifier.
There is a header <complex>
which defines a convenient complex number data type for you, all part of the standard library. Here is the documentation.
In C++, there is a header called <complex>
which defines a template class double<>
, which can be instantiated for float
, double
, and long double
. This allows you to define complex numbers and do the usual arithmetic operations and some functions.
There is no imaginary
class or type in standard C++, just complex numbers with a real part of 0.0. Any imaginary
class would be some sort of vendor extension. (Personally, I don't see that an imaginary
class would be of much use. It's not even closed under multiplication.)
Any name that begins with an underscore and is followed by a capital letter is specifically reserved for use by the implementation, and so there isn't anything standard C++ has to say about them. Any such type would be implementation-specific, and you haven't specified an implementation. Moreover, it's probably not something you should be using yourself.
精彩评论