Double - Float Problem !!!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nemesisneo
    New Member
    • Sep 2006
    • 1

    #1

    Double - Float Problem !!!!!

    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 ...
  • D_C
    Contributor
    • Jun 2006
    • 293

    #2
    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 :).

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by 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 :).
      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.

      Comment

      Working...