Home  >  Q&A  >  body text

java - Math.pow(23,29)%91 的结果为什么是错误的?

Math.pow(23,29)%91 的结果为什么是错误的?


public class T1 {

    public static void main(String[] args) {

        double c = Math.pow(23,29)%91.0;

        System.out.println(c);
    }

}

输出:28.0


        int c = (int)Math.pow(23,29)%91;

        System.out.println(c);

输出  36

然而这都不是正确答案

正确取余后的值是4才对

怪我咯怪我咯2743 days ago676

reply all(2)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:55:16

    Insufficient precision, 23 ^ 29是个40位Decimal number,

    • doubleThere are only 15 significant digits, so the exact value at the end cannot be expressed at all

    • intThe maximum value is only 10 digits, so the assignment has already overflowed

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:55:16

    Double is a floating point number. It is best to use BigInteger to solve your problem.

    reply
    0
  • Cancelreply