Reputation: 115
In the C programming language I'm having trouble getting accurate values reported for long double variables.
In order to calculate accurate averages of two unsigned long long variables I feel as though I need to use long double over standard double because standard double only handles 52 significant bits while the unsigned long long are 64 bits.
Here is an example of the problem I am experiencing:
unsigned long long int x = 0x00000000FE5F06AC;
unsigned long long int y = 0x000FFFFFFFFFFFFF;
long double z = (x+y)/2.0;
printf("z = %lf", z);
Such a sample of code will always print 0.0000 for z and I can't really understand why this is happening.
Is there underflow occurring, or some type of error in how I am formatting this?
Upvotes: 1
Views: 980
Reputation: 206557
I see couple of errors:
You have a G
in the first number. Hopefully that's just a typo or error in transcription.
The format for long double
is %Lf
, not %lf
.
Here's a link to a working example: http://ideone.com/jWRHit
Upvotes: 2