Could this prevent overflow in Java?
I was coding a problem in TopCoder. In one calculating step there could be
overflow for, the analysis says the solution is cast too long. I don't
understand why this would work.
int normalize(int pos) {
return (int) (((long) pos % MODULO + MODULO) % MODULO);
}
The variables pos and MODULO could both be from the range -2147483648 and
2147483647
I'm wondering what's the help of casting to long? thanks!
No comments:
Post a Comment