Sign up ×

The MSDN documentation mentions that double type includes negative zero. However, both -1.0 / double.PositiveInfinity and -double.Epsilon / 2 appear to return normal 0 (and compare equal to it). How can I get -0?

share|improve this question
Okay, I'm curious: since they're mathematically the same value, why do you want to distinguish +0 from -0? – Beska Jun 25 '09 at 13:34
Because I want to show the differences between normal mathematical calculations and floating point calculations. – Alexey Romanov Jun 25 '09 at 22:11

5 Answers 5

up vote 16 down vote accepted

Here is a practical example of differentiating between the two without examining the bits. MSDN links here and here assisted me in constructing this example.

static void Main(string[] args)
    float a = 5 / float.NegativeInfinity;
    float b = 5 / float.PositiveInfinity;
    float c = 1 / a;
    float d = 1 / b;



Take note that -0 and 0 both look the same for comparisons, output, etc. But if you divide 1 by them, you get a -Infinity or Infinity, depending on which zero you have.

share|improve this answer
> Take note that -0 and 0 both look the same for comparisons, output, etc. Thanks, that's what I missed. – Alexey Romanov Jun 25 '09 at 13:33
+1 Nice. Way better than my answer. – Simon P Stevens Jun 25 '09 at 13:40

Negative zero is to do with the way that the number is stored in binary, not any real achievable result from a mathematical calculation.

In floating point storage, the topmost bit is often used to denote sign. This leaves 31 bits for data (in a 32bit floating point value) so there are actually two representations for zero.

00000000 00000000 00000000 00000000
00000000 00000000 00000000 00000001

Both represent zero, but one with the sign bit set to negative.

Naturally, this would normally occur when you incremented the highest possible positive number, it would overflow back to negative zero.

In .net however I think by default the type does overflow checks and will throw an exception rather than let you overflow, so the only way to really archive this value is by setting it directly. Also, -0 should always compare equal to +0.

There is more about it on Wikipeida

share|improve this answer
Actually, Wiki says "whilst it is possible to obtain negative zero as the result of an expression (for instance as the result of arithmetic underflow on a negative number)" for languages including C#. – Alexey Romanov Jun 25 '09 at 13:29
"arithmetic underflow on a negative number" - Yeah, that's just the reverse of overflow of the highest positive number in my example. Anyway Brian's answer is much better than mine, listen to him =:) – Simon P Stevens Jun 25 '09 at 13:37

One way is to use the BitConverter.GetBytes. If you check the bytes, you will see that the sign bit for the value is actually set indicating that its negative.

byte[] zeroBytes = BitConverter.GetBytes(zero);
byte[] negZeroBytes = BitConverter.GetBytes(negZero);

bool sameBytes = zeroBytes[7] == negZeroBytes[7];
share|improve this answer
Doesn't seem to work for me. Firstly, zero & negZero, what are they? Secondly, if I make them doubles and set the values 0 & -0, I just get an array of bytes that are all 0 for both. – Simon P Stevens Jun 25 '09 at 13:39

Try this. If pz is positive zero and nz is negative zero:

Double.PositiveInfinity/pz => Double.PositiveInfinity
Double.PositiveInfinity/nz => Double.NegativeInfinity

I got this from ECMA C# specification.

You can obtain negative zero by dividing any positive number by negative infinity:

share|improve this answer

After checking, I see that -1.0 / double.PositiveInfinity does return -0. Indeed, 1.0 / (-1.0 / double.PositiveInfinity) returns -infinity.

share|improve this answer

Your Answer


