简体   繁体   English

了解DBL_MAX

[英]Understanding DBL_MAX

I just read about the IEEE 754 standard in order to understand how single-precision and double-precision floating points are implemented. 我刚刚阅读了IEEE 754标准,以了解如何实现单精度双精度浮点。

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;
}

Output: 输出:

8

9007199254740992.000000

9007199254740992.000000

179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000

What I don't understand is that I expected the last line of my output to be: (2^53-1) * 2^(1024-52), but the number on the last line corresponds approximately to 2^(2^10). 我不明白的是,我期望输出的最后一行是:(2 ^ 53-1)* 2 ^(1024-52),但是最后一行上的数字大约对应于2 ^(2 ^ 10)。 What am I missing? 我想念什么? How DBL_MAX is calculated exactly? 如何DBL_MAX计算DBL_MAX

EDIT: Little explanation about the exact value of DBL_MAX : 编辑:关于DBL_MAX确切值的很少解释:

As explained in the accepted answer the largest value of the exponent is 2^1023 and not 2^1024 as I tought. 如公认的答案中所述,指数的最大值是2 ^ 1023,而不是我所讲的2 ^ 1024。 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) 因此, DBL_MAX确切值为: (2^53-1)*(2^(1023-52)) (因此,由于尾数比2稍小,因此它比2 ^ 10略小)

Double are represented as m*2^e where m is the mantissa and e is the exponent. Double以m*2^e表示,其中m是尾数, e是指数。 Doubles have 11 bits for the exponent. 双打具有11位的指数。 Since the exponent can be negative there is an offset of 1023 . 由于指数可以为负,因此存在1023的偏移量。 That means that the real calculation is m*2^(e-1023) . 这意味着实际计算是m*2^(e-1023) The largest 11 bit number is 2047 . 最大的11位数字是2047 The exponent 2047 is reserved for storing inf and NaN . 保留指数2047用于存储infNaN This means the largest double is m*2^(2046-1023) = m*2^(1023) . 这意味着最大的m*2^(2046-1023) = m*2^(1023)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: 尾数是1到2之间的数字。这意味着,当m几乎为2时,可获得最大的倍数。

DBL_MAX = max(m)*2^1023 ~ 2*2^1023 = 2^1024 = 2^(2^10)

As you can see here this is pretty much the standard value of DBL_MAX . 如您所见这几乎是DBL_MAX的标准值。

DBL_MAX is the largest value a double can hold. DBL_MAX是double可以容纳的最大值 Its value is not related to the number of bits in the mantissa. 它的值与尾数的位数无关。

The limit is mostly related to the maximum exponent. 该限制主要与最大指数有关。 For IEEE-754, it is about 1.8e+308 or 2^1023. 对于IEEE-754,约为1.8e + 308或2 ^ 1023。

The definition is usually #define DBL_MAX 1.79769313486231470e+308 定义通常是#define DBL_MAX 1.79769313486231470e+308

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM