These two classes are located within the Java.math package and must be referenced before the class to use them: import Java.math.BigInteger; and import java.math.BigDecimal;
BigInteger and BigDecimal represent immutable arbitrary-precision integers and immutable signed arbitrary-precision decimal numbers (floating-point numbers), respectively. It is mainly used in high precision calculation. These two classes make large numbers in Java, and high-precision operations become simple.
Here is a simple comparison of BigInteger and Bigdecima from several aspects:
A. constant
BigInteger: One,zero,ten represents 1,0,10 respectively.
The definition is similar to the following: public static final BigInteger one = valueOf (1);
BigDecimal: In addition to the above three constants there are 8 more about rounding constants, which are not mentioned here, you can see the API's Help manual.
By the way, bigdecimal because of the rounding pattern, this class is more complex to use than the BigInteger. Here, you can see the Help Manual of the API, but usually not much for complex features, so it is possible to consult the API when used.
Two. Declaration Assignment
BigInteger: BigInteger bi = new BigInteger ("100"), or: BigInteger bi = biginteger.valueof (100);
The array definition is similar to the base type.
BigDecimal: BigDecimal bd = new BigDecimal (100) OR: BigDecimal BD = bigdecimal.valueof (100);
BigDecimal has more constructors than BigInteger and feels easier to use, such as the definition is wrong: BigInteger bi = new BigInteger (100);
By the way, the scanner class in the Java.util package implements the Nextbiginteger () and Nextbigdecimal () methods, which can be used to read the BigInteger and bigdecimal of the console input. Give an example:
Java code
- Scanner sc = new Scanner (system.in);
- while (Sc.hasnext ()) {
- BigInteger bi;
- BIGDECIMAL BD;
- bi = Sc.nextbiginteger ();//Read into BigInteger
- BD = Sc.nextbigdecimal ();//Read into BigDecimal
- System.out.println (Bi.tostring ());
- System.out.println (Bd.tostring ());
- }
Three. Related functions
The main introduction of arithmetic and other functions:
Use two examples to illustrate the more intuitive:
BigInteger:
Java code
- package factorial;
- Import Java.math.BigInteger;
- Import Java.util.Random;
- /**
- * Test Some functions of the BigInteger class
- * @author LY 2011-10-27
- * */
- public class Bigintegerdemo {
- public static void Main (string[] arguments) {
- System.out.println ("Construction of two BigInteger objects:");
- BigInteger (int numbits, Random rnd)
- Constructs a randomly generated BigInteger, which is a uniformly distributed value within the range of 0 to (2^NUMBITS-1) (inclusive)
- BigInteger bi1 = new BigInteger (55,new Random ());
- System.out.println ("bi1 =" + bi1);
- BigInteger (byte[] val)
- Converts a byte array containing BigInteger's binary complement representation to BigInteger.
- BigInteger Bi2 = new BigInteger (new byte[]{3,2,3});
- System.out.println ("Bi2 =" + Bi2);
- Add
- System.out.println ("bi1 + Bi2 =" + Bi1.add (BI2));
- Reducing
- System.out.println ("Bi1-bi2 =" + bi1.subtract (BI2));
- By
- SYSTEM.OUT.PRINTLN ("Bi1 * Bi2 =" + bi1.multiply (BI2));
- Exponential arithmetic
- System.out.println ("Bi1 2-time Square =" + Bi1.pow (2));
- Integer quotient
- System.out.println ("Bi1/bi2 's integer quotient:" + bi1.divide (BI2));
- Remainder
- System.out.println ("The remainder of the BI1/BI2:" + bi1.remainder (BI2));
- Integer quotient + remainder
- System.out.println ("Bi1/bi2 =" + Bi1.divideandremainder (BI2) [0] +
- "--" + Bi1.divideandremainder (BI2) [1]);
- System.out.println ("bi1 + Bi2 =" + Bi1.add (BI2));
- Compare size, or you can use Max () and Min ()
- if (Bi1.compareto (BI2) > 0)
- System.out.println ("BD1 is greater than Bd2");
- else if (Bi1.compareto (bi2) = = 0)
- System.out.println ("Bd1 is equal to Bd2");
- else if (Bi1.compareto (BI2) < 0)
- System.out.println ("BD1 is lower than Bd2");
- Returns the inverse number
- BigInteger Bi3 = Bi1.negate ();
- System.out.println ("Opposite number of Bi1:" + Bi3);
- Return absolute Value
- SYSTEM.OUT.PRINTLN ("Absolute value of Bi1:" + bi3.abs ());
- }
- }
Operation Result:
Java code
- Constructs two BigInteger objects:
- BI1 = 8893838204110884
- Bi2 = 197123
- Bi1 + Bi2 = 8893838204308007
- Bi1-bi2 = 8893838203913761
- BI1 * Bi2 = 1753180068308949786732
- Bi1 2-Time Square = 79100358000902314326836967261456
- Bi1/bi2 's integer quotient: 45118216565
- Remainder of Bi1/bi2:168389
- Bi1/bi2 = 45118216565--168389
- Bi1 + Bi2 = 8893838204308007
- BD1 is greater than bd2
- Inverse number of bi1:8893838204110884
- Absolute value of BI1:8893838204110884
BigDecimal:
Java code
- package factorial;
- import Java.math.BigDecimal;;
- /**
- * Test Some functions of the BigDecimal class
- * @author LY 2011-10-27
- * */
- public class Bigdecimaldemo {
- public static void Main (string[] arguments) {
- System.out.println ("Construction of two BigDecimal objects:");
- Create a BigDecimal object with the char[] array, the second parameter is offset offset,
- The third parameter specifies a length
- BigDecimal bd1 = new BigDecimal ("3464656776868432998434". ToCharArray (), 2,15);
- System.out.println ("BD1 =" + bd1);
- Create a BigDecimal object with a double type
- BigDecimal bd2 = new BigDecimal (134258767575867.0F);
- System.out.println ("Bd2 =" + Bd2);
- Add
- System.out.println ("BD1 + bd2 =" + Bd1.add (BD2));
- Reducing
- System.out.println ("bd1-bd2 =" + bd1.subtract (BD2));
- By
- SYSTEM.OUT.PRINTLN ("Bd1 * bd2 =" + bd1.multiply (BD2));
- Exponential arithmetic
- System.out.println ("Bd1 2-time Square =" + Bd1.pow (2));
- The integer part of the Fetch
- System.out.println ("Bd1/bd2 's integer quotient:" + bd1.dividetointegralvalue (BD2));
- The return remainder is calculated as: This.subtract (This.dividetointegralvalue (divisor). Multiply (divisor))
- System.out.println (Bd1.subtract (Bd1.dividetointegralvalue (BD2) Multiply (BD2)));
- System.out.println ("The remainder of the bd1/bd2:" + bd1.remainder (BD2));
- Pick-up and remainder, namely Bd1.dividetointegralvalue (BD2) and Bd1.remainder (BD2)
- System.out.println ("bd1/bd2 =" + Bd1.divideandremainder (BD2) [0] +
- "--" + Bd1.divideandremainder (BD2) [1]);
- Compare size, or you can use Max () and Min ()
- if (Bd1.compareto (BD2) > 0)
- System.out.println ("BD1 is greater than Bd2");
- else if (Bd1.compareto (bd2) = = 0)
- System.out.println ("Bd1 is equal to Bd2");
- else if (Bd1.compareto (BD2) < 0)
- System.out.println ("BD1 is lower than Bd2");
- Lowest data accuracy
- System.out.println ("Bd1 of the lowest data accuracy:" + bd1.ulp ());
- }
- }
Operation Result:
Java code
- Constructs two BigDecimal objects:
- BD1 = 646567768684329
- Bd2 = 134258765070336
- BD1 + Bd2 = 780826533754665
- Bd1-bd2 = 512309003613993
- BD1 * Bd2 = 86807390157840676971865964544
- BD1 2-Time Square = 418049879501431972683650180241
- Bd1/bd2 's integer quotient: 4
- Remainder of Bd1/bd2:109532708402985
- Bd1/bd2 = 4--109532708402985
- BD1 is greater than bd2
- BD1 the lowest data accuracy: 1
In this paper, the BigInteger and BigDecimal classes are relatively simple introduction and comparison, such as the two categories of interest, you can consult the API or study its source code.
Original: http://ly5633.iteye.com/blog/1218724
Analysis of large number processing class BigInteger and Bigdecimar in Java