java 为什么 float 和 int 具有如此不同的最大值,即使它们的位数相同?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11158476/
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-10-31 04:00:47  来源:igfitidea点击:

Why do float and int have such different maximum values even though they're the same number of bits?

c#javac++floating-pointint

提问by idish

Possible Duplicate:
what the difference between the float and integer data type when the size is same in java?

可能的重复:
当 java 中的大小相同时,float 和 integer 数据类型之间有什么区别?

As you probably know, both of these types are 32-bits.intcan hold only integer numbers, whereas floatalso supports floating pointnumbers (as the type names suggest).

您可能知道,这两种类型都是 32 位。int只能容纳整数,同时float也支持浮点数(正如类型名称所暗示的那样)。

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

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

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.

我认为它int的最大值容量应该高于浮点数,因为它不为浮点数节省内存并且只接受整数。在这种情况下,我会很高兴得到解释。

回答by AakashM

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

您的直觉非常正确地告诉您,一个中的信息内容不能多于另一个,因为它们都有 32 位。但这并不意味着我们不能使用这些位来表示不同的值。

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

假设我发明了两种新的数据类型,uint4以及foo4. uint4使用 4 位来表示一个整数,在标准的二进制表示中,所以我们有

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

But foo4uses 4 bits to represent these values:

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

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

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

尽管具有相同的位数,但现在foo4具有比广泛的值范围uint4!如何?因为有些uint4不能用 表示foo4,所以位映射中的那些“槽”可用于其他值。



It is the same for intand float- they can both store values from a set of 232values, just differentsets of 232values.

这是相同的intfloat-它们可以从一组2倍的两个存储值32的值,只是不同组2个的32值。

回答by Rotem

A float might store a higher number value, but it will not be precise even on digits before the decimal dot. Consider the following example:

浮点数可能存储更高的数字值,但即使在小数点之前的数字上也不会精确。考虑以下示例:

float a = 123456789012345678901234567890f; //30 digits
Console.WriteLine(a);  // 1.234568E+29

Notice that barely any precision is kept.

请注意,几乎没有保留任何精度。

An integer on the other hand will always precisely store any number within its range of values.

另一方面,整数将始终精确地存储其值范围内的任何数字。

For the sake of comparison, let's look at a double precision floating point number:

为了比较,我们来看一个双精度浮点数:

double a = 123456789012345678901234567890d; //30 digits
Console.WriteLine(a); // 1.23456789012346E+29

Notice that roughly twice as much significant digits are preserved.

请注意,保留的有效数字大约是原来的两倍。

回答by harold

The hint is in the "floating" part of "floating point". What you say basically assumes fixed point. A floating point number does not "reserve space" for the digits after the decimal point - it has a limited number of digits (23 binary) and remembers what power of two to multiply it by.

提示在“floating point”的“floating”部分。你所说的基本上假设固定点。浮点数不会为小数点后的数字“保留空间”——它的数字数量有限(23 位二进制)并记住要乘以的 2 的幂。

回答by kosa

These are based on IEEE754 floating point specification, that is why it is possible. Please read this documentation. It is not just about how many bits.

这些基于 IEEE754 浮点规范,这就是为什么它是可能的。请阅读本文档。这不仅仅是关于多少位。