BigInteger and BigDecimal Classes in Java are powerful

Source: Internet
Author: User
Tags greatest common divisor pow

The BigInteger class is Java in order to handle large number two specialized classes, can handle very large numbers (theoretically infinitely large), and can achieve large numbers similar to int all mathematical operations. For the algorithm problem, no longer afraid of the operation beyond the scope of int!

At the same time, the class dealing with large floating-point numbers is BigDecimal.

BigInteger mainly implements the following functions:

Direct read of large integers

Large integer subtraction, seeking redundancy

A power, an absolute value, an inverse number.

Determine if they are equal, ask for the maximum value of two number, minimum value

Find the number of conventions (great!) )

To find the number of digits after a number is turned into a binary number

Determine if a number is a prime (possibly wrong)

Gets the next possible prime number of a count

To be introduced when using the package as:import Java.math.BigInteger

Ⅰ Basic functions:

1.valueOf (parament); To convert a parameter to a developed type

such as int a = 3;

BigInteger B = biginteger.valueof (a);

Then b=3;

String s= "12345";

BigInteger C = biginteger.valueof (s);

Then C = 12345;

2.add (); Add large integers

BigInteger a=new BigInteger ("23");

BigInteger b=new BigInteger ("34");

A.add (b);

3.subtract (); Subtraction

4.multiply (); Multiply

5.divide (); Divide rounding

6.remainder (); Take surplus

7.pow (); A.pow (b) =a^b

8.GCD (); Greatest common divisor

9.abs (); Absolute

10.negate (); Take the inverse number

11.mod (); A.mod (b) =a%b=a.remainder (b);

12.max (); Min ();

13.public int Comareto ();

14.boolean equals (); is equal

15.shiftLeft (): Shift left, this << N, this*2^n;

Shiftright (): Shift right, this >> n,this/2^n;

16.bitLength: Returns the number of bits represented by the minimum twos complement of the numbers, that is, * does not include the * sign bit (ceil (log2 (this <0?-this:this + 1))). For positive numbers, this is equivalent to the number of bits in the normal binary representation.

17.bitCount: Returns the number of bits in the binary complement representation of the digit that do not pack the spread sign. This method is useful for implementing bit-vector-style collections on top of bigintegers.

18.isProbablePrime: Returns True if the BigInteger is likely to be a prime number, or False if it is explicitly a composite. Parameter certainty is a measure of uncertainty that the caller is willing to endure: if the probability of the number being a prime is greater than the 1-1/2**certainty method, the method returns True. The execution time is proportional to the value of the parameter certainty.

19.nextProbablePrime (): Gets the next possible prime number

20.BigInteger Constructors:

Generally used in the following two kinds:

BigInteger (String val);

Converts the specified string to a decimal representation;

BigInteger (String val,int radix);

Converts the string representation of a BigInteger of a specified cardinality to BigInteger

BigInteger and BigDecimal Classes in Java are powerful

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.