Main Site Documentation

Found a weird little NETMF bug


#1
			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… ::slight_smile: :wall:


#2

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. :smiley:


#3

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


#4

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

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


#5
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


#6

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());

            Thread.Sleep(Timeout.Infinite);
        }

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


#7

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


#8

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

If you made it result *= -1.0;

What happens?


#9

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? :wink: Think about it before clicking on the answer below.

Here is the answer: http://en.wikipedia.org/wiki/Signed_zero


#10

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


#11

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


#12

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

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#: http://msdn.microsoft.com/en-us/library/aa691146(VS.71).aspx


#13

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.


#14

Can you not use typed literals to generate zero?

double zero = 0D;

Andy