Konstantin
Konstantin

Reputation: 3957

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

Upvotes: 10

Views: 19341

Answers (3)

Danny Varod
Danny Varod

Reputation: 18068

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.

Upvotes: 2

Michał Piaskowski
Michał Piaskowski

Reputation: 3840

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

Upvotes: 1

mmmmmm
mmmmmm

Reputation: 32681

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

Upvotes: 20

Related Questions