# Found a weird little NETMF bug

``````			string zeroStr = "0";
double result = double.Parse(zeroStr) * -1;
Debug.Print(result.ToString());
``````

…This results in -0. WTF? 0 * -1 = 0

I guess, technically, this could be true, since -0 IS the same as 0, but still… : :wall:

Not really a bug per se, it has to do with the way a signed number is stored. A signed number sets the high bit to indicate that the number is negative. For example a short could be 1000 0000 0000 0000, which is -0. But if you use a -0 in any other expression will get you the same thing as a regular old +0.

I think the issue is in the parse method. IT doesn’t happen if you don’t have that htere and you just say 0 * -1

Definitely a parse issue, there is no high bit on a string

But as long as it ONLY happens on zero it doesn’t matter.

Now if it does that for 1 somebody has some ‘splainin’ to do

``````double result = double.Parse(zeroStr) * -1;

``````

This part “double.Parse(zeroStr)” evaluates to 0 when you then multiply it by -1 (which has high bit set) so you get -0. Strange but true, and it is not exclusive to one language or processor family.

I bet the other folks around here could come up with another few ways to generate a -0 as well. ;D

How to manufacture a -0:

Try this

``````        public static void Main()
{
double result = -0.0;
//double result = -0; // now try this one instead...

Debug.Print("(1) result = " + result.ToString());
result *= -1;
Debug.Print("(2) result = " + result.ToString());

}
``````

Try both ways of instantiating ‘result’ and discover when the -0 is created in each case. Interesting? :o

Wait, but a minus times a minus is a positive…?

I wonder if it has to do with the double times an int.

If you made it result *= -1.0;

What happens?

What I’m getting at is that if you instantiate ‘result’ this way:

``````double result = -0.0;

``````

The first value printed out is -0, and the * -1 makes it a positive. if however you instantiate ‘result’ as:

``````double result = -0;

``````

The first value printed out is 0, and the *-1 makes it a negative.

What does this tell us? Think about it before clicking on the answer below.

We have passed this on to Microsoft and they will look into this for future builds

I think that finding a bug in Microsoft’s code demands some EX points Gus

It is NOT a bug, it’s a feature. :

It is required by IEEE 754 that floating point types support both + and - zero (link I posted above) here is a bit more information from MS about floating point types in C#: [url]Microsoft Learn: Build skills that open doors in your career

Actually they found out that the core C++ give this results so in a way, it is correct. But, this needs to match the big .NET so they will look into it.

Can you not use typed literals to generate zero?

double zero = 0D;

Andy