Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用BigInteger?_Java - Fatal编程技术网

Java 如何使用BigInteger?

Java 如何使用BigInteger?,java,Java,如何准确地使用BigInteger?我正在努力做到以下几点: import java.util.*; import java.math.*; public class hello{ public static void main(String[] args){ for(int i = 0; i <= 1024; i++){ BigInteger a = new BigInteger(Math.pow(2,i)); Syste

如何准确地使用BigInteger?我正在努力做到以下几点:

import java.util.*;
import java.math.*;

public class hello{
   public static void main(String[] args){
       for(int i = 0; i <= 1024; i++){
           BigInteger a = new BigInteger(Math.pow(2,i));
           System.out.println(a);
       }
   }
}
我希望a保存这个潜在的巨大值,以便我可以在程序中操作它。

Math.pow返回一个double,2^I不能用double表示,因为它太大了

您需要使用BigInteger中的pow方法:


Java的Math类没有BigInteger方法。 请改用BigInteger的方法

BigInteger a = new BigInteger(Math.pow(2,i));
System.out.println(a);
应该是

BigInteger a = new BigInteger("2").pow(i); // String to BigInteger, and then power
System.out.println(a);

没有大整数双构造函数。尝试使用新的BigIntegerString,而不是Math.pow使用同样返回BigInteger的BigIntegerpow方法


把BigInteger想象成一个字符串。您不能像对待基本数据类型那样在其上使用任何算术、关系和一元运算符。相反,您必须使用BigInteger类中的方法对BigInteger执行操作。 例如,如果要将两个大整数相乘,则不能执行以下操作:

BigInteger a = (new BigInteger("5"))*(new BigInteger("7"));
相反,您必须声明两个大整数,然后将它们与.multiply相乘

因此,对于要打印二次幂的程序,必须以稍微不同的方式使用BigInteger

for(int i = 0; i <= 1024; i++){
       BigInteger a = new BigInteger("2").pow(i);
       System.out.println(a);
   }

请注意.pow中的值是如何在BigInteger中指定的int。从本质上讲,BigInteger在计算大值时是一个非常强大的工具,但它也更繁琐,需要更长、更复杂的代码。

您是否阅读了相应JavaDoc中的BigInteger文档?这是一个比在StackOverflow中提问更好的开始。你不觉得吗?这个问题似乎离题了,因为它是关于它在Javadoc中已经说过的内容,并且没有任何先前的研究。这个问题似乎离题了,因为它缺乏关于这个主题的任何先前研究。这是基本的。
for (int i = 0; i < 200; i++) {
    BigInteger a = new BigInteger("2").pow(i);
    System.out.println(a);
}
BigInteger a = (new BigInteger("5"))*(new BigInteger("7"));
BigInteger a = new BigInteger("5");//note how these numbers are like strings
BigInteger b = new BigInteger("7");
BigInteger c = a.multiply(b);
for(int i = 0; i <= 1024; i++){
       BigInteger a = new BigInteger("2").pow(i);
       System.out.println(a);
   }