Complex-C basics in C Language

Source: Internet
Author: User

Complex Number: For details about the complex number, see: complex number algebra C supports mathematical calculation of the complex number. The complex number Z can be expressed as: Z = x + y * I in Cartesian coordinates; x and y are real numbers, and I is a virtual number unit. Number x is called the real part, and number y is the virtual part. In C, a plural number is the real and virtual parts of the floating point type. Both have the same type, whether float, double, or long double. Float _ complex: the actual virtual files are all floatdouble _ complex: the actual virtual files are all doublelong double _ complex: the actual virtual files are all long double. If the c source file contains the header file complex. h, complex. h defines complex and I macros. The macro defines complex as synonymous with the keyword _ complex. We can replace _ complex with complex. the following is a simple example. Run the detailed code In debian 7 (32bit): Copy code 1/* 2 * Title: Complex Numbers 3 * Description: work with complex numbers in c 4 * Author: Eric. lee 5*6 */7 # include <stdio. h> 8 # include <complex. h> 9 10 # define Get_Array_Length (tempArray) (sizeof (tempArray)/sizeof (tempArray [0]) 11 12 void GetResult (char operate, double complex x, double complex y) 13 {14 double complex result = 0 + 0 * I; 15 switch (operate) 16 {17 case '+': 18 result = x + y; 19 break; 20 case '-': 21 result = x-y; 22 break; 23 case '*': 24 result = x * y; 25 break; 26 case '/': 27 result = x/y; 28 break; 29 default: 30 break; 31} 32 printf ("double complex x % c double complex y = %. 2f + %. 2fi \ n ", operate, creal (result), cimag (result); 33 34} 35 36 int main () 37 {38 double complex x = 10.0 + 15.0 * I; 39 double complex y = 20.0-5.0 * I; 40 41 printf ("working with complex number: \ n"); 42 printf ("Starting values: x = %. 2f + %. 2fi \ ty = %. 2f + %. 2fi \ n ", creal (x), cimag (x), creal (y), cimag (y); 43 char operates [] = {'+ ', '-', '*', '/'}; 44 char * op = operates; 45 int I = 0; 46 int operateLength = Get_Array_Length (operates ); 47 for (I = 0; I <= operateLength-1; I ++) 48 {49 GetResult (* (op ++), x, y); 50} 51 52 return 0; 53} copy code creal (x): Obtain the real part of the complex number (for double). If crealf (x) is used for float, use creall (x) for long double) cimag (x): Obtain the imaginary part of the plural number (for double). If float is used, use crealf (x). If long double is used, use creall (x) additionally, cos (), exp (), and sqrt () correspond to the plural methods, such as ccos (), cexp (), and csqrt ()

Related Article

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.