日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ES6 数值

發(fā)布時(shí)間:2024/4/13 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ES6 数值 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 二進(jìn)制和八進(jìn)制表示法

ES6提供了二進(jìn)制和八進(jìn)制數(shù)值的新的寫法,分別用前綴0b(或0B)和0o(或0O)表示。

0b111110111 === 503 // true 0o767 === 503 // true 復(fù)制代碼

2. Number.isFinite(), Number.isNaN()

Number.isFinite()判斷數(shù)值是不是有限的,

Number.isNaN()判斷是否是NaN

而這兩個(gè)新方法只對數(shù)值有效,非數(shù)值一律返回false。

3. Number.parseInt(), Number.parseFloat()

ES6將全局方法parseInt()和parseFloat(),移植到Number對象上面,行為完全保持不變。

// ES6的寫法 Number.parseInt('12.34') // 12 Number.parseFloat('123.45#') // 123.45 復(fù)制代碼

4. Number.isInteger()

Number.isInteger()用來判斷一個(gè)值是否為整數(shù)。需要注意的是,在JavaScript內(nèi)部,整數(shù)和浮點(diǎn)數(shù)是同樣的儲(chǔ)存方法,所以3和3.0被視為同一個(gè)值。

Number.isInteger(25) // true Number.isInteger(25.0) // true Number.isInteger(25.1) // false Number.isInteger("15") // false Number.isInteger(true) // false 復(fù)制代碼

5. Number.EPSILON

Number.EPSILON // 2.220446049250313e-16 復(fù)制代碼

Number.EPSILON可以用來描述一個(gè)可以接受的誤差范圍。

function withinErrorMargin (left, right) {return Math.abs(left - right) < Number.EPSILON; } withinErrorMargin(0.1 + 0.2, 0.3) // true withinErrorMargin(0.2 + 0.2, 0.3) // false 復(fù)制代碼

6. Math.trunc()

Math.trunc()方法用于去除一個(gè)數(shù)的小數(shù)部分,返回整數(shù)部分

Math.trunc(4.1) // 4 Math.trunc(4.9) // 4 Math.trunc(-4.1) // -4 Math.trunc(-4.9) // -4 Math.trunc(-0.1234) // -0 復(fù)制代碼

對于非數(shù)值,Math.trunc內(nèi)部使用Number方法將其先轉(zhuǎn)為數(shù)值

Math.trunc('123.456') // 123 復(fù)制代碼

對于空值和無法截取整數(shù)的值,返回NaN。

Math.trunc(NaN); // NaN Math.trunc('foo'); // NaN Math.trunc(); // NaN 復(fù)制代碼

7. Math.sign()

Math.sign方法用來判斷一個(gè)數(shù)到底是正數(shù)、負(fù)數(shù)、還是零。

參數(shù)為正數(shù),返回+1;

參數(shù)為負(fù)數(shù),返回-1;

參數(shù)為0,返回0;

參數(shù)為-0,返回-0;

其他值,返回NaN。

Math.sign(-5) // -1 Math.sign(5) // +1 Math.sign(0) // +0 Math.sign(-0) // -0 Math.sign(NaN) // NaN Math.sign('foo'); // NaN Math.sign(); // NaN復(fù)制代碼

8. Math.cbrt()

求立方根,參數(shù)不是數(shù)字轉(zhuǎn)為數(shù)字,非法參數(shù)返回NaN

Math.cbrt('8') // 2 Math.cbrt('hello') // NaN 復(fù)制代碼

9. Math.clz32()

JavaScript的整數(shù)使用32位二進(jìn)制形式表示,Math.clz32方法返回一個(gè)數(shù)的32位無符號(hào)整數(shù)形式有多少個(gè)前導(dǎo)0。

Math.clz32(0) // 32 Math.clz32(1) // 31 Math.clz32(1000) // 22 復(fù)制代碼

10. Math.imul()

Math.imul方法返回兩個(gè)數(shù)以32位帶符號(hào)整數(shù)形式相乘的結(jié)果,返回的也是一個(gè)32位的帶符號(hào)整數(shù)。

Math.imul(2, 4) // 8 Math.imul(-1, 8) // -8 Math.imul(-2, -2) // 4 復(fù)制代碼

如果只考慮最后32位,大多數(shù)情況下,Math.imul(a, b)與a * b的結(jié)果是相同的,即該方法等同于(a * b)|0的效果(超過32位的部分溢出)。之所以需要部署這個(gè)方法,是因?yàn)镴avaScript有精度限制,超過2的53次方的值無法精確表示。Math.imul方法可以返回正確的低位數(shù)值。

11. Math.fround()

Math.fround方法返回一個(gè)數(shù)的單精度浮點(diǎn)數(shù)形式。

Math.fround(0) // 0 Math.fround(1) // 1 Math.fround(1.337) // 1.3370000123977661 Math.fround(1.5) // 1.5 Math.fround(NaN) // NaN 復(fù)制代碼

12. Math.hypot()

Math.hypot方法返回所有參數(shù)的平方和的平方根。

Math.hypot(3, 4); // 5 復(fù)制代碼

13. 對數(shù)

//這里的=指數(shù)學(xué)的=

Math.expm1(x) = Math.exp(x) - 1 = ex - 1

Math.log1p(x) = Math.log(1 + x) = ln(1 + x)

//這里自帶的Math.log(x)是自然對數(shù)lnx,對于其他底的對數(shù),采用換底公式計(jì)算 logax = lnx / lna

Math.log10(x) = lgx

Math.log2(x) = log2x

14. 雙曲函數(shù)

Math.sinh(x)

Math.cosh(x)

Math.tanh(x)

Math.asinh(x)

Math.acosh(x)

Math.atanh(x)

雙曲函數(shù)有以下性質(zhì),和圓函數(shù)很相似

cosh2x - sinh2x = 1

tanhx * cothx = 1

sinh(x+y) = sinhxcoshy + coshxsinhy

cosh(x+y) = coshxcoshy + sinhxsinhy

......

15. 指數(shù)運(yùn)算符

ES7新增了一個(gè)指數(shù)運(yùn)算符(**),目前Babel轉(zhuǎn)碼器已經(jīng)支持。

2 ** 2 // 4 2 ** 3 // 8 復(fù)制代碼

指數(shù)運(yùn)算符可以與等號(hào)結(jié)合,形成一個(gè)新的賦值運(yùn)算符(**=)。

let a = 2; a **= 2; // 等同于 a = a * a; 復(fù)制代碼

轉(zhuǎn)載于:https://juejin.im/post/5b10fd8a6fb9a01e49293762

總結(jié)

以上是生活随笔為你收集整理的ES6 数值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。