Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Article says 1,056,964,609 / 1,056,964,610

Quick Java program says 1,065,353,216 / 1,065,353,217

Which one is right? Or are Java floats just "different"?

  static {
    float a = 0;
    long count = 0;
    while (a < 1) {
      count++;
      a = Math.nextAfter(a, 2);
    }
    System.out.println("count = " + count);
  }


nextAfter is probably also including the denormals (an additional 2^23 values near 0).


Yep:

  1,056,964,609 - 1,065,353,216 = 8388607 = 2^23 - 1
because the denormal that is all-zero is already present.


And the author explicitly stated he was excluding the denormals.


Offtopic, but:

Is this not just shorthand, but an actual legitimate Java program these days?


I was curious as well, so I took it for a spin[0]. In short, no. A static initializer still needs to be wrapped in a class that contains a main method.

    public class Main {
    
      static {
        System.out.println("Hello world");
      }
    
      public static void main(String[] args) {}
    }
[0]: https://repl.it/GDZG/1


A bit of both. Code still belongs in a class, not pictured here. `static` blocks are executed when the class is loaded and are usually used to fill `static final` members. In a toy example like this it's shorter than writing the `main()` incantation.


It might depend on the precise representation. This has become increasingly standardized lately which is why those numbers are really close and not wildly off.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: