問題描述
大小相同時浮點數和整數數據類型有什么區別?
What the difference between the float and integer data type when size is same?
推薦答案
float
存儲浮點值,即可能有小數位的值int
只存儲整數值,即整數float
stores floating-point values, that is, values that have potential decimal placesint
only stores integral values, that is, whole numbers
因此,雖然兩者都是 32 位寬,但它們的使用(和表示)卻大不相同.您不能將 3.141 存儲在整數中,但可以存儲在 float
中.
So while both are 32 bits wide, their use (and representation) is quite different. You cannot store 3.141 in an integer, but you can in a float
.
進一步剖析它們:
在整數中,所有位用于存儲數值.這是(在 Java 和許多計算機中)在所謂的 two's 補碼 中完成的.這基本上意味著您可以將 −231 的值表示為 231 − 1.
In an integer, all bits are used to store the number value. This is (in Java and many computers too) done in the so-called two's complement. This basically means that you can represent the values of −231 to 231 − 1.
在浮點數中,這 32 位分為三個不同的部分:符號位、指數和尾數.它們的布局如下:
In a float, those 32 bits are divided between three distinct parts: The sign bit, the exponent and the mantissa. They are laid out as follows:
S EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM
有一個位可以確定數字是負數還是非負數(零既不是正數也不是負數,但符號位設置為零).然后有 8 位指數和 23 位尾數.為了從中獲得有用的數字,(大致)執行以下計算:
There is a single bit that determines whether the number is negative or non-negative (zero is neither positive nor negative, but has the sign bit set to zero). Then there are eight bits of an exponent and 23 bits of mantissa. To get a useful number from that, (roughly) the following calculation is performed:
M × 2E
(還有更多內容,但對于本次討論來說應該足夠了)
(There is more to it, but this should suffice for the purpose of this discussion)
尾數本質上不過是一個 24 位整數.這將乘以 2 的指數部分的冪,大致是在 &-128 和 127 之間的數字.
The mantissa is in essence not much more than a 24-bit integer number. This gets multiplied by 2 to the power of the exponent part, which, roughly, is a number between −128 and 127.
因此,您可以準確地表示所有適合 24 位整數的數字,但數字范圍也更大,因為更大的指數允許更大的值.例如,float
的最大值約為 3.4 × 1038 而 int
只允許最大為 2.1 × 109 的值.
Therefore you can accurately represent all numbers that would fit in a 24-bit integer but the numeric range is also much greater as larger exponents allow for larger values. For example, the maximum value for a float
is around 3.4 × 1038 whereas int
only allows values up to 2.1 × 109.
但這也意味著,由于 32 位只有 4.2 × 109 種不同的狀態(這些狀態都用于表示 int
可以存儲的值),所以在float
數字范圍的較大端,數字間隔更寬(因為唯一的 float
數字不能多于唯一的 int
數字).那么,您不能準確地表示某些數字.例如,數字 2 × 1012 在 float
中的表示形式為 1,999,999,991,808.這可能 接近 到 2,000,000,000,000,但并不準確.同樣,將 1 添加到該數字不會改變它,因為 1 太小而無法影響 float
在那里使用的較大比例.
But that also means, since 32 bits only have 4.2 × 109 different states (which are all used to represent the values int
can store), that at the larger end of float
's numeric range the numbers are spaced wider apart (since there cannot be more unique float
numbers than there are unique int
numbers). You cannot represent some numbers exactly, then. For example, the number 2 × 1012 has a representation in float
of 1,999,999,991,808. That might be close to 2,000,000,000,000 but it's not exact. Likewise, adding 1 to that number does not change it because 1 is too small to make a difference in the larger scales float
is using there.
同樣,您也可以在 float
中表示非常小的數字(介于 0 和 1 之間),但無論數字是非常大還是非常小,float
只能精度約為 6 或 7 位十進制數字.如果您的數字很大,則這些數字位于數字的開頭(例如 4.51534 × 1035,即 451534 后跟 30 個零 - 而 float
無法分辨關于這 30 位數字實際上是零還是其他任何有用的信息),對于非常小的數字(例如 3.14159 × 10−27),它們位于數字的遠端,遠遠超出起始數字0.0000...
Similarly, you can also represent very small numbers (between 0 and 1) in a float
but regardless of whether the numbers are very large or very small, float
only has a precision of around 6 or 7 decimal digits. If you have large numbers those digits are at the start of the number (e.g. 4.51534 × 1035, which is nothing more than 451534 follows by 30 zeroes – and float
cannot tell anything useful about whether those 30 digits are actually zeroes or something else), for very small numbers (e.g. 3.14159 × 10−27) they are at the far end of the number, way beyond the starting digits of 0.0000...
這篇關于大小相同時,浮點數和整數數據類型有什么區別?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!