当大小相同时,float和integer数据类型之间有什么区别?

时间:2022-10-13 16:36:34

What the difference between the float and integer data type when size is same?

当大小相同时,float和integer数据类型之间有什么区别?

2 个解决方案

#1


77  

  • float stores floating-point values, that is, values that have potential decimal places
  • float存储浮点值,即具有潜在小数位的值

  • int only stores integral values, that is, whole numbers
  • int只存储整数值,即整数

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.

因此,虽然两者都是32位宽,但它们的使用(和表示)却完全不同。您不能以整数存储3.141,但可以在浮点数中存储。

Dissecting them both a little further:

将它们进一步解剖:

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.

在整数中,所有位用于存储数值。这是(在Java和许多计算机中)在所谓的二进制补码中完成的。这基本上意味着您可以表示-231到231 - 1的值。

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:

在浮点数中,这32位在三个不同的部分之间划分:符号位,指数和尾数。它们的布局如下:

S EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM

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:

有一个位确定数字是负数还是非负数(零既不是正数也不是负数,但是符号位设置为零)。然后有八位指数和23位尾数。为了从中获得有用的数字,(大致)执行以下计算:

M × 2E

M×2E

(There is more to it, but this should suffice for the purpose of this discussion)

(还有更多内容,但这应该足以满足本次讨论的目的)

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位整数。这将乘以指数部分的幂乘以2,这大致是-128到127之间的数字。

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.

因此,您可以准确地表示适合24位整数的所有数字,但数值范围也要大得多,因为较大的指数允许更大的值。例如,浮点数的最大值约为3.4×1038,而int仅允许值高达2.1×109。

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.

但这也意味着,因为32位只有4.2×109个不同的状态(它们都用来表示int可以存储的值),在float的数值范围的较大一端,数字间隔较宽(因为不能有更多)唯一的浮点数比有唯一的int数)。那么你不能完全代表一些数字。例如,数字2×1012的浮点数为1,999,999,991,808。这可能接近2,000,000,000,000,但这并不准确。同样地,将1添加到该数字不会改变它,因为1太小而不能在较大的尺度上使用浮点数。

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...

类似地,您也可以在浮点数中表示非常小的数字(介于0和1之间),但无论数字是非常大还是非常小,浮点数的精度大约为6或7位十进制数。如果您有大数字,那么这些数字位于数字的开头(例如4.51534×1035,这不超过451534跟随30个零 - 并且浮点数不能告诉任何有用的关于这30个数字是否实际为零或其他东西),对于非常小的数字(例如3.14159×10-27),它们位于数字的远端,超出了0.0000的起始位数...

#2


2  

Floats are used to store a wider range of number than can be fit in an integer. These include decimal numbers and scientific notation style numbers that can be bigger values than can fit in 32 bits. Here's the deep dive into them: http://en.wikipedia.org/wiki/Floating_point

浮点数用于存储比整数更宽的数字范围。这些包括十进制数字和科学记数法样式编号,可以是大于32位的值。以下是对它们的深入了解:http://en.wikipedia.org/wiki/Floating_point

#1


77  

  • float stores floating-point values, that is, values that have potential decimal places
  • float存储浮点值,即具有潜在小数位的值

  • int only stores integral values, that is, whole numbers
  • int只存储整数值,即整数

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.

因此,虽然两者都是32位宽,但它们的使用(和表示)却完全不同。您不能以整数存储3.141,但可以在浮点数中存储。

Dissecting them both a little further:

将它们进一步解剖:

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.

在整数中,所有位用于存储数值。这是(在Java和许多计算机中)在所谓的二进制补码中完成的。这基本上意味着您可以表示-231到231 - 1的值。

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:

在浮点数中,这32位在三个不同的部分之间划分:符号位,指数和尾数。它们的布局如下:

S EEEEEEEE MMMMMMMMMMMMMMMMMMMMMMM

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:

有一个位确定数字是负数还是非负数(零既不是正数也不是负数,但是符号位设置为零)。然后有八位指数和23位尾数。为了从中获得有用的数字,(大致)执行以下计算:

M × 2E

M×2E

(There is more to it, but this should suffice for the purpose of this discussion)

(还有更多内容,但这应该足以满足本次讨论的目的)

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位整数。这将乘以指数部分的幂乘以2,这大致是-128到127之间的数字。

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.

因此,您可以准确地表示适合24位整数的所有数字,但数值范围也要大得多,因为较大的指数允许更大的值。例如,浮点数的最大值约为3.4×1038,而int仅允许值高达2.1×109。

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.

但这也意味着,因为32位只有4.2×109个不同的状态(它们都用来表示int可以存储的值),在float的数值范围的较大一端,数字间隔较宽(因为不能有更多)唯一的浮点数比有唯一的int数)。那么你不能完全代表一些数字。例如,数字2×1012的浮点数为1,999,999,991,808。这可能接近2,000,000,000,000,但这并不准确。同样地,将1添加到该数字不会改变它,因为1太小而不能在较大的尺度上使用浮点数。

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...

类似地,您也可以在浮点数中表示非常小的数字(介于0和1之间),但无论数字是非常大还是非常小,浮点数的精度大约为6或7位十进制数。如果您有大数字,那么这些数字位于数字的开头(例如4.51534×1035,这不超过451534跟随30个零 - 并且浮点数不能告诉任何有用的关于这30个数字是否实际为零或其他东西),对于非常小的数字(例如3.14159×10-27),它们位于数字的远端,超出了0.0000的起始位数...

#2


2  

Floats are used to store a wider range of number than can be fit in an integer. These include decimal numbers and scientific notation style numbers that can be bigger values than can fit in 32 bits. Here's the deep dive into them: http://en.wikipedia.org/wiki/Floating_point

浮点数用于存储比整数更宽的数字范围。这些包括十进制数字和科学记数法样式编号,可以是大于32位的值。以下是对它们的深入了解:http://en.wikipedia.org/wiki/Floating_point