In-depth understanding of JavaScript Series: Why 03-0.2 is not equal to 0.1

Source: Internet
Author: User
Tags decimal to binary

51 Otaku Reading, so then update an article.

Today tell me why the 03-0.2 is not equal to 0.1 of the problem.

A bit of a title party flavor, in JavaScript, when you try to add and subtract decimals, sometimes you find that the result is not what we think, like the headline says, why do I use 0.3 to subtract 0.2 without getting 0.1?

When I came across this problem I didn't know exactly where the problem was, but he actually gave a 0.09999999999999998 conclusion.

In fact, the essence of the problem is that the computer on the processing of floating-point numbers, computer computing methods and data representation processing and other content is a large piece of content, And this piece of content as long as the computer professional students in the university has been exposed to and it is likely to be in your final exam papers on this one related topic is to detect you this knowledge point. So today in the circle of friends to send a dynamic, content is not good study of the end is you even why 03-0.2 not equal to 0.1 do not know.

The following is an easy-to-understand perspective on this issue.

1. How do I represent data in my computer?

The computer is a machine and all internal data and instructions are implemented on a 01 basis. That is, the cognitive level of the computer is confined to the binary, and we are accustomed to the decimal counting rules, so this is involved in a variety of system conversion between the problem. The number system has binary, octal, decimal and hexadecimal, of course, as long as you are happy, make a three-in-five system is not not possible, but these several systems we have established and widely used.

2. How is the conversion between the various numbering?

Really do not want to write this question, because can answer this question the answer in your textbook has already had very detailed and very comprehensive explanation, if you impetuous to only want to know the result and oneself do not understand but even the textbook does not want to turn the point, then this article actually also did not need to see.

But in order to be more coherent with the following, here is a brief description of how to convert from decimal to binary binary.

Decimal conversion to binary:

For example, 11 (10) This decimal number, we convert the binary to 11 (10) =8+2+1=11=23+21+20=1000+10+1=1011 (2); If you don't understand the conversion, then you go through the textbook, and here is not the formula or something.

Binary Conversion to decimal:

The binary is converted to decimal, which is a reverse process of converting decimal to binary, or 11 (10) For example. 1011 (2) =1000+10+1=23+21+20=8+2+1=11 (10).

3. What does it matter if0.3-0.2 is not equal to 0.1 and 12 questions?

The first one says that because we are used to non-binary, and the computer can only recognize the binary, so the code that we write in the programming language, in decimal notation, is based on the binary for the underlying computer implementation. So there's definitely a problem with a binary conversion.

When we write code in high-level language in decimal, and finally get a correct still decimal result of this process, the computer in the middle of the two number of system conversion, a decimal to binary, after the conversion of data operations, the operation is completed after the binary data from the binary conversion to decimal, Finally presents us with a result we have expected.

The reason why 0.3-0.2 is not equal to 0.1 appears in this process.

4. So 0.3-0.2 not equal to 0.1 how exactly is it formed?

  Immediately following the above statement, take 0.3-0.2 as an example to complete this computational process in the JavaScript language.

First we hit the 0.3-0.2 expression, we think we can easily get a 0.1 result, however, the facts unexpectedly, the result is to return us a 0.09999999999999998.

The classification of data in computer is roughly the number of symbols and unsigned number, fixed point number and floating point, which can be divided into unsigned fixed-point number, floating-point number and signed fixed-point numbers, floating-point. 0.3 (10) converted to binary data representation is: 0.3 (10) =2-3+2-4+2-7+2-8+......=0.001100110011001100110011001100110011001100110011001101 (2), JavaScript can be viewed using the Tosting method of the object, such as 0.3.toString (2); then we get the converted binary data and normalize the floating-point representation of the mantissa of the symbol bit order in the form of single-precision float or double-precision double. This completes the conversion process from decimal to binary representation.

Then the computer will compute the representation of the binary floating-point number in the form of 0.3 and 0.2, obviously, this result is the same as what we think 0.3-0.2 should be equal to 0.1 may be a bit different, because we in the process of the operation of the data to the order, that is, the order of two number of different words to adjust to the order of the uniform size, the adjustment process is generally The right shift will result in the possibility of a loss of precision in the process of moving right, so it is likely that the results are not what we want.

5.0.3-0.2 must not be equal to 0.1?

First of all, as long as the decimal, or floating-point operation, it is possible to have a loss of precision, but it is possible, it is not necessarily that the accuracy will be impaired. Depends on the operation number in the conversion process and binary numerical operation is not affected by the precision, you such as 0.5-0.25=2-1-2-2=2-2=0.25, this situation will not have the accuracy of damage, so the result is the same as we expected.

6. How to circumvent this problem?

All problems can be solved, provided that we know the reason. Now that we know why 0.3-0.2 is not equal to 0.1, consider the way to solve the problem in the opposite direction. That is to avoid their influence on the accuracy of the system conversion process and the binary numerical operation. For example, I have 3 cents, that is 0.3 yuan, and then bought an ice cream to spend my 2 cents, that is 0.2 yuan, ask how much money left? If it were in the past, we would certainly come to the conclusion that the 0.3-0.2=0.1 would take it for granted. But now that we have realized that this is going to be a problem, how can we avoid him? That's why I have to use yuan as a unit instead of hair or split units? If I use Mao as a unit 3-2=1, with the unit 30-20=10 will certainly not have problems. Because the operation of integers will certainly not have the problem of precision of floating-point arithmetic. So that's the way to solve the problem.

Finish!

I wish you a happy.

In-depth understanding of JavaScript Series: Why 03-0.2 is not equal to 0.1

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.