【java.math.BigInteger】【转】常见问题
好大的链接给原作
Q: 在java怎样将BigInteger类型的数据转成int类型的?
A:BigInteger的intValue()可以获得int类型数值。
Q: java.math.BigInteger有位数限制么?比如long是2的64次方。
A:从BigInteger的源码可以看出来,在BigInteger内部数值是通过:int[] mag存放数据的,总共可以存放2147483647个int数据,而每个int数据由4个字节表示,所以BigInteger理论上可以存放的数据最大为22147483647*4*8-1-1
Q: 如何生成一个随机的大数据?
A:利用BigInteger的构造函数:BigInteger(int numBits, Random rnd)构造一个随机产生的大整数,范围在0到2^numBits – 1之间.
Random r = new Random();
BigInteger data = new BigInteger(100,r);
System.out.println("data:"+data)
Q: BigInteger的四则运算实现过程是什么样的?
A:
1、add方法和subtract方法实际上进行的是数组对位相加和相减,这个过程用私有的函数来完成,返回的是为此重新分配空间的结果数组索引值(数组首地址)。然后再用结果数组来构造一个BigInteger作为最后的返回值。
2、multiply方法使用的是数组相乘,用数组来模拟数字相乘,同样使用一个私有的函数来完成这个过程,为相乘结果数组分配新的空间,最后用结果数组来构造一个BigInteger 作为返回值。
3、divide方法实现使用的是MutableBigInteger类,MutableBigInteger类不是公开的,它只供
java.math类库内部使用。MutableBigInteger是BigInteger类的另一个版本,它的特点是不创建临时对象的前提上使调用程
序得到象BigInteger类型的返回值(称为可变对象技术)。因为大整数的除法是由大量的其他算术操作组成的,所以需要大量的临时对象,而完成大量的操作而不创建新的对象可以极大地改善程序的性能,(因为创建对象的代价是很高的)所以在Java的大整数类中使用MutableBigInteger类中
的方法来执行大整数除法。MutableBigInteger类包含在java.math包中,BigInteger类的divide方法使用了
MutableBigInteger类的除法运算所返回的结果,使用这个MutableBigInteger类型的结果来构造一个BigInteger。
实际上在MutableBigInteger类中也使用了数组来模拟数字相除。
Q: BigInteger为什么设计成不可变的类,是在进行每一步运算时,都会产生一个新的对象的吗?
A:虽然BigInteger被设计成一个不可变的类,但是并不是每一步运算都生成一个新的对象,可以看下面的源码:
public BigInteger add(BigInteger val)
{
int[] resultMag;
if (val.signum == 0)
{
return this;
}
if (signum == 0)
{
return val;
}
if (val.signum == signum)
{
return new BigInteger(add(mag, val.mag), signum);
}
int cmp = intArrayCmp(mag, val.mag);
if (cmp==0)
{
return ZERO;
}
resultMag = (cmp>0 ? subtract(mag, val.mag) : subtract(val.mag, mag));
resultMag = trustedStripLeadingZeroInts(resultMag);
return new BigInteger(resultMag, cmp*signum);
}
Q: 在java中 有没有比BigInteger范围更大的?
A: 在java中没有比BigInteger范围更大数了。BigInteger类可以处理包含任意长度数字序列的数值,因为在BigInteger内部是通 过int 数组来表示和处理大数据的,int类型的最大值是2147483647,所以int数组最多有21亿个int值,这些数值足够大的,已经满足了超大数据的使用。
Q: 在哪里可以查看BigInteger的代码实现?
A: 在jdk里面的java.math包下面就可以看到
【java.math.BigInteger】【转】常见问题的更多相关文章
- 【java.math.BigInteger】常用函数
1. /* 返回此BigInteger的函数正负号. 此方法返回-1,0或1作为此BigInteger的值对应是负,零或正数. */ java.math.BigInteger.signum(BigIn ...
- [记录]java.math.biginteger cannot be cast to java.lang.long
可以直接使用BigInteger类型进行接收, BigInteger id = (BigInteger)QueryRunner(conn,"SELECT LAST_INSERT_ID&quo ...
- java.math.BigInteger使用心得总结(转)
今天参考课本写了一个关于二进制与十进制转换的程序,程序算法不难,但写完后测试发现不论是二转十还是十转二,对于大于21亿即超过整数范围的数不能很好的转换.都会变成0.参考书籍发现使用使用BigInteg ...
- Java中的java.math.BigInteger
Java中的java.math.BigInteger /** * */ package com.you.model; /** * @author YouHaidong * */ public clas ...
- mysql连接报java.math.BigInteger cannot be cast to java.lang.Long异常
使用hibernate出现以下错误 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot b ...
- 【问题解决:连接异常】 java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
问题描述: MySQL更新到8.0.11之后连接数据库时会报出错误 Your login attempt was not successful, try again.Reason: Could not ...
- 连接Mysql时出现java.math.BigInteger cannot be cast to java.lang.Long问题
今天遇见这样一个坑.在连接数据库进行查询数据时,大家可能会遇见这样一个问题:java.math.BigInteger cannot be cast to java.lang.Long,然后去检查代码中 ...
- Spring boot启动时报 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long错误
Spring boot启动时报 java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be ...
- 初学MyBatis(踩坑)Error querying database. Cause: java.sql.SQLException: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Long
最近在学习Mybatis,代码全部根据教程写好了,一运行结果报了一个错误,主要错误内容: Caused by: org.apache.ibatis.exceptions.PersistenceExce ...
随机推荐
- ASP.NET验证控件RegularExpressionValidator的常见表达式
可以输入非0和0开头的数字:“^(0*[1-9][0-9]*|[1-9][0-9]*)$”只能输入数字:“^[0-9]*$”只能输入n位的数字:“^\d{n}$”只能输入至少n位数字:“^\d{n,} ...
- Error opening trace file: No such file or directory (2)
想看sharepreference保存的键值对的数据,用到单元测,运行函数总是报这种错,且看不到file explorer-->data>对应工程包的xml文件:
- JS阻塞的问题
常见问题 http://www.zhihu.com/question/23101413 阻塞特性: JS 有个很无语的阻塞特性,就是当浏览器在执行JS 代码时,不能同时做其他任 ...
- Cocos2d-x利用CCHttpRequest获取网络图片并显示
利用CCHttpRequest获取网上http地址的图片并缓存到本地生成CCSprite用于显示 //图片结构class imgstruct : public CCObject { public: i ...
- ubuntu: qemu+gdb 调试linux kernel 学习笔记
声明: 本笔记内容并非本人原创,90%来自网络资料的整合.同时,由于自己是刚刚接触qemu & gdbserver remote debug,本文也就算不得教程,仅供有缘人参考而已. ---- ...
- NuMicro Coretex™-M0家族中哪种芯片支持UID (Unique ID)? 用户该怎么做才能对其芯片进行加密功能?
http://www.nuvoton.com/hq/chs/productfaqs/Pages/00000001.aspx 是的,使用者可利用UID来对以下系列芯片进行加密, Mini51 Serie ...
- javascrip keyCode属性备案
keycode 8 = BackSpace BackSpacekeycode 9 = Tab Tabkeycode 12 = Clearkeycode 13 = Enterkeyc ...
- Codeforces Gym 100231L Intervals 数位DP
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description Start with an integer, N0, ...
- C#编写的多生产者多消费者同步问题
// 多个生产者和多个消费者,能生产n个产品的情况 using System; using System.Threading; public class HoldIntegerSynchronized ...
- 怎样用JS来添加CSS样式
方法: document.getElementById("xx").style.xxx中的全部属性是什么 盒子标签和属性对比 CSS语法(不区分大写和小写) JavaScript语 ...