 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Remon van Vliet Guest
|
Posted: Thu May 19, 2005 3:29 pm Post subject: Oddity in ByteBuffer code? |
|
|
Hello,
A snippet from ByteBuffer.compareTo(), can anyone figure out what this code
does? :
if ((v1 != v1) && (v2 != v2)) // For float and double
Thanks, just curious,
Remon
|
|
| Back to top |
|
 |
Eric Sosman Guest
|
Posted: Thu May 19, 2005 3:46 pm Post subject: Re: Oddity in ByteBuffer code? |
|
|
Remon van Vliet wrote:
| Quote: | Hello,
A snippet from ByteBuffer.compareTo(), can anyone figure out what this code
does? :
if ((v1 != v1) && (v2 != v2)) // For float and double
|
The test produces `true' if both v1 and v2 are NaN.
--
[email]Eric.Sosman (AT) sun (DOT) com[/email]
|
|
| Back to top |
|
 |
Patricia Shanahan Guest
|
Posted: Thu May 19, 2005 3:52 pm Post subject: Re: Oddity in ByteBuffer code? |
|
|
Remon van Vliet wrote:
| Quote: | Hello,
A snippet from ByteBuffer.compareTo(), can anyone figure
out what this code does? :
if ((v1 != v1) && (v2 != v2)) // For float and double
Thanks, just curious,
Remon
|
It looks like a Not-a-Number test. NaNs do not compare equal
to anything, themselves included. However, sometimes it is
desirable to treat all NaNs as being equal to each other,
leading to tests for NaNs on both sides of a comparison.
If that is what is meant then
if (Double.isNaN(v1) && Double.isNaN(v2))
would be better style. It does the same test, but is
self-documenting.
The test does not seem to make sense in this context. I
don't see any way v1 or v2 could be double or float, and all
byte values compare equal to themselves. I think it may be a
case of over-enthusiastic copy-paste from DoubleBuffer,
which does need it.
Patricia
|
|
| Back to top |
|
 |
Chris Uppal Guest
|
Posted: Thu May 19, 2005 4:59 pm Post subject: Re: Oddity in ByteBuffer code? |
|
|
Patricia Shanahan wrote:
| Quote: | The test does not seem to make sense in this context. I
don't see any way v1 or v2 could be double or float, and all
byte values compare equal to themselves. I think it may be a
case of over-enthusiastic copy-paste from DoubleBuffer,
which does need it.
|
Or, from the comment at the head of the 1.5 version of the source:
// -- This file was mechanically generated: Do not edit! -- //
a case of a tool being misapplied.
-- chris
|
|
| Back to top |
|
 |
Patricia Shanahan Guest
|
Posted: Thu May 19, 2005 7:09 pm Post subject: Re: Oddity in ByteBuffer code? |
|
|
Chris Uppal wrote:
| Quote: | Patricia Shanahan wrote:
The test does not seem to make sense in this context. I
don't see any way v1 or v2 could be double or float, and all
byte values compare equal to themselves. I think it may be a
case of over-enthusiastic copy-paste from DoubleBuffer,
which does need it.
Or, from the comment at the head of the 1.5 version of the source:
// -- This file was mechanically generated: Do not edit! -- //
a case of a tool being misapplied.
|
That makes the non-use of Double.isNaN even worse, because
there was premeditated intent to use the code in pure
integer contexts, where it is likely to be read by people
who might not instantly recognize the (v1 != v1) idiom.
Patricia
|
|
| Back to top |
|
 |
Chris Uppal Guest
|
Posted: Fri May 20, 2005 2:18 pm Post subject: Re: Oddity in ByteBuffer code? |
|
|
Patricia Shanahan wrote:
| Quote: | That makes the non-use of Double.isNaN even worse, because
there was premeditated intent to use the code in pure
integer contexts, where it is likely to be read by people
who might not instantly recognize the (v1 != v1) idiom.
|
And not even an explanatory comment ("For float and double" doesn't count as
explanatory in my book. It scarcely even counts as a comment -- even the
famous "you are not expected to understand this" would have been better ;-)
<sigh/> What is the world coming to...?
-- chris
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|