Hi,
Does the Double has the facilty to define -0.
If it has, "==" or System.Math.Sig n() 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 8 4649
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.
Avin Patel wrote: Hi, Does the Double has the facilty to define -0.
If it has, "==" or System.Math.Sig n() 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.Ge tBytes( x);
// this assumes a little-endian machine
return ((bytes[bytes.Length-1] & 0x80) == 0x80);
}
// ...
Console.WriteLi ne( "IsSignBitS et( +0.0): {0}", IsSignBitSet( +0.0));
Console.WriteLi ne( "IsSignBitS et( -0.0): {0}", IsSignBitSet( -0.0));
The above code outputs:
IsSignBitSet( +0.0): False
IsSignBitSet( -0.0): True
--
mikeb
mikeb <ma************ @nospam.mailnul l.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.Ge tBytes, but regardless of endianness).
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
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.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] wrote: mikeb <ma************ @nospam.mailnul l.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.Ge tBytes, but regardless of endianness).
Alright - in the interest of endian-safeness, here's an alternative:
private readonly static long signBit =
BitConverter.Do ubleToInt64Bits ( -0.0);
public static bool IsSignBitSet( double x) {
long bits = BitConverter.Do ubleToInt64Bits ( x);
return( (bits & signBit) != 0);
}
--
mikeb
Hi Mikeb,
But compact C# doesn't have " BitConverter.Do ubleToInt64Bits ()" functionality?
"mikeb" wrote: Jon Skeet [C# MVP] wrote: mikeb <ma************ @nospam.mailnul l.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.Ge tBytes, but regardless of endianness).
Alright - in the interest of endian-safeness, here's an alternative:
private readonly static long signBit = BitConverter.Do ubleToInt64Bits ( -0.0);
public static bool IsSignBitSet( double x) { long bits = BitConverter.Do ubleToInt64Bits ( x);
return( (bits & signBit) != 0); }
-- mikeb
Avin Patel <Av*******@disc ussions.microso ft.com> wrote: But compact C# doesn't have " BitConverter.Do ubleToInt64Bits ()" functionality?
In that case, you can use BitConverter.Ge tBytes and just compare the
contents of the returned array with BitConverter.Ge tBytes(0d).
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Hi,
I guess this should be ok for compact case:
static public bool IsMinusZero(dou ble data)
{
byte[] minuszero =
BitConverter.Ge tBytes(-0.0);
byte[] bytes = BitConverter.Ge tBytes(data);
for(int i=0; i<minuszero.Len gth; 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.Do ubleToInt64Bits ()" functionality? "mikeb" wrote:
Jon Skeet [C# MVP] wrote: mikeb <ma************ @nospam.mailnul l.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.Ge tBytes, but regardless of endianness).
Alright - in the interest of endian-safeness, here's an alternative:
private readonly static long signBit = BitConverter.Do ubleToInt64Bits ( -0.0);
public static bool IsSignBitSet( double x) { long bits = BitConverter.Do ubleToInt64Bits ( x);
return( (bits & signBit) != 0); }
-- mikeb This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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.
To get around this limitation on double, I'd like to keep the bits of
the double the *same* but change its interpretation to long. I can use
"&" on longs. I tried to use reinterpret_cast for this purpose, but it
returned zero every time.
double...
|
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 Math.ceil/floor and parseInt +/- 1 to ensure the amount to
move was at least 1 and in the right direction. I made a small test to
see which one is faster and also included simply adding/subtracting 1.
parseInt generally took 50% to 100% longer than...
|
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 all-bits-zero, and does not therefore guarantee useful null
pointer values (see section 5 of this list) or floating-point zero values.
|
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 < N; i++)
|
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 touched the standards a
year ago, though I graduated in embedded SW development...
Anyway, to the problem at hand: I've stumbled upon the following
construct at work recently, and cannot really make up my mind about the
standard's take on the matter.
| |
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 serialisation framework
that cannot be done in binary format currently, so please no comments
about this ,-))
It took quite some time to shrink down the problem but it looks like
that C++ does not behave well in regards to very tiny numbers.
|
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
|
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 problems
elsewhere in another part of the system where that figure was used for
some calculation and some eventual truncation led to the system going
haywire. So my question is, given this code:
int main()
{
float f = 59.89F;
|
by: Steven Woody |
last post by:
long i = nnn;
long j;
double d;
d = i;
j = ( long )d;
in this case, i == j ?
thanks.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |