4 ms·
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"? s
by murkle 10y ago
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);
}
- astrodust 10y agoIt might depend on the precise representation. This has become increasingly standardized lately which is why those numbers are really close and not wildly off.
- drv 10y agonextAfter is probably also including the denormals (an additional 2^23 values near 0).
- wwweston 10y agoOfftopic, but: Is this not just shorthand, but an actual legitimate Java program these days?
- SpaceManiac 10y agoA 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.
- ezekg 10y agoI 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 https://repl.it/GDZG/1