comparing +zero and -zero
Author Message
comparing +zero and -zero

Hi,
Can anyone suggest me how I could compare a positive zero
and a negative zero without writing my own code to look
at the bit values. Is there anyway I can define(like a
macro) to make my own "signed zeros"?

TIA
Seshu

Thu, 27 Jun 2002 03:00:00 GMT
comparing +zero and -zero
If you are talking about built-in numeric classes then:

int x = +0;
int y = -0;

then the internal representation of x and y will be exactly the same (all
bits zero) and thus cannot be differentiated between. Do you mind telling us
what you are really trying to do here?

NeilB

Quote:
> Hi,
>    Can anyone suggest me how I could compare a positive zero
>    and a negative zero without writing my own code to look
>    at the bit values. Is there anyway I can define(like a
>    macro) to make my own "signed zeros"?

> TIA
> Seshu

Fri, 28 Jun 2002 03:00:00 GMT
comparing +zero and -zero

Quote:

> If you are talking about built-in numeric classes then:
> int x = +0;
> int y = -0;
> then the internal representation of x and y will be exactly the same (all
> bits zero) and thus cannot be differentiated between. Do you mind telling us
> what you are really trying to do here?

This valid for int.
But for float types binary representations of +0 and -0
are different.

--
Anton N. Breusov 'Antonz'.

Fri, 28 Jun 2002 03:00:00 GMT
comparing +zero and -zero

Quote:
> But for float types binary representations of +0 and -0
> are different.

This code:

#include <iostream>
using namespace std;

union silly {
void * p;
float f;

Quote:
};

int main() {

float x = +0.0f;
float y = -0.0f;

if ( x == y ) {
cout << "same" << endl;
}
else {
cout << "different" << endl;
}

silly s1, s2;
s1.f = x;
s2.f = y;

if ( s1.p == s2.p ) {   // compare pointers i.e. bit patterns
cout << "same" << endl;
}
else {
cout << "different" << endl;
}

Quote:
}

prints out "same" in both cases.

NeilB

Quote:

> > If you are talking about built-in numeric classes then:

> > int x = +0;
> > int y = -0;

> > then the internal representation of x and y will be exactly the same
(all
> > bits zero) and thus cannot be differentiated between. Do you mind
telling us
> > what you are really trying to do here?
> This valid for int.
> But for float types binary representations of +0 and -0
> are different.

> --
> Anton N. Breusov 'Antonz'.

Fri, 28 Jun 2002 03:00:00 GMT
comparing +zero and -zero
: Hi,
:    Can anyone suggest me how I could compare a positive zero
:    and a negative zero without writing my own code to look
:    at the bit values. Is there anyway I can define(like a
:    macro) to make my own "signed zeros"?

What type are you talking about?  Integer types have only a single
zero value on the platforms supported by VC.

--
Any opinions expressed are my own and not necessarily those of Laser-Scan.

Fri, 28 Jun 2002 03:00:00 GMT
comparing +zero and -zero
Replace your lines

Quote:
>  float x = +0.0f;
>  float y = -0.0f;

with

float x = 0.0f;
float y = -x;

And you will get "same", "different"

--
Truth,
James Curran
http://www.NJTheater.com
http://www.NJTheater.com/JamesCurran

Quote:
> > But for float types binary representations of +0 and -0
> > are different.

> This code:

> #include <iostream>
> using namespace std;

> union silly {
>  void * p;
>  float f;
> };

> int main() {

>  float x = +0.0f;
>  float y = -0.0f;

>  if ( x == y ) {
>   cout << "same" << endl;
>  }
>  else {
>   cout << "different" << endl;
>  }

>  silly s1, s2;
>  s1.f = x;
>  s2.f = y;

>  if ( s1.p == s2.p ) {   // compare pointers i.e. bit patterns
>   cout << "same" << endl;
>  }
>  else {
>   cout << "different" << endl;
>  }

> }

> prints out "same" in both cases.

> NeilB

> > > If you are talking about built-in numeric classes then:

> > > int x = +0;
> > > int y = -0;

> > > then the internal representation of x and y will be exactly the same
> (all
> > > bits zero) and thus cannot be differentiated between. Do you mind
> telling us
> > > what you are really trying to do here?
> > This valid for int.
> > But for float types binary representations of +0 and -0
> > are different.

> > --
> > Anton N. Breusov 'Antonz'.

Fri, 28 Jun 2002 03:00:00 GMT
comparing +zero and -zero
Which means that the internal representation of  a float constant is
different from a float variable?

NeilB

Quote:
> Replace your lines
> >  float x = +0.0f;
> >  float y = -0.0f;

> with

>  float x = 0.0f;
>  float y = -x;

>     And you will get "same", "different"

> --
> Truth,
> James Curran
> http://www.NJTheater.com
> http://www.NJTheater.com/JamesCurran

