C++11 4.9 Floating-integral conversions [conv.fpint]:
A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type.
If the value is -0.0
, is behavior defined? It comes down to whether "the truncated value cannot be represented in the destination type". Zero could be represented. Can negative zero? In this context, are the two zero values distinguished, or not distinguished?
A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type. This makes unsigned integer types a special case.
12.2. 1 Basics of Integer Overflow In contrast, the C standard says that signed integer overflow leads to undefined behavior where a program can do anything, including dumping core or overrunning a buffer.
An integer overflow occurs when you attempt to store inside an integer variable a value that is larger than the maximum value the variable can hold. The C standard defines this situation as undefined behavior (meaning that anything might happen).
The truncated value of -0.0
is 0
, which is representable in integral types (including unsigned integral types). There's no reason to suppose that the truncated value of -0.0
and 0.0
are different, any more than the truncated values of -0.25
and 0.3
are different.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With