C# 中浮点数和双精度数据类型的实际范围是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17759539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:25:10  来源:igfitidea点击:

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

c#typesfloating-pointdouble

提问by user1063287

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

我正在学习 C# 并试图获得 C# 中实际数据类型范围的逻辑可视化表示。

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

我已经遍历了整数,现在可以使用 float 和 double 数据类型。

  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.
  1. 8 位(1 字节),sbyte,-128 到 127。
  2. 8 位(1 字节),字节,0 到 255。
  3. 16 位(2 字节),短,-32,768 到 32,767。
  4. 16 位(2 字节),ushort,0 到 65535。
  5. 32 位(4 字节),int,-2,147,483,648 到 2,147,483,647。
  6. 32 位(4 字节),uint,0 到 4,294,967,295。
  7. 64 位(8 字节),长,-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
  8. 64 位(8 字节),ulong,0 到 18,446,744,073,709,551,615。

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

以下是 msdn 上对 float 和 double 数据类型大小的引用:

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

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

enter image description here

在此处输入图片说明

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

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

enter image description here

在此处输入图片说明

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?

因此,尝试遵守上面编号列表中指定实际数字范围的惯例,这两个范围实际上代表什么?

采纳答案by Eren Ers?nmez

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

为简洁起见,范围以“指数格式”表示。例如,+1.7e+308 表示 17 后跟 307 个零:

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

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 readingby Jon Skeet.

另外,看看Jon Skeet 的这篇读物

回答by Eric Postpischil

The ranges are actually –infinity to +infinity.

范围实际上是 –infinity 到 +infinity。

The largest finitefloatis 340282346638528859811704183484516925440. This is 2128–2128–24.

最大的有限float是 340282346638528859811704183484516925440。这是 2 128–2 128–24

The largest finitedoubleis 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368. This is 21024–21024–53.

最大的有限double是179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.这是2 1024-2 1024年至1053年