Thread: Unusual weird result when casting double to unsigned.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    18

    Unusual weird result when casting double to unsigned.

    Please help! I don't understand why tempCents = 10.00, but when it cast to unsigned, it become 9? WHY?
    This only happen if the price = 4.10; If i change it to 3.10, the unsigned cents will be normal '10'.

    Code:
    int main(void)
    {
    	double price = 0.0;
    	unsigned dollars;
    	double tempDollars;
    	unsigned cents;
    	double tempCents;
    	
    	price = 4.10;
    	dollars = (unsigned) price;
    	tempDollars = (double) dollars;
    	
    	tempCents = (price - tempDollars) * 100;
    	cents = (unsigned) tempCents;
    	
    	printf("\nPrice: %.2f\n", price);
    	printf("\nDollars: %u\n", dollars);
    	printf("\nTemporary Dollars: %.2f\n", tempDollars);
    	printf("\nTemporary Cents: %.2f\n", tempCents);
    	printf("\nCents: %u\n", cents);
    	
    	return EXIT_SUCCESS;
    }
    Result:

    Price: 4.10


    Dollars: 4


    Temporary Dollars: 4.00


    Temporary Cents: 10.00


    Cents: 9

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Floating point imprecision.

    In VC++ Express 10, price is NOT 4.10, but 4.0999999999999996, therefore, tempcent ends up being 9.9999999999999645.
    Last edited by Cynic; 04-28-2012 at 12:12 AM.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    Sorry that i don't really understand. Anyone can please explain it to me? Any solution to it? Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    131
    Quote Originally Posted by cprogbiginner View Post
    Sorry that i don't really understand. Anyone can please explain it to me? Any solution to it? Thanks!
    There is an infinite number of reals between 4.09 and 4.1, however, the computer has a limited number of bits to represent decimal numbers.

  5. #5
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Common solution is to store the value in cents as an `int`, and have a function that translates this internal representation to something that most people are more used to seeing.

    Maybe something like this:
    Code:
    // Code not necessarily correct or guaranteed to compile.
    #include <stdio.h>
    
    
    int dprice(int* dollars, int* cents, const int price);
    
    int main(void)
    {
            int price = 410;
            int dollars, cents;
            
            if ( dprice(&dollars, &cents, price) != -1) {
                    printf("Price of widget is $%d.%d\n", dollars, cents);
            } else {
                    printf("Something happened. We're sorry!");
                    return 1;
            }       
            
            return 0;
    }
    
    int dprice(int* dollars, int* cents, const int price)
    {
            if (d == NULL || c == NULL)
                    return -1;
            
            *dollars = price / 100;  // `int` division discards the reminder
            *cents = price % 100;  // whatever's left is the cents part
            
            return 0;
    }
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    What if the 4 is pass to a function as 4.10? can i just multiply it 4.10 * 100 then store it as int?
    After that only do something like the code provided?

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    I've tried it like that:

    price = 4.10;
    tempPrice = (int) (price * 100);

    but the result are:

    Price: 4.10


    Temporary Price: 409


  8. #8
    Novice
    Join Date
    Jul 2009
    Posts
    568
    Don't use floats or doubles for anything to do with money and you'll sleep better. Any floating point type is inherently imprecise due to what Cynic already explained -- they try represent in infinite number of possible values with a finite number of bits -- and that just doesn't always work out nearly as well some expect.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    Thanks everyone! Here is the best place for learning!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Weird result with code
    By raesoo80 in forum Tech Board
    Replies: 8
    Last Post: 12-14-2010, 02:30 PM
  2. Casting pointer to unsigned short
    By cks2k2 in forum C Programming
    Replies: 6
    Last Post: 03-25-2009, 05:33 AM
  3. int casting for unsigned chars
    By DarkMasterBosel in forum C++ Programming
    Replies: 4
    Last Post: 01-09-2008, 03:31 AM
  4. Casting unsigned long division to a double?
    By smoothdogg00 in forum C Programming
    Replies: 5
    Last Post: 12-22-2006, 09:22 AM
  5. weird computation result
    By axr0284 in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2006, 11:34 PM