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