Any reasons for assigning -0?

I am looking at old C code using Lint, which came across this line:

int16_t max = -0; 

The Lint message is that the expression "Constant" takes the value 0 in the operation "-".

Is there a reason someone would use -0?

+5
source share
4 answers

No, I see no reason for this. Others mentioned that it is possible to have platforms with a "negative zero", but such a negative zero can never be created by this expression, so it is useless.

The relevant paragraph in the C standard is 6.2.6.2 p3, my emphasis is:

If the implementation supports negative zeros, they should only be generated by:

- operators &, |, ^, ~, <<and โ†’ with operands that produce such a value;

- operators +, -, *, / and% , where one operand is negative zero , and the result is zero;

- connection based on the above cases.

To create a negative zero on such a platform, you could use, for example, ~INT_MAX , but that would not be zero for other views, so the code would not be very portable.

+4
source

In specification C (6.2.6.2 Integer types), it states the following (emphasis):

For signed integer types, the object representation bits must be divided into three groups: value bits, padding bits, and a sign bit. There should be no padding bits; There must be exactly one sign bit. Each bit, which is a value bit, must have the same value as one bit in the representation of the object of the corresponding unsigned type (if there are M bits in the signed type and N in the unsigned type, then M ยฃ N). If the sign bit is zero, it should not affect the resulting value. If the sign bit is one, the value must be changed in one of the following ways:

  • the corresponding value with the sign bit 0 is negated (sign and value);
  • the sign bit matters - (2N) (two additions);
  • the sign bit has a value of - (2N - 1) (one addition).

Which one is used is determined by the implementation, as well as a value with a sign bit 1 and all bits of a value zero (for the first two) or with a sign bit and all bits of a value 1 (for one addition), is a representation trap or a normal value. In the case of sign and magnitude, they complement, if this representation is a normal value, it is called a negative zero .

In other words, C supports three different representations for signed integers , and two of them have the concept of signed zero , which makes the difference between positive and negative zeros.

So my explanation is that perhaps the author of your code snippet was trying to create a negative null value. But, as stated in the Jens Gustedt answer , this expression cannot actually give a negative zero, which means that the author may have made the wrong assumption there.

+7
source

This is for an architecture that uses a CPU with one add-on .

  • This addition is a way of representing negative numbers having -0. Still used for floating point.
  • For unsigned numbers, the maximum number is really -0 : all 1s.

We are used to two add-on numbers that have another negative number. Although one pad has some problems around zero, the same is true for two pad around MIN_INT: -MIN_INT == MIN_INT .

In the above code, unsigned numbers were probably specified:

 uint16_t max = (uint16_t) -1; 
0
source

There is no reason for this with C99 or later.

int16_t is an integer type of exact width.

The name typedef intN_t denotes an integer type with a sign with a width of N, no padding bits, and a double padding representation. Thus, int8_t denotes such a signed integer type with a width of exactly 8 bits. C11dr ยง7.20.1.1 1

No zero sign with double padding. So the code is equivalent

 int16_t max = 0; 

IMO, expected result int16_t max = -0; on a platform other than level 2, had to initialize max to -0 to mark an array of length 0 or one containing only elements with a value of -0 .

0
source

Source: https://habr.com/ru/post/1232032/


All Articles