By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,871 Members | 2,337 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,871 IT Pros & Developers. It's quick & easy.

Double - Float Problem !!!!!

P: 1
Please find details of small program written by me:

( UNIX NCR MP-RAS, NCR C/C++ Complier, Double is 8 bytes (Just in case))

double j;

j = 3003486271;

printf("\n double - > %17.14E ",j);


j = 3003486271.0;

printf("\nDouble value 3003486271 with format - > %17.14E\n\n\n",j);

Ouput:


double - > -1.29148102500000E+09

Double value 3003486271 with format - > 3.00348627100000E+09

Why the first case is not showing the correct value ? Any Ideas ...
Sep 19 '06 #1
Share this Question
Share on Google+
2 Replies


100+
P: 293
D_C
Here is my guess. In the first assignment, you assign an integer value, which is then converted to a double. That integer is greater than INT_MAX, so it overflows to a negative value. Then the negative integer value is converted to a double. It looks like your problem is with int not double :).
Sep 19 '06 #2

Banfa
Expert Mod 5K+
P: 8,916
Here is my guess. In the first assignment, you assign an integer value, which is then converted to a double. That integer is greater than INT_MAX, so it overflows to a negative value. Then the negative integer value is converted to a double. It looks like your problem is with int not double :).
Nice thinking reckon you got it right, I didn't think of that :D

It is always a good idea to add a .0 to and constant that you want to be a double. If you want a float constant rather thana double constant suffix an F


12345 - Integer costant

12345.0 - double constant

12345.0F - float constant

You can do the same with L for long, U for unsigned and UL for unsigned long

12345 - int constant

12345U - unsigned constant

12345L - long constant

12345UL - unsigned long constant

I think that strictly speaking the letters should be uppercase but many compilers accept lower or upper case. Uppercase is better in the case of L because an l can be mistaken for 1 in some fonts.
Sep 20 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.