Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

.net

types

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

like image 329
Konstantin Avatar asked Oct 17 '09 16:10

Konstantin


People also ask

Which is bigger long or double C#?

Int64 (aka long): A signed integer with 64 bits (8 bytes) of space available. Single (aka float): A 32-bit floating point number. Double (aka double): A 64-bit floating-point number. Decimal (aka decimal): A 128-bit floating-point number with a higher precision and a smaller range than Single or Double.

What is the maximum value of double in C#?

The value of this constant is positive 1.7976931348623157E+308.

What is bigger a double or long?

Values range Furthermore, the data type long stores whole numbers from 9223372036854775808 to 9223372036854775807. On the other hand, double stores values from 1.7e-308 to 1.7e+038.

What is the largest value for double?

The biggest/largest integer that can be stored in a double without losing precision is the same as the largest possible value of a double. That is, DBL_MAX or approximately 1.8 × 10308 (if your double is an IEEE 754 64-bit double). It's an integer.


3 Answers

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

like image 186
Michał Piaskowski Avatar answered Oct 04 '22 17:10

Michał Piaskowski


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

like image 31
mmmmmm Avatar answered Oct 04 '22 16:10

mmmmmm


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.

like image 25
Danny Varod Avatar answered Oct 04 '22 16:10

Danny Varod