说明
入门
入门
例子
文档
核心
配置
链式调用
扩展
序列化
表达式
解析和评估
句法
表达式树
代数
定制
安全
数据类型
数字
大数字
分数
复数
矩阵
单位
参考
类
方法
常量
定制捆绑
命令行界面
历史
大数字 - Math.js文档 - 笔下光年
网站首页
大数字
对于任意精度的计算,math.js 支持 `BigNumber` 数据类型。 BigNumber 支持由 [decimal.js](https://github.com/MikeMcl/decimal.js/ "decimal.js") 提供支持。 ### Usage 可以使用函数 bignumber 创建 `BigNumber`: ```javascript math.bignumber('2.3e+500') // BigNumber, 2.3e+500 ``` Most functions can determine the type of output from the type of input: a number as input will return a number as output, a BigNumber as input returns a BigNumber as output. Functions which cannot determine the type of output from the input (for example `math.evaluate`) use the default number type `number`, which can be configured when instantiating math.js. To configure the use of BigNumbers instead of numbers by default, configure math.js like: ```javascript math.config({ number: 'BigNumber', // Default type of number: // 'number' (default), 'BigNumber', or 'Fraction' precision: 64 // Number of significant digits for BigNumbers }) // use math math.evaluate('0.1 + 0.2') // BigNumber, 0.3 ``` The default precision for BigNumber is 64 digits, and can be configured with the option `precision`. Note that we also change the configuration of `epsilon` to be close to the precision limit of our BigNumbers. `epsilon` is used for example in relational and rounding functions (`equal`, `larger`, `smaller`, `round`, `floor`, `etc`) to determine when a value is nearly equal, see [Equality](https://www.bixiaguangnian.com/manual/mathjs/374.html#Equality "Equality"). If we would leave `epsilon` unchanged, having the default value of `1e-12`, we could get inaccurate and misleading results since we’re now working with a higher precision. ### Support Most functions in math.js support BigNumbers, but not all of them. For example the function `random` doesn’t support BigNumbers. ### Round-off errors Calculations with BigNumber are much slower than calculations with Number, but they can be executed with an arbitrary precision. By using a higher precision, it is less likely that round-off errors occur: ```javascript // round-off errors with numbers math.add(0.1, 0.2) // Number, 0.30000000000000004 math.divide(0.3, 0.2) // Number, 1.4999999999999998 // no round-off errors with BigNumbers :) math.add(math.bignumber(0.1), math.bignumber(0.2)) // BigNumber, 0.3 math.divide(math.bignumber(0.3), math.bignumber(0.2)) // BigNumber, 1.5 ``` ### Limitations It’s important to realize that BigNumbers do not solve all problems related to precision and round-off errors. Numbers with an infinite number of digits cannot be represented with a regular number nor a BigNumber. Though a BigNumber can store a much larger number of digits, the amount of digits remains limited if only to keep calculations fast enough to remain practical. ```javascript const one = math.bignumber(1) const three = math.bignumber(3) const third = math.divide(one, three) console.log(third.toString()) // outputs 0.3333333333333333333333333333333333333333333333333333333333333333 const ans = math.multiply(third, three) console.log(ans.toString()) // outputs 0.9999999999999999999999999999999999999999999999999999999999999999 // this should be 1 again, but `third` is rounded to a limited number of digits 3 ``` ### Conversion BigNumbers 可以使用函数 number 和 bignumber 转换为数字,反之亦然。 将 BigNumber 转换为数字时,会丢失 BigNumber 的高精度。 当 BigNumber 太大而无法表示为 Number 时,它将被初始化为 Infinity。 ```javascript // converting numbers and BigNumbers const a = math.number(0.3) // number, 0.3 const b = math.bignumber(a) // BigNumber, 0.3 const c = math.number(b) // number, 0.3 // exceeding the maximum of a number const d = math.bignumber('1.2e500') // BigNumber, 1.2e+500 const e = math.number(d) // number, Infinity // loosing precision when converting to number const f = math.bignumber('0.2222222222222222222') // BigNumber, 0.2222222222222222222 const g = math.number(f) // number, 0.2222222222222222 ```
上一篇:
数字
下一篇:
分数