Small bug in my short C code. Why?

后端 未结 3 1107
不思量自难忘°
不思量自难忘° 2021-01-23 12:11

I can\'t figure out why this works for 90% of the inputs, but not the others. It is meant to tell you how many coins you would get back in change. Most test amounts work fine,

相关标签:
3条回答
  • 2021-01-23 12:30

    The other answers have it mostly covered: you should be working with fixed point here, not floating point. Be careful to round properly when going from the floating point input to your fixed point representation, though. Here is a short version I hacked up, which should work for all positive inputs:

    #include <stdlib.h>
    #include <stdio.h>
    int main(int argc, char ** argv)
    {
        float change = atof(argv[1]);
        int work = (int)(100*change+0.5);
        int quarters, dimes, nickels, pennies;
        quarters = work/25; work %= 25;
        dimes    = work/10; work %= 10;
        nickels  = work/5;  work %=  5;
        pennies  = work;
        printf("%.2f dollars = %d quarters, %d dimes, %d nickels and %d pennies: %d coins total\n",
        change, quarters, dimes, nickels, pennies, quarters+dimes+nickels+pennies);
        return 0;
     }
    

    For example:

    ./change 4.20
    4.20 dollars = 16 quarters, 2 dimes, 0 nickels and 0 pennies: 18 coins total
    
    0 讨论(0)
  • 2021-01-23 12:42

    Throw out the floating-point calculations. this is all based on hundredths at best, so just use integer division/modulo. Never rely on perfect accuracy in floating point numbers.

    #include <stdio.h>
    #include <cs50.h>
    
    int main(void){
    
        float fchange = 0.00;
        int change = 0;
    
        printf("How much change is owed? ");
        fchange = GetFloat();
    
        change = (int)roundf(fchange*100.0);
    
        int quarters = change/25;
        change = change % 25;
    
        int dimes = change/10;
        change = change % 10;
    
        int nickels = change/5;
        change = change % 5;
    
        printf("%d quarters, %d dimes, %d nickels, %d pennies\n", quarters, dimes, nickels, change);
    
        return 0;
    }
    
    0 讨论(0)
  • 2021-01-23 12:42

    The closest float value to 4.20 is slightly smaller than that (4.19999980926513671875, for the usual 32-bit IEEE754 floats). So after you subtracted the $4 from the 16 quarters, you have an amount left that is slightly smaller than 0.2. Dividing that by 0.1 results in a value slightly smaller than 2, so your nickels value is 1. The same happens then after you subtract your nickel, the value is slightly smaller than 0.1, dividing by 0.05 results in a quotient slightly smaller than 2.

    You should use integers only for such a computation, calculating in cents.

    0 讨论(0)
提交回复
热议问题