Complex class to represent complex numbers : Number Data Type « Language Basics « JavaScript DHTML

Complex class to represent complex numbers


Examples From
JavaScript: The Definitive Guide, Fourth Edition

Legal matters: these files were created by David Flanagan, and are
Copyright (c) 2001 by David Flanagan.  You may use, study, modify, and
distribute them for any purpose.  Please note that these examples are
provided "as-is" and come with no warranty of any kind.

David Flanagan

 * Complex.js:
 * This file defines a Complex class to represent complex numbers.
 * Recall that a complex number is the sum of a real number and an
 * imaginary number, and that the imaginary number i is the
 * square root of -1.

 * The first step in defining a class is defining the constructor
 * function of the class. This constructor should initialize any
 * instance properties of the object. These are the essential
 * "state variables" that make each instance of the class different.
function Complex(real, imaginary) {
    this.x = real;       // The real part of the number
    this.y = imaginary;  // The imaginary part of the number

 * The second step in defining a class is defining its instance
 * methods (and possibly other properties) in the prototype object
 * of the constructor. Any properties defined in this object will
 * be inherited by all instances of the class. Note that instance
 * methods operate implicitly on the this keyword. For many methods,
 * no other arguments are needed.

// Return the magnitude of a complex number. This is defined
// as its distance from the origin (0,0) of the complex plane.
Complex.prototype.magnitude = function() {
    return Math.sqrt(this.x*this.x + this.y*this.y);

// Return a complex number that is the negative of this one.
Complex.prototype.negative = function() {
    return new Complex(-this.x, -this.y);

// Convert a Complex object to a string in a useful way.
// This is invoked when a Complex object is used as a string.
Complex.prototype.toString = function() {
    return "{" + this.x + "," + this.y + "}";

// Return the real portion of a complex number. This function
// is invoked when a Complex object is treated as a primitive value.
Complex.prototype.valueOf = function() { return this.x; }

 * The third step in defining a class is to define class methods,
 * constants, and any needed class properties as properties of the
 * constructor function itself (instead of as properties of the
 * prototype object of the constructor). Note that class methods
 * do not use the this keyword: they operate only on their arguments.

// Add two complex numbers and return the result.
Complex.add = function (a, b) {
    return new Complex(a.x + b.x, a.y + b.y);

// Subtract one complex number from another.
Complex.subtract = function (a, b) {
    return new Complex(a.x - b.x, a.y - b.y);

// Multiply two complex numbers and return the product.
Complex.multiply = function(a, b) {
    return new Complex(a.x * b.x - a.y * b.y,
                       a.x * b.y + a.y * b.x);

// Here are some useful predefined complex numbers.
// They are defined as class properties, where they can be used as
// "constants." (Note, though, that they are not actually read-only.) = new Complex(0,0); = new Complex(1,0);
Complex.i = new Complex(0,1);


Related examples in the same category

1.Define variables, assign values and output
2.Dividing by Zero
3.Add Four Numbers from an HTML Form (and Display the Results)
4.Add characters
5.A Function That Returns the Sum of Three Numbers (Stripped-Down Version)
6. Concatenating Variables and Displaying the Value Contained
7.JavaScript Loan Calculator
9.Converting a Number to a String
10.Creating Number Objects Rather than Performing String-to-Number Conversions
11.Using toString() with Radix Values
12.Using JavaScript Integers
13.Conversion of Logical Values to Numeric Values
14.Using Floating-Point Numbers
15.Automatic Conversion between Types
16.Explicit Conversion Functions
17.Date Object Calculations
18.Operator Precedence and Different Data Types
19.Converting Base 10 to Base 16 Using Bitwise Operators
20.Number Calculation
21.Format a number
22.Format a number 2
23.Decimal to 2 Hex
24.Concatenate integer variable to a string variable