#include<iostream>
#include<string>
#include <iomanip> // for setprecision()
usingnamespace std;
main()
{
cout<<setprecision(7); //7 decimals
float v = 1;
float j = 3;
float cc;
cc = v/j;
//TEST with FLOAT NUMMERS
float ff = 0.3333333; // 7 decimals as set in "setprecusion(7);"
if(cc<ff) {
cout<<"cc is smaller then ff"<<endl;
}
elseif(cc>ff) {
cout<<"cc is bigger then ff"<<endl;
}
else{
cout<<"cc equals to ff"<<endl;
}
cout<<cc<<" = cc"<<endl;
cout<<ff<<" = ff"<<endl;
return 0;
}
The output gives me that cc is bigger than ff…
I don’t understand why as I set precision to 7 and my var ff has also 7 decimals.
They should both be equal to eachother.
I tested it with 1 and 3 and had the same result as I defined them as floats.
Peter87 is absolutely correct: what you see isn't what you actually get...
I have set cout<<setprecision(30) and I get different numbers after the 7th digit.
I guess I need to be careful using == when comparing floats :)