Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (511)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Funny code in src.zip  (Read 3257 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2008-10-08 23:06:45 »

ByteBuffer.equals(Object obj)
1  
2  
3  
4  
5  
6  
       byte v1 = this.get(i);
       byte v2 = that.get(j);
       if (v1 != v2) {
      if ((v1 != v1) && (v2 != v2))   // For float and double
          continue;
      return false;


FloatBuffer.equals(Object obj)
1  
2  
3  
4  
5  
6  
       float v1 = this.get(i);
       float v2 = that.get(j);
       if (v1 != v2) {
      if ((v1 != v1) && (v2 != v2))   // For float and double
          continue;
      return false;



Can anybody explain (v1 != v1) && (v2 != v2) to me? Smiley

Seems like somebody messed up with the code-generation tool.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Abuse

JGO Knight


Medals: 13


falling into the abyss of reality


« Reply #1 - Posted 2008-10-08 23:20:01 »

Presumably the question you are asking is why the above code is present in ByteBuffer?
as i'm sure you're aware for floats it's testing for NaN values.

Looks to me like whoever wrote ByteBuffer based it upon a copy of FloatBuffer & relied a little too much on their ide's refactoring  persecutioncomplex

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2008-10-09 05:44:03 »

Yeah, I phrased it a bit poorly.

One would expect that this code would have been optimized to the bone (!) for all integer primitives.

I might run a benchmark to see whether it impacts performance, or that JIT is smart enough..

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline irrisor

Junior Duke





« Reply #3 - Posted 2008-10-09 07:22:43 »

as i'm sure you're aware for floats it's testing for NaN values.
How's that? NaN != NaN returns false, but any other float value returns false as well, no? It would make sense with a ==, of course.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #4 - Posted 2008-10-09 07:34:26 »

Haha, that's pretty good. If it was because of NaN, then of course it would make more sense to say if (v1 != NaN).

See my work:
OTC Software
Offline irrisor

Junior Duke





« Reply #5 - Posted 2008-10-09 07:36:46 »

it would make more sense to say if (v1 != NaN).
No, that check would always return false, as well.
Offline Abuse

JGO Knight


Medals: 13


falling into the abyss of reality


« Reply #6 - Posted 2008-10-09 09:49:53 »

How's that? NaN != NaN returns false, but any other float value returns false as well, no? It would make sense with a ==, of course.

NaN == NaN -> false

NaN != NaN -> !(NaN == NaN) -> true

Make Elite IV:Dangerous happen! Pledge your backing at KICKSTARTER here! https://dl.dropbox.com/u/54785909/EliteIVsmaller.png
Offline irrisor

Junior Duke





« Reply #7 - Posted 2008-10-09 10:01:11 »

oh, interesting - did not know that != is equivalent to ! ==. Thanks.
Offline jezek2
« Reply #8 - Posted 2008-10-09 11:05:59 »

This has nothing with != vs ! == (which is logically equivalent). It's simple as this:

NaN == NaN -> false
NaN != NaN -> true

It's perfectly logical for NaNs, just think about it Smiley

For best clarity, it's better to use Float.isNaN method instead.

Sun's programmers used this probably for not complicating their generator for Buffer classes. For floats/doubles it works and for integers it's harmless.

Also I wouldn't be worried with concrete code used in these classes, as it's most likely replaced with optimized code by HotSpot in many places.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #9 - Posted 2008-10-09 19:02:51 »

NaN != NaN returns false, but any other float value returns false as well, no?

Noo..! You can perfectly compare floats with ==

It's just that values that seem to be the same (by logic or by textual representation), might not be the same.

For example:
I wouldn't be sure whether 0.5*0.5 == 2.0*0.25
yet I'm sure that 0.5*0.5 == 0.5*0.5 is true

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Addictman

Senior Duke


Medals: 3
Projects: 1


Java games rock!


« Reply #10 - Posted 2008-10-09 20:01:11 »

For example:
I wouldn't be sure whether 0.5*0.5 == 2.0*0.25
yet I'm sure that 0.5*0.5 == 0.5*0.5 is true

Hehe, I can be pretty sure that 0.5*0.5 != 2.0*0.25 ;-)
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (50 views)
2014-10-17 03:59:02

Norakomi (40 views)
2014-10-16 15:22:06

Norakomi (31 views)
2014-10-16 15:20:20

lcass (36 views)
2014-10-15 16:18:58

TehJavaDev (67 views)
2014-10-14 00:39:48

TehJavaDev (65 views)
2014-10-14 00:35:47

TehJavaDev (58 views)
2014-10-14 00:32:37

BurntPizza (72 views)
2014-10-11 23:24:42

BurntPizza (44 views)
2014-10-11 23:10:45

BurntPizza (84 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!