Thread: remainder and floats

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    180

    remainder and floats

    I have a variable type float named change and i want to get remainder. I couldnt do change % 1 so i used fmod(change, 1). Variable change had value 2.97 but fmod returned 0.000000 insted of .97
    What's the reason? Isn't it supposed to work with floats

  2. #2
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Oh sorry. mistake is before this, it's with strtol i think. I input 2,97 but change only gets value 2.00 Can't strtol be used with floats? I know it stops when it gets to non number character.

    Code:
    int main(){
        float change;
        char userInput[20];
        
        printf("change?");
        fgets(userInput, 80, stdin);
        change = strtol(userInput, &ptr, 10);
        printf("change = %f\n", change);
        
        printf("fmod(change, 1) = %f\n", fmod(change, 1.00));

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    No. The % operator does not work with floats.

    From the C99 Standard: "ISO/IEC 9899:TC3 Committee Draft — Septermber 7, 2007 WG14/N1256"

    6.5.5 Multiplicative operators

    Each of the operands shall have arithmetic type. The operands of the % operator shall
    have integer type.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Can't strtol be used with floats?
    strtol = string to long

    Look into strtod().

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Quote Originally Posted by telmo_d View Post
    Oh sorry. mistake is before this, it's with strtol i think. I input 2,97 but change only gets value 2.00 Can't strtol be used with floats? I know it stops when it gets to non number character.

    Code:
    int main(){
        float change;
        char userInput[20];
        
        printf("change?");
        fgets(userInput, 80, stdin);
        change = strtol(userInput, &ptr, 10);
        printf("change = %f\n", change);
        
        printf("fmod(change, 1) = %f\n", fmod(change, 1.00));
    You need strtof() or strtod().

    $ man strtof

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If you're trying to read dollars and cents you might want to read them into separate variables. Something as simple as this might work:
    Code:
    int dollars = 0, cents = 0;
    if (sscanf(line, "%d.%d", &dollars, &cents) == 0)
        sscanf(line, ".%d", &cents);
    And note that you have a serious discrepancy in the size of your userInput array and the size you pass to fgets.

  7. #7
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Quote Originally Posted by rstanley View Post
    You need strtof() or strtod().

    $ man strtof
    I was thinking it would convert it to float fine, apparently not!

    Quote Originally Posted by algorism View Post
    If you're trying to read dollars and cents you might want to read them into separate variables. Something as simple as this might work:
    Code:
    int dollars = 0, cents = 0;
    if (sscanf(line, "%d.%d", &dollars, &cents) == 0)
        sscanf(line, ".%d", &cents);
    And note that you have a serious discrepancy in the size of your userInput array and the size you pass to fgets.
    Right now iam going to see if i can make it work with strtof.
    With size i guess better safe than sorry! What would be good size? Also is there need to initialize variables?

  8. #8
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    strtof solved the problem. Now i'am facing another problem(and thinking i should have gone with your aproach algorism... ).
    So i have a line #define DIME 0.10 and in the main program cents at some moment had value 0.200000. Next line it did cents / DIME and this is where the proble lies. Instead f giving output of 2 it gives 1.999998 and then numCoinsDimes gets value 1 instead of 2.

    remainder and floats-dd3ca5ae39-png

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,110
    Also is there need to initialize variables?
    In all my programs, I always initialize my locals to some non-zero value where appropriate, or zero, and all my pointers to NULL.

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by telmo_d View Post
    Now i'am facing another problem(and thinking i should have gone with your aproach algorism... ).
    I strongly second algorism's approach - it's the cleanest way to handle currency (as you're apparently finding out in your code).

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

  11. #11
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    I did the sscanf to map the user input to the variables dollars and cents. Iam having a little issue that is if user enters 4.2 as input dollars gets 4 and cents gets value 2 but if he enters 4.20 dollars still gets 4 but cents is now 20 despite input being the same. Iam thinking of checking number of number of digits, if it's one only multiply by 10. Is there easier solution?

  12. #12
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by telmo_d View Post
    I did the sscanf to map the user input to the variables dollars and cents. Iam having a little issue that is if user enters 4.2 as input dollars gets 4 and cents gets value 2 but if he enters 4.20 dollars still gets 4 but cents is now 20 despite input being the same. Iam thinking of checking number of number of digits, if it's one only multiply by 10. Is there easier solution?
    I didn't consider that problem. Maybe something like this (no error checking, though).
    Code:
    double d;
    scanf("%lf", &d);
    int cents = (int)(d * 100 + 0.5);
    Then use cents for your calculations, finally outputting in dollars and cents like:
    Code:
    printf("%d.%02d", cents/100, cents%100);

  13. #13
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    I used division by to check if its single digit or not, then multiplied by 10 if it was.

    Another question, i want to do input validation. What is the propriety of the empty string, "" ? Do i just check the first position of the string and see if '\0' is there? Also i do i test for this empty string? if i just hit enter i send a '\n' right?

  14. #14
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by telmo_d View Post
    I used division by to check if its single digit or not, then multiplied by 10 if it was.
    Have you confirmed that this gives correct results in all cases (for instance, if the input was "4.02")?

    Quote Originally Posted by telmo_d View Post
    Another question, i want to do input validation. What is the propriety of the empty string, "" ? Do i just check the first position of the string and see if '\0' is there?
    That is one way, yes. You could confirm this yourelf with a quick test program.

    Quote Originally Posted by telmo_d View Post
    Also i do i test for this empty string? if i just hit enter i send a '\n' right?
    If you use "fgets()", and the user only presses enter, it will not be an empty string (unless of course you include code that strips the newline, if present, from the input). So to tell if the user did not enter "anything", you'd probably want to check for an empty string, or a string that just contains "\n", depending on the circumstances.

  15. #15
    Registered User
    Join Date
    Apr 2015
    Posts
    180
    Quote Originally Posted by Matticus View Post
    Have you confirmed that this gives correct results in all cases (for instance, if the input was "4.02")?
    Yep there is a bug that was not showing that easily because it would use bigger coins, say 10c instead of 1c and give same resulst but once it had to use 25c it was obviously wrong :|
    It's reading and storing 4.02 and 4.2 same value 2 in variable cents which is a problem. Have to think of another solution.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Remainder program
    By Gohmer in forum C++ Programming
    Replies: 2
    Last Post: 02-09-2015, 09:42 PM
  2. How to find a remainder of a division?
    By keivi in forum C Programming
    Replies: 11
    Last Post: 06-11-2013, 05:52 PM
  3. manipulating the remainder of a float
    By thestien in forum C++ Programming
    Replies: 5
    Last Post: 10-04-2006, 06:55 AM
  4. how to get remainder of a value
    By seal in forum C Programming
    Replies: 2
    Last Post: 09-22-2005, 07:03 AM
  5. Determining if a number has a remainder?
    By Captain Penguin in forum C Programming
    Replies: 3
    Last Post: 09-01-2001, 07:07 AM