简体   繁体   中英

What are the actual ranges of floating point and double data types in C#?

I'm learning C# and trying to get a logical visual representation of the actual range of data types in C#.

I have moved through the integers and am now up to float and double data types.

  1. 8 bits (1 byte), sbyte, -128 to 127.
  2. 8 bits (1 byte), byte, 0 to 255.
  3. 16 bits (2 bytes), short, -32,768 to 32,767.
  4. 16 bits (2 bytes), ushort, 0 to 65535.
  5. 32 bits (4 bytes), int, -2,147,483,648 to 2,147,483,647.
  6. 32 bits (4 bytes), uint, 0 to 4,294,967,295.
  7. 64 bits (8 bytes), long, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
  8. 64 bits (8 bytes), ulong, 0 to 18,446,744,073,709,551,615.

Here are the references to float and double data types sizes at msdn:

Float: http://msdn.microsoft.com/en-us/library/b1e65aza(v=vs.110).aspx

在此处输入图片说明

Double: http://msdn.microsoft.com/en-us/library/678hzkk9.aspx

在此处输入图片说明

So, trying to keep with the convention of specifiying the actual range of numbers as in the numbered list above, what do these two ranges actually represent?

The ranges are actually –infinity to +infinity.

The largest finite float is 340282346638528859811704183484516925440. This is 2 128 –2 128–24 .

The largest finite double is 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368. This is 2 1024 –2 1024–53 .

The ranges are represented in "exponential format" for conciseness. For example, +1.7e+308 means 17 followed by 307 zeros:

1,700,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000

So the exponential format is preferred for such large numbers. And the same goes for extremely small numbers.

Also, take a look at this reading by Jon Skeet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM