C# double 的范围是否比 long 的范围大?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13022016/
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 01:22:50  来源:igfitidea点击:

Does double have a greater range than long?

c#typessize

提问by Nyx

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

在 MSDN 上的一篇文章中,它指出double数据类型的范围为“-1.79769313486232e308 .. 1.79769313486232e308”。而long数据类型的范围只有“-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807”。如果它们的大小都是 64 位,double那么如何容纳比 a 多得多的数据long

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

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

采纳答案by will

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

编辑:这实际上可能更有帮助: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

Edit2:这更好: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.

*根据该链接,似乎实际上有更多的长,因为由于 NaN 和其他特殊数字的表示方式,一些双打丢失了。

回答by Tejas Sharma

longis a signed 64-bit integer value and doubleis a 64-bit floating point value. Looking at their FCL types might make more sense. longmaps to System.Int64and doublemaps to System.Double.

long是一个有符号的 64 位整数值并且double是一个 64 位浮点值。查看他们的 FCL 类型可能更有意义。long映射到System.Int64double映射到System.Double

回答by Ronnie 'Madolite' Solbakken

A simple answer is that doubleis only accurate to 15-16 total digits, as opposed to longwhich (as an integer type) has an absolute accuracywithin an explicit digit limit, in this case 19 digits. (Keep in mind that digitsand valuesare semantically different.)

一个简单的答案是,double它只能精确到 15-16 位总位数,而long后者(作为整数类型)在明确的位数限制内具有绝对精度,在这种情况下为 19 位。(请记住,数字在语义上是不同的。)

double: -/+ 0.000,000,000,000,01 to +/- 99,999,999,999,999.9 (at 100% accuracy, with a loss in accuracy starting from 16th digit, as represented in "-1.79769313486232e308 .. 1.79769313486232e308".)

double: -/+ 0.000,000,000,000,01 到 +/- 99,999,999,999,999.9(准确度为 100%,准确度从第 16 位开始损失,如“-1.79769313486232.86192.37.37”中所示)

long: -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

long: -9,223,372,036,854,775,808 至 +9,223,372,036,854,775,807

ulong: 0 to 18,446,744,073,709,551,615 (1 more digit than long, but identical value range (since it's only been shifted to exclude negative returns).

ulong: 0 到 18,446,744,073,709,551,615(比 long 多 1 位,但值范围相同(因为它只是为了排除负回报而移动)。

In general, int-type real numbersare preferred over floating-point decimal values, unless you explicitly need a decimal representation (for whichever purpose).

通常,int 类型的实数优先于浮点十进制值,除非您明确需要十进制表示(无论出于何种目的)。



In addition, you may know that signedare preferred over unsigned, since the former is much less bug-prone (consider the statement uint i;, then i - x;where x > i).

此外,您可能知道signedunsigned受欢迎,因为前者更不容易出错(考虑 statement uint i;,然后i - x;where x > i)。