I am trying to run the following program and it works but when I enter a value that is more than 6 decimal places it keeps getting rounded/truncated e.g. 2.999999 –> 3. How do I set it so it stops doing this?
int main()
{
double n=0, x=0;
while (cin >> n >> x) //will keep going until an integer is not entered
{
cout << "You entered the two integers " << x << " and " << n << endl;
if (x-n <= (1.0/10000000) && n-x <= (1.0/10000000))
cout << "The numbers are almost equal" << endl;
}
return 0;
}
You can change precision of the values that you print by using
std::setprecision:Link to ideone.