Source code:
QComplexNumber.js
Real numbers ℝ
Our regular, ordinary, everyday numbers are called real numbers. These include integers and decimals. You can visualize real numbers as existing along an infinite number line—with zero in the middle, positive numbers counting up forever to infinity on the right, and negative numbers doing the exact opposite on the left.
When a real number is multiplied by itself the product is always positive. For example, if we choose the number 2 we see that 2 × 2 = 4. Similarly, had we chosen the negative number 2, the product would still be positive because two negative numbers multiplied together also produce a positive result; 2 × 2 = 4. For brevity we could rewrite these equations as 2^{2} = 4 and (2)^{2} = 4, respectively.
The square root of a real number has two possible answers. The square root of 4, for example, is both 2 and 2 because both are solutions for x in the equation x = √4.
Imaginary numbers 𝕀
But suppose we wanted to find the square root of a negative number. Is there any number that could solve for x in the equation x = √(4)? Sadly, there is not. Or more precisely: there is not any real solution for the square root of a negative number.
In his 1991 address on “Creativity in Management”, Monty Python’s John Cleese articulates Edward de Bono’s concept of the intermediate impossible (32:06) as a useful stepping stone towards a novel and useful solution. Imaginary numbers might be considered an intermediate impossible. The symbol i is defined as the imaginary solution to the equation x = √(1), therefore i^{2} = 1. With this imaginary device we now have a solution to the above equation x = √(4) and that solution is 2i. (And also 2i, of course! We can indicate this “plus or minus” possibility as ±2i.) Let’s inspect this more closely.
𝒙 = √(4)
𝒙 = √( 4 × 1)
𝒙 = √4 × √(1)
𝒙 = ±2 × √(1)
𝒙 = ±2 × i
𝒙 = ±2i
2i is an imaginary number that consists of a real number multiplier, 2, and our imaginary solution to √(1), called i. Like real numbers, imaginary numbers also exist along an infinite number line. We plotted our real number line horizontally, so let’s plot our imaginary number line vertically.
Complex numbers ℂ
We just saw that multiplying a real number by i yields an imaginary number. But what if you add a real number to an imaginary one? Things get complex. A complex number is a number that can be expressed in the form a + bi, where a is the real component and bi is the imaginary component. Some examples might be 1 + 2i or 3  4i.
This is what the ComplexNumber
class was created to handle.
Open up your JavaScript console and paste in the following:
var
cat = new Q.ComplexNumber( 1, 2 ),
dog = new Q.ComplexNumber( 3, 4 )
cat.toText()// Returns '1 + 2i'
dog.toText()// Returns '3  4i'
Now we have two variables, cat
and dog
, that we can operate with.
As you might guess, ComplexNumber
includes instance methods for common operations like
addition, subtraction, multiplication, and division.
Try the following lines individually in your JavaScript console:
cat.add( dog ).toText() // Returns '4  2i'
cat.subtract( dog ).toText()// Returns '2 + 6i'
cat.multiply( dog ).toText()// Returns '11 + 2i'
cat.divide( dog ).toText() // Returns '0.2 + 0.4i'
We can now verify that i^{2} = 1.
var i = new Q.ComplexNumber( 0, 1 )
i.toText()// Returns 'i'
i.power( 2 ).toText()// Returns '1'
Operation functions on Q.ComplexNumber
instances generally accept as
arguments both sibling instances and pure Number
instances, though the
value returned is always an instance of Q.ComplexNumber
.
Constructor
ComplexNumber
Function([ real: Number or Q.ComplexNumber ][, imaginary: Number ]]) => Q.ComplexNumber
Expects zero to two arguments.
If the first argument is a ComplexNumber
then that value is cloned and any remaining arguments are ignored.
If either of the arguments are undefined
they are assumed to be zero.
If the first argument is not a ComplexNumber
and either of the arguments are not numberlike
then an error is thrown.
var
ape = new Q.ComplexNumber(),
bee = new Q.ComplexNumber( 1 ),
elk = new Q.ComplexNumber( 1, 2 ),
fox = new Q.ComplexNumber( elk )
ape.toText()// Returns '0'
bee.toText()// Returns '1'
elk.toText()// Returns '1 + 2i'
fox.toText()// Returns '1 + 2i'
elk.isEqualTo( fox ) // Returns true
elk.index === fox.index// Returns false

 real
Number
Traditionally the first argument, a numberlike value representing the real component of the complex number. 
 imaginary
Number
Traditionally the second argument, a numberlike value representing the imaginary component of the complex number. 
 index
