Closed
Labels
Description
Description
com.thealgorithms.maths.Pow.pow(int a, int b) multiplies into a long without checking whether the result is representable. For example, Pow.pow(2, 63) mathematically equals 9223372036854775808, which is greater than Long.MAX_VALUE, but Java long overflow causes the method to return Long.MIN_VALUE. That is a plausible-looking but incorrect result for a utility documented as computing a power and returning a long.
Steps to reproduce
- Call
Pow.pow(2, 63)(orPow.pow(2, 64)). - Observe the returned value:
Long.MIN_VALUEfor the first call, and0for the second, due to overflow wrapping.
Excepted behavior
When the exact result cannot fit in a long, the method should detect overflow and report it (for example, by throwing ArithmeticException), or explicitly document wraparound as its intended behavior.
Screenshots
No response
Additional context
No response
Activity