double signed comparison in C++
-
double a = -60 ; double b = -7500 bool flag; if (a < b) { flag = true; } For the above check flag is getting true, it is not supposed to, how can I make the double negative comparison?
Comparison of double values behaves as expected, of course (and, as it stands, your code doesn't compile): the program
#include int main()
{
double a = -60 ;
double b = -7500;bool flag = false;
if (a < b)
{
flag = true;
}std::cout << std::boolalpha << flag << std::endl;;
}ouptus
false
-
double a = -60 ; double b = -7500 bool flag; if (a < b) { flag = true; } For the above check flag is getting true, it is not supposed to, how can I make the double negative comparison?
It is because your**
Quote:
bool flag;
**was NOT initialized and contains garbage that is treated as true just because it is not zero. Try:
double a = -60 ;
double b = -7500bool flag = (a < b);