首页 > 编程语言 >Java问题之超过数值表示范围(例如64位)结果是什么 (阶乘)

Java问题之超过数值表示范围(例如64位)结果是什么 (阶乘)

时间:2022-09-18 15:55:51浏览次数:80  
标签:BigInteger Java 数字 valueOf other 64 阶乘

关于老师在课上所提及的这个问题 我做了验证 截图如下

 

 只是一个简单的计算阶乘的代码 在运行时得到了如下结果

 

 

 可以看到,对于部分数字如果超出范围会从64位处自动截断,而这时由于是二进制表示,首位默认是符号位,所以会认为是负数,对于过大的数字直接就成0了

在这里如果我们想进行超大数字的表示则需要其他方法

BigInteger a=BigInteger.valueOf(100);

 java中提供了Big--这一类,既可以将普通数字转化为大数字

常用方法如下

BigInteger add(BigInteger other) 加
BigInteger subtract(BigInteger other)减
BigInteger multiply(BigInteger other)乘
BigInteger divide(BigInteger other)除

在这里要注意的是不能用常规的加减乘除对大数字进行运算

public class Print {
public static void main(String[] args) {
BigInteger a=BigInteger.valueOf(2);
for (int i = 0; i < 100; i++) {
a=a.multiply(BigInteger.valueOf(2));
}
System.out.println(a);//2535301200456458802993406410752
}
}

标签:BigInteger,Java,数字,valueOf,other,64,阶乘
From: https://www.cnblogs.com/Arkiya/p/16704909.html

相关文章