Wrong result by Java Math.pow

后端 未结 6 1852
挽巷
挽巷 2020-12-06 17:17

If you try to run the following code

public class Main {
   public static void main(String[] args) {
       long a = (long)Math.pow(13, 15);
       System.ou         


        
6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 17:59

    You've exceeded the number of significant digits available (~15 to 16) in double-precision floating-point values. Once you do that, you can't expect the least significant digit(s) of your result to actually be meaningful/precise.

    If you need arbitrarily precise arithmetic in Java, consider using BigInteger and BigDecimal.

提交回复
热议问题