Find the answer to your Linux question:
Results 1 to 7 of 7
hi iam trying to convert float to int and sometimes it dosent work as wanted.. problem: Code: #include <stdio.h> char line[100]; float dollar; int pennies; /*origianl program includes quarters,dimes, nickels ...
  1. #1
    Just Joined! degus's Avatar
    Join Date
    Dec 2007
    Posts
    10

    (C) convert float to int -> f-up logic

    hi iam trying to convert float to int and sometimes it dosent work as wanted..

    problem:

    Code:
    #include <stdio.h>
    
    char line[100];
    float dollar;
    int pennies; /*origianl program includes quarters,dimes, nickels to /*
    
    int main () {
    
              printf("Please enter a value $.$$");
              fgets(line,sizeof(line),stdin);
              sscanf(line,"%f",&dollar);
    
    /* here there are similar code as below to work out quarters, dimes and nickels /*
    
    
              float pen;   */ testing variable /*
              pen = dollar / 0.01;  */ testing calculating without converting /*
              pennies = dollar / 0.01; */ converting answer to int /*
    
              printf("float pen: %f, int pennies: %d\n"); */ PROBLEM MARK 1 /*
    
    
              if (pennies != 0) {
                        printf("Pennies: %d\n",pennies);
                        dollar = dollar - (pennies * 0.01);
              }
              printf("dollar: %f", dollar");
    
    return (0);
    }
    running the program should behave like following

    >Please enter a value $.$$: 0.04
    >float pen: 4.00000, int pennies: 4
    >Pennies: 4
    >dollar: 0.00000

    but it dosent instead i get ..

    >Please enter a value $.$$: 0.04
    >float pen: 4.00000, int pennies: 3
    >Pennies: 3
    >dollar: 0.01000

    and the dollar variable still has 0.01???

    well i understand that it is the converting part that is the problem..
    but what should i do to convert it without problems.. ?

    i have only noticed this problem with the pennies, but my guess is that quarters, dimes and nickels could do the same..

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Show your code to your instructor and ask him why floating point roundoff errors will get you into trouble here.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined! degus's Avatar
    Join Date
    Dec 2007
    Posts
    10
    Quote Originally Posted by wje_lf View Post
    Show your code to your instructor and ask him why floating point roundoff errors will get you into trouble here.
    would be nice if i even had an instructor..
    iam reading the "Practical C programming" book from O'rilley.

    managed to google for "floating point roundoff errors" didnt find anything that helped out tho..

    when i search for converting float to int it generaly just says

    int x;
    float y;

    x = y;

    so it seems like iam not doing anything wrong.. or am i wrong?

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Pardon the delay. Too much time at the computer rots the brain, so I've been spending most of the day on yardwork. Very restorative.

    First, a point of etiquette.

    You did well to post your program verbatim, along with your expected output and the actual output. But if you fix your program so that it has no syntax or other known errors, it is courteous to post the correct one, so that someone who wishes to help you can do without fixing those errors again.

    A very good thing that you did was to use fgets() and sscanf(), instead of just scanf(). It keeps you out of some difficulties if your program inputs data which is in the wrong format.

    Your difficulty is that 0.01 cannot be represented precisely in most processors (including yours, I imagine). That is, 0.01 cannot be represented as the sum of a finite number of (negative) powers of 2.

    Consider the decimal number system. Try to represent 1/3 in it precisely, with a finite number of digits. You can't.

    Similarly, 0.01 cannot be represented in the binary number system precisely, with a finite number of digits. Roundoff error ensues.

    This is why accounting software deals with dollar amounts not as floating point dollars, but as integer pennies (or integer mils sometimes, I guess).

    A couple of good Wikipedia articles about roundoff error are here and here.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    Linux Guru budman7's Avatar
    Join Date
    Oct 2004
    Location
    Knee deep in Grand Rapids, Michigan
    Posts
    3,242
    This is an obvious homework question.
    I am hoping that wje_lf has set you on the right track.
    I am closing this thread under the "no homework questions allowed" rule.
    How to know if you are a geek.
    when you respond to "get a life!" with "what's the URL?"
    - Birger

    New users read The FAQ

  6. #6
    Linux Guru budman7's Avatar
    Join Date
    Oct 2004
    Location
    Knee deep in Grand Rapids, Michigan
    Posts
    3,242
    This thread is being reopened after degus explained how he was trying to learn C on his own.
    Proof thread.
    How to know if you are a geek.
    when you respond to "get a life!" with "what's the URL?"
    - Birger

    New users read The FAQ

  7. #7
    Just Joined! degus's Avatar
    Join Date
    Dec 2007
    Posts
    10
    Big thanks to wje_lf for the help and thanks to budman7 for reopening the thread.

    solution:

    program asks for an input in $, if less than 1$ then it calculate how many quarters, dimes, nickels and pennies you will get.

    Code:
    #include <stdio.h>
    
    char line[100];
    
    int dollar;  /* dollars $. */
    int d_cents; /* dollars .$$  */
    
    int quarters; /* for calculation (all below) */
    int dimes; 
    int nickels;
    int pennies;
    
    int main() {
    	
    	printf("please enter value in $.$$: ");
    	fgets(line,sizeof(line),stdin);
    	sscanf(line,"%d.%d", &dollar,&d_cents);
    	
    	if (dollar == 0){
    		printf("Your money %d.%d$ can be given in following coins.\n",dollar,d_cents);
    		
    		quarters = d_cents / 25;
    		if (quarters != 0){
    			printf("Quarters: %d\n",quarters);
    			d_cents = d_cents - quarters * 25;
    			
    		}
    		
    		dimes = d_cents / 10;
    		if ( dimes != 0){
    			printf("Dimes: %d\n",dimes);
    			d_cents = d_cents - dimes * 10;
    			
    		}
    		
    		nickels = d_cents / 5;
    		if ( nickels != 0){
    			printf("Nickels: %d\n",nickels);
    			d_cents = d_cents - nickels * 5;
    			
    		}
    		
    		pennies = d_cents / 1;
    		if ( pennies != 0){
    			printf("Pennies: %d\n",pennies);
    			d_cents = d_cents - pennies * 1;
    			
    		}
    		
    		if (d_cents != 0){ /* if this line below prints, an error occured */
    			printf("The rest of the money you will lose.");
    		} 
    	}
    	else{
    		printf("You have %d.%d$",dollar,d_cents);
    	}
    		
    	return (0);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...