int 和 float 的大小
我有一个关于 int 和 float 的范围的问题:
如果它们都具有相同的 4 字节大小,为什么它们有不同的范围?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
它们完全不同 - 通常
int
只是一个简单的 2 的补码有符号整数,而float
是单精度浮点表示,具有 23 位尾数、8 位指数和 1位符号(参见 http://en.wikipedia.org/wiki/IEEE_754-2008)。They are totally different - typically
int
is just a straightforward 2's complement signed integer, whilefloat
is 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).由于其内容的解释不同,因此它们具有不同的值范围;换句话说,它们有不同的表现形式。
浮点型和双精度型通常表示为:
用 1 位来表示符号 s(0 表示正数,1 表示负数),用一些位数表示指数 e,以及有效数字的剩余位,或小数 f。表示的值是s * f * 2e。
可以表示的值的范围由指数中的位数决定;指数中的位数越多,可能值的范围就越宽。
精度(非正式地,可表示值之间的间隙大小)由有效数中的位数确定。并非所有浮点值都可以用给定的位数精确表示。有效数中的位数越多,任何两个可表示值之间的差距就越小。
有效数中的每一位代表 1/2n,其中 n 是从左侧数起的位数:
这是每个人都应该添加书签的链接:每个计算机科学家应该了解的浮点知识算术。
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
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.
The range of 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 n is the bit number counting from the left:
Here's a link everyone should have bookmarked: What Every Computer Scientist Should Know About Floating Point Arithmetic.
具有相同字节大小的两种类型肯定可以具有不同的范围。
例如,signed int 和 unsigned int 都是 4 个字节,但其中的 32 位之一保留用于符号,默认情况下会将最大值降低 2 倍。此外,范围也不同,因为范围可能为负数。另一方面,浮点数失去了值范围,有利于使用一些位作为十进制范围。
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.
该标准没有指定字节大小,但指定了各种整数类型必须能够容纳的最小范围。您可以从中推断出最小大小(以字节为单位)。
标准保证的最小范围(来自“C 和 C++ 中的整数类型”):
实际的特定于平台的范围值可在 C 或 C++ 中找到(或者更好的是,标头中的模板化 std::numeric_limits)。
标准仅要求:
sizeof(short int) <= sizeof(int) <= sizeof(long int)
float
不具有与int
尽管它们看起来大小相似。int
是 2 的补码,而float
由 23 位尾数、8 位指数和 1 位符号组成。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++"):
Actual platform-specific range values are found in in C, or in C++ (or even better, templated std::numeric_limits in header).
Standard only requires that:
sizeof(short int) <= sizeof(int) <= sizeof(long int)
float
does not have the same "resolution" as anint
despite their seemingly similar size.int
is 2's complement whereasfloat
is made up of 23 bits Mantissa, 8 bits of exponent, and 1 bit of sign.您正在混合数字的表示,这取决于您(或其他人)定义的一些规则,以及您在计算机中保存数字的方式(字节)。
例如,您可以仅使用一位来保存数字,并确定
0
代表-100,1
代表+100。或者0
代表 0.5,1
代表 1.0。数据和数据的含义这两件事是独立的。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 number in the computer (the bytes).
For example, you can use only one bit to keep a number, and decide that
0
represents -100, and1
represents +100. Or that0
represents .5 and1
represents 1.0. The 2 things, the data and the meaning of the data, are independent.整数只是一个数字...
它的范围取决于位数(有符号或无符号整数不同)。
浮点数是完全不同的东西。
这只是用二进制表示浮点数的约定...
它用符号位、指数字段和尾数进行编码。
阅读以下文章:
http://www.eosgarden.com/en/articles/float/< /a>
它将让您从二进制角度理解什么是浮点值。你就会明白范围的事情......
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/
It will make you understand what are floating point values, from a binary perspective. The you'll understand the range thing...