-1

Although I'm aware that there are other threads about this, I've created my own because I don't want to see others' solutions. All of the results print fine except for 4.2. I'm aware of floating point imprecision and have tried to solve it, but cannot do so.

Can someone point me in the right direction on this? (No solutions, just a nod in the right direction.)

#include<stdio.h>
#include<cs50.h>
#include<math.h>

int main(void)
{
    double quarter = 0.250, dimes = 0.100, nickels = 0.050, pennies = 0.010;
    double $change = 0.00;
    int coins = 0;
    double rem = 0.00;
    double rem2 = 0;
    double rem3 = 0;
    double a = 0;
    int b = 0;
    double c = 0;
    double d = 0;

    do
    {
        $change = get_float("Change owed: ");
    }
    while( $change < 0);
    int cents = round( $change * 100);

    a = $change / quarter;
    rem = fmod($change, quarter);

    b = rem / dimes;
    rem2 = fmod(rem, dimes);

    c = rem2 / nickels;
    rem3 = fmod(rem2, nickels);

    d = rem3 / pennies;

    coins = a + b + c + d;

    printf("%i\n", coins);
    return 0;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    What's the question? You are entering `4.2` to the `get_float` and it does something wrong? What would that be and what should happen instead? – GSerg May 24 '20 at 16:51
  • 3
    Multiply all the numbers by 100, and use integer math. And don't use dollar signs in variable names. – user3386109 May 24 '20 at 16:52
  • 1
    Please see [Why not use Double or Float to represent currency?](https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency) The decimal value `4.2` cannot be *exactly* represented in floating point. OK, you used `round` to obtain `cents` but then you don't use `cents` again. – Weather Vane May 24 '20 at 16:56
  • Thanks for the responses guys. I should have been a little clearer with the question. All amounts of money I enter - print the right amount of coins (the minimum number) except for 4.2 ($4.20) which prints 24 coins rather than the number it should return, 18. I will check out all of the links here. Using get_float and floats is a requirement of the problem btw. – ComicBookGuy May 24 '20 at 18:17
  • Re “Although I'm aware that there are other threads about this, I've created my own because I don't want to see others' solutions.”: Stack Overflow is not a personal tutorial service for you. A primary purpose is to create a durable repository of questions and answers to serve as a resource for people. And having and answer spoon-fed to you is certainly not a better way to learn by figuring it out yourself either searching for existing information or doing it on your own. Respect the time of other participants and do not expect them to serve your personal desires. – Eric Postpischil May 24 '20 at 19:47

1 Answers1

1

Don't use floats/doubles Why not use Double or Float to represent currency?

You may work with integers with the minimum quantity be 1 penny for example. You may convert everything to penny to do calculations and then you can convert it back to anything to display as, but you should always treat reminders like for example for divisions some part can have an extra penny. Or if you want more generic solution then maybe you may use decimals such as https://github.com/libdfp/libdfp and How to print/convert decimal floating point values in GCC?

you may search for more https://github.com/search?l=C&q=decimal&type=Repositories

Abdurrahim
  • 2,078
  • 1
  • 17
  • 23