Calculating powers of integers

前端 未结 16 2072
情书的邮戳
情书的邮戳 2020-12-08 06:18

Is there any other way in Java to calculate a power of an integer?

I use Math.pow(a, b) now, but it returns a double, and that is usually a

16条回答
  •  孤城傲影
    2020-12-08 06:33

    Integers are only 32 bits. This means that its max value is 2^31 -1. As you see, for very small numbers, you quickly have a result which can't be represented by an integer anymore. That's why Math.pow uses double.

    If you want arbitrary integer precision, use BigInteger.pow. But it's of course less efficient.

提交回复
热议问题