Number
An identification number assigned to the instance, used for minding the total number of instances created.
Static properties

 help
Function ⇒ String
Calls and returns the value ofQ.help
, passingQ.ComplexNumber
as the argument. 
 index
Number
The number of instances created so far.
Constants and constant creation

 constants
Object
Constants are appended directly to theQ.ComplexNumber
object. For convenience they are also appended to thisQ.ComplexNumber
.constants object to make looking up constants in the JavaScript console trivial, and to make iterating across all constants convenient via functions likeObject.entries
,Object.keys
,Object.values
, and so on. The intention that a property act as a constant is signaled by its labelling in alluppercase. 
 createConstant
Function( key: String, value: * )
Appends a property named bykey
with a value ofvalue
to both theQ
object and itsconstants
property. 
 createConstants
Function( … )
Expects an even number of arguments. Will use each pair in the sequence of arguments to callcreateConstant
. 
 ZERO
Q.ComplexNumber
Initialized asnew Q.ComplexNumber( 0, 0 )
. Described as 0. 
 ONE
Q.ComplexNumber
Initialized asnew Q.ComplexNumber( 1, 0 )
. Described as 1. 
 E
Q.ComplexNumber
Initialized asnew Q.ComplexNumber( Math.E, 0 )
. Described as e ≈ 2.7183. 
 PI
Q.ComplexNumber
Initialized asnew Q.ComplexNumber( Math.PI, 0 )
. Described as exactly equal to 3. 
 I
Q.ComplexNumber
Initialized asnew Q.ComplexNumber( 0, 1 )
. Described as i. 
 EPSILON
Q.ComplexNumber
Initialized asnew Q.ComplexNumber( Q.EPSILON, Q.EPSILON )
. Described as ≈ 1.3323 × 10^{15} + 1.3323 × 10^{15}i. 
 INFINITY
Q.ComplexNumber
Initialized asnew Q.ComplexNumber( Infinity, Infinity )
. Described as ∞ + ∞i. 
 NAN
Q.ComplexNumber
Initialized asnew Q.ComplexNumber( NaN, NaN )
.Array( 16 ).join( NaN ) +' BATMAN!'
Inspection

 isNumberLike
Function( n: * ) ⇒ Boolean
Returns true ifn
’stypeof
is equal to theString
'number'
or ifn instanceof Number
istrue
, otherwise returnsfalse
. 
 isNaN
Function( c: Q.ComplexNumber ) ⇒ Boolean
Returnstrue
if either of this instance’sreal
orimaginary
components areNaN
, otherwise returnsfalse
. 
 isZero
Function( c: Q.ComplexNumber ) ⇒ Boolean
Returnstrue
if both of this instance’sreal
andimaginary
absolute values are equal to zero, otherwise returnsfalse
. See “Signed zero” for an explanation of why the absolute value of zero must be taken. 
 isFinite
Function( c: Q.ComplexNumber ) ⇒ Boolean
Returnstrue
if both of this instance’sreal
andimaginary
values are finite, otherwise returnsfalse
. 
 isInfinite
Function( c: Q.ComplexNumber ) ⇒ Boolean
Returnstrue
if both of this instance’sreal
andimaginary
values are not finite and are also notNaN
, otherwise returnsfalse
. 
 areEqual
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Boolean
Returnstrue
if the argumentsa
andb
are withinQ.EPSILON
of each other, otherwise returnsfalse
.
Maths

 absolute
Function( c: Q.ComplexNumber ) ⇒ Number
Calls and returns the value ofQ.hypotenuse
usingc
’sreal
andimaginary
properties as arguments. 
 conjugate
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumber
Returns a new complex number with the valuesc.real
andc.imaginary * 1
. 
 sine
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumber
Returns the sine ofc
. 
 cosine
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumber
Returns the cosine ofc
. 
 arcCosine
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumber
Returns the arccosine ofc
. 
 arcTangent
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumber
Returns the arctangent ofc
. 
 power
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Returnsa
raised to the power ofb
. 
 squareRoot
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumber
Returns the square root ofc
. 
 log
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumber
Returns the log ofc
. 
 operate
Function( name: String, a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber, numberAndNumber: Function, numberAndComplex: Function, complexAndNumber: Function, complexAndComplex: Function ) ⇒ *
Meta function for performing operations on two values that may each be either numbers or complex numbers. Thename
argument indicates the name of the operation being performed and is used in error logging should the operation fail. The intent is to return aQ.ComplexNumber
, though this is up to the functions passed in as it is their return values that are returned. 
 multiply
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
UsesQ.ComplexNumber.operate
to return the result of multiplyinga
andb
. 
 divide
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
UsesQ.ComplexNumber.operate
to return the result of dividinga
byb
. 
 add
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
UsesQ.ComplexNumber.operate
to return the result of addinga
andb
. 
 subtract
