Velocity Reviews > C++ > Re: Different results if compared as double or __int64?

# Re: Different results if compared as double or __int64?

John Tsiombikas (Nuclear / the Lab)
Guest
Posts: n/a

 06-30-2003
Josef Mehrpur wrote:
> __int64 x = ...;
> __int64 y = ...;

I assume you understand that this is not standard.

>
> bool result = ((x > y) == (*((double *) &x) > *((double *) &y));

What is this? you cast the address of the integer x to a double pointer,
then dereference that (as if it was double) I don;t think the results of
such an operation are defined... the result could be anything afaik.

> Question:
> Is result always true, no matter what values are in x and y?
>
> (Please note, I don't mean:
> bool result = ((x > y) == ((double) x > (double) y)

That would be perfectly defined and would be always true
>
> Josef Mehrpur

Victor Bazarov
Guest
Posts: n/a

 06-30-2003
"John Tsiombikas (Nuclear / the Lab)" <(E-Mail Removed)> wrote...
> Josef Mehrpur wrote:
> > __int64 x = ...;
> > __int64 y = ...;

>
> I assume you understand that this is not standard.
>
> >
> > bool result = ((x > y) == (*((double *) &x) > *((double *) &y));

>
> What is this? you cast the address of the integer x to a double pointer,
> then dereference that (as if it was double) I don;t think the results of
> such an operation are defined... the result could be anything afaik.
>
> > Question:
> > Is result always true, no matter what values are in x and y?
> >
> > (Please note, I don't mean:
> > bool result = ((x > y) == ((double) x > (double) y)

>
> That would be perfectly defined and would be always true

Nope. Precision of a double is usually smaller than 63 bits.
So, if x and y are both greater than 53 bits and differ in very
few bits at the end, casting them to double can result in two
precisely same values. So, 'result' might not get the 'true'
just because while x > y, (double)x may be == (double)y (not to
mention that the statement that declares 'result' has a syntax
error in it).

Victor