Why is there no pow function for integers?
# announcements
b
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
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
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
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
They can be infinite, but not nan
n
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
Oh wow, I mistakenly thought there was an Int.POSITIVE/NEGATIVE_INFINITY
disregard what I said then
n
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
This isn't the same as the question on this thread, just FYI
191 Views