Accuracy of float and double

I have a program that takes in a cash amount in dollars and converts it to coins. Whenever the amount of pennies is over 1 the program output says that one less pennies than the actual amount is needed. For the amount to be inputed I first used a double. This is when the problem occured. I debugged the code and found that double was actually a hair smaller than the actual value. i.e (if the value is supposed to be 3.1 after calculations it will really be 3.09999999998) or something under the expected value. When the code computed amount of pennies it truncated the fractional part. But when using a float the actual value was just a hair over so the program ran correctly. Why is the value of so small a number not exact? Here is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
int main()
{
    //must be set greater than a dollar for the while condition to run
    float amount = 5.01; //Amount of money to be converted to q, d, n, and p's
    
    cout << "Enter a dollar amount\n"
         << "less than $5.00\n";
         
    while(amount >= 5.0)
    {
        cin >> amount;
        
        /* data checking here... I just omitted the code 
           Everything is fine here */
    }
    
    int quarters = 0; //number of quarters, nickels, dimes, and pennies
    int dimes = 0;
    int nickels = 0;
    int pennies = 0; 
        
    quarters = static_cast<int>(amount * 4);
    amount = amount - (quarters * .25); /***************Problem code****/
    
    dimes = static_cast<int>(amount * 10);
    amount = amount - (dimes * .10); /***************Problem code****/
    
    nickels = static_cast<int>(amount * 20);
    amount = amount - (nickels * .05); /***************Problem code****/
    
    pennies = static_cast<int>(amount * 100);
    amount = amount - (pennies * .01); /***************Problem code****/


Where the problem code is marked the value of a double and float is not exact as it should be. amount either adds a few hundred-thousanths onto the number or subtracts it.
Last edited on
Decimal 0.1 is a recurring decimal in binary, which is the system floating point numbers use. That means that in order to precisely represent decimal 0.1 in binary, an infinite amount of space is required. Of course, the computer only has finite memory, so the representation is imprecise.

You may want to consider using an integer-based fixed point system: 1=1 cent, 100=$1, 1000=$10, etc.
Last edited on
Okay, I just wanted to know the logic behind the problem.
helios is correct. You're experiencing rounding errors that occur because of how floating point numbers are held and manipulated.

One way to avoid them, in your case, is to perform the calculations in cents so you use can integers, which will suffice for your problem.
closed account (z05DSL3A)
If you want a 'good' read google: "What every computer scientist should know about Floating-Point Arithmetic"
why don't you just
double quarterValue, etc;
quarterValue = 1/4.0;
amount -= quarters*quarterValue;

and get rid of all that static cast?

or if your problem is showing more then 2 decimal then that is an easy fix as well
Last edited on
@Brutal static casts are used to shut the compiler up. Technically I don't need them at all but then it get a list of warnings.

This error was just something I noticed when testing the program. I never knew that floating point values were not as exact as they should be.
Last edited on
Article: Rounding Algorithms
http://www.cplusplus.com/forum/articles/3638/
Topic archived. No new replies allowed.