29
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScriptの整数の精度が保たれるのは9007199254740991まで

Last updated at Posted at 2016-05-29

Javascriptの整数の精度が保たれるのは2の64乗ではなく、2の53乗までらしい。

console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(Math.pow(2, 53) - 1); // 9007199254740991
console.log(Math.pow(2, 53) + 0); // 9007199254740992
console.log(Math.pow(2, 53) + 1); // 9007199254740992 誤差
console.log(Math.pow(2, 53) + 2); // 9007199254740994
console.log(Math.pow(2, 53) + 3); // 9007199254740996 誤差
console.log(Math.pow(2, 53) + 4); // 9007199254740996
console.log(Math.pow(2, 53) + 5); // 9007199254740996 誤差

なお、9007199254740991Number.MAX_SAFE_INTEGERで定義されています。

これは、JavaScriptには、整数型がなく全て浮動小数点型で表現されるためのようです。JavaScriptの浮動小数点型はIEEE 754に準拠していると思われます。

64ビット倍精度
2000px-IEEE_754_Double_Floating_Point_Format.svg.png
IEEE 754 - Wikipediaより

29
23
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?