Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the behavior of unsigned(-0.0) defined in C++?

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?

like image 968
Jeff Walden Avatar asked Jun 21 '13 00:06

Jeff Walden


People also ask

Is unsigned overflow defined C?

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.

Is overflow undefined in C?

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.

Is overflow defined in C?

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).


1 Answers

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.

like image 141
Adam H. Peterson Avatar answered Oct 10 '22 02:10

Adam H. Peterson