https://kotlinlang.org logo
b

bjonnh

12/05/2020, 7:03 PM
Why is there no pow function for integers?
One explanation I see (on SO) is that it would quickly overflow an Integer anyway. But I wonder if that's the reason behind the design choice (of Java it seems)
a

adamratzman

12/05/2020, 7:18 PM
My theory is that there’s no Int.NaN, but there is a Float.NaN and Double.NaN, and I don’t think the stdlib designers would consider a pow function “correct” without accounting for the cases where NaN should be returned
n

Nir

12/05/2020, 7:18 PM
Why does NaN have anything to do with it?
I don't follow that theory at all
Exponentiation is better defined than division
And you can divide integers even with no way to account for dividing by zero
n

Nir

12/05/2020, 7:20 PM
An integer to the power of an integer is well defined for all values
You only get NaN for NaN inputs or infinite
Which integers don't support, so they can't be inputs
a

adamratzman

12/05/2020, 7:21 PM
They can be infinite, but not nan
n

Nir

12/05/2020, 7:21 PM
Integers can't be infinite
Unless this is some crazy feature of JVM integers that nobody else uses
Maybe a more compelling reason is Int.power(Int) returns a non Integer for any negative second argument
a

adamratzman

12/05/2020, 7:23 PM
Oh wow, I mistakenly thought there was an Int.POSITIVE/NEGATIVE_INFINITY
disregard what I said then
n

Nir

12/05/2020, 7:23 PM
So perhaps the result needs to be floating point regardless
But it would still be useful for convenience IMHO
Yeah floating point has infinite because it's part of IEEE and had native support
(NaN too)
With Int you'd need to emulate it all with branching and perf is bad
n

Nir

12/05/2020, 9:00 PM
This isn't the same as the question on this thread, just FYI
20 Views