Thread: Casting Problems

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    3

    Question Casting Problems

    #include <stdio.h>
    main()
    {

    long l;
    long l_corr,l_corr2;
    double d = 2.03;

    l = d*100;

    printf( " l = %ld\n",l);
    }

    Pls can you explain me why does l prints 202 instead of 203 .

    Also if d would have been equal to 2.04 it would have printed properly as 204 .

    I know the solution for this , but just wanted the reason for it !!

    Thanx

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Probably because 2.03 is really stored as 2.029999999

    Which becomes 202.9999999 when you multiply by 100.0

    But since you're assigning to an int, this truncates to 202, no matter how close you might be to 203.

    Floats are approximations to real numbers - so you occasionally get these types of rounding errors.

  3. #3
    Unregistered
    Guest
    Thanx Salem ,

    May be your Right . But then why d = 2.04 works fine ??

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    3
    That was me Salem I just forgot to Enter my user name .

    I tried with 2.04 , 2.06 it gave correct results

    But 2.05 gives 204 .

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Tip: add 0.5 before converting from double to long:
    Code:
    l = (long)((d * 100)+0.5);
    This should solve your problem.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    3
    Thanx


    But I know the solution

    l = (float) (d*100) does work ... but I wanted to know why it happens

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but I wanted to know why it happens
    I may have read between the lines, but Salem told you why it happens. If, however, you want a truly frightening description of the details of floating-point arithmetic, try this:
    http://cch.loria.fr/documentation/IE...M/goldberg.pdf

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User lliero's Avatar
    Join Date
    Oct 2001
    Posts
    59

    ah

    ya, you better try that. prelude is right again
    " programming is 1% syntax and 99% logic "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-17-2007, 11:06 PM
  2. Casting Question (I think)
    By fayte in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 05:31 PM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 12:17 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 02:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 05:01 AM