Use the complex library in the c99 Standard

Source: Internet
Author: User

The ISO/iec9899: 1999 standard (commonly known as c99) introduces a complex database that is very useful for the signal processing field and is also very convenient to use. Enable c99, gnu99, C11, or gnu11 for your c language compiler and include <complex. h> in the source file.

The c99 standard introduces two keywords used to describe the plural: _ complex and _ imaginary. _ Complex is used in combination with float and double to represent a plural number. Since the gcc4.6 version I used does not yet support the _ imaginary keyword (I don't think it will be supported in my life), I will not introduce it here. _ Imaginary indicates the imaginary part of a plural number. I estimate the number defined by _ imaginary as a pure virtual number.

If _ complex is used in combination with a float, both the number part and the virtual part are in the float type. If you want to use a constant to represent the pure virtual number of a single precision floating point, you can actually write it like this: 1.0if, here I and F can be either the size or lower case, therefore, writing to 1.0if is no problem, and the compiler will not be stupid enough to use this if as a condition to judge the keyword.

If _ complex is used in combination with a double type, both the number part and the virtual number part are of the double type. If you want to use a constant to represent the pure virtual number of a double-precision floating point, you can write it as follows: 1.0i.

BelowCodeFragment. You can run it:

# Include <math. h> # include <stdio. h> # include <complex. h> int main (void) {float _ complex F =-1.0f; F = csqrtf (f) + 1.0if; printf ("the complex number is: % G + I % G \ n ", crealf (F), cimagf (f); double _ complex d = cexp (2.0i * m_pi ); printf ("e ^ (i2pi) = % F + I % F \ n", Creal (D), cimag (d ));}

The Creal (double _ complex) in the library function is to take the real part of a complex number; cimag (double _ complex) is to take the virtual part of a complex number.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.