Summary of large numerical problems in Java

Source: Internet
Author: User

Large numerical problems:



If the basic integers (for example: int, long) and the precision of the floating-point number are not sufficient for the dog, then it is possible to use the two classes in the Java.math package BigInteger (integers of arbitrary precision) and bigdecimal (floating-point numbers with arbitrary precision).

These two classes can realize the numeric value of one-time-length numbers

BigInteger a = biginteger.valueof (100);//convert 100 to a large number type integer
Use the valueof () method to convert a normal value to a large number

So then, how do you use large numbers to achieve numerical operations?

The addition method available 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 provides only string connections, overloads the + operator, Java does not provide overloaded functionality for operators, and does not give Java programmers the opportunity to write operator overloads in their own classes.


Number of calculated combinations:

c = c.multiply (Biginteger.valueof (n-i+1). Divide (biginteger.valueof (i)));//(n-i+1)/I
/** Description: There are n balls, one hand can grasp M, ask how many crawl way, N to 0 input to end the idea: the application of the Java.math package BigInteger class using a.multiply (b) to derive a*b product */import Java.math.biginteger;import Java.util.scanner;public class Main {public static void main (string[] args) {Scanner in = new S Canner (system.in); int n = 1,m;//The combined number 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)//summation//biginteger subtract (BigInteger it)//differential//biginteger multiply (BigInteger it)//quadrature/ /biginteger Divide (BigInteger it)//Business//biginteger mod (BigInteger it)//Find Model//int CompareTo (BigInteger it)//used to determine the size of two large numbers, The large number is equal to another large number of it, returns 0, is less than, returns a negative, is greater than the return positive//static biginteger.valueof (long x) static BigInteger valueOf (long xx)//A large integer that returns a value equal to XX



BigDecimal:

BigDecimal Add (BigDecimal it)//summation BigDecimal subtract (BigDecimal it)//differential BigDecimal multiply (BigDecimal it)// Quadrature BigDecimal mod (BigDecimal it)//Find a model

Exception:

BigDecimal Divide (BigDecimal it roundingmode mode) 5.0//rounding want to calculate must give rounding method, as for other rounding methods can refer to the API document static BigDecimal ValueOf (long x)///return value equals x large real number static BigDecimal valueOf (long x,int k)//Large real number with return value of x/(10 k-th square)






Summary of large numerical problems in Java

Related Article

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.