0
public static void main(String[] args){

int res = (int)Math.pow(2017, 202);

System.out.println(res);
}

This outputs 2147483647

Which is obviously wrong and if i type in other exponents the output remains the same. How do i remove this cap and what is causing it?

  • 1
    Simply use `BigInteger`: `BigInteger result = BigInteger.valueOf(2017).pow(202)` – Lino Jul 16 '20 at 09:04
  • If you are doing that calculation for some kind of coding / math quiz / question then chances are that actually calculating the power is not needed at all, e.g. if you `mod` afterwards – luk2302 Jul 16 '20 at 09:06

1 Answers1

1

To remove the cap, use a different datatype.

int is capped at 2^31-1, long is capped at 2^63-1, but AFAIK BigInteger is not capped:

BigInteger res = new BigInteger("2017").pow(202)

Don't use it for normal calculations, however, since it's much slower.

GeertPt
  • 16,398
  • 2
  • 37
  • 61
  • 1
    [`BigInteger` is capped, but not enforced by the specs](https://stackoverflow.com/q/12693273/5515060) – Lino Jul 16 '20 at 09:07
  • [Here's what `Math.pow(2017, 202)` is.](https://www.wolframalpha.com/input/?i=2017%5E202) – akuzminykh Jul 16 '20 at 09:13