UNPKG

mathjs

Version:

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser with support for symbolic computation, comes with a large set of built-in functions and constants, and offers an integrated solution to work with dif

150 lines (147 loc) 4.79 kB
import { factory } from '../utils/factory.js'; import { deepMap } from '../utils/collection.js'; var name = 'number'; var dependencies = ['typed']; /** * Separates the radix, integer part, and fractional part of a non decimal number string * @param {string} input string to parse * @returns {object} the parts of the string or null if not a valid input */ function getNonDecimalNumberParts(input) { var nonDecimalWithRadixMatch = input.match(/(0[box])([0-9a-fA-F]*)\.([0-9a-fA-F]*)/); if (nonDecimalWithRadixMatch) { var radix = { '0b': 2, '0o': 8, '0x': 16 }[nonDecimalWithRadixMatch[1]]; var integerPart = nonDecimalWithRadixMatch[2]; var fractionalPart = nonDecimalWithRadixMatch[3]; return { input, radix, integerPart, fractionalPart }; } else { return null; } } /** * Makes a number from a radix, and integer part, and a fractional part * @param {parts} [x] parts of the number string (from getNonDecimalNumberParts) * @returns {number} the number */ function makeNumberFromNonDecimalParts(parts) { var n = parseInt(parts.integerPart, parts.radix); var f = 0; for (var i = 0; i < parts.fractionalPart.length; i++) { var digitValue = parseInt(parts.fractionalPart[i], parts.radix); f += digitValue / Math.pow(parts.radix, i + 1); } var result = n + f; if (isNaN(result)) { throw new SyntaxError('String "' + parts.input + '" is not a valid number'); } return result; } export var createNumber = /* #__PURE__ */factory(name, dependencies, _ref => { var { typed } = _ref; /** * Create a number or convert a string, boolean, or unit to a number. * When value is a matrix, all elements will be converted to number. * * Syntax: * * math.number(value) * math.number(unit, valuelessUnit) * * Examples: * * math.number(2) // returns number 2 * math.number('7.2') // returns number 7.2 * math.number(true) // returns number 1 * math.number([true, false, true, true]) // returns [1, 0, 1, 1] * math.number(math.unit('52cm'), 'm') // returns 0.52 * * See also: * * bignumber, bigint, boolean, numeric, complex, index, matrix, string, unit * * @param {string | number | BigNumber | Fraction | boolean | Array | Matrix | Unit | null} [value] Value to be converted * @param {Unit | string} [valuelessUnit] A valueless unit, used to convert a unit to a number * @return {number | Array | Matrix} The created number */ var number = typed('number', { '': function _() { return 0; }, number: function number(x) { return x; }, string: function string(x) { if (x === 'NaN') return NaN; var nonDecimalNumberParts = getNonDecimalNumberParts(x); if (nonDecimalNumberParts) { return makeNumberFromNonDecimalParts(nonDecimalNumberParts); } var size = 0; var wordSizeSuffixMatch = x.match(/(0[box][0-9a-fA-F]*)i([0-9]*)/); if (wordSizeSuffixMatch) { // x includes a size suffix like 0xffffi32, so we extract // the suffix and remove it from x size = Number(wordSizeSuffixMatch[2]); x = wordSizeSuffixMatch[1]; } var num = Number(x); if (isNaN(num)) { throw new SyntaxError('String "' + x + '" is not a valid number'); } if (wordSizeSuffixMatch) { // x is a signed bin, oct, or hex literal // num is the value of string x if x is interpreted as unsigned if (num > 2 ** size - 1) { // literal is too large for size suffix throw new SyntaxError("String \"".concat(x, "\" is out of range")); } // check if the bit at index size - 1 is set and if so do the twos complement if (num >= 2 ** (size - 1)) { num = num - 2 ** size; } } return num; }, BigNumber: function BigNumber(x) { return x.toNumber(); }, bigint: function bigint(x) { return Number(x); }, Fraction: function Fraction(x) { return x.valueOf(); }, Unit: typed.referToSelf(self => x => { var clone = x.clone(); clone.value = self(x.value); return clone; }), null: function _null(x) { return 0; }, 'Unit, string | Unit': function Unit_string__Unit(unit, valuelessUnit) { return unit.toNumber(valuelessUnit); }, 'Array | Matrix': typed.referToSelf(self => x => deepMap(x, self)) }); // reviver function to parse a JSON object like: // // {"mathjs":"number","value":"2.3"} // // into a number 2.3 number.fromJSON = function (json) { return parseFloat(json.value); }; return number; });