Java always give me wrong result with huge numbers [duplicate]
问题 This question already has answers here : Wrong result by Java Math.pow (6 answers) Closed 2 years ago . I wrote this line of code: System.out.println(Math.pow(7, 23) % 143); // 7^23 mod 143 I expected the output to be 2 but the output was 93.0 . Does someone know what I'm doing wrong? 回答1: The number "overflows" double , which is what Math.pow() expects and returns. Use a BigInteger instead: BigInteger.valueOf(7) .pow(23) .mod(BigInteger.valueOf(143)) Or in a single step, as suggested by