Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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より

suin
Qiita 4位/TypeScript入門書執筆中/TypeScripterのための座談会「YYTypeScript」主催/『実践ドメイン駆動設計』書籍邦訳レビュア/分報Slack考案/YYPHP主催/CodeIQマガジン執筆/株式会社クラフトマンソフトウェア創設/Web自動テスト「ShouldBee」の開発/TypeScript/DDD/OOP
https://yyts.connpass.com/
shouldbee
開発者向けテスト支援サービスShouldBeeを開発・運営するスタートアップ(onlab第8期)
http://shouldbee.at
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした