Large number processing classes in Java

Source: Internet
Author: User
Tags array definition pow

Transferred from: http://ly5633.iteye.com/blog/1218724

————————————————————————————————————————

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
    1. Scanner sc = new Scanner (system.in);
    2. while (Sc.hasnext ()) {
    3. BigInteger bi;
    4. BIGDECIMAL BD;
    5. bi = Sc.nextbiginteger ();//Read into BigInteger
    6. BD = Sc.nextbigdecimal ();//Read into BigDecimal
    7. System.out.println (Bi.tostring ());
    8. System.out.println (Bd.tostring ());
    9. }
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
  1. package factorial;
  2. Import Java.math.BigInteger;
  3. Import Java.util.Random;
  4. /**
  5. * Test Some functions of the BigInteger class
  6. * @author LY 2011-10-27
  7. * */
  8. public class Bigintegerdemo {
  9. public static void Main (string[] arguments) {
  10. System.out.println ("Construction of two BigInteger objects:");
  11. BigInteger (int numbits, Random rnd)
  12. Constructs a randomly generated BigInteger, which is a uniformly distributed value within the range of 0 to (2^NUMBITS-1) (inclusive)
  13. BigInteger bi1 = new BigInteger (55,new Random ());
  14. System.out.println ("bi1 =" + bi1);
  15. BigInteger (byte[] val)
  16. Converts a byte array containing BigInteger's binary complement representation to BigInteger.
  17. BigInteger Bi2 = new BigInteger (new byte[]{3,2,3});
  18. System.out.println ("Bi2 =" + Bi2);
  19. Add
  20. System.out.println ("bi1 + Bi2 =" + Bi1.add (BI2));
  21. Reducing
  22. System.out.println ("Bi1-bi2 =" + bi1.subtract (BI2));
  23. By
  24. SYSTEM.OUT.PRINTLN ("Bi1 * Bi2 =" + bi1.multiply (BI2));
  25. Exponential arithmetic
  26. System.out.println ("Bi1 2-time Square =" + Bi1.pow (2));
  27. Integer quotient
  28. System.out.println ("Bi1/bi2 's integer quotient:" + bi1.divide (BI2));
  29. Remainder
  30. System.out.println ("The remainder of the BI1/BI2:" + bi1.remainder (BI2));
  31. Integer quotient + remainder
  32. System.out.println ("Bi1/bi2 =" + Bi1.divideandremainder (BI2) [0] +
  33. "--" + Bi1.divideandremainder (BI2) [1]);
  34. System.out.println ("bi1 + Bi2 =" + Bi1.add (BI2));
  35. Compare size, or you can use Max () and Min ()
  36. if (Bi1.compareto (BI2) > 0)
  37. System.out.println ("BD1 is greater than Bd2");
  38. else if (Bi1.compareto (bi2) = = 0)
  39. System.out.println ("Bd1 is equal to Bd2");
  40. else if (Bi1.compareto (BI2) < 0)
  41. System.out.println ("BD1 is lower than Bd2");
  42. Returns the inverse number
  43. BigInteger Bi3 = Bi1.negate ();
  44. System.out.println ("Opposite number of Bi1:" + Bi3);
  45. Return absolute Value
  46. SYSTEM.OUT.PRINTLN ("Absolute value of Bi1:" + bi3.abs ());
  47. }
  48. }
Package Factorial;import java.math.biginteger;import java.util.random;/** * Test Some functions of 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 value evenly distributed within 0 to (2^NUMBITS-1) (inclusive) range BigInteger Bi1 = new Bigin Teger (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);//Plus SYSTEM.OUT.PRINTLN ("bi1 + Bi2 =" + Bi1.add (BI2));//Minus System.out.println ("Bi1-bi2 = + bi1.subtract (BI2));//Multiply System.out.println ("bi1 * Bi2 =" + bi1.multiply (BI2));//exponential Operation System.out.println ("Bi1 2 times =" + Bi1.pow (2));//integer quotient System.out.println ("Bi1/bi2 's integer quotient:" + bi1.divide (BI2));//Remainder System.out.println ("Bi1/bi2 remainder:" + 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, can also 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 opposite number BigInteger Bi3 = Bi1.negat E (); System.out.println ("Opposite number of Bi1:" + bi3);//returns absolute value System.out.println ("absolute value of Bi1:" + bi3.abs ());}}

Operation Result:

Java code
    1. Constructs two BigInteger objects:
    2. BI1 = 8893838204110884
    3. Bi2 = 197123
    4. Bi1 + Bi2 = 8893838204308007
    5. Bi1-bi2 = 8893838203913761
    6. BI1 * Bi2 = 1753180068308949786732
    7. Bi1 2-Time Square = 79100358000902314326836967261456
    8. Bi1/bi2 's integer quotient: 45118216565
    9. Remainder of Bi1/bi2:168389
    10. Bi1/bi2 = 45118216565--168389
    11. Bi1 + Bi2 = 8893838204308007
    12. BD1 is greater than bd2
    13. Inverse number of bi1:8893838204110884
    14. Absolute value of BI1:8893838204110884
Construction of two BigInteger objects: bi1 = 8893838204110884bi2 = 197123bi1 + Bi2 = 8893838204308007BI1-BI2 = 8893838203913761bi1 * Bi2 = 17 53180068308949786732bi1 2-square = 79100358000902314326836967261456bi1/bi2 integer quotient: remainder of 45118216565bi1/bi2:168389bi1/bi2 = 45118216565--168389bi1 + Bi2 = 8893838204308007bd1 is greater the opposite number of than bd2bi1: absolute value of -8893838204110884BI1:  8893838204110884

BigDecimal:

Java code
  1. package factorial;
  2. import Java.math.BigDecimal;;
  3. /**
  4. * Test Some functions of the BigDecimal class
  5. * @author LY 2011-10-27
  6. * */
  7. public class Bigdecimaldemo {
  8. public static void Main (string[] arguments) {
  9. System.out.println ("Construction of two BigDecimal objects:");
  10. Create a BigDecimal object with the char[] array, the second parameter is offset offset,
  11. The third parameter specifies a length
  12. BigDecimal bd1 = new BigDecimal ("3464656776868432998434". ToCharArray (), 2,15);
  13. System.out.println ("BD1 =" + bd1);
  14. Create a BigDecimal object with a double type
  15. BigDecimal bd2 = new BigDecimal (134258767575867.0F);
  16. System.out.println ("Bd2 =" + Bd2);
  17. Add
  18. System.out.println ("BD1 + bd2 =" + Bd1.add (BD2));
  19. Reducing
  20. System.out.println ("bd1-bd2 =" + bd1.subtract (BD2));
  21. By
  22. SYSTEM.OUT.PRINTLN ("Bd1 * bd2 =" + bd1.multiply (BD2));
  23. Exponential arithmetic
  24. System.out.println ("Bd1 2-time Square =" + Bd1.pow (2));
  25. The integer part of the Fetch
  26. System.out.println ("Bd1/bd2 's integer quotient:" + bd1.dividetointegralvalue (BD2));
  27. The return remainder is calculated as: This.subtract (This.dividetointegralvalue (divisor). Multiply (divisor))
  28. System.out.println (Bd1.subtract (Bd1.dividetointegralvalue (BD2) Multiply (BD2)));
  29. System.out.println ("The remainder of the bd1/bd2:" + bd1.remainder (BD2));
  30. Pick-up and remainder, namely Bd1.dividetointegralvalue (BD2) and Bd1.remainder (BD2)
  31. System.out.println ("bd1/bd2 =" + Bd1.divideandremainder (BD2) [0] +
  32. "--" + Bd1.divideandremainder (BD2) [1]);
  33. Compare size, or you can use Max () and Min ()
  34. if (Bd1.compareto (BD2) > 0)
  35. System.out.println ("BD1 is greater than Bd2");
  36. else if (Bd1.compareto (bd2) = = 0)
  37. System.out.println ("Bd1 is equal to Bd2");
  38. else if (Bd1.compareto (BD2) < 0)
  39. System.out.println ("BD1 is lower than Bd2");
  40. Lowest data accuracy
  41. System.out.println ("Bd1 of the lowest data accuracy:" + bd1.ulp ());
  42. }
  43. }
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[] argument s) {System.out.println ("constructs two BigDecimal objects:"), creates BigDecimal objects with char[] array, the second parameter specifies the length of the displacement offset,//third parameter bigdecimal BD1 = New BigDecimal ("3464656776868432998434". ToCharArray (), 2,15); System.out.println ("BD1 =" + BD1);//Create BigDecimal object with double type BigDecimal bd2 = new BigDecimal (134258767575867.0F);  System.out.println ("Bd2 =" + Bd2);//Plus SYSTEM.OUT.PRINTLN ("BD1 + bd2 =" + Bd1.add (BD2));//Minus System.out.println ("bd1-bd2 = + bd1.subtract (BD2));//Multiply System.out.println ("bd1 * bd2 =" + bd1.multiply (BD2));//exponential Operation System.out.println ("BD1 2 times =" + Bd1.pow (2));//The integer portion of the 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 ("Remainder of Bd1/bd2:" + bd1.remainder (BD2));//Fetch quotient 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, can also 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");//Last Data Precision System.out.println ("Bd1 Bit data accuracy: "+ bd1.ulp ());}}

Operation Result:

Java code
    1. Constructs two BigDecimal objects:
    2. BD1 = 646567768684329
    3. Bd2 = 134258765070336
    4. BD1 + Bd2 = 780826533754665
    5. Bd1-bd2 = 512309003613993
    6. BD1 * Bd2 = 86807390157840676971865964544
    7. BD1 2-Time Square = 418049879501431972683650180241
    8. Bd1/bd2 's integer quotient: 4
    9. Remainder of Bd1/bd2:109532708402985
    10. Bd1/bd2 = 4--109532708402985
    11. BD1 is greater than bd2
    12. BD1 the lowest data accuracy: 1
Construction of two BigDecimal objects: bd1 = 646567768684329bd2 = 134258765070336bd1 + bd2 = 780826533754665BD1-BD2 = 512309003613993BD1 * BD 2 = 2 86807390157840676971865964544bd1 of the 418049879501431972683650180241BD1/BD2 = integer quotient of the 4bd1/bd2: remainder of: 109532708402985BD1/BD2 = 4--109532708402985BD1 is greater than bd2bd1 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.

Large number processing classes in Java

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.