float fValue1 = 1.345f;
float fValue2 = 1.123f;
float fTotal = fValue1 + fValue2; // should be 2.468
if ( fTotal == 2.468 )
std::cout << "fTotal is 2.468";
std::cout << "fTotal is not 2.468";
std::cout << "\n" << std::setprecision(10) << fTotal << std::endl;
Not surprisingly, the result is fTotal is not 2.468, even though it is due to the round off error. the reason is fTotal has been stored as 2.467999935 in the computer. My question is Is there a way to force the computer to store a variable in a limited digits? Let's say in the aforementioned code in four digits so the fTotal should be 2.468?