我知道JavaScript数字只是“双”数,并且对于小数部分只有52位精度。但是,REAL JavaScript数字似乎对于大量数字具有更多实际精度。
例如,预定义常量Number.MAX_VALUE
表示Number类型的最大正有限值,约为1.7976931348623157e + 308。在这里,我可以使用模数运算符访问此值的尾随数字。
> Number.MAX_VALUE
1.7976931348623157e+308
> Number.MAX_VALUE % 10000000000
4124858368
从这个结果我可以假设这个数字是7fef ffff ffff ffff
代表(1 +(1 - 2 ** -52))×2 ** 1023(Wikipedia)并且可以转录成确切的形式如下:
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
...我们只看到这309位数字的尾随10位数字。所以我认为每个JavaScript编号都必须有十进制格式的精确数字。
我的问题是:如何在JavaScript中获取这个309位的字符串?像Number.MAX_VALUE / 10000000000 % 10000000000
这样的挑战因为如此庞大而失败了。
此外,Number.MIN_VALUE
等微小数字怎么样?这必须是小数形式的以下分数。
0.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004940656458412465441765687928682213723650598026143247644255856825006755072702087518652998363616359923797965646954457177309266567103559397963987747960107818781263007131903114045278458171678489821036887186360569987307230500063874091535649843873124733972731696151400317153853980741262385655911710266585566867681870395603106249319452715914924553293054565444011274801297099995419319894090804165633245247571478690147267801593552386115501348035264934720193790268107107491703332226844753335720832431936092382893458368060106011506169809753078342277318329247904982524730776375927247874656084778203734469699533647017972677717585125660551199131504891101451037862738167250955837389733598993664809941164205702637090279242767544565229087538682506419718265533447265625
答案 0 :(得分:0)
MAX_VALUE的所有数字是:
179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
查看以下代码:
答案 1 :(得分:0)
IEEE浮点数的实际实现有点(小!!!)令人困惑。
如果你想到一个更简单的形式,我觉得它会有所帮助,除了在IEEE格式更好的溢出和下溢之外,它会在任何地方做出相应的反应。
这是表格:
浮点数包括:
数字的符号很简单 - 它前面有一个减号。
该值计算如下:
v*2ⁿ
如果指数的符号为正,则指数基本上为2 * 2 * 2 * ... * 2,指定的数量为两个。如果一个大数字用十进制表示,它将有很多位数一直到小数点但是它们没有意义。如果在大约53个二进制数字后显示二进制数字,则其余数字将为零,您无法更改它们。
注意,使用正指数所有这些都是整数,浮点数(包括IEEE数)只要你不溢出就会用整数精确计算。当你溢出时,它们仍然表现良好,它们只有较低位的零。
只有当指数为负数时才有陌生感
v/(2ⁿ)
您为负指数获得的值仍然基于2 * 2 * 2 * ... * 2值,但您可以将其除以。因此,你试图用一半,四分之一,八分之一等代表十分之一......但这并不完全正常,所以你会得到四舍五入的错误和所有可爱的浮点问题。 / p>
您的示例值:
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368
二进制文件是
1111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
最后有很多零。
What every computer scientist should know about floating point