JAVA big value problem summary

Source: Internet
Author: User

JAVA big value problem summary

Big value problems:



If the precision of the basic integers (such as int and long) and floating-point numbers is not satisfactory, you can use java. the math package contains two classes: BigInteger (integer of any precision) and BigDecimal (floating point of any precision ).

These two classes can realize the numerical value of a person's length at a time.

BigInteger a = BigInteger. valueOf (100); // converts 100 to an integer of the large number type.
You can use the valueOf () method to convert a common value to a large number.

Then, how can we use a large number to calculate the value?

The addition method provided in JAVA is:

BigInteger sum = a.add(b);//c = a + b;
BigInteger ji = a. multiply (B. add (BigInteger. valueOf (2); // ji = a * (B + 2)


Note:

Unlike C ++, java only provides string connections, and the + operator is overloaded. java does not provide the operator overload function, there is no chance for java programmers to write Operator Overloading in their own classes.


Calculate the number of combinations:

c = c.multiply(BigInteger.valueOf(n-i+1).divide(BigInteger.valueOf(i)));//(n-i+1)/i
/** Description: There are n balls, and m balls can be captured in one hand. To learn how many capturing methods are available, run the "n" command with 0 input. The idea is to apply java. the BigInteger class in the math package uses. multiply (B) generates a * B product */import java. math. bigInteger; import java. util. role; public class Main {public static void main (String [] args) {role in = new role (System. in); int n = 1, m; // evaluate the combination of C (m, n) while (0! = N) {n = in. nextInt (); m = in. nextInt (); BigInteger c = BigInteger. valueOf (1); for (int I = 1; I <= m; I ++) {c = c. multiply (BigInteger. valueOf (n-I + 1 )). divide (BigInteger. valueOf (I);} System. out. println (c );}}}


BigInteger:

// BigInteger add (BigInteger it) // sum // BigInteger subtract (BigInteger it) // calculate the difference // BigInteger multiply (BigInteger it) // accumulate // BigInteger divide (BigInteger it) // evaluate the quotient // BigInteger mod (BigInteger it) // evaluate the modulo // int compareTo (BigInteger it) // it is used to determine the size of two large numbers. The large number is equal to the it of the other. The return value is 0, less than, and negative. The return value is greater than the return value of positive/static BigInteger. valueOf (long x) static BigInteger valueOf (long xx) // the return value is a big integer of xx.



BigDecimal:

BigDecimal add (BigDecimal it) // sum BigDecimal subtract (BigDecimal it) // evaluate BigDecimal multiply (BigDecimal it) // evaluate the product BigDecimal mod (BigDecimal it) // evaluate the modulo

Exceptions:

BigDecimal divide (BigDecimal it RoundingMode mode) 5.0 // The rounding method must be provided for calculation. For other Rounding Methods, see the API documentation static BigDecimal valueOf (long x) // The return value is equal to the large real number static BigDecimal valueOf (long x, int k) of x. // the return value is a large real number of x/(k power of 10 ).






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.