mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-28 21:32:20 +00:00
Fix coding style in docs/number.md
This commit is contained in:
parent
7f66176c1f
commit
aecabb2e2b
@ -141,7 +141,7 @@ Number.isInteger("15") // false
|
|||||||
Number.isInteger(true) // false
|
Number.isInteger(true) // false
|
||||||
```
|
```
|
||||||
|
|
||||||
ES5可以通过下面的代码,部署Number.isInteger()。
|
ES5可以通过下面的代码,部署`Number.isInteger()`。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
(function (global) {
|
(function (global) {
|
||||||
@ -196,7 +196,7 @@ Number.EPSILON.toFixed(20)
|
|||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
function withinErrorMargin (left, right) {
|
function withinErrorMargin (left, right) {
|
||||||
return Math.abs(left - right) < Number.EPSILON
|
return Math.abs(left - right) < Number.EPSILON;
|
||||||
}
|
}
|
||||||
withinErrorMargin(0.1 + 0.2, 0.3)
|
withinErrorMargin(0.1 + 0.2, 0.3)
|
||||||
// true
|
// true
|
||||||
@ -286,9 +286,9 @@ function trusty (left, right, result) {
|
|||||||
Number.isSafeInteger(right) &&
|
Number.isSafeInteger(right) &&
|
||||||
Number.isSafeInteger(result)
|
Number.isSafeInteger(result)
|
||||||
) {
|
) {
|
||||||
return result
|
return result;
|
||||||
}
|
}
|
||||||
throw new RangeError('Operation cannot be trusted!')
|
throw new RangeError('Operation cannot be trusted!');
|
||||||
}
|
}
|
||||||
|
|
||||||
trusty(9007199254740993, 990, 9007199254740993 - 990)
|
trusty(9007199254740993, 990, 9007199254740993 - 990)
|
||||||
@ -334,7 +334,7 @@ Math.trunc(); // NaN
|
|||||||
```javascript
|
```javascript
|
||||||
Math.trunc = Math.trunc || function(x) {
|
Math.trunc = Math.trunc || function(x) {
|
||||||
return x < 0 ? Math.ceil(x) : Math.floor(x);
|
return x < 0 ? Math.ceil(x) : Math.floor(x);
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
### Math.sign()
|
### Math.sign()
|
||||||
@ -368,7 +368,7 @@ Math.sign = Math.sign || function(x) {
|
|||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
return x > 0 ? 1 : -1;
|
return x > 0 ? 1 : -1;
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
### Math.cbrt()
|
### Math.cbrt()
|
||||||
@ -449,9 +449,9 @@ Math.clz32(true) // 31
|
|||||||
`Math.imul`方法返回两个数以32位带符号整数形式相乘的结果,返回的也是一个32位的带符号整数。
|
`Math.imul`方法返回两个数以32位带符号整数形式相乘的结果,返回的也是一个32位的带符号整数。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
Math.imul(2, 4); // 8
|
Math.imul(2, 4) // 8
|
||||||
Math.imul(-1, 8); // -8
|
Math.imul(-1, 8) // -8
|
||||||
Math.imul(-2, -2); // 4
|
Math.imul(-2, -2) // 4
|
||||||
```
|
```
|
||||||
|
|
||||||
如果只考虑最后32位,大多数情况下,`Math.imul(a, b)`与`a * b`的结果是相同的,即该方法等同于`(a * b)|0`的效果(超过32位的部分溢出)。之所以需要部署这个方法,是因为JavaScript有精度限制,超过2的53次方的值无法精确表示。这就是说,对于那些很大的数的乘法,低位数值往往都是不精确的,`Math.imul`方法可以返回正确的低位数值。
|
如果只考虑最后32位,大多数情况下,`Math.imul(a, b)`与`a * b`的结果是相同的,即该方法等同于`(a * b)|0`的效果(超过32位的部分溢出)。之所以需要部署这个方法,是因为JavaScript有精度限制,超过2的53次方的值无法精确表示。这就是说,对于那些很大的数的乘法,低位数值往往都是不精确的,`Math.imul`方法可以返回正确的低位数值。
|
||||||
@ -471,11 +471,11 @@ Math.imul(0x7fffffff, 0x7fffffff) // 1
|
|||||||
Math.fround方法返回一个数的单精度浮点数形式。
|
Math.fround方法返回一个数的单精度浮点数形式。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
Math.fround(0); // 0
|
Math.fround(0) // 0
|
||||||
Math.fround(1); // 1
|
Math.fround(1) // 1
|
||||||
Math.fround(1.337); // 1.3370000123977661
|
Math.fround(1.337) // 1.3370000123977661
|
||||||
Math.fround(1.5); // 1.5
|
Math.fround(1.5) // 1.5
|
||||||
Math.fround(NaN); // NaN
|
Math.fround(NaN) // NaN
|
||||||
```
|
```
|
||||||
|
|
||||||
对于整数来说,`Math.fround`方法返回结果不会有任何不同,区别主要是那些无法用64个二进制位精确表示的小数。这时,`Math.fround`方法会返回最接近这个小数的单精度浮点数。
|
对于整数来说,`Math.fround`方法返回结果不会有任何不同,区别主要是那些无法用64个二进制位精确表示的小数。这时,`Math.fround`方法会返回最接近这个小数的单精度浮点数。
|
||||||
@ -504,7 +504,7 @@ Math.hypot(-3); // 3
|
|||||||
|
|
||||||
上面代码中,3的平方加上4的平方,等于5的平方。
|
上面代码中,3的平方加上4的平方,等于5的平方。
|
||||||
|
|
||||||
如果参数不是数值,Math.hypot方法会将其转为数值。只要有一个参数无法转为数值,就会返回NaN。
|
如果参数不是数值,`Math.hypot`方法会将其转为数值。只要有一个参数无法转为数值,就会返回NaN。
|
||||||
|
|
||||||
### 对数方法
|
### 对数方法
|
||||||
|
|
||||||
@ -515,9 +515,9 @@ ES6新增了4个对数相关方法。
|
|||||||
`Math.expm1(x)`返回e<sup>x</sup> - 1,即`Math.exp(x) - 1`。
|
`Math.expm1(x)`返回e<sup>x</sup> - 1,即`Math.exp(x) - 1`。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
Math.expm1(-1); // -0.6321205588285577
|
Math.expm1(-1) // -0.6321205588285577
|
||||||
Math.expm1(0); // 0
|
Math.expm1(0) // 0
|
||||||
Math.expm1(1); // 1.718281828459045
|
Math.expm1(1) // 1.718281828459045
|
||||||
```
|
```
|
||||||
|
|
||||||
对于没有部署这个方法的环境,可以用下面的代码模拟。
|
对于没有部署这个方法的环境,可以用下面的代码模拟。
|
||||||
@ -533,10 +533,10 @@ Math.expm1 = Math.expm1 || function(x) {
|
|||||||
`Math.log1p(x)`方法返回`1 + x`的自然对数,即`Math.log(1 + x)`。如果`x`小于-1,返回`NaN`。
|
`Math.log1p(x)`方法返回`1 + x`的自然对数,即`Math.log(1 + x)`。如果`x`小于-1,返回`NaN`。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
Math.log1p(1); // 0.6931471805599453
|
Math.log1p(1) // 0.6931471805599453
|
||||||
Math.log1p(0); // 0
|
Math.log1p(0) // 0
|
||||||
Math.log1p(-1); // -Infinity
|
Math.log1p(-1) // -Infinity
|
||||||
Math.log1p(-2); // NaN
|
Math.log1p(-2) // NaN
|
||||||
```
|
```
|
||||||
|
|
||||||
对于没有部署这个方法的环境,可以用下面的代码模拟。
|
对于没有部署这个方法的环境,可以用下面的代码模拟。
|
||||||
@ -549,14 +549,14 @@ Math.log1p = Math.log1p || function(x) {
|
|||||||
|
|
||||||
**(3)Math.log10()**
|
**(3)Math.log10()**
|
||||||
|
|
||||||
`Math.log10(x)`返回以10为底的x的对数。如果x小于0,则返回NaN。
|
`Math.log10(x)`返回以10为底的`x`的对数。如果`x`小于0,则返回NaN。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
Math.log10(2); // 0.3010299956639812
|
Math.log10(2) // 0.3010299956639812
|
||||||
Math.log10(1); // 0
|
Math.log10(1) // 0
|
||||||
Math.log10(0); // -Infinity
|
Math.log10(0) // -Infinity
|
||||||
Math.log10(-2); // NaN
|
Math.log10(-2) // NaN
|
||||||
Math.log10(100000); // 5
|
Math.log10(100000) // 5
|
||||||
```
|
```
|
||||||
|
|
||||||
对于没有部署这个方法的环境,可以用下面的代码模拟。
|
对于没有部署这个方法的环境,可以用下面的代码模拟。
|
||||||
@ -569,15 +569,15 @@ Math.log10 = Math.log10 || function(x) {
|
|||||||
|
|
||||||
**(4)Math.log2()**
|
**(4)Math.log2()**
|
||||||
|
|
||||||
`Math.log2(x)`返回以2为底的x的对数。如果x小于0,则返回NaN。
|
`Math.log2(x)`返回以2为底的`x`的对数。如果`x`小于0,则返回NaN。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
Math.log2(3) // 1.584962500721156
|
Math.log2(3) // 1.584962500721156
|
||||||
Math.log2(2) // 1
|
Math.log2(2) // 1
|
||||||
Math.log2(1) // 0
|
Math.log2(1) // 0
|
||||||
Math.log2(0) // -Infinity
|
Math.log2(0) // -Infinity
|
||||||
Math.log2(-2) // NaN
|
Math.log2(-2) // NaN
|
||||||
Math.log2(1024) // 10
|
Math.log2(1024) // 10
|
||||||
Math.log2(1 << 29) // 29
|
Math.log2(1 << 29) // 29
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -593,12 +593,12 @@ Math.log2 = Math.log2 || function(x) {
|
|||||||
|
|
||||||
ES6新增了6个三角函数方法。
|
ES6新增了6个三角函数方法。
|
||||||
|
|
||||||
- Math.sinh(x) 返回x的双曲正弦(hyperbolic sine)
|
- `Math.sinh(x)` 返回`x`的双曲正弦(hyperbolic sine)
|
||||||
- Math.cosh(x) 返回x的双曲余弦(hyperbolic cosine)
|
- `Math.cosh(x)` 返回`x`的双曲余弦(hyperbolic cosine)
|
||||||
- Math.tanh(x) 返回x的双曲正切(hyperbolic tangent)
|
- `Math.tanh(x)` 返回`x`的双曲正切(hyperbolic tangent)
|
||||||
- Math.asinh(x) 返回x的反双曲正弦(inverse hyperbolic sine)
|
- `Math.asinh(x)` 返回`x`的反双曲正弦(inverse hyperbolic sine)
|
||||||
- Math.acosh(x) 返回x的反双曲余弦(inverse hyperbolic cosine)
|
- `Math.acosh(x)` 返回`x`的反双曲余弦(inverse hyperbolic cosine)
|
||||||
- Math.atanh(x) 返回x的反双曲正切(inverse hyperbolic tangent)
|
- `Math.atanh(x)` 返回`x`的反双曲正切(inverse hyperbolic tangent)
|
||||||
|
|
||||||
## 指数运算符
|
## 指数运算符
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user