# long vs double ?

#### Mathematics!

Joined Jul 21, 2008
1,036
Both long and double have size 8byte which means
the max size is either 2^64 if unsigned or 2^63 - 1 if signed
But my question is why does when I look up the max size of a double it
say's
+/- 4.9406x10-324 to 1.7977x10308
not this
-9223372036854775808 to 9223372036854775807(signed) or 0-18446744073709551616(unsigned)

How can one contain a greater value range ? They both use the same sizes so it would be impossible to get a greater value range in the same size data type I would think? Unless they are using some secret extra datatype behind the scenes that I am not aware off for double values.

Same question applies when I look at integer (4bytes , 32bits ) and floats ( 4bytes , 32 bits )

curious if anybody knows how the floats and doubles are implemented to acheive this?

Joined Jul 7, 2009
1,583
Most modern computers use an IEEE floating point standard.

#### Mathematics!

Joined Jul 21, 2008
1,036
Ok , I found it IEEE754
http://en.wikipedia.org/wiki/Double_precision

apparently it uses this formula

.png

which makes since that the max and min values are different.
It uses 52 bit for fractional part

I am still wondering though since the range of values is greater for a double (only because of the way we are interpreting the bits of the store 8byte value not because we have any add space)
what actually happens when converting a long to a double , and visa-versa?

I know when converting from an int to a long nothing occurs but more space being allocated. And going from long to int casts you just truncate off the top 4bytes and store the low 4bytes in the int type.

But when it comes to double to long , and long to double I am stumped. By the above formula is their any way that it mantains consistency between the display values? Because I cann't see that a cast (double)-1231243456 = displaying 1231243456 maybe I am wrong haven't tried it yet.

I mean I know the range contains the long values but does casting screw up the display values if you where to cast from a long to a double ?

Last edited:

#### ftsolutions

Joined Nov 21, 2009
48
The long in your compiler is an integer number. The double type is indeed IEEE754 format floating point (sign, mantissa, exponent). They are very different 'animals'..

#### Mathematics!

Joined Jul 21, 2008
1,036
I know they are different animals but I am curious what would happen if you used these data types interchangeable?

For example if I had a long value could I assign it to a double and still get the same value when I print out the double. Or will the cast to a double destroy the value.

I know the double is using a normalization to represent a number but I am wondering if the cast operator takes this into account so I can be sure that any long or integer data types can be assigned to a double with out the value getting screwed up?

#### kubeek

Joined Sep 20, 2005
5,724
They will get screwed - loose precision. How else could you stick a 64 bit number, lets say 62^2 for instance, into 52bits? The number gets rounded to its equivalent 52bit number times the exponent.

#### kubeek

Joined Sep 20, 2005
5,724
An example:
Rich (BB code):
void main(void){
unsigned long int i=(1<<31)-2;
float f=i;
printf("%d %f", i, f);
}
outputs:
Rich (BB code):
2147483646 2147483648.000000