.Net Framework 4.0: Complex numbers

.Net Framework 4.0 Beta 2 introduces new class in System.Numerics namespace: Complex. Complex represents complex numbers and enables different arithmetic operations with complex numbers. In this posting I will show you how to use complex numbers in .Net Framework 4.0 applications.

Complex class has two constructors – one of them has no arguments and the other takes real and complex parts of complex number. Complex numbers have also properties for phase and magnitude. Let’s see the following code.

There are also other mathematical operations defined for complex numbers like trigonometric ones and logarithms, so you can do basically everything you like with complex numbers. I found one thing missing – Parse() and TryParse() methods. Let’s hope these methods are available in stable version of .Net Framework 4.0.

9 Comments

Interesting... but I don't find this sort of "functionality" worth of being added as part of the framework core... unless it accesses native CPU instructions specially designed for working with complex numbers, of course.

@Héctor: I disagree. By that argument, *nothing* is worth adding to the framework core unless you use it.

Without the new System.Numerics classes, anyone who needs to manipulate complex numbers or arbitrarily large integers has to roll their own version of the code. Two assemblies using different implementations of a complex number can't work together.

By adding these classes to the CLR, everyone can use the same implementation, without having to re-invent the wheel.

I agree that adding this type of stuff to the Framework is very useful. It is something that's not so specialized that only a few developers would find use for it. It also helps give consistency for applications working with complex numbers in this case.