大小相同时,浮点数和整数数据类型有什么区别

What is the difference between the float and integer data type when the size is the same?(大小相同时,浮点数和整数数据类型有什么区别?)

本文介绍了大小相同时,浮点数和整数数据类型有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大小相同时浮点数和整数数据类型有什么区别?

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 places
    • int 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 × 1038int 只允许最大为 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 × 1012float 中的表示形式为 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...

      这篇关于大小相同时,浮点数和整数数据类型有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:大小相同时,浮点数和整数数据类型有什么区别

基础教程推荐