Hi guys, anyone know the reason why Int.pow(int) d...
# stdlib
v
Hi guys, anyone know the reason why Int.pow(int) doesn't exists in Kotlin & Java ?
k
Very frustrating indeed.
v
https://stackoverflow.com/q/8071363/5912422 << I understand the reason but it still frustrating
k
I think the overflow excuse is dumb, yes numbers overflow and yes we all know about it. I just want to do
3 pow 5
sometimes dammit!
s
True.. but even if you buy the overflow argument then it ought to be on
StrictMath
l
Well, I don't think that the overflow argument is dumb at all, since for bigger numbers you will suddenly get nonsense results and you will be wondering why 🙂 . Anyway if the result of the
3 pow 5
would be a
Double
, then why not - that should be fine.
k
If you add/multiply/subtract two large `Int`s you also get nonsense results, but no one complains about those: everyone knowns about overflow already. And no we don't want
Double
powers, we have that already.
(can you tell I'm salty about this)
l
Perhaps it's because it's much much much more easier to overflow it with pow than with add/multiply. E.g. if I am not mistaken, the
9 pow 10
should already overflow the
Int.MAX_VALUE
. So from my point of view the range of numbers you could realistically use in such function is so narrow, that it doesn't even make much of a sense 🙂. Anyway if you never use bigger nums in the second argument, let's say than 5, then perhaps you could write your own extension function which might have some realistic use for you, but I would bet that you will overflow the Int MAX_VALUE very soon though 😁
k
Yeah I've got a function I copy and paste between functions, and it happily overflows and I've never had any issues. Btw multiplication overflow is really eazy,
50_000 * 50_000
already overflows.
l
Yeah, it seems the multiplication is the second most dangerous 😁. Just for fun I did some tests and if you used only the pow of 2, then the biggest number you can use is 46341, for the power of 3 it's just 1291 and for the pow of 4 only 216. So it depends ... anyway if it works good for you, you might consider making a library to avoid copy pasting - also for whoever needs to make such computations on (very) small numbers.
k
Well thing is maintaining and including a library for it is more work then just copying the 5 lines of code every time 🤷
i
If we introduce such
pow
operation for ints, what would you expect it to do in case of an overflow: - throw some exception, e.g.
ArithmeticException
- return some value, e.g. the result mod 2^31 or 2^32 for unsigned ints? Would it be a suitable behavior in the cases, where you need it?
2️⃣ 5
1️⃣ 15
k
I don't have a strong opinion on this, because in my use cases they never overflow anyway. That said, I would go for silently overflow (ie. mod ...) like all other integer math operators for performance reasons. And maybe put a safer, exception throwing variant in something like
StrictMath
.
2
Hmm looks like Kotlin doesn't have anything like Java's
StrictMath
yet, is there a multiplatform way to have exceptions throws when
plus
overflows?