> > > But for float types binary representations of +0 and -0
> > > are different.

> > This code:

> > #include <iostream>
> > using namespace std;

> > union silly {
> >  void * p;
> >  float f;
> > };

> > int main() {

> >  float x = +0.0f;
> >  float y = -0.0f;

> >  if ( x == y ) {
> >   cout << "same" << endl;
> >  }
> >  else {
> >   cout << "different" << endl;
> >  }

> >  silly s1, s2;
> >  s1.f = x;
> >  s2.f = y;

> >  if ( s1.p == s2.p ) {   // compare pointers i.e. bit patterns
> >   cout << "same" << endl;
> >  }
> >  else {
> >   cout << "different" << endl;
> >  }

> > }

> > prints out "same" in both cases.

> > NeilB

> > > > If you are talking about built-in numeric classes then:

> > > > int x = +0;
> > > > int y = -0;

> > > > then the internal representation of x and y will be exactly the same
> > (all
> > > > bits zero) and thus cannot be differentiated between. Do you mind
> > telling us
> > > > what you are really trying to do here?
> > > This valid for int.
> > > But for float types binary representations of +0 and -0
> > > are different.

> > > --
> > > Anton N. Breusov 'Antonz'.

Fri, 28 Jun 2002 03:00:00 GMT
comparing +zero and -zero

Quote:

>> But for float types binary representations of +0 and -0
>> are different.
> This code:

[skip]

I wrote more complex program:
===================float.cpp==============
#pragma optimize("",off)
#include <iostream.h>

union silly
{
void* p;
float f;

Quote:
};

void main()
{

volatile float z1 = +0.0f;
volatile float z2 = -z1;
volatile float z3 = -0.0f;
volatile float z4 = -z3;

if ( z1 == z2 )
{
cout << "same" << endl;
}
else
{
cout << "different" << endl;
}

if ( z3 == z4 )
{
cout << "same" << endl;
}
else
{
cout << "different" << endl;
}

volatile silly s1, s2;
s1.f = z1;
s2.f = z2;

if ( s1.p == s2.p )
{   // compare pointers i.e. bit patterns
cout << "same" << endl;
}
else
{
cout << "different" << endl;
}

volatile silly ss1, ss2;
ss1.f = z3;
ss2.f = z4;

if (ss1.p == ss2.p)
{
cout << "same" << endl;
}
else
{
cout << "different" << endl;
}

Quote:
}

===============end===============
Output was:
same
same
different
different

Now disasembly:
===================================
13:       volatile float z1 = +0.0f;
00401058   mov         dword ptr [ebp-4],0
14:       volatile float z2 = -z1;
0040105F   fld         dword ptr [ebp-4]
00401062   fchs        <-----------------This floating point operation changes
representation from +0 to -0
00401064   fstp        dword ptr [ebp-8]
15:       volatile float z3 = -0.0f;
00401067   mov         dword ptr [ebp-0Ch],0
16:       volatile float z4 = -z3;
0040106E   fld         dword ptr [ebp-0Ch]
00401071   fchs
00401073   fstp        dword ptr [ebp-10h]
18:       if ( z1 == z2 )
00401076   fld         dword ptr [ebp-4]
00401079   fcomp       dword ptr [ebp-8] <----And this is floating point
compare function, that knows how to compare
positive and negative floats.
0040107C   fnstsw      ax
0040107E   test        ah,40h
00401081   je          main+60h (004010a0)
===================================

P.S. Sorry for my bad English, I'm Ukrainian. :-)

--
Anton N. Breusov 'Antonz'.

Fri, 28 Jun 2002 03:00:00 GMT
comparing +zero and -zero
Hi,
Thanks for the response. But when I do the same
comparison using a double I don't get "different".
I mean using the union defined by

union silly {
void *p;
double d;

Quote:
}

---------
reason why I need it?

Since the compiler does not differentiate between +0.0 and
-0.0(I'm talking in double format) I want to define my own
+zero and -zero in a macro so that I can check for the
signed zeros later on in my program which is realted to
interval arithmetic. What do you think?
Is this a good idea?

:  Replace your lines
:  >  float x = +0.0f;
:  >  float y = -0.0f;
:
:  with
:
:   float x = 0.0f;
:   float y = -x;
:
:      And you will get "same", "different"
:
:
:  --
:  Truth,
:  James Curran
:  http://www.NJTheater.com
:  http://www.NJTheater.com/JamesCurran
:

:  > > But for float types binary representations of +0 and -0
:  > > are different.
:  >
:  > This code:
:  >
:  > #include <iostream>
:  > using namespace std;
:  >
:  > union silly {
:  >  void * p;
:  >  float f;
:  > };
:  >
:  > int main() {
:  >
:  >  float x = +0.0f;
:  >  float y = -0.0f;
:  >
:  >  if ( x == y ) {
:  >   cout << "same" << endl;
:  >  }
:  >  else {
:  >   cout << "different" << endl;
:  >  }
:  >
:  >  silly s1, s2;
:  >  s1.f = x;
:  >  s2.f = y;
:  >
:  >  if ( s1.p == s2.p ) {   // compare pointers i.e. bit patterns
:  >   cout << "same" << endl;
:  >  }
:  >  else {
:  >   cout << "different" << endl;
:  >  }
:  >
:  > }
:  >
:  > prints out "same" in both cases.
:  >
:  > NeilB

Fri, 28 Jun 2002 03:00:00 GMT
comparing +zero and -zero
The reason I used float in the original example was that sizeof( float) ==
sizeof( void *) for VC++, whereas sizeof( double) > sizeof( void *). That
means that in the latter case there is not a one-to-one bit mapping between
the two types in the union.

I still don't really understand what's going on here though :-)

