AI-generated (stable diffusion) ge of "cyclon writing with a pen".

The sporadic blog of David J A Cooper. I write sci-fi, teach software engineering, and occasionally say related (or not related) things.

Check out Nova Sapiens: The Believers.

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
    System.nanoTime(\u002f\u002a);
    boolean even = true;
    double i = 0.0;
    while(i <= infinity)
    {
        even = !even;
        i += 1.0;
    }
    System.normalTime(\u002a\u002f);
    System.out.println("Infinity is even: " + even);
    System.exit(0);\u007d
}

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


Posted

in

by

Comments

2 responses to “Horrible Java”

  1. Neil Avatar
    Neil

    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 :-).

    1.  Avatar
      Dave C

      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. 😛