473,466 Members | 1,346 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Implementation of Complex Numbers in C

94 New Member
In this article, I'll show you a way to implement complex numbers with the C programming language. Please note there is no warranty when it comes to using the code here provided.

A complex number is a number, say, z, such that z = realPart + i * imaginaryPart, where i is the imaginary unit, sometimes denoted by j. Also, i * i = -1, and this is important when finding the product/division of two complex numbers.

Naturally, we can implement a complex number as a struct:

Expand|Select|Wrap|Line Numbers
  1. /* Definition of a Complex number: */
  2. typedef struct complex {
  3.     double realPart;
  4.     double imaginaryPart;
  5. } Complex;
We can separate the interface from the implementation as follows.

First, we have the header Complex.h, which defines the Complex structure and contains the prototypes for the functions that manipulate one or more Complex numbers:

Expand|Select|Wrap|Line Numbers
  1. /* Complex.h */
  2.  
  3. #ifndef COMPLEX_H
  4. #define COMPLEX_H
  5.  
  6. /* Definition of a Complex number: */
  7. typedef struct complex {
  8.     double realPart;
  9.     double imaginaryPart;
  10. } Complex;
  11.  
  12. /* Prototypes for functions to manipulate Complex number(s) (these functions are implemented in file Complex.c): */
  13. Complex addition( const Complex *pointerToComplexNumber1, const Complex *pointerToComplexNumber2 );
  14. Complex subtraction( const Complex *pointerToComplexNumber1, const Complex *pointerToComplexNumber2 );
  15. Complex multiplication( const Complex *pointerToComplexNumber1, const Complex *pointerToComplexNumber2 );
  16. Complex division(  const Complex *pointerToComplexNumber1, const Complex *pointerToComplexNumber2 );
  17. void print( const Complex *pointerToComplexNumber );
  18.  
  19. #endif
The file Complex.c implements the functions above:

Expand|Select|Wrap|Line Numbers
  1. /* Complex.c */
  2.  
  3. #include <stdio.h>
  4. #include "Complex.h"
  5.  
  6. /* Adds two Complex numbers, returning a new one, without modifying the ones received: */
  7. Complex addition( const Complex *pointerToComplexNumber1, const Complex *pointerToComplexNumber2 )
  8. {
  9.     Complex complexNumber3;
  10.  
  11.     complexNumber3.realPart = pointerToComplexNumber1->realPart + pointerToComplexNumber2->realPart;
  12.     complexNumber3.imaginaryPart = pointerToComplexNumber1->imaginaryPart + pointerToComplexNumber2->imaginaryPart;
  13.  
  14.     return complexNumber3;
  15. }
  16.  
  17. /* Subtracts two Complex numbers, returning a new one, without modifying the ones received: */
  18. Complex subtraction( const Complex *pointerToComplexNumber1, const Complex *pointerToComplexNumber2 )
  19. {
  20.     Complex complexNumber3;
  21.  
  22.     complexNumber3.realPart = pointerToComplexNumber1->realPart - pointerToComplexNumber2->realPart;
  23.     complexNumber3.imaginaryPart = pointerToComplexNumber1->imaginaryPart - pointerToComplexNumber2->imaginaryPart;
  24.  
  25.     return complexNumber3;
  26. }
  27.  
  28. /* Multiplies two Complex numbers, returning a new one, without modifying the ones received: */
  29. Complex multiplication( const Complex *pointerToComplexNumber1, const Complex *pointerToComplexNumber2 )
  30. {
  31.     Complex complexNumber3;
  32.  
  33.     complexNumber3.realPart = pointerToComplexNumber1->realPart * pointerToComplexNumber2->realPart -
  34.         pointerToComplexNumber1->imaginaryPart * pointerToComplexNumber2->imaginaryPart;
  35.  
  36.     complexNumber3.imaginaryPart = pointerToComplexNumber1->realPart * pointerToComplexNumber2->imaginaryPart +
  37.         pointerToComplexNumber1->imaginaryPart * pointerToComplexNumber2->realPart;
  38.  
  39.     return complexNumber3;
  40. }
  41.  
  42. /* Divides two Complex numbers, returning a new one, without modifying the ones received: */
  43. Complex division(  const Complex *pointerToComplexNumber1, const Complex *pointerToComplexNumber2 )
  44. {
  45.     Complex complexNumber3;
  46.     double commonDenominator = pointerToComplexNumber2->realPart * pointerToComplexNumber2->realPart +
  47.         pointerToComplexNumber2->imaginaryPart * pointerToComplexNumber2->imaginaryPart;
  48.  
  49.     complexNumber3.realPart = ( pointerToComplexNumber1->realPart * pointerToComplexNumber2->realPart +
  50.         pointerToComplexNumber1->imaginaryPart * pointerToComplexNumber2->imaginaryPart ) / commonDenominator;
  51.  
  52.     complexNumber3.imaginaryPart = ( pointerToComplexNumber1->imaginaryPart * pointerToComplexNumber2->realPart -
  53.         pointerToComplexNumber1->realPart * pointerToComplexNumber2->imaginaryPart ) / commonDenominator;
  54.  
  55.     return complexNumber3;
  56. }
  57.  
  58. /* Prints a Complex number in the form ( realPart ) + i( imaginaryPart ), without modifying it: */
  59. void print( const Complex *pointerToComplexNumber )
  60. {
  61.     printf( "( %.4f ) + i( %.4f )", pointerToComplexNumber->realPart, pointerToComplexNumber->imaginaryPart );
  62. }
