As in the description. I'm wondering why using floats in my program works fine. But when I change them to doubles there is lack of 0.1$...
The code below is working great using floats. When I change them to doubles, the result changes.
// Example program
#include <iostream>
using namespace std;
int main()
{
float money_rest,how_much_i_paid,real_cost_of_product;
how_much_i_paid = 8;
real_cost_of_product = 6.2;
if (how_much_i_paid >= real_cost_of_product)
{
money_rest = how_much_i_paid - real_cost_of_product;
}
cout<<"I have to get back: "<<money_rest<<endl; // 1.8
float nominals [7] = {10,5,2,1,0.5,0.2,0.1};
int how_much_nominals_ill_get_back = 0;
int i = 0;
while (i<7 && money_rest!= 0)
{
if (money_rest >= nominals[i])
{
how_much_nominals_ill_get_back = money_rest / nominals[i];
cout << nominals[i] << " $$ x " << how_much_nominals_ill_get_back << endl;
money_rest -= nominals[i] * how_much_nominals_ill_get_back;
}
i++;
}
}