complex128.js (4081B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive; 24 var defineProperty = require( '@stdlib/utils/define-property' ); 25 var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); 26 var toString = require( './tostring.js' ); // eslint-disable-line stdlib/no-redeclare 27 var toJSON = require( './tojson.js' ); 28 29 30 // MAIN // 31 32 /** 33 * 128-bit complex number constructor. 34 * 35 * @constructor 36 * @param {number} real - real component 37 * @param {number} imag - imaginary component 38 * @throws {TypeError} must invoke using the `new` keyword 39 * @throws {TypeError} real component must be a number primitive 40 * @throws {TypeError} imaginary component must be a number primitive 41 * @returns {Complex128} 128-bit complex number 42 * 43 * @example 44 * var z = new Complex128( 5.0, 3.0 ); 45 * // returns <Complex128> 46 */ 47 function Complex128( real, imag ) { 48 if ( !( this instanceof Complex128 ) ) { 49 throw new TypeError( 'invalid invocation. Constructor must be called with the `new` keyword.' ); 50 } 51 if ( !isNumber( real ) ) { 52 throw new TypeError( 'invalid argument. Real component must be a number primitive. Value: `'+real+'`.' ); 53 } 54 if ( !isNumber( imag ) ) { 55 throw new TypeError( 'invalid argument. Imaginary component must be a number primitive. Value: `'+imag+'`.' ); 56 } 57 defineProperty( this, 're', { 58 'configurable': false, 59 'enumerable': true, 60 'writable': false, 61 'value': real 62 }); 63 64 defineProperty( this, 'im', { 65 'configurable': false, 66 'enumerable': true, 67 'writable': false, 68 'value': imag 69 }); 70 71 // Note: we do not use a backing store coupled with getters, as numbers in JavaScript are double-precision by default. 72 73 return this; 74 } 75 76 /** 77 * Size (in bytes) of each component. 78 * 79 * @name BYTES_PER_ELEMENT 80 * @memberof Complex128 81 * @type {integer} 82 * @returns {integer} size of each component 83 * 84 * @example 85 * var nbytes = Complex128.BYTES_PER_ELEMENT; 86 * // returns 8 87 */ 88 setReadOnly( Complex128, 'BYTES_PER_ELEMENT', 8 ); 89 90 /** 91 * Size (in bytes) of each component. 92 * 93 * @name BYTES_PER_ELEMENT 94 * @memberof Complex128.prototype 95 * @type {integer} 96 * @returns {integer} size of each component 97 * 98 * @example 99 * var z = new Complex128( 5.0, 3.0 ); 100 * 101 * var nbytes = z.BYTES_PER_ELEMENT; 102 * // returns 8 103 */ 104 setReadOnly( Complex128.prototype, 'BYTES_PER_ELEMENT', 8 ); 105 106 /** 107 * Length (in bytes) of a complex number. 108 * 109 * @name byteLength 110 * @memberof Complex128.prototype 111 * @type {integer} 112 * @returns {integer} byte length 113 * 114 * @example 115 * var z = new Complex128( 5.0, 3.0 ); 116 * 117 * var nbytes = z.byteLength; 118 * // returns 16 119 */ 120 setReadOnly( Complex128.prototype, 'byteLength', 16 ); 121 122 /** 123 * Serializes a complex number as a string. 124 * 125 * @name toString 126 * @memberof Complex128.prototype 127 * @type {Function} 128 * @returns {string} serialized complex number 129 * 130 * @example 131 * var z = new Complex128( 5.0, 3.0 ); 132 * 133 * var str = z.toString(); 134 * // returns '5 + 3i' 135 */ 136 setReadOnly( Complex128.prototype, 'toString', toString ); 137 138 /** 139 * Serializes a complex number as a JSON object. 140 * 141 * ## Notes 142 * 143 * - `JSON.stringify()` implicitly calls this method when stringifying a `Complex128` instance. 144 * 145 * 146 * @name toJSON 147 * @memberof Complex128.prototype 148 * @type {Function} 149 * @returns {Object} serialized complex number 150 * 151 * @example 152 * var z = new Complex128( 5.0, 3.0 ); 153 * 154 * var obj = z.toJSON(); 155 * // returns { 'type': 'Complex128', 're': 5.0, 'im': 3.0 } 156 */ 157 setReadOnly( Complex128.prototype, 'toJSON', toJSON ); 158 159 160 // EXPORTS // 161 162 module.exports = Complex128;