C语言 int 和 float 的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7079468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:24:52  来源:igfitidea点击:

Size of int and float

cfloating-pointintegerfloating-point-conversion

提问by atul

I have a question about the ranges of ints and floats:

我有一个关于整数和浮点数范围的问题:

If they both have the same size of 4 bytes, why do they have different ranges?

如果它们都具有相同的 4 个字节大小,为什么它们的范围不同?

回答by Paul R

They are totally different - typically intis just a straightforward 2's complement signed integer, while floatis a single precision floating point representation with 23 bits of mantissa, 8 bits exponent and 1 bit sign (see http://en.wikipedia.org/wiki/IEEE_754-2008).

它们完全不同 - 通常int只是一个简单的 2 的补码有符号整数,float而是具有 23 位尾数、8 位指数和 1 位符号的单精度浮点表示(参见http://en.wikipedia.org/wiki/ IEEE_754-2008)。

回答by John Bode

They have different ranges of values because their contents are interpreted differently; in other words, they have different representations.

它们具有不同的值范围,因为它们的内容被不同地解释;换句话说,它们有不同的表示

Floats and doubles are typically represented as something like

浮点数和双精度数通常表示为

+-+-------+------------------------+
| |       |                        |
+-+-------+------------------------+
 ^    ^                ^
 |    |                |
 |    |                +--- significand
 |    +-- exponent
 |
 +---- sign bit

where you have 1 bit to represent the sign s(0 for positive, 1 for negative), some number of bits to represent an exponent e, and the remaining bits for a significand, or fraction f. The value is being represented is s * f * 2e.

其中有 1 位表示符号s(0 表示正,1 表示负),一些位表示指数e,其余位表示有效数或分数f。表示的值是s * f * 2 e

The rangeof values that can be represented is determined by the number of bits in the exponent; the more bits in the exponent, the wider the range of possible values.

可以表示的值的范围由指数中的位数决定;指数中的位数越多,可能值的范围就越宽。

The precision(informally, the size of the gap between representable values) is determined by the number of bits in the significand. Not all floating-point values can be represented exactly in a given number of bits. The more bits you have in the significand, the smaller the gap between any two representable values.

精度(非正式地,可表示的值之间的间隙的尺寸)是由在有效数位的数目确定的。并非所有浮点值都可以用给定的位数精确表示。有效数中的位数越多,任何两个可表示值之间的差距就越小。

Each bit in the significand represents 1/2n, where nis the bit number counting from the left:

有效数中的每一位代表 1/2 n,其中n是从左边开始计数的位数:

 110100...
 ^^ ^
 || |  
 || +------ 1/2^4 = 0.0625
 || 
 |+-------- 1/2^2 = 0.25
 |
 +--------- 1/2^1 = 0.5
                    ------
                    0.8125

Here's a link everyone should have bookmarked: What Every Computer Scientist Should Know About Floating Point Arithmetic.

这是每个人都应该收藏的链接:What Every Computer Scientist should Know About Floating Point Arithmetic

回答by John Humphreys - w00te

Two types with the same size in bytes can have different ranges for sure.

具有相同字节大小的两种类型肯定可以具有不同的范围。

For example, signed int and unsigned int are both 4 bytes, but one has one of its 32 bits reserved for the sign, which lowers the maximum value by a factor of 2 by default. Also, the range is different because the one can be negative. Floats on the other hand lose value range in favor of using some bits for decimal range.

例如,signed int 和 unsigned int 都是 4 个字节,但其中一个为符号保留了 32 位中的一个,默认情况下将最大值降低 2 倍。此外,范围是不同的,因为一个可以是负数。另一方面,浮点数会丢失值范围,而倾向于使用一些位作为十进制范围。

回答by John Humphreys - w00te

The standard does not specify the size in bytes, but it specifies minimum ranges that various integral types must be able to hold. You can infer minimum size in bytes from it.

该标准没有指定以字节为单位的大小,但它指定了各种整数类型必须能够容纳的最小范围。您可以从中推断出最小大小(以字节为单位)。

Minimum ranges guaranteed by the standard (from "Integer Types In C and C++"):

标准保证的最小范围(来自“C 和 C++ 中的整数类型”):

signed char: -127 to 127
unsigned char: 0 to 255
"plain" char: -127 to 127 or 0 to 255 (depends on default char signedness)
signed short: -32767 to 32767
unsigned short: 0 to 65535
signed int: -32767 to 32767
unsigned int: 0 to 65535
signed long: -2147483647 to 2147483647
unsigned long: 0 to 4294967295
signed long long: -9223372036854775807 to 9223372036854775807
unsigned long long: 0 to 18446744073709551615

Actual platform-specific range values are found in in C, or in C++ (or even better, templated std::numeric_limits in header).

实际特定于平台的范围值可在 C 或 C++ 中找到(甚至更好,标头中的模板化 std::numeric_limits)。

Standard only requires that:

标准只要求:

sizeof(short int) <= sizeof(int) <= sizeof(long int)

sizeof(short int) <= sizeof(int) <= sizeof(long int)

floatdoes not have the same "resolution" as an intdespite their seemingly similar size. intis 2's complement whereas floatis made up of 23 bits Mantissa, 8 bits of exponent, and 1 bit of sign.

floatint尽管它们的大小看似相似,但没有与 a 相同的“分辨率” 。int是 2 的补码,而float由 23 位尾数、8 位指数和 1 位符号组成。

回答by Itamar Katz

You are mixing the representation of a number, which is dependent upon some rules you (or somebody else) defines, and the way you use to keep the numberin the computer (the bytes).

您正在混合一个 number表示,这取决于您(或其他人)定义的一些规则,以及您用来将数字保留在计算机中的方式(字节)。

For example, you can use only one bit to keep a number, and decide that 0represents -100, and 1represents +100. Or that 0represents .5 and 1represents 1.0. The 2 things, the data and the meaning of the data, are independent.

例如,你可以只用一位来保留一个数字,并决定0代表-100,1代表+100。或者0代表 0.5 和1代表 1.0。数据和数据的含义这两件事是独立的。

回答by Macmade

An integer is just a number... It's range depends on the number of bits (different for a signed or unsigned integer).

整数只是一个数字......它的范围取决于位数(有符号或无符号整数不同)。

A floating point number is a whole different thing. It's just a convention about representing a floating point number in binary...

浮点数是完全不同的东西。这只是关于以二进制表示浮点数的约定......

It's coded with a sign bit, an exponent field, and a mantissa.

它用符号位、指数字段和尾数编码。

Read the following article:

阅读以下文章:

http://www.eosgarden.com/en/articles/float/

http://www.eosgarden.com/en/articles/float/

It will make you understand what are floating point values, from a binary perspective. The you'll understand the range thing...

它将让您从二进制的角度了解什么是浮点值。你会理解范围的事情......