当大小相同时,float和integer数据类型有什么区别? [英] What is the difference between the float and integer data type when the size is the same?

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

问题描述

float 存储浮点值,即具有潜在小数位的值
  • int 只存储整数值,即整数



  • )是完全不同的。你不能在一个整数中存储3.141,但你可以在 float 中。




    $ b

    在一个整数中,所有的位用于存储数字值。这是(在Java和许多计算机也是)在所谓的二进制补码中完成。这基本上意味着你可以表示2到2到31之间的值。1 /
    $ b $在一个浮点数中,这32位分为三个不同的部分:符号位,指数和尾数。它们的布局如下:

    $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ p>

    确定数字是负数还是非负数(零不是正数也不是负数,而是将符号位设置为零)有一个位。然后有8位指数和23位尾数。为了得到一个有用的数字,(大致)进行下面的计算:

    lockquote

    M ×2 < E>


    (还有更多,但这应该足够这个讨论的目的)
    $ b $尾数本质上不比24位整数大得多。这个指数部分的幂乘以2,这个数字大概是在-128到127之间的数字。因此,你可以准确地表示所有的数字将适合24位整数,但数字范围也大得多,因为较大的指数允许较大的值。例如, float 的最大值大约是3.4×10 38 ,而 int 只允许值高达2.1×10 9

    但是这也意味着,由于32位只有4.2×10 9 不同的状态(全部用来表示 int 可以存储的值),在 float 的数字范围的数字间隔较宽(因为不能有更多的独特的浮动数字比有独特的 int 数字)。那么你不能完全代表一些数字。例如,数字2×10 12 float 中的表示形式为1,999,999,991,808。这可能是2000万美元,但它不是确切的。同样的,加1到那个数字也不会改变它,因为1太小,不能在大的范围内使用 float 在那里使用。



    同样,你也可以在 float 中表示非常小的数字(0到1之间),但是不管数字是非常大还是非常小, float 的精度只有6或7个十进制数字。如果你有很多数字,那么这些数字在数字的开头(例如4.51534×10 35 ,只不过是451534跟着30个零),而 float - >; 27 ),它们位于数字的最后端,c $ c>不能说出任何有用的信息,超出了0.0000的起始数字...


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

    解决方案

    • float stores floating-point values, that is, values that have potential decimal places
    • int only stores integral values, that is, whole numbers

    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.

    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.

    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
    

    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)

    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.

    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.

    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.

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

    这篇关于当大小相同时,float和integer数据类型有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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