0.01 does not equal 0.01???

I am working on a program to calculate how many of each type of coin you will get back as change, but it seems that the "OR EQUAL TO" part of ">=" doesn't work but I’m not sure why. If anyone can tell me why this happens or how to fix it I would be very thankful.

PS. I apologize ahead of time for any spelling errors that there might be. I am also aware that this might not be the most efficient way of doing this, but its how I chose to do it.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace std;

int main()
{
	double change = 0.00;
	int dollars = 0;
	int quarters = 0;
	int dimes = 0;
	int nickels = 0;
	int pennies = 0;

	cout << "Change: ";
	cin >> change;

	while(change >= 1.00)
	{
		dollars++;
		change -= 1.00;
	}
	while(change >= 0.25)
	{
		quarters++;
		change -= 0.25;
	}
	while(change >= 0.10)
	{
		dimes++;
		change -= 0.10;
	}
	while(change >= 0.05)
	{
		nickels++;
		change -= 0.05;
	}
	while(change >= 0.01)
	{
		pennies++;
		change -= 0.01;
	}

	cout << "\nDollars:\t" << dollars << endl;
	cout << "Quarters:\t" << quarters << endl;
	cout << "Dimes:\t\t" << dimes << endl;
	cout << "Nickels:\t" << nickels << endl;
	cout << "Pennies:\t" << pennies << endl;

	return 0;
}
You know how you can't write the exact value of 1/3 or 1/6 in decimal? Well, similar problems exist in other numbering systems. Floating point numbers are inherently incapable of exactly representing certain values, such as 0.2, 0.1, 0.05, or 0.01. Such numbers have infinitely long fractional parts. Since the computer only has finite memory, it can only do the next best thing, which is storing an approximation of the number.

The lesson here is: don't use floating point types to store monetary values. A simple alternative would be to store the number of pennies in an integer.
Thank you for the quick response, that helps a lot.
Topic archived. No new replies allowed.