473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Double minus zero

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 4607
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
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
mikeb <ma************@nospam.mailnull.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Frank Oquendo <no******@here.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Jon Skeet [C# MVP] wrote:
mikeb <ma************@nospam.mailnull.com> 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 #6
Hi Mikeb,
But compact C# doesn't have " BitConverter.DoubleToInt64Bits()" functionality?

"mikeb" wrote:
Jon Skeet [C# MVP] wrote:
mikeb <ma************@nospam.mailnull.com> 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 #7
Avin Patel <Av*******@discussions.microsoft.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
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<minuszero.Length; i++) {
if(bytes[i] != minuszero[i]) { return false; }
}
return true;
}

Thanks for replies,
Avin Patel

"Avin Patel" wrote:
Hi Mikeb,
But compact C# doesn't have " BitConverter.DoubleToInt64Bits()" functionality?

"mikeb" wrote:
Jon Skeet [C# MVP] wrote:
mikeb <ma************@nospam.mailnull.com> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Suzanne Vogel | last post by:
I'd like to convert a double to a binary representation. I can use the "&" bit operation with a bit mask to convert *non* float types to binary representations, but I can't use "&" on doubles. ...
6
by: RobG | last post by:
I am writing a script to move an absolutely positioned element on a page by a factor using style.top & style.left. The amount to move by is always some fraction, so I was tossing up between...
53
by: Zhiqiang Ye | last post by:
Hi, All I am reading FAQ of this group. I have a question about this: http://www.eskimo.com/~scs/C-faq/q7.31.html It says: " p = malloc(m * n); memset(p, 0, m * n); The zero fill is...
3
by: Joakim Hove | last post by:
Hello, I have an array of doubles, allocated with dbarr = malloc(N * sizeof(double)); I then want to set all the elements of the array to zero, this is currently done with for (i=0; i <...
13
by: Marc | last post by:
Hi, I've been lurking on clc for a few months now, and want to start by thanking the regulars here for opening my eyes to a whole new dimension of "knowing c". Considering I had never even...
5
by: soeren | last post by:
Hello, two days ago I stumbled across a very strange problem that came up when we were printing tiny double numbers as strings and trying to read them on another place. This is part of an object...
60
by: Erick-> | last post by:
hi all... I've readed some lines about the difference between float and double data types... but, in the real world, which is the best? when should we use float or double?? thanks Erick
116
by: Dilip | last post by:
Recently in our code, I ran into a situation where were stuffing a float inside a double. The precision was extended automatically because of that. To make a long story short, this caused...
11
by: Steven Woody | last post by:
long i = nnn; long j; double d; d = i; j = ( long )d; in this case, i == j ? thanks.
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.