473,698 Members | 2,234 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Double - Float Problem !!!!!

1 New Member
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("\nDoubl e value 3003486271 with format - > %17.14E\n\n\n", j);

Ouput:


double - > -1.2914810250000 0E+09

Double value 3003486271 with format - > 3.0034862710000 0E+09

Why the first case is not showing the correct value ? Any Ideas ...
Sep 19 '06 #1
2 3189
D_C
293 Contributor
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
9,065 Recognized Expert Moderator Expert
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

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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.