java - Math.pow(23,29)%91 的结果为什么是错误的?
怪我咯
怪我咯 2017-04-18 10:53:16
0
2
664

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才对

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(2)
伊谢尔伦

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

洪涛

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!