数学计算,修正浮点数计算问题
- Description:
- 数学计算,修正浮点数计算问题 
 
- Source:
- See:
Example
// 从 4.12.0 版本开始,规范了有效数值。(注意:4.12.3 对有效数值重新定义)
// 有效数值即能通过 Number(value) 转为数字,且不能为 NaN 。
// 以下为有效数值:
// 1. 数字: 1, -1, 1e-2, 1.312, 0.1, Infinity
// 2. 字符串: '1', '10e2', '-1', '0.1', '', ' ', '  15', ' 15   '
// 3. 其他类型: null, new Date(), [], new Array(), true, false,...
// 以下为无效数值:
// 1. 字符串: '1a', '-12a', '10.2.2', '10e2.1'
// 2. 其他类型: undefined, [], {}, Symbol(), function(){}, ()=>{}
// 计算说明:
// 四则运算的第二个参数都是有默认值(乘数和除数默认1,加数和减数默认0)
plus(); // NaN  0个参数时,被加数转换为 Number(undefined) NaN ,NaN+0 = NaN 。其他计算方法如果没有参数一样返回 NaN 。
plus(0.1); // 0.1  第二个参数,加数默认为 0
plus(undefined, 0.1); // NaN  第一个参数被加数转换为 Number(undefined) NaN ,NaN+0 = NaN 。其他计算方法如果第一个参数为无效数值一样返回 NaN 。
plus(true, null); // 1  Number(true) 转换为 1 , Number(null) 转换为 0 , 1+0=1
// 2. 参数中包含无效数值,返回NaN
plus('0.1', ' a'); // NaN
plus(true, {}); // NaN
plus(true, 0.1, Symbol()); // NaN
// 注意:
// 如果第二个及后面的参数如果值为 undefined 取默认值,即乘除数取 1 ,加减法取 0 。
plus(0.1, undefined); // 0.1
plus(0.1, undefined, 0.2, undefined); // 0.3  后面的 undefined 取默认值 0
times(0.1, undefined, 0.2, undefined); // 0.02  后面的 undefined 取默认值 1Methods
(static) divide(…nums) → {number}
Example
divide(1.21); // 1.21 除数默认为 1 ,即 1.21/1 = 1.21
divide(1.21, 1.1); // 1.1
divide(1000, 10, 10); // 10
divide(1000, 10, 10, 10); // 1
divide(); // NaN  如果没有传入参数,被除数默认为 undefined 。 Number(undefined) 转换为 NaN ,NaN/1 = NaN
divide(null); // 0  Number(null) 转换为 0 , 0/1 = 0
divide('1.5 ', 0.5); // 3  Number('1.5 ') 转换为 1.5 ,1.5/0.5 = 3Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
| nums | number | string | <repeatable> | 被除数和除数 | 
Returns:
商数
- Type
- number
(static) gcd(…nums) → {number}
- Description:
- 最大公约数,使用辗转相除法。 - 遵循以下约定: - 如果参数中包含无效数值,返回 NaN。
- 如果全部参数都为 0,返回0。
- 如果参数包含 0,仅计算非零的数。
- 如果参数为负数,将转为绝对值的正数。
- 如果参数包含小数点,将转为四舍五入的整数。
 
- 如果参数中包含无效数值,返回 
 
- Source:
- Since:
- 4.20.0
 
- See:
Example
gcd(8, 14); // 2
gcd(57, 48); // 3
gcd(140, 21, 42); // 7
gcd('foo', 'bar'); // NaN
gcd(0, 10); // 10
gcd(2.3, 3.8, 8, -10); // 2Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
| nums | number | string | <repeatable> | 两个或多个整数。 | 
Returns:
最大公约数。
- Type
- number
(static) lcm(…nums) → {number}
- Description:
- 最小公倍数。 - 遵循以下约定: - 如果参数中包含无效数值,返回 NaN。
- 如果只有一个参数,另一个参数默认为 1。
- 如果全部参数都为 0,返回NaN。因为作为除数的最大公约数为0,结果为NaN。
- 如果全部参数都为 Infinity,返回NaN。因为除数和被除数都是Infinity,结果为NaN。
- 如果参数包含 0,返回0。
- 如果参数为负数,将转为绝对值的正数。
- 如果参数包含小数点,将转为四舍五入的整数。
 
- 如果参数中包含无效数值,返回 
 
- Source:
- Since:
- 4.20.0
 
- See:
Example
lcm(3, 4); // 12
lcm(8, 4); // 8
lcm(2, 4, 8); // 32
lcm('foo', 'bar'); // NaN
lcm(0, 10); // 0
lcm(2.3, 3.8, 8, -10); // 320Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
| nums | number | string | <repeatable> | 两个或多个整数。 | 
Returns:
最小公倍数。
- Type
- number
(static) minus(…nums) → {number}
Example
minus(1); // 1
minus(1, 0.9); // 0.1
minus(1, 0.9, 0.02); // 0.08
minus(1, 0.9, 0.02, 0.08); // 0Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
| nums | number | string | <repeatable> | 相减的数 | 
Returns:
差
- Type
- number
(static) plus(…nums) → {number}
Example
plus(0.1); // 0.1
plus(0.1, 0.2); // 0.3
plus(0.1, 0.2, 0.3); // 0.6
plus(0.1, 0.2, 0.3, 0.4); // 1Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
| nums | number | string | <repeatable> | 相加的数 | 
Returns:
总和
- Type
- number
(static) round(num, precisionopt) → {number}
Example
round(4.006); // 4
round(4.006, 2); // 4.01
round(4060, -2); // 4100Parameters:
| Name | Type | Attributes | Default | Description | 
|---|---|---|---|---|
| num | number | string | 要四舍五入的数字 | ||
| precision | number | <optional> | 0 | 四舍五入的精度,默认 | 
Returns:
四舍五入的数字
- Type
- number
(static) times(…nums) → {number}
Example
times(3); // 3
times(3, 0.6); // 1.8
times(3, 0.6, 2); // 3.6
times(3, 0.6, 2, 10); // 36Parameters:
| Name | Type | Attributes | Description | 
|---|---|---|---|
| nums | number | string | <repeatable> | 相乘的数 | 
Returns:
乘积
- Type
- number