Horrible Java

Apologies to non-geeks. The following Java code determines whether infinity is even or odd. It compiles, runs, finishes immediately, and outputs “false” (meaning that infinity is odd).

class Infinity \u007b static \u0062\u006f\u006f\u006c\u0065\u0061\u006e\u0020\u0065\u0076\u0065\u006e\u003b static
    // Configure infinite speed
    boolean even = true;
    double i = 0.0;
    while(i <= infinity)
        even = !even;
        i += 1.0;
    System.out.println("Infinity is even: " + even);

Yes, it's all smoke and mirrors, but I've been having fun with it.

2 thoughts on “Horrible Java

  1. While I have no idea what Java is doing to deal with a concept of infinity, I have a problem with the code before it gets as far as using whatever infinity it has… The parity of a number is only defined for integers, so declaring i to be a double is not technically correct, and we all know technically correct is the best type of correct :-).

    • Doubles can represent integers. In fact, half of all the possible values of a double are integer values. The problem is that, beyond a certain magnitude, a double will not have enough precision to distinguish even from odd.

      The real problem is that no data type, including the “arbitrary precision” ones, can possibly represent all integers up to infinity. Consider Graham’s Number for a start.

      The starting point for understanding how this code works is in recognising that it is patently impossible. 😛

Comments are closed.