LongArray minimum value overflow [solved]

Lately I have used arrays a lot. I’ve started finding various quirks and asking many questions around them. One of the weirdest issues happens when I try to initialize a primitive array LongArray with minimum value. Instead of having a values of -922…08L it overflows and becomes 0. Here’s an example:

val longArray = longArrayOf(Long.MIN_VALUE, 0, Long.MAX_VALUE)
println("longArray min: ${longArray[0]}")
println("long min value: ${Long.MIN_VALUE}")

What I see is this:

longArray min: 0
long min value: -9223372036854775808

What just happened? Shouldn’t they be the same value of -922…something?

From definition of Long.MIN_VALUE I can see this:Screenshot 2020-11-29 at 19.07.13

Why do we have that -1? Why first println shows 0 (zero)? I’m confused.

1 Like

Can’t reproduce it myself. I get the correct value. Are you sure that you are using the correct index into the array in your code? I doubt an error like this wouldn’t be caught.
Anyways if this error persist I would suggest you open an issue at https://kotl.in/issue


Oh, you’re right. The actual problem is different but after many trials my code changed so much that I’ve accumulated many small errors.

The actual issue was this:

It explains why we have 922...something..07L - 1 in the Primitives class. Seems like a known issue to me that has a workaround.

I will close this question as resolved.