I was looking into your v5.1 source code after getting some warnings during compilation.
At line 1016 the assert command gives this warning :
comparison of unsigned expression in ‘>= 0’ is always true [-Wtype-limits]
The concerning assert line is assert(allocated_time * (2 * research - 1) / 10 >= 0);
To me this warning is strange : i can imagine 'research' is 0, then (2 * research - 1) will be -1 and the expression on the left side will be negative.
ChatGPT gave me this answer :
The warning you are seeing is related to how C++ handles unsigned integer types in comparisons, and it can indeed be a bit tricky. Let's break it down.
Key Points:
-
Unsigned Integers in C++: If allocated_time is an unsigned integer type (like unsigned int, size_t, etc.), then any arithmetic operation involving it will also be treated as an unsigned operation.
-
Unsigned Arithmetic: When you multiply an unsigned integer by a signed integer (like (2 * research - 1)), the result can still be an unsigned integer if the other operand (in this case, allocated_time) is unsigned. If the signed operand is negative, it will be interpreted as a large positive number when treated as unsigned.
-
Comparison Against Zero: The comparison >= 0 for an unsigned type is always true because unsigned integers can never be negative. This is what the warning is pointing out.
Analysis of Your Expression:
-
allocated_time: Assuming it’s unsigned, this is the crucial part.
-
(2 * research - 1): This can be negative if research is 0. However, when you multiply it with allocated_time (which is unsigned), the negative result is converted to an unsigned number, which effectively becomes a large positive number due to underflow.
-
Final Expression: The result of allocated_time * (2 * research - 1) / 10 is unsigned. Thus, when you compare it to 0, the comparison is always true, because unsigned numbers are always >= 0.
Why the Warning?
The warning is there because the compiler detects that you're comparing an unsigned value against zero, which is always true, making the comparison unnecessary and possibly indicating a logic error.
What do you think about this ?
Btw. sure this is a minor issue, it's just a warning, but i'm wondering and i want to learn.
I was looking into your v5.1 source code after getting some warnings during compilation.
At line 1016 the assert command gives this warning :
comparison of unsigned expression in ‘>= 0’ is always true [-Wtype-limits]The concerning assert line is
assert(allocated_time * (2 * research - 1) / 10 >= 0);To me this warning is strange : i can imagine 'research' is 0, then (2 * research - 1) will be -1 and the expression on the left side will be negative.
ChatGPT gave me this answer :
The warning you are seeing is related to how C++ handles unsigned integer types in comparisons, and it can indeed be a bit tricky. Let's break it down.
Key Points:
Unsigned Integers in C++: If allocated_time is an unsigned integer type (like unsigned int, size_t, etc.), then any arithmetic operation involving it will also be treated as an unsigned operation.
Unsigned Arithmetic: When you multiply an unsigned integer by a signed integer (like (2 * research - 1)), the result can still be an unsigned integer if the other operand (in this case, allocated_time) is unsigned. If the signed operand is negative, it will be interpreted as a large positive number when treated as unsigned.
Comparison Against Zero: The comparison >= 0 for an unsigned type is always true because unsigned integers can never be negative. This is what the warning is pointing out.
Analysis of Your Expression:
allocated_time: Assuming it’s unsigned, this is the crucial part.
(2 * research - 1): This can be negative if research is 0. However, when you multiply it with allocated_time (which is unsigned), the negative result is converted to an unsigned number, which effectively becomes a large positive number due to underflow.
Final Expression: The result of allocated_time * (2 * research - 1) / 10 is unsigned. Thus, when you compare it to 0, the comparison is always true, because unsigned numbers are always >= 0.
Why the Warning?
The warning is there because the compiler detects that you're comparing an unsigned value against zero, which is always true, making the comparison unnecessary and possibly indicating a logic error.
What do you think about this ?
Btw. sure this is a minor issue, it's just a warning, but i'm wondering and i want to learn.