Search code examples
c#typessize

Does double have a greater range than long?


In an article on MSDN, it states that the double data type has a range of "-1.79769313486232e308 .. 1.79769313486232e308". Whereas the long data type only has a range of "-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807". How can a double hold so much more data than a long if they are both 64 bits in size?

http://msdn.microsoft.com/en-us/library/cs7y5x0x(v=vs.90).aspx


Solution

  • The number of possible doubles, and the number of possible longs is the same, they are just distributed differently*.

    The longs are uniformly distributed, while the floats are not. You can Read more here.

    I'd write more, but for some reason the cursor is jumping around all over the place on my phone.

    Edit: This might actually be more helpful: http://en.wikipedia.org/wiki/Double-precision_floating-point_format#section_1

    Edit2: and this is even better: http://blogs.msdn.com/b/dwayneneed/archive/2010/05/07/fun-with-floating-point.aspx

    * According to that link, it would seem that there are actually more longs, since some doubles are lost due to the way NaNs and other special numbers are represented.