Witam, mam pytanie, dlaczego mi źle liczy liczby po przecinku, z jakiegoś powodu są dodawane lub odejmowane liczby 0,0003 lub coś w tym stylu i źle mi wydaje monety/banknoty.
#include <iostream>
using namespace std;
int main()
{
float a;
cout<<"Do zap³aty : ";
cin>>a;
float b;
cout<<"Zap³acono : ";
cin>>b;
float reszta = b-a;
cout<<"reszta = "<<reszta<<endl<<endl;
double tab[15] = {500, 200, 100, 50, 20, 10, 5, 2, 1, 0.5, 0.2, 0.1, 0.05, 0.02, 0.01};
for (int i=0, j=0; i<12 && j<15; i++)
{
reszta = reszta - tab[j];
if(reszta < 0)
{
reszta = reszta + tab[j];
if(i>0 )
{
if(j>=6)
{
cout<<i<<" monet "<<tab[j]<<" zl"<<endl;
cout<<"reszta = "<<reszta<<endl;
j++;
i=-1;
}
else if (j<6)
{
cout<<i<<" banknotow "<<tab[j]<<" zl"<<endl;
cout<<"reszta = "<<reszta<<endl;
j++;
i=-1;
}
}
else if (i==0)
{
j++;
i--;
}
}
if(reszta > 0 & i==9)
{
if(j>=6)
{
cout<<i+1<<" monet "<<tab[j]<<" zl"<<endl;
cout<<"reszta = "<<reszta<<endl;
j++;
i=-1;
}
else if (j<6)
{
cout<<i+1<<" banknotow "<<tab[j]<<" zl"<<endl;
cout<<"reszta = "<<reszta<<endl;
j++;
i=-1;
}
}
}
return 0;
}