UNPKG

mathjs

Version:

Math.js is an extensive math library for JavaScript and Node.js. It features a flexible expression parser and offers an integrated solution to work with numbers, big numbers, complex numbers, units, and matrices.

115 lines (98 loc) 3.1 kB
module.exports = function (math) { var util = require('../../util/index'), BigNumber = math.type.BigNumber, Complex = require('../../type/Complex'), collection = require('../../type/collection'), isNumber = util.number.isNumber, isInteger = util.number.isInteger, isBoolean = util['boolean'].isBoolean, isComplex = Complex.isComplex, isCollection = collection.isCollection; /** * Round a value towards the nearest integer * * round(x) * round(x, n) * * For matrices, the function is evaluated element wise. * * @param {Number | BigNumber | Boolean | Complex | Array | Matrix} x * @param {Number | BigNumber | Boolean | Array} [n] number of decimals (by default n=0) * Must be an integer between 0 and 15 * @return {Number | BigNumber | Complex | Array | Matrix} res */ math.round = function round(x, n) { if (arguments.length != 1 && arguments.length != 2) { throw new math.error.ArgumentsError('round', arguments.length, 1, 2); } if (n == undefined) { // round (x) if (isNumber(x)) { return Math.round(x); } if (isComplex(x)) { return new Complex ( Math.round(x.re), Math.round(x.im) ); } if (x instanceof BigNumber) { return x.toDecimalPlaces(0); } if (isCollection(x)) { return collection.deepMap(x, round); } if (isBoolean(x)) { return Math.round(x); } throw new math.error.UnsupportedTypeError('round', math['typeof'](x)); } else { // round (x, n) if (!isNumber(n) || !isInteger(n)) { if (n instanceof BigNumber) { n = parseFloat(n.valueOf()); } else if (isBoolean(n)) { return round(x, +n); } else { throw new TypeError('Number of decimals in function round must be an integer'); } } if (n < 0 || n > 15) { throw new Error ('Number of decimals in function round must be in te range of 0-15'); } if (isNumber(x)) { return roundNumber(x, n); } if (isComplex(x)) { return new Complex ( roundNumber(x.re, n), roundNumber(x.im, n) ); } if (x instanceof BigNumber) { return x.toDecimalPlaces(n); } if (isCollection(x) || isCollection(n)) { return collection.deepMap2(x, n, round); } if (isBoolean(x)) { return round(+x, n); } throw new math.error.UnsupportedTypeError('round', math['typeof'](x), math['typeof'](n)); } }; /** * round a number to the given number of decimals, or to zero if decimals is * not provided * @param {Number} value * @param {Number} decimals number of decimals, between 0 and 15 (0 by default) * @return {Number} roundedValue */ function roundNumber (value, decimals) { var p = Math.pow(10, decimals); return Math.round(value * p) / p; } };