Hello! Is there builtin solution to check for over...
# stdlib
p
Hello! Is there builtin solution to check for overflow when converting Long.toInt() ?
m
You can check that the resulting it is equal to the original long
e
otherwise
it == it.toInt()
as mentioned above works, as does
it in Int.MIN_VALUE..Int.MAX_VALUE
p
Thanks! I hoped there must be multiplatform stdlib solution...😒
e
I wish we had saturating and checked versions of all arithmetic operators in Kotlin, but sadly we don't 😞
t
TIL what “saturation arithmetic” is. Thanks! 🙂
m
Might be worth an issue if there's none already?
e
I actually created a small library that provides multiplatform implementations of the java.math functions that aren't part of the stdlib, including toIntExact(). https://github.com/erikc5000/javamath2kmp I haven't really promoted it, but it's used internally by Island Time. Some of the exact math functions could be made more efficient on native using compiler intrinsics, so it would definitely be nice to see them make their way into the stdlib.
👍 1
t
Wow, neat!
Thanks Erik.