了解DBL_MAX [英] Understanding DBL_MAX
问题描述
我刚刚看了一下 IEEE 754 以了解的单precision 并的双击precision 浮动点实现的。
I just read about the IEEE 754 standard in order to understand how single-precision and double-precision floating points are implemented.
所以我写了这个检查我的理解:
So I wrote this to check my understanding:
#include <stdio.h>
#include <float.h>
int main() {
double foo = 9007199254740992; // 2^53
double bar = 9007199254740993; // 2^53 + 1
printf("%d\n\n", sizeof(double)); // Outputs 8. Good
printf("%f\n\n", foo); // 9007199254740992.000000. Ok
printf("%f\n", bar); // 9007199254740992.000000. Ok because Mantissa is 52 bits
printf("%f\n\n", DBL_MAX); // ??
return 0;
}
输出:
8
9007199254740992.000000
9007199254740992.000000
179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000
我不明白的是,我希望我的输出的最后一行是:(2 ^ 53-1)* 2 ^(1024至1052年),但在最后一行数字大致相当于2 ^ (2 ^ 10)。我在想什么?如何 DBL_MAX
被精确计算?
编辑:
Little explanation about the exact value of DBL_MAX
:
作为公认的答案解释了指数的最大值为2 ^ 1023,而不是2 ^ 1024,我因子评分。因此,在精确 DBL_MAX
的值是:
(2 ^ 53-1)*(2 ^(1023年至1052年))
(以便预期是略小于2 ^ 10自尾数小于一个第2位)
As explained in the accepted answer the largest value of the exponent is 2^1023 and not 2^1024 as I tought. So the exact value of DBL_MAX
is:
(2^53-1)*(2^(1023-52))
(so as expected it's slightly smaller than 2^10 since the mantissa is a bit smaller than 2)
推荐答案
双重新presented为 M * 2 ^ E
,其中米
是尾数和电子
是指数。双打有指数11位。由于该指数可以是负的存在的偏移 1023
。这意味着,真正的计算 M * 2 ^(E-1023)
。最大的11位编号为 2047
。该指数 2047
被保留用于存储 INF
和 NaN的
。这意味着最大的双为 M * 2 ^(2046年至1023年)= M * 2 ^(1023)
。尾数是一个数字1和2之间这意味着最大的是双时达到 M
几乎是2。因此,我们有:
Double are represented as m*2^e
where m
is the mantissa and e
is the exponent. Doubles have 11 bits for the exponent. Since the exponent can be negative there is an offset of 1023
. That means that the real calculation is m*2^(e-1023)
. The largest 11 bit number is 2047
. The exponent 2047
is reserved for storing inf
and NaN
. This means the largest double is m*2^(2046-1023) = m*2^(1023)
. The mantissa is a number between 1 and 2. This means that the largest double is attained when m
is almost 2. So we have:
DBL_MAX = max(m)*2^1023 ~ 2*2^1023 = 2^1024 = 2^(2^10)
正如你所看到的这rel=\"nofollow\">是pretty多少标准值 DBL_MAX
。
As you can see here this is pretty much the standard value of DBL_MAX
.
这篇关于了解DBL_MAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!