7 ms·
Here's one absurd bug in Java that really surprised me: Math.abs(-2147483647); // 2147483647 Math.abs(-2147483648); // -2147483648 When you know this behav
by ikusalic 12y ago
Here's one absurd bug in Java that really surprised me:
Math.abs(-2147483647); // 2147483647
Math.abs(-2147483648); // -2147483648
When you know this behavior, it's kinda obvious why, but still...
- foxhill 12y agowell, i'd only consider that a bug if java defines that integers will use at least as many bits as needed to store their value. i'd guess this is not the case (but i don't know for sure)
- rwallace 12y agoWell, not just kind of obvious why, but kind of obvious it's not a bug in Java! If you're dealing with quantities that don't fit in a 32-bit twos complement integer, you need to use a larger datatype.