432,537 Members | 1,743 Online + Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,537 IT Pros & Developers. It's quick & easy.

# Double minus zero

 P: n/a Hi, Does the Double has the facilty to define -0. If it has, "==" or System.Math.Sign() doesn't able to differentiate between -0 & 0. i.e. Double x = -0.0d Double y = 0.0d if(value == -0.0d) {//both x & y drops in this statement. //is minus zero } else { //Not minus zero } How to differentiate -0.0d & 0.0d in if statement? Thank you, Avin Patel Nov 16 '05 #1
8 Replies

 P: n/a Avin Patel wrote: How to differentiate -0.0d & 0.0d in if statement? How can a sign have any meaning when discussing the absence of a value? -- Things never go according to plan. So plan accordingly. Nov 16 '05 #2

 P: n/a Avin Patel wrote: Hi, Does the Double has the facilty to define -0. If it has, "==" or System.Math.Sign() doesn't able to differentiate between -0 & 0. i.e. Double x = -0.0d Double y = 0.0d if(value == -0.0d) {//both x & y drops in this statement. //is minus zero } else { //Not minus zero } How to differentiate -0.0d & 0.0d in if statement? Yup - Math.Sign() is documented to return 0 if the double value is 0 (and positive zero is equal to negative zero according to the IEEE spec). To determine if you have negative zero, I think you'll have to write a bit of code that looks at the sign bit directly: public static bool IsSignBitSet( double x) { byte[] bytes = BitConverter.GetBytes( x); // this assumes a little-endian machine return ((bytes[bytes.Length-1] & 0x80) == 0x80); } // ... Console.WriteLine( "IsSignBitSet( +0.0): {0}", IsSignBitSet( +0.0)); Console.WriteLine( "IsSignBitSet( -0.0): {0}", IsSignBitSet( -0.0)); The above code outputs: IsSignBitSet( +0.0): False IsSignBitSet( -0.0): True -- mikeb Nov 16 '05 #3

 P: n/a mikeb wrote: How to differentiate -0.0d & 0.0d in if statement? Yup - Math.Sign() is documented to return 0 if the double value is 0 (and positive zero is equal to negative zero according to the IEEE spec). To determine if you have negative zero, I think you'll have to write a bit of code that looks at the sign bit directly: Alternatively, check for whether it's equal to positive 0 but has a different bit sequence to positive 0 (still using BitConverter.GetBytes, but regardless of endianness). -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #4

 P: n/a Frank Oquendo wrote: How to differentiate -0.0d & 0.0d in if statement? How can a sign have any meaning when discussing the absence of a value? Very easily. -0 and +0 are mathematically distinct when considering limits, where something can tend to 0 from above or below, for instance. It's all well-defined in IEEE arithmetic, I believe. -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #5

 P: n/a Jon Skeet [C# MVP] wrote: mikeb wrote:How to differentiate -0.0d & 0.0d in if statement?Yup - Math.Sign() is documented to return 0 if the double value is 0(and positive zero is equal to negative zero according to the IEEE spec).To determine if you have negative zero, I think you'll have to write abit of code that looks at the sign bit directly: Alternatively, check for whether it's equal to positive 0 but has a different bit sequence to positive 0 (still using BitConverter.GetBytes, but regardless of endianness). Alright - in the interest of endian-safeness, here's an alternative: private readonly static long signBit = BitConverter.DoubleToInt64Bits( -0.0); public static bool IsSignBitSet( double x) { long bits = BitConverter.DoubleToInt64Bits( x); return( (bits & signBit) != 0); } -- mikeb Nov 16 '05 #6

 P: n/a Hi Mikeb, But compact C# doesn't have " BitConverter.DoubleToInt64Bits()" functionality? "mikeb" wrote: Jon Skeet [C# MVP] wrote: mikeb wrote:How to differentiate -0.0d & 0.0d in if statement?Yup - Math.Sign() is documented to return 0 if the double value is 0(and positive zero is equal to negative zero according to the IEEE spec).To determine if you have negative zero, I think you'll have to write abit of code that looks at the sign bit directly: Alternatively, check for whether it's equal to positive 0 but has a different bit sequence to positive 0 (still using BitConverter.GetBytes, but regardless of endianness). Alright - in the interest of endian-safeness, here's an alternative: private readonly static long signBit = BitConverter.DoubleToInt64Bits( -0.0); public static bool IsSignBitSet( double x) { long bits = BitConverter.DoubleToInt64Bits( x); return( (bits & signBit) != 0); } -- mikeb Nov 16 '05 #7

 P: n/a Avin Patel wrote: But compact C# doesn't have " BitConverter.DoubleToInt64Bits()" functionality? In that case, you can use BitConverter.GetBytes and just compare the contents of the returned array with BitConverter.GetBytes(0d). -- Jon Skeet - http://www.pobox.com/~skeet If replying to the group, please do not mail me too Nov 16 '05 #8

 P: n/a Hi, I guess this should be ok for compact case: static public bool IsMinusZero(double data) { byte[] minuszero = BitConverter.GetBytes(-0.0); byte[] bytes = BitConverter.GetBytes(data); for(int i=0; i wrote:>>How to differentiate -0.0d & 0.0d in if statement?>>Yup - Math.Sign() is documented to return 0 if the double value is 0>(and positive zero is equal to negative zero according to the IEEE spec).>>To determine if you have negative zero, I think you'll have to write a>bit of code that looks at the sign bit directly: Alternatively, check for whether it's equal to positive 0 but has a different bit sequence to positive 0 (still using BitConverter.GetBytes, but regardless of endianness). Alright - in the interest of endian-safeness, here's an alternative: private readonly static long signBit = BitConverter.DoubleToInt64Bits( -0.0); public static bool IsSignBitSet( double x) { long bits = BitConverter.DoubleToInt64Bits( x); return( (bits & signBit) != 0); } -- mikeb Nov 16 '05 #9

### This discussion thread is closed

Replies have been disabled for this discussion. 