question about huge double value
Author 
Message 
step #1 / 6

question about huge double value
Hi there, in my c src, I defined a huge double value "huge1" and want to get another huge value "huge2" which is a little small then the first one, I write the code like ollows: ... #define huge1 18446744073709549568.0 ... huge2 = huge  1.0; printf("huge1=%f\nhuge2=%f\n",huge1,huge2); ... the output seems not what I wanted: huge1=18446744073709549568.000000 huge2=18446744073709549568.000000 I'm puzzled, does anyone know why? is there any method to get a huge value that is just a little small than huge1? thanks!

Thu, 14 Apr 2005 18:18:44 GMT 


CBFalcone #2 / 6

question about huge double value
Quote:
> in my c src, I defined a huge double value "huge1" and want to > get another huge value "huge2" which is a little small then the > first one, I write the code like ollows: > ... > #define huge1 18446744073709549568.0 > ... > huge2 = huge  1.0; > printf("huge1=%f\nhuge2=%f\n",huge1,huge2); > ... > the output seems not what I wanted: > huge1=18446744073709549568.000000 > huge2=18446744073709549568.000000 > I'm puzzled, does anyone know why? is there any method to > get a huge value that is just a little small than huge1?
Try compiling and running the following, which should give you suitable factors for your system. #include <stdio.h> #include <float.h> int main(void) { printf("DBL_DIG is %d\n" "DBL_EPSILON is %.*f\n", DBL_DIG, DBL_DIG + 1, DBL_EPSILON); return 0; Quote: } /* main */
then "huge2 = huge1  (huge1 * DBL_EPSILON);" might make sense. 
Available for consulting/temporary embedded and systems. <http://cbfalconer.home.att.net> USE worldnet address!

Thu, 14 Apr 2005 18:53:19 GMT 


Nejat AYDI #3 / 6

question about huge double value
Quote:
> Hi there, > in my c src, I defined a huge double value "huge1" and want to get > another huge value "huge2" which is a little small then the first one, > I write the code like ollows: > ... > #define huge1 18446744073709549568.0 > ... > huge2 = huge  1.0; > printf("huge1=%f\nhuge2=%f\n",huge1,huge2); > ... > the output seems not what I wanted: > huge1=18446744073709549568.000000 > huge2=18446744073709549568.000000 > I'm puzzled, does anyone know why?
The reason is that the number (18446744073709549568.0  1.0) cannot be represented exactly in your implementation, i.e. there are not enough mantissa (significand) bits (assuming base 2, which most implementation use) in the representation of types ``double'' in your implementation. Quote: > is there any method to > get a huge value that is just a little small than huge1?
If you have a C99 compliant compiler, you can try #include <math.h> ... double huge2 = nextafter(huge1, 0.0);

Thu, 14 Apr 2005 21:31:30 GMT 


William L. Bah #4 / 6

question about huge double value
You only have so many bits of resolution with a floating point value. On many systems a double has about 15 or 16 significant decimal digits. There is a defined constant in the float.h header that tells you the smallest value for which one plus that value is not equal to one. If you multiply that constant times you huge1, that will tell you (very closely) what the smallest change in huge1 is that you can make and see the difference.
Quote: > Hi there, > in my c src, I defined a huge double value "huge1" and want to get > another huge value "huge2" which is a little small then the first one, > I write the code like ollows: > ... > #define huge1 18446744073709549568.0 > ... > huge2 = huge  1.0; > printf("huge1=%f\nhuge2=%f\n",huge1,huge2); > ... > the output seems not what I wanted: > huge1=18446744073709549568.000000 > huge2=18446744073709549568.000000 > I'm puzzled, does anyone know why? is there any method to > get a huge value that is just a little small than huge1? > thanks!

Fri, 15 Apr 2005 07:18:48 GMT 


glen herrmannsfeld #5 / 6

question about huge double value
Quote: > Hi there, > in my c src, I defined a huge double value "huge1" and want to get > another huge value "huge2" which is a little small then the first one, > I write the code like ollows: > ... > #define huge1 18446744073709549568.0
This number seems to be 2**64, and it is rare for a double variable to have 64 bits of precision. I do wonder how you accidentally found 2**64 as the number you would like to subtract 1 from. I suggest you try different numbers, starting small, and find out how large a number you can have such that subtracting one gives a different number.  glen

Fri, 15 Apr 2005 15:43:47 GMT 


step #6 / 6

question about huge double value
Thanks CBFalconer very much! your method seems make sense for me. thank you ;) Quote:
> > in my c src, I defined a huge double value "huge1" and want to > > get another huge value "huge2" which is a little small then the > > first one, I write the code like ollows: > > ... > > #define huge1 18446744073709549568.0 > > ... > > huge2 = huge  1.0; > > printf("huge1=%f\nhuge2=%f\n",huge1,huge2); > > ... > > the output seems not what I wanted: > > huge1=18446744073709549568.000000 > > huge2=18446744073709549568.000000 > > I'm puzzled, does anyone know why? is there any method to > > get a huge value that is just a little small than huge1? > Try compiling and running the following, which should give you > suitable factors for your system. > #include <stdio.h> > #include <float.h> > int main(void) > { > printf("DBL_DIG is %d\n" > "DBL_EPSILON is %.*f\n", > DBL_DIG, DBL_DIG + 1, DBL_EPSILON); > return 0; > } /* main */ > then "huge2 = huge1  (huge1 * DBL_EPSILON);" might make sense.

Sat, 16 Apr 2005 13:13:19 GMT 


