-2

How can I calculate with very large numbers in Java. When I say crazy, I mean like 8192 bits integers and floats.

Is this possible and what is the maximum an ordinary 64 bit computer can calculate with?

user3854743
  • 511
  • 1
  • 4
  • 9

3 Answers3

10

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

That's really not that crazy. It's not like that's going to tax the memory in your machine or anything.

Use BigInteger for integers, and BigDecimal for floating point values. I don't believe there's a type for "big binary floating point numbers".

Just as an example of the kind of numbers you're looking at:

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));
   }
}

Output:

541113332904944621257027340160900091917693549115318562261104546273807066972024025361193585096486923349385062726999774256581849977197274906557009492831443201105551171691244178403919717984227039278443359980180226761679803089497594605997671485724763907145598953406431990173442500426575664262787949112583386253705131668104786750182412791680855944865715259696410188367886612037506345419830856183841501926493918229094077315951142102521296377217539380498798690167426476561663037383738887233213887519273211725579880674800387600568470737270104457912998752875078530446871686526249410822677146900086180880813247518266724528982615032997773901413876848296339184483653166555279106371868396901081861539095911471442610063951293879120365878948439891423650415705034797749122786629924812156703812528640850454060722116295309871801737746764999201088806655284597015419746259510509499349561721654707610989789654471438735169020704030761573632885829361191231553677205472467298253617875576235292510432481272402684906949137107846984996664405910584729193117175984160182199443928924561776902598406361920212049977569423122238936613720193835991801078589862197148943845799227101449150630448578266698756989391702826257605172788293230378218508822387001316270205970944491970492276551254070011941344182057711282913107165296221553726157902456370601394371723685760200355834698473938397945899759397125036210760680478306465830666897406840404024476493134998816143107896676743539769317406617364687861711800984142442948514215249454927820436180492589816736874897449912514943129230209940739231794049768749574548833516764431351090987436249707697734825718908151379544029650456181452943969453122168249071093299640857962065914461325933612617848859243931833842937043427913950964027234614991099243281546044057820429916880334769902849242692796234674560857035410231544279377588197273344847249842671676677537861236399982441213013590542156075455205317955214656111885489459439151729409777140396834780673618790448115444670040022974650543938394323796233646015156947899183947183401173446094248041971411220471893676853128455398177922600837292255766818543666617068690067956364492828867704414948545668364232596008593050588673137731481867410830313713128431917941710625939870646270407030930197645725794246194984570796611923844444224132832526549988639036039442164277785045016465216623043972724482648160510104973400460750404533502159516766838971279926484762092518157058058042524433149400681767325045143455597943916743044982093381632
user692942
  • 16,398
  • 7
  • 76
  • 175
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I say that's one crazy large number :) – Marko Topolnik Jan 16 '15 at 11:01
  • @MarkoTopolnik If that's "crazy large", then what would you call a number that had that many digits? Or, how about, take a number with that many digits, and raise it to its own power? Or, take _that_ number and... IMO the "crazy large" numbers are the ones for which crazy mathematicians have to invent new notations so that they can even talk about them. (http://en.wikipedia.org/wiki/Conway_chained_arrow_notation) – Solomon Slow Jan 20 '15 at 23:13
  • BigDecimal is _not_ a floating point representation. BigDecimal is mostly _fixed point_, but with certain operations where the program can explicitly ask for a result that has a different scale from the given operands. If floating point is like having an automatic transmission, and BigInteger is like having the engine connected directly to the wheels, then BigDecimal is kind of like having a stick shift. – Solomon Slow Jan 20 '15 at 23:29
  • 2
    @jameslarge: I disagree. In my view, a floating point number is one where both the mantissa and the exponent (as well as sign) as part of the value - they can be different for each value of the type. Compare that with a fixed point representation, where only the mantissa varies, and the exponent is fixed. I don't view where the exponent comes from (within the API) as being relevant to whether it's floating point or not. – Jon Skeet Jan 21 '15 at 08:07
  • For that matter, `BigDecimal` is even more floating-point because the exact same number can be represented with different combinations of mantissa and exponent. – Marko Topolnik Jan 21 '15 at 09:20
  • After reading more, I agree that BigDecimal is not fixed point, but it is not floating point either. The Wikipedia _Floating point_ article aligns with what I've been taught all my life--that floating point is characterized by approximate results, with a fixed-size representation, and a radix point that automatically "floats" to the most appropriate position. All of the academic papers I've read about characteristics and pitfalls of floating point math are based on the same definition. I don't know a generic name for BigDecimal, but it deserves one that is different from _floating point_. – Solomon Slow Jan 21 '15 at 13:41
  • @jameslarge: From Wikipedia: "Where greater precision is desired, floating-point arithmetic can be implemented (typically in software) with variable-length significands (and sometimes exponents) that are sized depending on actual need and depending on how the calculation proceeds. This is called arbitrary-precision floating-point arithmetic." Doesn't that describe `BigDecimal`? Possibly just calling it an "arbitrary-precision floating point type" is good enough to distinguish it from "fixed-precision floating point types" like float/double. I don't see why the precision should have to be fixed – Jon Skeet Jan 21 '15 at 13:45
1

Yes, it's possible. Utilize the BigDecimal class

For instance:

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

BigDecimal result = decOne.subtract(decTwo);

Then simply print it out.

unbindall
  • 514
  • 1
  • 13
  • 29
Ahmed Elmir
  • 163
  • 6
1

BigInteger(for integers) and BigDecimal(for exact floating point numbers) is what you need. They are only limited by the available memory.

LionC
  • 3,106
  • 1
  • 22
  • 31