Cannot correctly compare two numbers

unsigned long long value = 0;

bool result = value >= std::numeric_limits<signed int>::min();

This should give true but gives false? Why and how can I fix it?

Upvotes: 2

Views: 1379

Answers (1)

Erik
Erik

Reputation: 91320

The int converts to an unsigned int for comparison. You could cast your unsigned long long to a long long.

5/9:

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

...

Then, if either operand is unsigned long the other shall be converted to unsigned long.

...

Otherwise, if either operand is unsigned, the other shall be converted to unsigned.

unsigned long long and long long are not standard types in c++03, but it's quite likely that the compiler will treat these types using the mechanisms defined above. The mentioned conversion rules would then cover this particular conversion.

Upvotes: 3

Related Questions