Skip to content

dstructs/complex64

Repository files navigation

Complex64

NPM version Build Status Coverage Status Dependencies

Complex numbers (64-bit).

Installation

$ npm install dstructs-complex64

Usage

var Complex64 = require( 'dstructs-complex64' );

Complex64( real, imag )

64-bit complex number constructor, where real and imag correspond to the real and imaginary components, respectively.

var z = new Complex64( 3, -2 );
// returns Complex64( 3 - 2i )

Properties

A Complex64 instance has the following properties...

real

A read-only property returning the real component.

var real = z.real;
// returns <number>

imag

A read-only property returning the imaginary component.

var imag = z.imag;
// returns <number>

Methods

A Complex64 instance has the following methods...

Accessor Methods

These methods do not mutate a Complex64 instance and, instead, return some representation.

Complex64.prototype.toString()

Returns a string representation of a Complex64 instance.

var z = new Complex64( 3, -2 );
var str = z.toString();
// returns '3 - 2i'

Complex64.prototype.toJSON()

Returns a JSON representation of a Complex64 instance. JSON#stringify implicitly calls this method when stringifying a Complex64 instance.

var z = new Complex64( 3, -2 );

var json = z.toJSON();
/*
	{
		"type": "Complex64",
		"real": 3,
		"imag": -2
	}
*/

To a revive a Complex64 number from a JSON string,

// Complex64 reviver:
var reviver = require( 'dstructs-complex64-reviver' );

var z = new Complex64( 3, -2 );

// Stringify a complex number (implicitly calls `.toJSON`):
var str = JSON.stringify( z );
// returns '{"type":"Complex64","real":3,"imag":-2}'

// Revive a Complex64 instance from a JSON string:
var z = JSON.parse( str, reviver );
// returns Complex64( 3 - 2i )

Examples

var Complex64 = require( 'dstructs-complex64' );

var z = new Complex64( 3, -2 );

console.log( 'type: %s', typeof z );
// returns 'object'

console.log( 'str: %s', z );
// returns '3 - 2i'

console.log( 'real: %d', z.real );
// returns 3

console.log( 'imag: %d', z.imag );
// returns -2

console.log( 'JSON: %s', JSON.stringify( z ) );
// returns {"type":"Complex64","real":3,"imag":-2}

To run the example code from the top-level application directory,

$ node ./examples/index.js

Tests

Unit

This repository uses tape for unit tests. To run the tests, execute the following command in the top-level application directory:

$ make test

All new feature development should have corresponding unit tests to validate correct functionality.

Test Coverage

This repository uses Istanbul as its code coverage tool. To generate a test coverage report, execute the following command in the top-level application directory:

$ make test-cov

Istanbul creates a ./reports/coverage directory. To access an HTML version of the report,

$ make view-cov

Browser Support

This repository uses Testling for browser testing. To run the tests in a (headless) local web browser, execute the following command in the top-level application directory:

$ make test-browsers

To view the tests in a local web browser,

$ make view-browser-tests

License

MIT license.

Copyright

Copyright © 2016. The Compute.io Authors.

About

Complex numbers (64-bit).

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published