为什么float和int具有不同的最大值,即使它们的位数相同? [英] Why do float and int have such different maximum values even though they're the same number of bits?

查看:602
本文介绍了为什么float和int具有不同的最大值,即使它们的位数相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

您可能知道,这两种类型都是32位。 int 只能包含整数,而 float 也支持浮点数(如类型名称所示)。

As you probably know, both of these types are 32-bits.int can hold only integer numbers, whereas float also supports floating point numbers (as the type names suggest).

那么的最大值怎么可能呢? int 是2 31 float 的最大值是3.4 * 10 38 ,虽然它们都是32位?

How is it possible then that the max value of int is 231, and the max value of float is 3.4*1038, while both of them are 32 bits?

我认为 int 的最大值容量应该高于float因为它没有为浮点数保存内存而只接受inte ger号码。在这种情况下,我会很高兴得到解释。

I think that int's max value capacity should be higher than the float because it doesn't save memory for the floating number and accepts only integer numbers. I'll be glad for an explanation in that case.

推荐答案

你的直觉很正确地告诉你,不能再有了<强>信息内容在一个比另一个,因为它们都有32位。但这并不意味着我们不能将这些位用于表示不同的值。

Your intuition quite rightly tells you that there can be no more information content in one than the other, because they both have 32 bits. But that doesn't mean we can't use those bits to represent different values.

假设我发明了两种新的数据类型, uint4 foo4 uint4 使用4位来表示标准二进制表示形式的整数,因此我们有

Suppose I invent two new datatypes, uint4 and foo4. uint4 uses 4 bits to represent an integer, in the standard binary representation, so we have

bits   value
0000       0
0001       1
0010       2
...
1111      15

但是 foo4 使用4位来表示这些值:

But foo4 uses 4 bits to represent these values:

bits   value
0000       0
0001      42
0010     -97
0011       1
...
1110      pi
1111       e

现在 foo4 具有比 uint4 更多更宽的值,尽管具有相同的位数!怎么样?因为有一些 uint4 不能 foo4 表示,所以那些位映射中的槽可用于其他值。

Now foo4 has a much wider range of values than uint4, despite having the same number of bits! How? Because there are some uint4 values that can't be represented by foo4, so those 'slots' in the bit mapping are available for other values.

相同int float - 它们都可以存储来自一组2个 32 值的值,只是不同成套2 32 值。

It is the same for int and float - they can both store values from a set of 232 values, just different sets of 232 values.

这篇关于为什么float和int具有不同的最大值,即使它们的位数相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