Robert Eisele
Engineer, Systems Architect and DBA

Complex numbers in JavaScript

Complex numbers sound complex, but in fact they are the most natural numbers - even if the name is a bit misleading. When extending the natural numbers to integers, to rational numbers, to real numbers and finally to complex numbers; it's the first field in which every operation is well defined.

Complex numbers have a wide vareity of applications, like calculation of AC voltage, representation of rotations in 2D, Discrete Fourier Transform, complex geometry and even Riemann hypothesis involve complex numbers. That's why I have written complex.js to make work with complex numbers an easy task in JavaScript.

The library is well tested and contains a lot of functions, which try to overcome numerical imprecision and are implemented with performance in mind. To make the API more robust, every operation returns a new instance of the complex class. New JavaScript engines do a really good job on this and immutable functions are really fast nowadays.

Simple Example

var Complex = require('complex.js');

var c = new Complex("99.3+8i");
c.mul({re: 3, im: 9}).div(4.9).sub(3, 2);

Test Complex.js

123+4i div 4.33
123.345-i div 3.1112

You might also be interested in the following


Sorry, comments are closed for this article. Contact me if you want to leave a note.