hellodang
hellodang

Reputation: 13

cash division error in C with floating points

Im currently working on the cs50 "cash" problem: estimating the amount of coins needed to pay some change.

ex: $0.41 owed = 1 quarters, 1 dime, 1 nickel, 1 penny.

however, when estimating the amount of coins needed i end up being off with the pennies i believe is due to error on my part as it always seems to be off by one or 2 coins (pennies).

I have included multiple printf statement to try and track what i could be doing but i can't seem to figure out why the division isn't working.

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

int main(void)
{
    // change
    float change = get_float("how much change is owed?: ");
    int coins = 0;

    //reprompt
    while (change < 0)
    {
        change = get_float("how much change is owed?: ");
    }

    //quarter
    float quarter = 0.25;
    float quarters = change / quarter;
    quarters = (int) quarters;
    change = change - (quarters * quarter);

    printf("%f quarters\n", quarters);

    //dimes
    float dime = 0.10;
    float dimes = change / dime;
    dimes = (int) dimes;
    change = change - (dimes * dime);

    printf("%f dimes\n", dimes);

    //nickels
    float nickel = 0.05;
    float nickels = change / nickel;
    nickels = (int) nickels;
    change = change - (nickels * nickel);

    printf("%f nickels\n", nickels);
    printf("%f in change left change\n", change);

    //pennies
    float penny = 0.010000;
    float pennies = change / penny;
    pennies = (int) pennies;
    change = change - (pennies * penny);

    printf("%f pennies\n", pennies);

    //coins
    coins = quarters + dimes + nickels + pennies;
    printf("%i\n", coins);

    //printf("%f\n", change);
}

Upvotes: 1

Views: 312

Answers (3)

pjaj
pjaj

Reputation: 235

Just a suggestion, why not perform all the calculations in integer arithmetic? Convert the float "change" into an integer "cents"

(int) cents = roundf(100.0 * change); // see comments below answer

Then keep everything as integers thereafter, for example

//quarter
const int quarter = 25;
int quarter_count = cents / quarter;
cents = cents % quarter; // use modulus operator

May I also respectfully suggest that using variable names which are very similar, for example quarter and quarters, is likely to cause errors.

As you will never want to change the number of cents in a quarter (especially accidentally) you can declare it as a const int.

You might also want to check that the user has entered a decimal (float) amount that they believe to be dollars rather than an integer number of cents. But this is beyond the scope of this question.

Upvotes: 0

Start with this line:

#include <math.h>


#define PENNIES_IN_GBP (100)


int main(void)
{
        int change_pn = roundf(PENNIES_IN_GBP * get_float("how much change is owed?: "));

        /* ... */

        return 0;
}

The rest should be easy ;)

Upvotes: 1

Eric Postpischil
Eric Postpischil

Reputation: 222660

In your C implementation, these lines:

float dime = 0.10;
float nickel = 0.05;
float penny = 0.010000;

sets dime to 0.100000001490116119384765625, nickel to 0.0500000007450580596923828125, and penny to 0.00999999977648258209228515625. This results in the calculation for each coin being slightly off. Furthermore, the calculations of change after processing each coin have rounding errors.

To fix this, after getting change with get_float, convert it to a number of cents with:

int cents = roundf(change * 100);

Then perform all calculations with integer arithmetic. (Include <math.h> to get the declaration of roundf.)

Upvotes: 4

Related Questions