Click here to Skip to main content
13,297,547 members (45,731 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 3 Jun 2007

A C# class for complex numbers

, 3 Jul 2007
Rate this:
Please Sign up or sign in to vote.
Implementation of the most common functions of complex numbers.


Here you go: a simple but mathematically rigorous implementation of complex numbers in one small C# library. No problem in square rooting negative numbers anymore!


  • Absolute value
  • Addition
  • Argument
  • Conjugation
  • Cosine
  • Exponential function
  • Exponentiation
  • Division
  • Hyperbolic functions (Sinh, Cosh, Tanh, Coth, Sech, Csch)
  • Logarithm
  • Multiplication
  • Sine
  • Square root
  • Subtraction

Using the code

Either add a reference to CompLib.dll to your project, or directly use the class Complex.cs within your project.

The actual usage is intuitive:

Complex I = Complex.I; // imaginary unit
Complex a = new Complex(1, 3); // inits a = 1+3i
Complex a2 = 1 + 3 * I; // a equals a2

Complex z = Complex.Pow((Complex.Sin(1/(1+I))), 1/3);

Points of interest

One more thing: Complex logarithm is not a unique operation; the main value is computed as is common in the CAS world. E.g., the equation z^4 = -1 has four complex solutions, but only one is returned when trying "z = Complex.Sqrt(Complex.Sqrt(-1));" (as does Maple, for instance). This is due to the computation of the exponentiation:

Pow(a,b) := Exp(b * Log(a))


Coming soon

  • init complex number with format string such as "3+4i" using regex.

Update July 3, 2007 #2

  • Major bug in Arg() fixed (thanks Petr Stanislav!); this affects Log(), Pow(), and Sqrt().

Update July 3, 2007

  • Added hyperbolic functions.

Update June 10, 2007

  • Replaced ^-operator with "public static Complex Pow", similar to Math.Pow.

Update June 7, 2007

  • Added Zero and One as constants (e.g., use "Complex z = Complex.One;" instead of "Complex z = new Complex(1)").
  • Major bug of division operation removed (using a/b = a*Conj(b)*(1/(Abs(b)*Abs(b)) now).
  • ToString method bug fixed.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Germany Germany
No Biography provided

You may also be interested in...


Comments and Discussions

Questioncode licence Pin
enrico.deg10-Jun-10 23:53
memberenrico.deg10-Jun-10 23:53 
Generalstruct instead of class Pin
Member 190663510-Jun-08 2:17
memberMember 190663510-Jun-08 2:17 
GeneralRe: struct instead of class Pin
PIEBALDconsult2-Apr-09 13:56
memberPIEBALDconsult2-Apr-09 13:56 
General7-Zip will extract Pin
Dom Rositas20-Jun-07 17:22
memberDom Rositas20-Jun-07 17:22 
GeneralRe: 7-Zip will extract [modified] Pin
hanzzoid3-Jul-07 3:19
memberhanzzoid3-Jul-07 3:19 
GeneralWinZip unknown compression type Pin
Dom Rositas20-Jun-07 8:49
memberDom Rositas20-Jun-07 8:49 
GeneralCannot extract files from ZIP archive Pin
Tom14-Jun-07 4:54
memberTom14-Jun-07 4:54 
GeneralRe: Cannot extract files from ZIP archive Pin
Tom118-Jun-07 6:39
memberTom118-Jun-07 6:39 
GeneralExponentiation ALWAYS binds more strongly than multiplication. Pin
sherifffruitfly3-Jun-07 12:08
membersherifffruitfly3-Jun-07 12:08 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pin
Keith Rule3-Jun-07 21:16
memberKeith Rule3-Jun-07 21:16 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pin
peterchen3-Jun-07 23:20
memberpeterchen3-Jun-07 23:20 
I think sherifffruitfly is right, and using the XOR-Operator for pow is dangerous.

The whole point of operator overloading is to make expressions more intuitive, and close to the "problem domain". With overloading pow, you seem to extend that, but introduce a difference that may be sublte but devastating (depending on the context it is used).

It would be OK if all other operators would generally evaluate from left to right and parantheses for mathematically correct evaluation would always be required.

In C++, this already has been discussed to death. Virtually all of the discussion, however, is the inability to overload a exponentiation operator. So C# could define a ** operator, and make millions happy Wink | ;)

We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!|FoldWithUs! | sighist

GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pin
hanzzoid3-Jul-07 3:15
memberhanzzoid3-Jul-07 3:15 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pin
PIEBALDconsult2-Apr-09 13:59
memberPIEBALDconsult2-Apr-09 13:59 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pin
sherifffruitfly2-Apr-09 14:22
membersherifffruitfly2-Apr-09 14:22 
GeneralRe: Exponentiation ALWAYS binds more strongly than multiplication. Pin
PIEBALDconsult2-Apr-09 17:28
memberPIEBALDconsult2-Apr-09 17:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.171207.1 | Last Updated 3 Jul 2007
Article Copyright 2007 by hanzzoid
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid