csignum.js (1605B)
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 cabs = require( './../../../../base/special/cabs' ); 24 25 26 // MAIN // 27 28 /** 29 * Evaluates the signum function of a complex number. 30 * 31 * @private 32 * @param {(Array|TypedArray|Object)} out - output array 33 * @param {number} re - real component 34 * @param {number} im - imaginary component 35 * @returns {(Array|TypedArray|Object)} function result 36 * 37 * @example 38 * var out = new Array( 2 ); 39 * 40 * var v = csignum( out, -4.2, 5.5 ); 41 * // returns [ -0.6069136033622302, 0.79476781392673 ] 42 * 43 * var bool = ( v === out ); 44 * // returns true 45 * 46 * @example 47 * var out = new Array( 2 ); 48 * var v = csignum( out, 0.0, 0.0 ); 49 * // returns [ 0.0, 0.0 ] 50 * 51 * @example 52 * var out = new Array( 2 ); 53 * var v = csignum( out, NaN, NaN ); 54 * // returns [ NaN, NaN ] 55 */ 56 function csignum( out, re, im ) { 57 var az = cabs( re, im ); 58 if ( az === 0.0 ) { 59 out[ 0 ] = re; 60 out[ 1 ] = im; 61 return out; 62 } 63 out[ 0 ] = re / az; 64 out[ 1 ] = im / az; 65 return out; 66 } 67 68 69 // EXPORTS // 70 71 module.exports = csignum;