NeilB

Quote:
> Hi,
>    Thanks for the response. But when I do the same
>    comparison using a double I don't get "different".
> I mean using the union defined by

> union silly {
> void *p;
> double d;
> }

> ---------
> reason why I need it?

> Since the compiler does not differentiate between +0.0 and
> -0.0(I'm talking in double format) I want to define my own
> +zero and -zero in a macro so that I can check for the
> signed zeros later on in my program which is realted to
> interval arithmetic. What do you think?
> Is this a good idea?

> :  Replace your lines
> :  >  float x = +0.0f;
> :  >  float y = -0.0f;
> :
> :  with
> :
> :   float x = 0.0f;
> :   float y = -x;
> :
> :      And you will get "same", "different"
> :
> :
> :  --
> :  Truth,
> :  James Curran
> :  http://www.NJTheater.com
> :  http://www.NJTheater.com/JamesCurran
> :

> :  > > But for float types binary representations of +0 and -0
> :  > > are different.
> :  >
> :  > This code:
> :  >
> :  > #include <iostream>
> :  > using namespace std;
> :  >
> :  > union silly {
> :  >  void * p;
> :  >  float f;
> :  > };
> :  >
> :  > int main() {
> :  >
> :  >  float x = +0.0f;
> :  >  float y = -0.0f;
> :  >
> :  >  if ( x == y ) {
> :  >   cout << "same" << endl;
> :  >  }
> :  >  else {
> :  >   cout << "different" << endl;
> :  >  }
> :  >
> :  >  silly s1, s2;
> :  >  s1.f = x;
> :  >  s2.f = y;
> :  >
> :  >  if ( s1.p == s2.p ) {   // compare pointers i.e. bit patterns
> :  >   cout << "same" << endl;
> :  >  }
> :  >  else {
> :  >   cout << "different" << endl;
> :  >  }
> :  >
> :  > }
> :  >
> :  > prints out "same" in both cases.
> :  >
> :  > NeilB

Fri, 28 Jun 2002 03:00:00 GMT
comparing +zero and -zero

Quote:

> Hi,
>    Thanks for the response. But when I do the same
>    comparison using a double I don't get "different".
> I mean using the union defined by
> union silly {
> void *p;
> double d;
> }

Try to use this:
===================================
union silly2
{
struct
{
void* p1;
void* p2;
};
double d;

Quote:
};

silly2 a1, a2;
a1.d =  0.0f;
a2.d = -a1.d;

// and compare operator:

if ((a1.p1 == a2.p1) && (a1.p2 == a2.p2))
{
.....

Quote:
}

==================================
or __int64 format.

--
Anton N. Breusov 'Antonz'.

Sat, 29 Jun 2002 03:00:00 GMT
comparing +zero and -zero
Floats cannot be compared using the equality operator as they have no exact
representation.  They must be formatted to a common precision using the same
rounding method prior to the comparison.

Quote:
> Hi,
>    Can anyone suggest me how I could compare a positive zero
>    and a negative zero without writing my own code to look
>    at the bit values. Is there anyway I can define(like a
>    macro) to make my own "signed zeros"?

> TIA
> Seshu

Sat, 29 Jun 2002 03:00:00 GMT
comparing +zero and -zero
Surely zero _does_ have an exact representation?

NeilB

Quote:
> Floats cannot be compared using the equality operator as they have no
exact
> representation.  They must be formatted to a common precision using the
same
> rounding method prior to the comparison.

> > Hi,
> >    Can anyone suggest me how I could compare a positive zero
> >    and a negative zero without writing my own code to look
> >    at the bit values. Is there anyway I can define(like a
> >    macro) to make my own "signed zeros"?

> > TIA
> > Seshu

Sat, 29 Jun 2002 03:00:00 GMT

 Page 1 of 1 [ 13 post ]

Relevant Pages

Powered by phpBB® Forum Software