i have a question about relational operators are they always give correct results ? because if we run this line of code it will result 1 instead of 0
cout<<(ULLONG_MAX==-1);
and if we keep subtracting 1 of both sides it will keep resulting 1
so this could give wrong results in our programs
what is the solution to it?
Since you are looking for solutions:
Enabling warnings (
-Wall
or more specifically-Wsign-compare
on clang or gcc) should give you a warning if you accidentally do this:And the fix is to make sure everything has the same type.
If you really meant to compare two objects of different types, you can use
std::cmp_equal
(or the example implementation if you can't use C++20):which directly compares the values of the integers, regardless of the type.