import java.math.BigInteger;
public class GaayuProbOne {
static void power(int N, int P) {
BigInteger result = new BigInteger("10");
BigInteger res = result.pow(P);
System.out.println(res);
}
static double power1(int N, int P) {
double res =Math.pow(N,P);
return res;
}
public static void main(String[] args) {
int N = 10;
double P = 25*power1(10,25);
System.out.println(P);
int q = (int) P;
power(N, q);
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码是java中计算10 25 10 25的程序。
如何计算呢?
线程“主”java 中的异常。郎。算术异常:大整数会溢出支持的范围
有没有在java程序中计算10 25 10 25的方法?
注意:自从发布此答案以来,问题发生了很大变化。它最初要求一种计算 10^25 x 10^25 的方法。
该代码有多个问题:
power方法忽略了NMath.pow)int当 的最大值int仅为 2147483647时,您将一个非常非常大的数字 (25 * 10^25) 投射到您需要的实际代码要简单得多:
BigInteger执行此操作的简单代码:
import java.math.BigInteger;
public class Test {
public static void main(String[] args) {
int n = 10;
int p = 25;
BigInteger tmp = BigInteger.valueOf(n).pow(p);
BigInteger result = tmp.multiply(tmp);
System.out.println(result);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
466 次 |
| 最近记录: |