Function( a: Number or Q.ComplexNumber, b: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
UsesQ.ComplexNumber.operate
to return the result of subtractingb
froma
.
Prototype properties

 clone
Function ⇒ Q.ComplexNumber
Returns a new instance with the values forreal
andimaginary
copied from this instance. 
 copy$
Function( c: Q.ComplexNumber ) ⇒ Q.ComplexNumber
Copies the values forreal
andimaginary
from the suppliedQ.ComplexNumber
argument.
Inspections (nondestructive)

 isNaN
Function ⇒ Boolean
Calls and returns the result of theisNaN
static method, passing the calling instance as the argument. Will return aBoolean
value, thereby halting “Fluent interface” method chaining for this instance. 
 isZero
Function ⇒ Boolean
Calls and returns the result of theisZero
static method, passing the calling instance as the argument. Will return aBoolean
value, thereby halting “Fluent interface” method chaining for this instance. 
 isFinite
Function ⇒ Boolean
Calls and returns the result of theisFinite
static method, passing the calling instance as the argument. Will return aBoolean
value, thereby halting “Fluent interface” method chaining for this instance. 
 isInfinite
Function ⇒ Boolean
Calls and returns the result of theisInfinite
static method, passing the calling instance as the argument. Will return aBoolean
value, thereby halting “Fluent interface” method chaining for this instance. 
 isEqualTo
Function( n: Number or Q.ComplexNumber ) ⇒ Boolean
Calls and returns the result of theareEqual
static method, passing the calling instance as the first argument andn
as the second argument. Will return aBoolean
value, thereby halting “Fluent interface” method chaining for this instance. 
 reduce
Function ⇒ Q.ComplexNumber or Number
If noimaginary
component exists, returns aNumber
representing theQ.ComplexNumber real
component, otherwise returns the instance itself. May return aNumber
value, thereby halting “Fluent interface” method chaining for this instance. 
 toText
Function([ roundToDecimal: Number ]) ⇒ String
Returns the value of this instance expressed as text in a form similar to a+bi. IfroundToDecimal
is supplied, will round both thereal
andimaginary
components by passing themselves androundToDecimal
toQ.round
as arguments. Will return aString
value, thereby halting “Fluent interface” method chaining for this instance.
Maths (nondestructive)

 absolute
Function ⇒ Number
Passes this instance as an argument to theabsolute
static method and returns the result. Will return aNumber
value, thereby halting “Fluent interface” method chaining for this instance. 
 conjugate
Function ⇒ Q.ComplexNumber
Passes this instance as an argument to theconjugate
static method and returns the result. 
 power
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Passes this instance as the first argument andn
as the second argument to thepower
static method and returns the result. 
 squareRoot
Function ⇒ Q.ComplexNumber
Passes this instance as an argument to thesquareRoot
static method and returns the result. 
 log
Function ⇒ Q.ComplexNumber
Passes this instance as an argument to thelog
static method and returns the result. 
 multiply
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Passes this instance as the first argument andn
as the second argument to themultiply
static method and returns the result. 
 divide
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Passes this instance as the first argument andn
as the second argument to thedivide
static method and returns the result. 
 add
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Passes this instance as the first argument andn
as the second argument to theadd
static method and returns the result. 
 subtract
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Passes this instance as the first argument andn
as the second argument to thesubtract
static method and returns the result.
Maths (destructive)

 conjugate$
Function ⇒ Q.ComplexNumber
Calls theconjugate
instance method andcopies
the result to this instance. 
 power$
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Calls thepower
instance method withn
as an argument andcopies
the result to this instance. 
 squareRoot$
Function ⇒ Q.ComplexNumber
Calls thesquareRoot
instance method andcopies
the result to this instance. 
 log$
Function ⇒ Q.ComplexNumber
Calls thelog
instance method andcopies
the result to this instance. 
 multiply$
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Calls themultiply
instance method withn
as an argument andcopies
the result to this instance. 
 divide$
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Calls thedivide
instance method withn
as an argument andcopies
the result to this instance. 
 add$
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Calls theadd
instance method withn
as an argument andcopies
the result to this instance. 
 subtract$
Function( n: Number or Q.ComplexNumber ) ⇒ Q.ComplexNumber
Calls thesubtract
instance method withn
as an argument andcopies
the result to this instance.