码迷,mamicode.com
首页 > Web开发 > 详细

JS-Number 的精度

时间:2019-01-27 21:46:49      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:name   表示   就是   sign   lock   exp   nta   ase   double   

JS 使用 IEEE 754 的双精度数表示数字,1 位符号,10 位指数,53 位底数。

所以 JS 数字精度近似为 15.95 位 10 进制(10 ** 15.95)。

也就是说整部加小数部分超过 15 位再用 Number 类型就不合适了,可以peterolson/BigInteger.jsrauschma/strint(有 bug,代码少可以用来学习)等库用字符串进行计算。

注:准确来说精度是2 ** 53 = 9007199254740992,超过这个值计算出来的数据就不准了,例如:

> 2 ** 53
9007199254740992
> 2 ** 53 + 1
9007199254740992(false)
> 2 ** 53 + 2
9007199254740994(true)
> 2 ** 53 + 3
9007199254740996(false)

wiki 上的《IEEE 754 基本交换格式表》,binary64 就是 JS 中用的

Name Common name Base Significand Bits[b]/Digits Decimal digits Exponent bits Decimal E max Exponent bias[10] E min E max Notes
binary16 Half precision 2 11 3.31 5 4.51 24?1 = 15 ?14 +15 not basic
binary32 Single precision 2 24 7.22 8 38.23 27?1 = 127 ?126 +127
binary64 Double precision 2 53 15.95 11 307.95 210?1 = 1023 ?1022 +1023

参考:

JS-Number 的精度

标签:name   表示   就是   sign   lock   exp   nta   ase   double   

原文地址:https://www.cnblogs.com/jffun-blog/p/10327322.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!