sign.js (1891B)
1 import { factory } from '../../utils/factory.js'; 2 import { deepMap } from '../../utils/collection.js'; 3 import { signNumber } from '../../plain/number/index.js'; 4 var name = 'sign'; 5 var dependencies = ['typed', 'BigNumber', 'Fraction', 'complex']; 6 export var createSign = /* #__PURE__ */factory(name, dependencies, _ref => { 7 var { 8 typed, 9 BigNumber: _BigNumber, 10 complex, 11 Fraction: _Fraction 12 } = _ref; 13 14 /** 15 * Compute the sign of a value. The sign of a value x is: 16 * 17 * - 1 when x > 0 18 * - -1 when x < 0 19 * - 0 when x == 0 20 * 21 * For matrices, the function is evaluated element wise. 22 * 23 * Syntax: 24 * 25 * math.sign(x) 26 * 27 * Examples: 28 * 29 * math.sign(3.5) // returns 1 30 * math.sign(-4.2) // returns -1 31 * math.sign(0) // returns 0 32 * 33 * math.sign([3, 5, -2, 0, 2]) // returns [1, 1, -1, 0, 1] 34 * 35 * See also: 36 * 37 * abs 38 * 39 * @param {number | BigNumber | Fraction | Complex | Array | Matrix | Unit} x 40 * The number for which to determine the sign 41 * @return {number | BigNumber | Fraction | Complex | Array | Matrix | Unit}e 42 * The sign of `x` 43 */ 44 return typed(name, { 45 number: signNumber, 46 Complex: function Complex(x) { 47 return x.im === 0 ? complex(signNumber(x.re)) : x.sign(); 48 }, 49 BigNumber: function BigNumber(x) { 50 return new _BigNumber(x.cmp(0)); 51 }, 52 Fraction: function Fraction(x) { 53 return new _Fraction(x.s, 1); 54 }, 55 'Array | Matrix': function ArrayMatrix(x) { 56 // deep map collection, skip zeros since sign(0) = 0 57 return deepMap(x, this, true); 58 }, 59 Unit: function Unit(x) { 60 if (!x._isDerived() && x.units[0].unit.offset !== 0) { 61 throw new TypeError('sign is ambiguous for units with offset'); 62 } 63 64 return this(x.value); 65 } 66 }); 67 });