Java - 用疯狂的大数计算

Java - Calculate with crazy large numbers

如何计算 Java 中非常大的数字。当我说疯狂时,我的意思是像 8192 位整数和浮点数。

这可能吗?普通 64 位计算机最多可以计算多少?

When I say crazy, I mean like 8192 bits integers and floats.

那真的没那么疯狂。这不会对你的机器或任何东西的内存征税。

对浮点值使用BigInteger for integers, and BigDecimal。我不相信 "big binary floating point numbers".

有类型

举个您正在查看的数字类型的例子:

import java.math.BigInteger;

class Test {
   public static void main(String[] args) {
       byte[] bytes = new byte[8192/8];
       bytes[0] = 127;
       System.out.println(new BigInteger(bytes));
   }
}

输出:

541113332904944621257027340160900091917693549115318562261104546273807066972024025361193585096486923349385062726999774256581849977197274906557009492831443201105551171691244178403919717984227039278443359980180226761679803089497594605997671485724763907145598953406431990173442500426575664262787949112583386253705131668104786750182412791680855944865715259696410188367886612037506345419830856183841501926493918229094077315951142102521296377217539380498798690167426476561663037383738887233213887519273211725579880674800387600568470737270104457912998752875078530446871686526249410822677146900086180880813247518266724528982615032997773901413876848296339184483653166555279106371868396901081861539095911471442610063951293879120365878948439891423650415705034797749122786629924812156703812528640850454060722116295309871801737746764999201088806655284597015419746259510509499349561721654707610989789654471438735169020704030761573632885829361191231553677205472467298253617875576235292510432481272402684906949137107846984996664405910584729193117175984160182199443928924561776902598406361920212049977569423122238936613720193835991801078589862197148943845799227101449150630448578266698756989391702826257605172788293230378218508822387001316270205970944491970492276551254070011941344182057711282913107165296221553726157902456370601394371723685760200355834698473938397945899759397125036210760680478306465830666897406840404024476493134998816143107896676743539769317406617364687861711800984142442948514215249454927820436180492589816736874897449912514943129230209940739231794049768749574548833516764431351090987436249707697734825718908151379544029650456181452943969453122168249071093299640857962065914461325933612617848859243931833842937043427913950964027234614991099243281546044057820429916880334769902849242692796234674560857035410231544279377588197273344847249842671676677537861236399982441213013590542156075455205317955214656111885489459439151729409777140396834780673618790448115444670040022974650543938394323796233646015156947899183947183401173446094248041971411220471893676853128455398177922600837292255766818543666617068690067956364492828867704414948545668364232596008593050588673137731481867410830313713128431917941710625939870646270407030930197645725794246194984570796611923844444224132832526549988639036039442164277785045016465216623043972724482648160510104973400460750404533502159516766838971279926484762092518157058058042524433149400681767325045143455597943916743044982093381632

是的,这是可能的。利用 BigDecimal class

例如:

BigDecimal decOne = new BigDecimal("334");
BigDecimal decTwo = new BigDecimal("33");

BigDecimal result = decOne.subtract(decTwo);

然后简单地打印出来。

BigInteger(for integers) and BigDecimal(对于精确的浮点数)是您所需要的。它们仅受可用内存的限制。