Finally, we have a file called ComplexTest.c, which tests files Complex.c and Complex.h inside the main function, which is the starting point of our C program:

Expand|Select|Wrap|Line Numbers
  1. /* ComplexTest.c */
  2.  
  3. #include <stdio.h>
  4. #include "Complex.h"
  5.  
  6. int main( void )
  7. {
  8.     Complex number1;
  9.     Complex number2;
  10.     Complex sumOfNumber1WithNumber2;
  11.     Complex subtractionOfNumber2FromNumber1;
  12.     Complex multiplicationOfNumber1WithNumber2;
  13.     Complex divisionOfNumber1PerNumber2;
  14.  
  15.     number1.realPart = 1.2343;
  16.     number1.imaginaryPart = 4.7621;
  17.  
  18.     number2.realPart = -3.213;
  19.     number2.imaginaryPart = -9.8;
  20.  
  21.     sumOfNumber1WithNumber2 = addition( &number1, &number2 );
  22.     subtractionOfNumber2FromNumber1 = subtraction( &number1, &number2 );
  23.     multiplicationOfNumber1WithNumber2 = multiplication( &number1, &number2 );
  24.     divisionOfNumber1PerNumber2 = division( &number1, &number2 );
  25.  
  26.     printf( "Number1 = " );
  27.     print( &number1 );
  28.     printf( "\nNumber2 = " );
  29.     print( &number2 );
  30.     printf( "\nNumber1 + Number2 = " );
  31.     print( &sumOfNumber1WithNumber2 );
  32.     printf( "\nNumber1 - Number2 = " );
  33.     print( &subtractionOfNumber2FromNumber1 );
  34.     printf( "\nNumber1 * Number2 = " );
  35.     print( &multiplicationOfNumber1WithNumber2 );
  36.     printf( "\nNumber1 / Number2 = " );
  37.     print( &divisionOfNumber1PerNumber2 );
  38.     putchar( '\n' );
  39.  
  40.     return 0;
  41. }
To compile this program with Visual Studio 2013, from the command line, open the Developer Command Prompt for VS2013, go to the directory where the three files above are, and enter the command

cl ComplexTest.c Complex.c

Then, run the program with the command

ComplexTest
May 2 '14 #1
0 7818

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: Blair | last post by:
could someone PLEASE tell me why this doesn't work... ----------------------------------------- #include <complex> using namespace std; typedef complex<long double> cld; void main() { cld...
34
by: Pmb | last post by:
I've been working on creating a Complex class for my own learning purpose (learn through doing etc.). I'm once again puzzled about something. I can't figure out how to overload the assignment...
3
by: Russ | last post by:
I'd like to get output formatting for my own classes that mimics the built-in output formatting. For example, >>> x = 4.54 >>> print "%4.2f" % x 4.54 In other words, if I substitute a class...
7
by: john134 | last post by:
Complex Complex::operator+( const Complex &operand2 ) const { return Complex( real + operand2.real, imaginary + operand2.imaginary ); } this is my addition operator for 2 complex numbers. e.g....
17
by: Jon Harrop | last post by:
I can't seem to find an implementation of complex numbers in the C# standard library. Is there one? -- Dr Jon D Harrop, Flying Frog Consultancy The F#.NET Journal...
27
by: David Marsh | last post by:
I understand that C99 supports a complex type and complex arithmetic. There is nothing about it in the FAQ and online searches turned up very little except synopses. Can anyone point me toward...
25
by: jacob navia | last post by:
The C99 standard forgot to define the printf equivalent for complex numbers Since I am revising the lcc-win implementation of complex numbers I decided to fill this hole with "Z" for...
9
by: void main | last post by:
I'm rather new to complex numbers in C and was wondering, how do I initialize a complex variable properly if the imaginary part is 0. I tried -------- #include <complex.h> float complex c...
16
by: skip | last post by:
The thread on sorting in Python 3 got me to thinking. How could I sort a list of complex numbers using key? As expected: Traceback (most recent call last): File "<stdin>", line 1, in...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.