# long vs double ?

Discussion in 'Programmer's Corner' started by Mathematics!, Oct 6, 2011.

1. ### Mathematics! Thread Starter Senior Member

Jul 21, 2008
1,022
4
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?

Jul 7, 2009
1,585
141
Most modern computers use an IEEE floating point standard.

3. ### Mathematics! Thread Starter Senior Member

Jul 21, 2008
1,022
4
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: Oct 6, 2011
4. ### ftsolutions Active Member

Nov 21, 2009
48
2
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'..

5. ### Mathematics! Thread Starter Senior Member

Jul 21, 2008
1,022
4
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?

6. ### kubeek AAC Fanatic!

Sep 20, 2005
4,687
805
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.

7. ### kubeek AAC Fanatic!

Sep 20, 2005
4,687
805
An example:
Code ( (Unknown Language)):
1. void main(void){
2. unsigned long int i=(1<<31)-2;
3. float f=i;
4. printf("%d %f", i, f);
5. }
outputs:
Code ( (Unknown Language)):
1. 2147483646 2147483648.000000