Precision Analysis of floating point addition, subtraction, multiplication, and division in javascript

Source: Internet
Author: User

The University major is computer shoes or many or small ones know that computers store and process numbers in binary format. They cannot be precise to floating point numbers, and javascript does not have such a method, so there will be errors during browser computing. For example, if I want to use 3.3/1.1, the answer is 3, no, but we can print out the results. We are surprised that it is 2.9999999999999996. now we want to avoid precision differences, the number that needs to be calculated is upgraded (multiplied by the n power of 10) into an integer that can be precisely recognized by the computer, after the calculation is complete, downgrade (divided by the n power of 10 ). in this way, normal results can be achieved. Basic principle: the number to be calculated is upgraded (multiplied by the n power of 10) to an integer that can be precisely recognized by the computer, and then downgraded (divided by the n power of 10) after calculation ). in this way, normal results can be achieved. How does Javascript solve the problem of "addition" accuracy, the basic principle of subtraction, multiplication, and Division is the same. Multiply the power of 10 to an integer that can be recognized by the computer, and then divide it by the power of 10.. Now let's list a column, for example, var a = 1.3, B = 2.3. Then I print console. log (a + B) =? In the Firefox browser? The result is displayed in Firefox: 3.5999999999999996. This means that the computer has a precision problem with floating point calculation. Now we want to write a js function method to avoid this situation. Basic Principle of the function: first pass in two parameters: arg1 and arg2. First, if both parameters are floating-point numbers, the length after the decimal point of the two parameters is obtained, save them to firstArg and lastArg respectively. Otherwise, if the parameter is not a floating point, assign the value of fisrtArg or lastArg to 0. then the difference differ = Math is obtained. abs (firstArg-lastArg ). (The purpose is to determine whether their decimal places are the same .) Then obtain the maximum n power, for example, m = Math. pow (10, Math. max (firstArg, lastArg); 1. then, if the length of the digits after the decimal point is the same, for example, if the digits after the decimal point 1.3 and the decimal point 2.3 are both 3, their lengths are the same (1 ). then convert the value to an integer and remove the decimal point, for example, arg1 = Number (arg1.toString (). replace (". "," "); arg2 = Number (arg2.toString (). replace (". "," "); then divide them by their n power, for example, return (arg1 + arg2)/m; then return the result. 2. if the length of the digits after the decimal point is inconsistent, obtain the power of n times through their difference differ, for example, var dm = Math. pow (10, differ); then judge if firstArg> lastArg then arg1 = Number (arg1.toString (). replace (". "," "); arg2 = Number (arg2.toString (). replace (". "," ") * dm; otherwise, arg1 = Number (arg1.toString (). replace (". "," ") * dm; arg2 = Number (arg2.toString (). replace (". "," "); for example: var a = 1.31, B = 2.3; then the firstArg length is 2 lastArg length is 1; because 2> 1 then arg1 = 131; arg2 = 2 3 * dm (above: dm = Math. pow (10, differ); the final calculation is as follows: return (arg1 + arg2)/m --> (arg1 + arg2)/m ---> (131 + 23*10) /Math. pow (10, Math. max (firstArg, lastArg); --> (131 + 230)/Math. pow (361/100) --> 3.61 = 1.31; the result is consistent with that of 2.3 +. The same is true for firstArg <lastArg. I will not talk about it here. To illustrate the problem in more detail, let's take a look at the flowchart:

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.