I think I have a glich in my program using integer and double values

This is a discussion on I think I have a glich in my program using integer and double values within the C Programming forums, part of the General Programming Boards category; Ok basically what I want to do is turn say $3.35 all in pennies whic will be 335. My problem ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    67

    I think I have a glich in my program using integer and double values

    Ok basically what I want to do is turn say $3.35 all in pennies whic will be 335. My problem is how do I stored a floating point value 3.335 like so into an int value so I can have 334 not 333.

    here is my code I think it works sometimes but not all of them. what i think I did was copy the value of a float into an int to two values after the decimal point and then multiply it by 100. I noticed that some times will not match my float value by one cent.

    Code:
    
    	float unitp = 1.25;
    	float want;
    	float totalamount;
    	float  payment;
    	float change;	
    	int dchange;
    	int newchange;
    
    
    	printf("Welcome to crocop's meat market\n");
    	printf("the meat is priced at $1.25 each pound\n");
    	printf("How many pounds would you like to buy?\n");
    		scanf("%f", &want);
    
    	totalamount=want * unitp;
    	
    	printf("the total you owe is $%.2f\n", totalamount);
    
    	printf("How much do you want to make the payment for?\n");
    		scanf("%f", &payment);
    
    
    	change = payment-totalamount ;
    
    	printf("okay you will recive $%.2f in change\n", change);
    
    		dchange=( 100* change+.5);
    
    		printf("dchange = %d\n", dchange);
    I want the lines highlighted in red to alwasy print the same values. Thank you for your help.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    I haven't tested it, but you might want to try:
    Code:
    dchange = (int)round(100 * change);
    Note: You might have to #include <math.h>, and possibly link in the math library if you're using gcc (add "-lm" to the compile line)

    The bottom line is, floating point multiplication is goofy and fraught with rounding errors like this.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Quote Originally Posted by itsme86 View Post
    I haven't tested it, but you might want to try:
    Code:
    dchange = (int)round(100 * change);
    Note: You might have to #include <math.h>, and possibly link in the math library if you're using gcc (add "-lm" to the compile line)

    The bottom line is, floating point multiplication is goofy and fraught with rounding errors like this.
    yeah I tried it and gives me some weird values. thanks for the help though.

    ok what I did now is change it to

    dchange=( 100* change+.4); so now is .4 not .5 and it seems to be working and I don't know why.
    Last edited by newbc; 03-25-2011 at 05:55 AM.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    76
    Do you have an example of failing values? I copied and pasted it into visual studio and cannot get it to fail even when the change due back is 3.335. (I get 3.34 and 334).

    I think visual studio automatically uses a double when you want a float, maybe that is what you want?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    Quote Originally Posted by dolfaniss View Post
    Do you have an example of failing values? I copied and pasted it into visual studio and cannot get it to fail even when the change due back is 3.335. (I get 3.34 and 334).

    I think visual studio automatically uses a double when you want a float, maybe that is what you want?
    ok try to to buy 3.5 pounds and then pay with 10. I am using putty's gcc to compile it.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    76
    I get 5.63 and 563. The only time I got a mistake is when I accidently entered 3 lbs (for 3.75) and tried to pay 3.335 for it. In that case I got -41 and -40 otherwise I can't get a failure.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Posts
    67
    i guess is gcc compiler then.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    76
    Could be, I'm not sure maybe some of the more savy members here would know. I'm actually in a C class that seems to be pretty cose to in line with where you are right now. I'm a beginner myself who likes to try to help out here for practice, but I'm mostly here to learn. From my experience being here nobody would have a problem telling me my code sucks if they see something wrong, LOL.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Can you restate the problem? What number is the calculation failing on?

  10. #10
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Quote Originally Posted by dolfaniss View Post
    Do you have an example of failing values? I copied and pasted it into visual studio and cannot get it to fail even when the change due back is 3.335. (I get 3.34 and 334).

    I think visual studio automatically uses a double when you want a float, maybe that is what you want?
    For your explaination: Suppose i do
    Code:
    float x=2.24;
    2.24 is actually treated as double but implicit type cast will convert it into float.
    So if you want to keep it float,
    Code:
    float x=2.24f;
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    76
    Quote Originally Posted by Mr.777 View Post
    For your explaination: Suppose i do
    Code:
    float x=2.24;
    2.24 is actually treated as double but implicit type cast will convert it into float.
    So if you want to keep it float,
    Code:
    float x=2.24f;
    Thanks for clearing that up for me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  3. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21