简体   繁体   English

C中数据类型的最大值

[英]Max value of datatypes in C

I am trying to understand the maximum value that I can store in C. I tried doing printf("%f", pow(2, x)) . 我试图了解可以在C中存储的最大值。我尝试执行printf("%f", pow(2, x)) The answer holds good until x = 1023. It says Inf when x = 1024. 直到x = 1023为止,答案一直有效。当x = 1024时,答案为Inf

I am sorry that it is a basic question but I am trying to understand how C assigns datatypes' sizes based on my machine. 抱歉,这是一个基本问题,但是我试图了解C如何根据我的机器分配数据类型的大小。

I have a Mac (64-bit processor). 我有一台Mac(64位处理器)。 A clear understanding that I have is that my processor being a 64-bit one, it will be able to do calculations up to the value (2 64 ). 我清楚地知道,我的处理器是64位处理器,它最多可以进行计算(2 64 )。 Clearly pow(2, 1023) is greater than that. 显然pow(2, 1023)大于该值。 But my program is working fine till x = 1023. How is this possible? 但是我的程序在x = 1023之前运行良好。这怎么可能? Is GNU compiler has something to do with this? GNU编译器与此有关吗?

If this is a duplicate of other question kindly give the link. 如果这是其他问题的重复,请提供链接。

In C the pow() functions returns a double , and the double type is typically a 64-bit IEEE format representation of a floating point number. 在C中, pow()函数返回double ,并且double类型通常是浮点数的64位IEEE格式表示。

The basic idea of floating point is to express a number in the same general way as eg 1.234×10 56 . 浮点数的基本思想是用与1.234×10 56相同的通用方式表示数字。 Here you have a mantissa 1.234 and an exponent 56. C++, and probably also C, allows decimal representation for floating point numbers (but not for integer types), but in practice the internal representation will be binary, with a power of 2 rather than a power of 10. 在这里,您有一个尾数 1.234和一个指数 56。C++(可能还有C)允许使用浮点数的十进制表示形式(但不能用于整数类型),但实际上内部表示形式是二进制的,乘方为2,而不是2 10的幂。

The limit you ran up against was the supported range for the exponent in your compiler's representation of double numbers; 您遇到的限制是编译器的double数表示形式所支持的指数范围; probably 64-bit IEEE 754 . 大概是64位IEEE 754


The limits of the various built-in integral numerical types are available as symbolic constants from <limits.h> . 各种内置整数数值类型的极限值可以从<limits.h>作为符号常量获得。 The limits of the built-in floating point types are available as symbolic constants from <float.h> . 内置浮点类型的限制可从<float.h>作为符号常量使用。 See the table over at cppreference.com for more details. 有关更多详细信息,请参见cppreference.com上的表格。

In C++ these limits are also available via the numeric_limits class template from <limits> . 在C ++中,这些限制也可以通过<limits>numeric_limits类模板获得。

"64-bit processor" typically means that it can deal with integers that contain at most 64 bits at a time (ie in a single instruction), not that it can only process numbers with 64 binary digits or less. “64位处理器” 一般意味着它可以处理包含至多64个比特在时间 (即,在单个指令中)的整数,而不是它只能用64位二进制数字处理的数字或更少。 Using arbitrary precision arithmetic you can do calculations on numbers that are arbitrarily large, provided that you have enough memory (and time), just like how us humans can do operations on big values with only 10 fingers. 使用任意精度算术 ,只要您有足够的内存(和时间),就可以对任意大的数字进行计算,就像人类可以只用十根手指对大值进行运算一样。 Read more here: What is the biggest number you can generate using a 64-bit processor? 在此处阅读更多信息: 使用64位处理器可以产生的最大数量是多少?

However pow(2, 1023) is a little bit different. 但是pow(2, 1023)有点不同。 It's not an integer but a floating-point number (of type double in C) represented by a sign, a mantissa and an exponent like this (-1) sign × 1 × 2 1023 . 它不是整数,而是由符号,尾数和指数(如(-1) 符号 ×1×2 1023 )表示的浮点数 (C中为double类型)。 Not all the digits are stored so it's only accurate to the first few digits. 并非所有数字都已存储,因此仅精确到前几个数字。 However most systems use binary floating-point types so they can store the precise value of a power of 2 up to a large exponent depending on the exponent range. 但是,大多数系统使用二进制浮点类型,因此根据指数范围,它们可以存储2的幂的精确值,直到较大的指数。 Most modern systems' floating-point types conform to IEEE-754 standard with double maps to binary64/double precision , therefore the maximum value will be 大多数现代系统的浮点类型都符合IEEE-754标准,并具有对double64 / double precision的 double映射,因此最大值为

2 1023 × (1 + (1 − 2 −52 )) ≈ 1.7976931348623157 × 10 308 2 1023 ×(1 +(1 − 2 −52 ))≈1.7976931348623157× 10308

The maximum value for a double is DBL_MAX . double DBL_MAX的最大值是DBL_MAX This is defined by <float.h> in C, or <cfloat> in C++. C中<float.h>或C ++中的<cfloat> 定义 The numeric value may vary across systems, but you can always refer to it by the macro DBL_MAX . 数值可能会因系统而异,但是您始终可以通过宏DBL_MAX引用它。

You can print this: 您可以打印以下内容:

printf("%f\n", DBL_MAX);

The integer data types all have similar macros defined in <limits.h> : eg ULLONG_MAX is the biggest value for unsigned long long . 整数数据类型都具有在<limits.h>定义的相似宏:例如, ULLONG_MAXunsigned long long If printing with printf make sure to use the correct format specifier. 如果使用printf打印,请确保使用正确的格式说明符。

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

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