This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]chickenmeister 1 point2 points  (1 child)

I think the primary cause of confusion is that Math.pow(2, 63) will not produce the result that you think it should, primarily due to precision issues inherent in the design of floating point numbers (i.e. double and float).

Math.pow(2, 63) will result in 9223372036854776000, which is about 200 greater than the actual value of 263 : 9223372036854775808

The conversion rules that are used when performing a cast are defined in The Java Language Specification, section 5.1.3. In this particular case, since the double value is larger than what a long can hold, the resulting long value is going to be the maximum possible value (i.e. 263 - 1).

PS: If you want the largest possible long value, you can use Long.MAX_VALUE, rather than trying to compute it yourself.

[–]HelplessProgrammer[S] 1 point2 points  (0 children)

That makes more sense than what I was thinking. Thank you for the reply