Board index » Help Help » Technical Support




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Stupid Java Programming Trick
PostPosted: Sat Dec 22, 2007 8:41 pm 
Offline
User avatar
Unthought Known
 Profile

Joined: Sun Oct 17, 2004 3:11 am
Posts: 6822
Location: College Station, TX, USA
Gender: Male
I have no idea why this amuses me so much, but anyway...

In Java there is a defined constant value known as NaN which when assigned to a variable signifies that it is "not a number." The use for this comes up when you're dealing with floating point, but this is all irrelevant and doesn't make much sense anyway.

What I'm getting at is that at some point it may be necessary to test for a value of NaN (error checking is a good example here). Now, you can be all boring and see if x == Math.NaN resolves to true. And sure, doing that will make your code clear and easy to understand.

However, any sadistic programmer like me loves to make things difficult for others because it validates our pathetic lives. Therefore, I prefer to take advantage of a little known property of our friend NaN. It is the only value for which an identifier becomes unequal to itself. Thus, in order to test for NaN, you can check if x != x resolves to true.

I consider this a much more interesting and confusing way to do things and thus highly recommend it. There's nothing I like more than spitting in the face of the rules of math.

_________________
.whoop


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Sat Dec 22, 2007 11:02 pm 
Offline
User avatar
Administrator
 Profile

Joined: Sat Oct 16, 2004 10:51 pm
Posts: 14534
Location: Mesa,AZ
:shock:

_________________
John Adams wrote:
In my many years I have come to a conclusion that one useless man is a shame, two is a law firm, and three or more is a congress.


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Sun Dec 23, 2007 3:18 am 
Offline
User avatar
Administrator
 Profile

Joined: Sat Oct 16, 2004 10:51 pm
Posts: 14534
Location: Mesa,AZ
I like Java's treatment of infinity even better.

Code:
public class Test
{
        public static void main(String[] args)
        {
                double x = Double.POSITIVE_INFINITY;
                double y = Double.NEGATIVE_INFINITY;

                System.out.println("" + (x + y));
                System.out.println("" + (x * y));
                System.out.println("" + (x / y));
                System.out.println("" + (x / x));

                System.out.println("" + (3 / 0.0));
                System.out.println("" + (3 / -0.0));
        }
}


Interestingly, Infinity - Infinity = NaN, Infinity / -Infinity = NaN, and Infinity / Infinity = NaN, but Infinity * -Infinity = -Infinity.

I also like how there's a -0. 3 / 0 = Infinity, and 3 / -0 = -Infinity.

Code:
NaN
-Infinity
NaN
NaN
Infinity
-Infinity

_________________
John Adams wrote:
In my many years I have come to a conclusion that one useless man is a shame, two is a law firm, and three or more is a congress.


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Sun Dec 23, 2007 8:13 am 
Offline
User avatar
Unthought Known
 Profile

Joined: Sun Oct 17, 2004 3:11 am
Posts: 6822
Location: College Station, TX, USA
Gender: Male
Yeah, floating point is pretty much hilarious in Java.

_________________
.whoop


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Sun Dec 23, 2007 4:07 pm 
Offline
User avatar
a saucerful of secrets
 Profile

Joined: Thu Feb 02, 2006 11:08 pm
Posts: 15892
Location: a wee green island
Gender: Male
I nearly wet meself.

_________________
cutuphalfdead wrote:
Man, we were all a bunch of faggots.



You talkin to me?


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Sun Dec 23, 2007 5:52 pm 
Offline
User avatar
Unthought Known
 Profile

Joined: Sun Oct 17, 2004 3:11 am
Posts: 6822
Location: College Station, TX, USA
Gender: Male
Speaking of funny things in Java, I love this quote from The Java Programming Language, Fourth Edition.

Quote:
Enum is actually a generic class defined as Enum<T extends Enum<T>>. This circular definition is probably the most confounding generic type definition you are likely to encounter. We're assured by the type theorists that this is quite valid and significant, and that we should simply not think about it too much, for which we are grateful.

_________________
.whoop


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Sun Dec 23, 2007 6:08 pm 
Offline
User avatar
Administrator
 Profile

Joined: Sat Oct 16, 2004 10:51 pm
Posts: 14534
Location: Mesa,AZ
I like C# better than Java.

_________________
John Adams wrote:
In my many years I have come to a conclusion that one useless man is a shame, two is a law firm, and three or more is a congress.


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Sun Dec 23, 2007 7:25 pm 
Offline
User avatar
Got Some
 Profile

Joined: Sun Oct 17, 2004 2:27 pm
Posts: 1965
Location: 55344
$úñ_DëV|L wrote:
I like C# better than Java.


i used java in school, but my work is a microsoft shop so i've become quite the c# proponent and it really is very pleasant to work with.


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Sun Dec 23, 2007 7:36 pm 
Offline
User avatar
Unthought Known
 Profile

Joined: Sun Oct 17, 2004 3:11 am
Posts: 6822
Location: College Station, TX, USA
Gender: Male
I need to post a correction to my initial post. Because NaN is the only value that doesn't equal itself, you can't simply do an equality check because...

x == Float.NaN and x == Double.NaN always equal false and
x != Float.NaN and x != Double.NaN always equal true.

Therefore you use Float.isNaN(float) or Double.isNaN(double) which return a boolean value.


Or, of course, you can see if x != x equals true. :P




And speaking of funny quotes from that book, this one is unintentionally hilarious when taken out of context:
Quote:
Yes the 1.4 specification version is a bug.



I lol'd hard.

_________________
.whoop


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Sat Feb 02, 2008 4:17 pm 
Offline
User avatar
Unthought Known
 WWW  Profile

Joined: Sat Aug 13, 2005 11:31 pm
Posts: 7162
Location: The Only "Non-NESN" County CT
Gender: Male
:|

_________________
dirtyfrank0705 wrote:
At the age of 40, PunkDavid will check his own prostate and then bill his doctor.


Frank RIP


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Fri Feb 08, 2008 4:14 am 
Offline
User avatar
Administrator
 Profile

Joined: Sat Oct 16, 2004 10:51 pm
Posts: 14534
Location: Mesa,AZ
I've been playing around with the more enterprisey Java stuff the last few days, like JSP and Web Services. There's some pretty neat stuff there.

_________________
John Adams wrote:
In my many years I have come to a conclusion that one useless man is a shame, two is a law firm, and three or more is a congress.


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Fri Feb 08, 2008 4:36 am 
Offline
User avatar
Temporary Secretary
 WWW  Profile

Joined: Thu Apr 13, 2006 3:51 am
Posts: 43609
Location: My city smells like Cheerios
Gender: Male
does this mean that we are getting a chatroom?

_________________
"No matter how hard you kill Jesus, he would always just come back and hit you twice as hard."


Top
 
 Post subject: Re: Stupid Java Programming Trick
PostPosted: Sun Feb 10, 2008 9:31 am 
Offline
User avatar
Unthought Known
 Profile

Joined: Wed Oct 20, 2004 8:35 pm
Posts: 8770
Location: flap flap flap hey no fair i made my saving throw
fuck I haven't used Java in so long. I almost miss it

_________________
New Age bullshit is just a bunch of homo shit that some rich fuck came up with to scam people. It's exactly the same as scientology and every other religion: fake.


Top
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

Board index » Help Help » Technical Support


Who is online

Users browsing this forum: No registered users and 12 guests


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 post attachments in this forum

Search for:
Jump to:  
It is currently Sun Jan 25, 2026 10:07 am