iT邦幫忙

2021 iThome 鐵人賽

DAY 8
0
Modern Web

初學者跪著學JavaScript系列 第 8

初學者跪著學JavaScript Day8 : 資料型別:BigInt

ㄧ日客家話:中文:去哪裡 客語:誒 ㄏㄧ 賴 誒

BigInt對數學、金融、科學來說是很重要的,因為當number大於某範圍會有精確度問題,故會將值當作string處理,但BigInt是可以表示為numeric values


JavaScript 是雙精度浮點數

會留一個位置給科學記好左邊的1
所以是2的53次方

Number最大安全表示:=9,007,199,254,740,991

9,007,199,254,740,991是最大且安全的整數

console.log(9007199254740991 + 1 === 9007199254740991 + 2);// true
console.log(9007199254740991n + 1n === 9007199254740991n + 2n);// false

要安全地使用大於此值的整數,您需要使用BigInt。
ECMA:

  • 數值型別numeric type
  • 為了表示64位元的整數但BigInt可以達到數百萬個位數
  • BigInt() as a function for converting regular JavaScript numbers or strings to BigInt values
  • 只能表示整數

使用方式

方式一:BigInt literals :數字+小寫n (預設十進位)ex:10n

也可以使用其他進位:
0b(二進位):0b11n
0o(八進位):0o17n
base10(十進位):10n
0x(十六進位):0x1fn

方式二:呼叫BigInt()將數字或字串轉為BigInt值

console.log(Number.MAX_SAFE_INTEGER);//9007199254740991(2 ** 53 - 1) js最安全大值
console.log(Number.MIN_SAFE_INTEGER);//-9007199254740991(-(2 ** 53 - 1)). js最小安全值

console.log(BigInt(Number.MAX_SAFE_INTEGER));
//9007199254740991n

console.log(BigInt(Number.MIN_SAFE_INTEGER));
//-9007199254740991n

當使用浮點數則無法使用

console.log(BigInt(100.6));
//RangeError: The number 100.6 cannot be converted to a BigInt because it is not an integer

可以進行算數運算 : +、-、*、/、%、 **

const value = 300n + 200n;
console.log(value);//500n

const a = BigInt(300);
const b = BigInt(200);
console.log(a + b)//500n
console.log(a - b)//100n
console.log(a % b)//100n

那進行除法運算呢?餘數會直接捨棄

const a = BigInt(300);
const b = BigInt(200);
console.log(a / b);//1n

數字和bigint不能混用

let bigint = 10n;
let number = 100;
console.log(bigint + number);//TypeError: Cannot mix BigInt and other types, use explicit conversions

只能Bigint轉成數字或number轉成Bigint

console.log(bigint + BigInt(number));//110n
console.log(Number(bigint) + number);//110

無法使用+來轉型別

let bigint = 1n;
console.log(+bigint);
//TypeError: Cannot convert a BigInt value to a number
//無法轉譯

比較

console.log(1 == 1n); 
//true
console.log(1 === 1n);
//false

運算2的53次方-1以上時只使用BigInt

當Number超過2的53次方-1以上的數轉成BigInt也會失準

b = BigInt(9007199254740993)
console.log(b)//9007199254740992n
console.log(Boolean(0n)); //false

v8使用Arbitrary-precision arithmetic 解決Number浮點精度問題

BigInt 相乘實作方式

有興趣可以到V8官網研究

最後大家可以斟酌何時使用number和BigInt

在tc39/proposal-bigint 說
Don't break math
The semantics of all operators should ideally be based on some mathematical first principles, to match developer expectations. The division and modulo operators are based on conventions from other programming languages for integers.

IEEE-754 References
V8
知乎:BigInt:JavaScript 中的任意精度整数
BigInt, arbitrary precision integers in JavaScript
mdn
js大全
tc39-proposal-bigint


上一篇
初學者跪著學JavaScript Day7 : 資料型別 : Symbol
下一篇
初學者跪著學JavaScript Day9 : 讓我困惑的Literals
系列文
初學者跪著學JavaScript30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

0
Hooo
iT邦新手 4 級 ‧ 2021-09-23 15:19:28

終於了解BigInt 棒棒!

0
南國安迪
iT邦新手 3 級 ‧ 2021-09-23 19:42:00

長知識了
/images/emoticon/emoticon18.gif

我要留言

立即登入留言