Java knowledge learned by 16.10.17

Source: Internet
Author: User

1.

Example: What is the value of 3-2.6==0.4? Perhaps at first glance, the feeling is to return true because 3-2.6=0.4,0.4==0.4;

However, the above analysis is wrong in Java.

Because the operation of floating-point numbers is imprecise in Java, 3-2.6=0.39999999999999 can be seen, so it is clear that 3-2.6==0.4 returns false.

The question is, why are floating-point numbers not accurate in Java?

A preliminary understanding is related to the representation of floating-point numbers in a computer.

(using BigDecimal, a parameter is a string type, and a parameter is the difference between a double type)

After questioning the Niang, know: "This real number by an integer or fixed-point number (that is, the mantissa) multiplied by a cardinality (usually 2 in the computer) of the whole power to get, this representation is similar to the base of 10 scientific notation.
"," The problem of precision is thus generated, many of the numbers are not fully accurate in the finite n, we can only use a larger n value to more accurately represent this number

As a result, the expression of floating-point numbers is not the most accurate, then, it is obvious that the results of the operation of floating-point numbers are not inaccurate results.

And the solution to this problem (that is, how can floating point numbers be more accurate?) ) is to use the BigDecimal class.

Note Initialize with a parameter of type string.

Here you'll learn about the BigDecimal class:

There are altogether 4 construction methods of the BigDecimal:

BigDecimal (int) Creates an object with the integer value specified by the parameter.

BigDecimal (double) creates an object with the double value specified by the parameter.

BigDecimal (long) creates an object with a long integer value specified by the parameter.

BigDecimal (String) creates an object that has the numeric value specified by the parameter as a string.

BigDecimal operation does not support +-*/This type of operation it has its own method of operation:

BigDecimal Add (BigDecimal augend) addition operation

BigDecimal Subtract (BigDecimal subtrahend) subtraction operation

BigDecimal Multiply (BigDecimal multiplicand) multiplication operation

BigDecimal Divide (BigDecimal divisor) Division operation "

2.

Re-read the teacher's courseware, found that this stuff is very useful.

Java knowledge learned by 16.10.17

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.