API
在 github 的 README 上有快速入门的介绍或文档地址
提示
在下面的所有示例中, var 和分号不显示, 如果注释后面的内容带有引号表示已经调用了 toString 方法。
- 在加载库时,会自动根据Decimal的构造函数定义一个Decimal函数。
- 如果有需要,你可以创建多个Decimal构造函数,每个构造函数都独享自己的配置,例如精度、范围,这些配置只对当前构造函数创建的实例有效。
- 可以通过调用已经存在Decimal构造函数的 clone 来产生一个新的Decimal构造函数。
构造函数
Decimal(value) => Decimal
注意
value : number | string | Decimal
一个合法的 value 可以是 integer 或者 float 类型的数字或者字符串, 包括 ±0、±Infinity 、NaN。
value 没有位数限制,JavaScript 数组的最大值和实际需要的处理时间除外。
value 的允许范围参见最大指数 maxE,最小指数 minE.
和十进制的数字一样,如果数据包含合法的前缀也可初始化, 比如: 十六进制前缀为 0x
或 0X
, 二进制前缀为 0b
或者 0B
, 八进制前缀为 0o
或者 0O
。
十进制和其它进制的数据都可以使用指数法(浮点数)和普通形式表示。
使用指数法表示时 e
或者 E
表示十进制的十进制幂,p
或者 P
表示非十进制的二次幂。
错误示范
x = new Decimal(9) // '9'
y = new Decimal(x) // '9'
new Decimal('5032485723458348569331745.33434346346912144534543')
new Decimal('4.321e+4') // '43210'
new Decimal('-735.0918e-430') // '-7.350918e-428'
new Decimal('5.6700000') // '5.67'
new Decimal(Infinity) // 'Infinity'
new Decimal(NaN) // 'NaN'
new Decimal('.5') // '0.5'
new Decimal('-0b10110100.1') // '-180.5'
new Decimal('0xff.8') // '255.5'
new Decimal(0.046875) // '0.046875'
new Decimal('0.046875000000') // '0.046875'
new Decimal('0.046_875_000_000') // '0.046875'
new Decimal(4.6875e-2) // '0.046875'
new Decimal('468.75e-4') // '0.046875'
new Decimal('0b0.000011') // '0.046875'
new Decimal('0o0.03') // '0.046875'
new Decimal('0x0.0c') // '0.046875'
new Decimal('0b1.1p-5') // '0.046875'
new Decimal('0o1.4p-5') // '0.046875'
new Decimal('0x1.8p-5') // '0.046875'
方法
abs(绝对值)
/**
* 绝对值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
abs(x) => Decimal
// 示例
a = Decimal.abs(x)
b = new Decimal(x).abs()
a.equals(b) // true
acos(反余弦值)
/**
* 反余弦值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
acos(x) => Decimal
// 示例
a = Decimal.acos(x)
b = new Decimal(x).acos()
a.equals(b) // true
acosh(反双曲余弦值)
/**
* 反双曲余弦值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
acosh(x) => Decimal
// 示例
a = Decimal.acosh(x)
b = new Decimal(x).acosh()
a.equals(b) // true
add(加法)
/**
* 加法
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
add(x) => Decimal
// 示例
a = Decimal.add(x, y)
b = new Decimal(x).plus(y)
a.equals(b) // true
asin(反正弦值)
/**
* 反正弦值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
asin(x) => Decimal
// 示例
a = Decimal.asin(x)
b = new Decimal(x).asin()
a.equals(b) // true
asinh(反双曲正弦值)
/**
* 反双曲正弦值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
asinh(x) => Decimal
// 示例
a = Decimal.asinh(x)
b = new Decimal(x).asinh()
a.equals(b) // true
atan(反正切值)
/**
* 反正切值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
atan(x) => Decimal
// 示例
a = Decimal.atan(x)
b = new Decimal(x).atan()
a.equals(b) // true
atanh(反双曲正切值)
/**
* 反双曲正切值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
atanh(x) => Decimal
// 示例
a = Decimal.atanh(x)
b = new Decimal(x).atanh()
a.equals(b) // true
atan2(反正切值增强)
计算 y 和 x 的反正切值, 并使用 rounding(舍入) 模式, precision(精确) 到有效数字, 然后返回一个新的 Decimal对象。
y 和 x 的符号用于确定结果所在的象限。
取值范围: [-Infinity, Infinity]
值范围: [-pi, pi]
/**
* 反正切值增强
* @param {number|string|Decimal} x
* @param {number|string|Decimal} y
* @returns {Decimal}
*/
atan2(x, y) => Decimal
// 示例
r = Decimal.atan2(y, x)
cbrt(立方根)
/**
* 立方根
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
cbrt(x) => Decimal
// 示例
a = Decimal.cbrt(x)
b = new Decimal(x).cbrt()
a.equals(b) // true
ceil(上入整数)
/**
* 上入整数
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
ceil(x) => Decimal
// 示例
a = Decimal.ceil(x)
b = new Decimal(x).ceil()
a.equals(b) // true
clone(拷贝)
返回一个新的Decimal构造函数,该构造函数具有描述对象的设置(请参阅set), 如果省略了object,则和原来的Decimal构造函数具有相同的设置
/**
* 拷贝
* @param {object} [object]
* @returns {Decimal}
*/
clone([object]) => Decimal
// 示例
Decimal.set({ precision: 5 })
Decimal9 = Decimal.clone({ precision: 9 })
a = new Decimal(1)
b = new Decimal9(1)
a.div(3) // 0.33333
b.div(3) // 0.333333333
// 相当于 Decimal9 = Decimal.clone({ precision: 9 }):
Decimal9 = Decimal.clone()
Decimal9.set({ precision: 9 })
如果 object 的 "defaults" 属性为 true,那么新的构造函数将使用默认的配置。
D1 = Decimal.clone({ defaults: true })
// 使用默认值(精度precision属性除外)
D2 = Decimal.clone({ defaults: true, precision: 50 })
cos(余弦值)
/**
* 余弦值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
cos(x) => Decimal
// 示例
a = Decimal.cos(x)
b = new Decimal(x).cos()
a.equals(b) // true
cosh(双曲余弦值)
/**
* 双曲余弦值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
cosh(x) => Decimal
// 示例
a = Decimal.cosh(x)
b = new Decimal(x).cosh()
a.equals(b) // true
div(除法)
/**
* 除法
* @param {number|string|Decimal} x
* @param {number|string|Decimal} y
* @returns {Decimal}
*/
div(x, y) => Decimal
// 示例
a = Decimal.div(x, y)
b = new Decimal(x).div(y)
a.equals(b) // true
exp(自然指数)
/**
* 自然指数
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
exp(x) => Decimal
// 示例
a = Decimal.exp(x)
b = new Decimal(x).exp()
a.equals(b) // true
floor(向下取整)
/**
* 向下取整
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
floor(x) => Decimal
// 示例
a = Decimal.floor(x)
b = new Decimal(x).floor()
a.equals(b) // true
hypot(欧几里德范数)
返回所有参数的平方和的平方根, 并使用rounding(舍入)模式,precision(精确)到有效数字,然后返回一个新的 Decimal对象
/**
* 欧几里德范数
* @param {number|string|Decimal} x
* @param {number|string|Decimal} y
* @returns {Decimal}
*/
hypot([x [, y, ...]]) => Decimal
// 示例
r = Decimal.hypot(x, y)
isDecimal(是否为Decimal)
/**
* 是否为Decimal
* @param {any} x
* @returns {boolean}
*/
isDecimal(object) => boolean
// 示例
a = new Decimal(1)
b = {}
a instanceof Decimal // true
Decimal.isDecimal(a) // true
Decimal.isDecimal(b) // false
ln(自然对数)
/**
* 自然对数
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
ln(x) => Decimal
// 示例
a = Decimal.ln(x)
b = new Decimal(x).ln()
a.equals(b) // true
log(对数)
默认的底数是 10,它与 JavaScript 的 Math.log() 不同,后者返回自然对数(底数e)。
/**
* 对数
* @param {number|string|Decimal} x
* @param {number|string|Decimal} [base=10]
* @returns {Decimal}
*/
log(x, [base=10]) => Decimal
// 示例
a = Decimal.log(x, y)
b = new Decimal(x).log(y)
a.equals(b) // true
log2(2底对数)
返回 x 以 2 为底的对数, 并使用rounding(舍入)模式, precision(精确)到有效数字,然后返回一个新的 Decimal对象。
/**
* 2底对数
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
log2(x) => Decimal
// 示例
r = Decimal.log2(x)
log10(10底对数)
返回 x 以 10 为底的对数, 并使用rounding(舍入)模式, precision(精确)到有效数字,然后返回一个新的 Decimal对象。
/**
* 10底对数
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
log10(x) => Decimal
// 示例
r = Decimal.log10(x)
max(最大值)
返回参数的最大值为一个新的Decimal
/**
* 最大值
* @param {number|string|Decimal} x
* @param {number|string|Decimal} y
* @returns {Decimal}
*/
max(x, y) => Decimal
// 示例
r = Decimal.max(x, y)
min(最小值)
返回参数的最小值为一个新的Decimal
/**
* 最小值
* @param {number|string|Decimal} x
* @param {number|string|Decimal} y
* @returns {Decimal}
*/
min(x, y) => Decimal
// 示例
r = Decimal.min(x, y)
mod(取模)
/**
* 取模
* @param {number|string|Decimal} x
* @param {number|string|Decimal} y
* @returns {Decimal}
*/
mod(x, y) => Decimal
// 示例
a = Decimal.mod(x, y)
b = new Decimal(x).mod(y)
a.equals(b) // true
mul(乘法)
/**
* 乘法
* @param {number|string|Decimal} x
* @param {number|string|Decimal} y
* @returns {Decimal}
*/
mul(x, y) => Decimal
// 示例
a = Decimal.mul(x, y)
b = new Decimal(x).mul(y)
a.equals(b) // true
noConflict(仅限于浏览器)
将 Decimal 变量恢复为加载该库之前的值,并返回对原始Decimal构造函数的引用,以便可以将其分配给具有不同名称的变量
<script> Decimal = 1 </script>
<script src='/path/to/decimal.js'></script>
<script>
a = new Decimal(2) // '2'
D = Decimal.noConflict()
Decimal // 1
b = new D(3) // '3'
</script>
pow(幂)
/**
* 幂
* @param {number|string|Decimal} base
* @param {number|string|Decimal} exponent
* @returns {Decimal}
*/
pow(base, exponent) => Decimal
// 示例
a = Decimal.pow(x, y)
b = new Decimal(x).pow(y)
a.equals(b) // true
random(随机数)
/**
* 随机数
* @param {number|integer} dp 包括0 到 1e+9
* @returns {Decimal} 返回一个 大于等于 0,小于1 伪随机数的 Decimal 对象。
*/
random([dp]) => Decimal
// 示例
r = Decimal.random()
返回 dp 位的小数,如果位数不足,直接返回小数位。 如果省略 dp ,小数位数为默认的precision精度设置。
如果当前 Decimal 的构造函数的属性 crypto 的值为 true, 并且 crypto 对象在环境中可用, 则由 crypto.getRandomValues(现代浏览器中的Web密码API) 或者 crypto.randomBytes(Node.js)来生成, 如果 crypto 的属性值为 false, 则返回值由Math.random(最快)生成。
要使 crypto 对象在 Node.js 全局中可用,可以使用:
global.crypto = require('crypto')
如果 crypto 设置为 true , 但是 crypto 对象又不可用,将会抛出异常。
如果使用crypto(加密)方法,则返回的十进制值是加密安全的,并且与随机值在统计上是无法区分的。
Decimal.set({ precision: 10 })
Decimal.random() // '0.4117936847'
Decimal.random(20) // '0.78193327636914089009'
round(四舍五入)
/**
* 四舍五入
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
round(x) => Decimal
// 示例
a = Decimal.round(x)
b = new Decimal(x).round()
a.equals(b) // true
set(设置)
说明
为指定的 Decimal 构造函数配置 '全局' 的参数,既对当前创建的 Decimal 实例生效。
Returns: 当前 Decimal 对象的实例.
配置对象 object 可以包含全部或者部分配置,这些属性在 Properties(属性)中有详细描述,以下有示例:
检查配置对象属性的值的有效性,然后将其存储为此Decimal构造函数的同名属性。
如果 object 的 'defaults' 为 true,则所有未指定的属性都将重置为其默认值。
Throws:无效的配置参数.
// 默认值
Decimal.set({
precision: 20,
rounding: 4,
toExpNeg: -7,
toExpPos: 21,
maxE: 9e15,
minE: -9e15,
modulo: 1,
crypto: false
})
// 将所有属性重置为其默认值
Decimal.set({ defaults: true })
// 将precision设置为50,并将所有其他属性设置为其默认值
Decimal.set({ precision: 50, defaults: true })
也可以直接使用Decimal构造函数来设置,但是这回跳过参数有效性检查,如果你知道是如何进行有效配置,那么这不是问题。
Decimal.precision = 40
sign(符号函数)
/**
* 符号函数
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
sign(x) => Decimal
// 示例
r = Decimal.sign(x)
返回 | |
---|---|
1 | x 大于0 |
-1 | x 小于0 |
0 | x 等于0 |
-0 | x 小于0 |
NaN | x 是NaN |
sin(正弦值)
/**
* 正弦值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
sin(x) => Decimal
// 示例
a = Decimal.sin(x)
b = new Decimal(x).sin()
a.equals(b) // true
sinh(双曲正弦值)
/**
* 双曲正弦值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
sinh(x) => Decimal
// 示例
a = Decimal.sinh(x)
b = new Decimal(x).sinh()
a.equals(b) // true
sqrt(平方根)
/**
* 平方根
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
sqrt(x) => Decimal
// 示例
a = Decimal.sqrt(x)
b = new Decimal(x).sqrt()
a.equals(b) // true
sub(减法)
/**
* 减法
* @param {number|string|Decimal} x
* @param {number|string|Decimal} y
* @returns {Decimal}
*/
sub(x, y) => Decimal
// 示例
a = Decimal.sub(x, y)
b = new Decimal(x).sub(y)
a.equals(b) // true
sum(求和)
/**
* 求和
* @param {number|string|Decimal} x
* @param {number|string|Decimal} y 可选
* @param {...number|string|Decimal} args 可选
* @returns {Decimal}
*/
sum(x [, y, ...]) => Decimal
// 示例
x = 5
y = '16'
z = new Decimal(-11)
Decimal.sum(x, y, z) // '10'
tan(正切值)
/**
* 正切值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
tan(x) => Decimal
// 示例
a = Decimal.tan(x)
b = new Decimal(x).tan()
a.equals(b) // true
tanh(双曲正切值)
/**
* 双曲正切值
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
tanh(x) => Decimal
// 示例
a = Decimal.tanh(x)
b = new Decimal(x).tanh()
a.equals(b) // true
trunc(去除小数)
/**
* 去除小数
* @param {number|string|Decimal} x
* @returns {Decimal}
*/
trunc(x) => Decimal
// 示例
a = Decimal.trunc(x)
b = new Decimal(x).trunc()
a.equals(b) // true