The BigDecimal of accurate Java computing

Source: Internet
Author: User

The computation in Java is mainly double,float,int,long,bigdecimal

1, float and double major users scientific and engineering calculations , which perform binary floating-point operations, are designed to provide more accurate, fast approximation calculations over a wide range of numerical ranges. However, they do not provide completely accurate results, so they should not be used in situations where precise calculations are required.

eg

Double Double_price1 = 1.03;
Double double_price2 = 0.42;
SYSTEM.OUT.PRINTLN ("The computed value of double:" + (DOUBLE_PRICE1-DOUBLE_PRICE2));

Theoretical value: 0.61

Execution result: Double calculated value: 0.6100000000000001

Comparing theoretical values and execution results, it is found that a double calculation results in an error.

2, the accurate calculation mainly uses: BigDecimal, int or long.

2.1 Disadvantages of using BigDecimal:

A. Compared to the basic type, the calculation of BigDecimal is very inconvenient, need to use some methods of the BigDecimal class, can not directly use the +-*/and other mathematical operators;

B. The efficiency of implementation is slow;

2.2 Disadvantages of using int, long: You need to handle the decimal point yourself.

3, BigDecimal need to pay attention to the problem:

3.1 New BigDecimal (String val) is better than new BigDecimal (double val)

eg

BigDecimal Bigdecimal_price1 = new BigDecimal (1.03);
System.out.println ("bigdecimal_price1=" +bigdecimal_price1);

Execution Result: bigdecimal_price1=1.0300000000000000266453525910037569701671600341796875

Analysis: Because of the construction method used here: The new BigDecimal (double val) Double is affected by the precision, so the constructed BigDecimal object is biased from the theoretical value.

BigDecimal bigdecimal_price3 = new BigDecimal ("1.03");
System.out.println ("bigdecimal_price3=" +bigdecimal_price3);

Execution result: bigdecimal_price3=1.03 is not biased with theoretical expectation.

3.2 When using the Divede () method, you need to pay attention to precision.

Eg1:divide (BigDecimal divisor)

System.out.println (New BigDecimal ("ten"). Divide (New BigDecimal ("3"));

Execution Result: Throw exception

java.lang.arithmeticexception:non-terminating decimal expansion; No exact representable decimal result.

Analysis:

The result of 10/3 is an infinite decimal, exceeding the bigdecimal limit.

Eg2:divide (BigDecimal divisor, int roundingmode)

System.out.println ("0.08/12=" +new BigDecimal ("0.08"). Divide (New BigDecimal ("12"), 8));

Execution Result: Throw exception

java.lang.arithmeticexception:non-terminating decimal expansion; No exact representable decimal result.

System.out.println ("0.08/12=" +new BigDecimal ("0.08"). Divide (New BigDecimal ("12"), 6));

Execution result: 0.08/12=0.01 and expected result error

Why keep 8 decimal places will be abnormal, and retain 6 decimal places can not be abnormal, I because of limited experience, failed to find out the answer, I hope you are not hesitate to enlighten the veteran.

Eg3:divide (BigDecimal divisor, int scale, Roundingmode roundingmode)

System.out.println ("0.08/12=" +new BigDecimal ("0.08"). Divide (New BigDecimal ("n"), 8,bigdecimal.round_half_up));

Execution Result: 0.08/12=0.00666667 is consistent with expected results

The BigDecimal of accurate Java computing

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.