4 ms·
I do not know this language (Java?), but doesn't the line int offset = (int) (((size - 1) * ((long) key - min)) / (max - min)); compute (max - min) as an i
by pascal_cuoq 16y ago
I do not know this language (Java?), but doesn't the line
int offset = (int) (((size - 1) * ((long) key - min)) / (max - min));
compute (max - min) as an int (potentially overflowing), and then convert it to long, defeating the precautions obviously taken against this kind of event with the cast to long elsewhere?
- huherto 16y agoLet me try to break it down: (max - min) is int ((long) key - min) is long long / int is long since a long is involved. (size - 1) is int int * long is long I think the purpose if the long cast is that the last multiplication produces a long and it doesn't overflow. But then it is casted to an int anyway defeating the purpose.
- feijai 16y ago> (max - min) is int Incorrect. These are signed values. If you subtract a large negative integer from a large positive integer, the result is larger than an integer.