Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any reason to assign -0?

I am going through some old C code using Lint which stumbled upon this line:

int16_t max = -0;

The Lint message is that the "Constant expression evaluates to 0 in operation '-'".

Is there any reason why someone would use -0?

like image 340
qnyz Avatar asked Sep 22 '15 13:09

qnyz


People also ask

What is the meaning of to assign?

to give to someone a job or responsibility, or to decide on a person for a particular job or responsibility: We assigned Alberto the task of watching the children.

What is a good sentence for assign?

Verb The teacher assigned us 50 math problems for homework! She was assigned to the embassy in India. The new teacher was assigned to the science laboratory.

Did you assign or assigned?

If you assign a piece of work to someone, you give them the work to do. If you assign something to someone, you say that it is for their use. If someone is assigned to a particular place, group, or person, they are sent there, usually in order to work at that place or for that person.

Have been assigned meaning?

to give a particular job or piece of work to someone: [ + two objects ] UN troops were assigned the task of rebuilding the hospital. The case has been assigned to our most senior officer.


2 Answers

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

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

  • the corresponding value with sign bit 0 is negated (sign and magnitude);
  • the sign bit has the value -(2N) (two’s complement);
  • the sign bit has the value -(2N - 1) (one’s complement).

Which of these applies is implementation-defined, as is whether the value with sign bit 1 and all value bits zero (for the first two), or with sign bit and all value bits 1 (for one’s complement), is a trap representation or a normal value. In the case of sign and magnitude and one’s 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 a distinction between a positive and a negative zero.

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

like image 149
Theodoros Chatzigiannakis Avatar answered Sep 27 '22 21:09

Theodoros Chatzigiannakis


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

The corresponding paragraph in the C standard is 6.2.6.2 p3, emphasis is mine:

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

— the &, |, ^, ~, <<, and >> operators with operands that produce such a value;

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

— compound assignment operators based on the above cases.

To produce a negative zero on such a platform you could use ~INT_MAX, for example, but that would not be a zero for other representations, so the code wouldn't be very portable.

like image 37
Jens Gustedt Avatar answered Sep 27 '22 21:09

Jens Gustedt