Explicitly and without apology a marketing vehicle MaryMaryQuiteContrary

Friday Aug 13, 2004



we've got oh-so-much to be excited about today, people!

Friday Free Stuff* has a brand-spanking-new puzzler straight from the personal email account of Dr. Josh. (i just love it when he sends me personal email. have you bought his book yet?)

We've got a spectacular prize* this week...
picture of books

...my entire library of books about the Java platform. (all pristine condition, never even opened them.)

And most importantly, the XXVIII Olympiad officially begins tonight, bringing the games gloriously back to the country where they were born. ZHTO HELLAS!

picture of greek flag
(we've got a greek flag outside my house in celebration.)

So let's get straight to it. Because I've got to figure out where we're going to watch the opening ceremony as we have no TV. And my dad and mom (who live across the street) disconnected their cable when they took off for the summer. So I gotta work some angles....

Here's the puzzler:

The following loop looks like it should terminate immediately (after zero iterations):
   while (i != i)
;
But suppose you declare i like this:
   float i = 0.0/0.0;
If you try this and see what happens, you'll find out that it's an infinite loop! According to the spec 0.0/0.0 is evaluates to Float.NaN. NaN is an abbreviation for "not a number." The IEEE 754 specification says that NaN is not equal to itself (!), which explains the observed behavior. In similar fashion (but without resorting to floating point), provide declarations for i and j that turn each of the loops below into infinite loops:
  1.    while (j != j + 0)
    ;
  2.    while (k != 0)
    k >>>= k;
Please post your answers in the comments section of this blog entry. The person who most correctly, or in some other arbitrary way, answers the puzzler, wins* the prize*.

i love you too!

mary

* Friday Free Stuff is not a contest. It is me giving away something that I personally own to somebody that I choose. I pay for postage with stamps that I buy at the post office. But please, for my sake, if you live outside the US or Canada, please don't play this week. It would cost me a fortune to ship these books half-way round the world.

p.s. sorry about the quality of the pictures today. the digital camera eats up batteries like M&Ms. and i had no fresh ones to put in there. so i had to use the camera in my Java Powered mobile phone.
Comments:

Oh, I mailed you but I've just seen you'd like the solution here. Mine is: 1.) String j = ""; 2.) int k = Integer.MIN_VALUE;

Posted by Christoph on August 13, 2004 at 07:22 AM PDT #

Here is mine: #1 String j = "0"; while (j != j + 0) ; #2: int k = 32; while (k != 0) k >>>= k;

Posted by Sue Henshaw on August 13, 2004 at 08:01 AM PDT #

Mary

You and your family and head on over to our house to watch the opening ceremonies. Sounds like we will be grilling tonight. Just let us know what time you will be getting here.

dl

Posted by DL on August 13, 2004 at 09:29 AM PDT #

you make me smile, Dan. mary

Posted by mary on August 13, 2004 at 09:38 AM PDT #

Well. I couldn't really find an answer to the first puzzle. Let me remind you - the puzzle was to find a solution which would loop "infinate". To me this means until I stop the VM (or it craches) but if you use a String in the first puzzle as many said (which COULD be ok) it will only loop until my VM runs out of heap (hits the -Xmx loft) whereas the second is just using the same int and not building a loooong string (and actually many intermediate but they should B GC'ed). Well mayB its definition of "infinite" loops :-)

Posted by Lars Borup on August 14, 2004 at 11:48 AM PDT #

I was thinking about an answer to the first puzzle without resorting to Strings and the only solution Ive come across which might work would be to use j2se 5 and Integer (the object). Now I don't have a j2se 5 compiler with me so I've been unable to test my theory but here it goes: Using j2se 5 an Integer and the boxing/unboxing feature I might be able to set Integer j = new Integer(0); So while (j != j + 0); will perhaps run infinit if the VM unboxes j, adds 0 and boxes it to a new Integer object which will let j ref another Integer and == does not compare... Dang - wheres a j2se 5 compiler when you need one :-) Well - I give up! :(

Posted by Lars Borup on August 16, 2004 at 01:55 AM PDT #

this puzzler is officially closed. stay tuned to the MaryMaryQuiteContary blog for breaking developments. shift. reload.

Posted by mary on August 16, 2004 at 11:55 AM PDT #

double j = 0.0 / 0.0; int k = 256; For your example, float i = 0.0 / 0.0 doesn't compile, because the division returns a double instead of a float.

Posted by Ka_Hing Cheung on September 20, 2004 at 11:48 AM PDT #

Oh oops, didn't see the "without resorting to floating point part". In this case other posters seem to be more correct.

Posted by Ka_Hing Cheung on September 20, 2004 at 11:48 AM PDT #

Forgot to mention, the reason why 0.0 / 0.0 returns a double is because 0.0 is by default a double. To force it to be a float you need to say 0.0f.

Posted by Ka_Hing Cheung on September 20, 2004 at 11:48 AM PDT #

Post a Comment:
Comments are closed for this entry.