开发者

Maximum value vs. size of long and double in .NET

It's known that in .NET size of both long and double is 8 bytes. However, double can store significantly larger number than 开发者_高级运维long. How can it be, considering that double also needs to store data on digits after decimal point?

Shorter version of the question:

Math.Pow(2, 64) == long.MaxValue Math.Pow(2, 64) < double.MaxValue


Short answer double only stores the most significant figure and not all the digits that could be in the number. e.g. if you have a double > max value of long it will not be storing any information for digits after the decimal point or any of the figure just to the left of the deciaml point.

For all details see What every computer scientist should know about Floating-Point Arithmetic


Integer based types have whole number ranges from -2^(n-1)2 ... 2^(n-1)-1 (signed), or 0 ... 2^n-1 (unsigned).

Fixed point variables are the same as integer based types, only with a constant factor (e.g. for 0.01: 0.01*(0...2^n-1)).

Floating point variables (float and double) in any language use a few bits for the exponent and the rest for the number before the exponent. They are less accurate (x+1 might equal x, is x is a very large number), but have a much larger range.


double is floating point number. Whitch bassicaly meams that it as the number stored in double gets bigger it's beeing rouned, and the least significiant part is being dismissed.

For example in double when you have a number like 100 billion. It might be exactly 100 000 000 000 or it might be 100 000 000 000,000 000 000 000 000 001

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