https://kotlinlang.org logo
#getting-started
Title
# getting-started
t

TwoClocks

10/26/2021, 10:21 PM
is there a lib that'll do saturation math, or error / exception on overflow?
or do you just do it in
BigInteger
and check at the end?
e

ephemient

10/26/2021, 10:56 PM
I've actually been working on one, https://github.com/ephemient/kotlin-numeric saturating and exact math should all work, with tests, but I'm not using it in any real projects yet so no guarantees (also has a multi-platform BigInteger and I'm planning a multi-platform BigDecimal, but there's some tricky cases I need to work through)
a bit complicated by codegen but the basic idea is simple
for error on overflow on JVM, there's standard Java methods
for Int-sized math, step up to Long
for Long, instead of using BigInteger you just have a variety of cases to handle based on sign
if you're only targeting JVM then Guava has all the exact/saturating arithmetic methods too
t

TwoClocks

10/27/2021, 8:01 PM
very cool. Yeah, I'm JVM only. And there are just a few times I want an error if I'm overflowing a long.
I have code for ULong but you probably don't need that
8 Views