mirror of
https://github.com/josdejong/mathjs.git
synced 2025-12-08 19:46:04 +00:00
60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
var collection = require('../../type/collection');
|
|
|
|
function factory (type, config, load, typed) {
|
|
/**
|
|
* Create a BigNumber, which can store numbers with arbitrary precision.
|
|
* When a matrix is provided, all elements will be converted to BigNumber.
|
|
*
|
|
* Syntax:
|
|
*
|
|
* math.bignumber(x)
|
|
*
|
|
* Examples:
|
|
*
|
|
* 0.1 + 0.2; // returns Number 0.30000000000000004
|
|
* math.bignumber(0.1) + math.bignumber(0.2); // returns BigNumber 0.3
|
|
*
|
|
*
|
|
* 7.2e500; // returns Number Infinity
|
|
* math.bignumber('7.2e500'); // returns BigNumber 7.2e500
|
|
*
|
|
* See also:
|
|
*
|
|
* boolean, complex, index, matrix, string, unit
|
|
*
|
|
* @param {Number | String | Array | Matrix | Boolean | null} [value] Value for the big number,
|
|
* 0 by default.
|
|
* @returns {BigNumber} The created bignumber
|
|
*/
|
|
var bignumber = typed('bignumber', {
|
|
'': function () {
|
|
return new type.BigNumber(0);
|
|
},
|
|
|
|
'number': function (x) {
|
|
// convert to string to prevent errors in case of >15 digits
|
|
return new type.BigNumber(x + '');
|
|
},
|
|
|
|
'string': function (x) {
|
|
return new type.BigNumber(x);
|
|
},
|
|
|
|
'BigNumber': function (x) {
|
|
// we assume a BigNumber is immutable
|
|
return x;
|
|
},
|
|
|
|
'Array | Matrix': function (x) {
|
|
return collection.deepMap(x, bignumber);
|
|
}
|
|
});
|
|
|
|
return bignumber;
|
|
}
|
|
|
|
exports.name = 'bignumber';
|
|
exports.factory = factory;
|