# On the problem of inaccurate and automatic scientific counting for JS mathematical calculation

Source: Internet
Author: User

In JavaScript, mathematical computing is a very big problem,

Due to the error of binary decimal system, the operation precision of floating-point number is not ideal, and some unexpected problems often occur.

and JS automatically convert decimals to scientific counting, but also let some of their own math plugin students pain.

First, floating-point calculation accuracy:

For example: 1.2*3 = 3.599999999 ....

Workaround:

⑴ multiply the floating-point number by 10 N power to an integer, then calculate it, then divide it by the N power of 10 to get the value we want. i.e.: 1.2*3 = 12*3/10 = 3.6; (n is the sum of digits after the decimal point);

As for the multiplication of multiple floating-point numbers, it is not much to say that the algorithm is the same .

Note: This method also has some computational flaws, previously did the scientific calculator seems to have a problem with the algorithm, but the specific too long I can not remember. But this approach is enough to handle general operations.

⑵ shifts the floating-point number to the right, and when the floating-point number is all integer, the decimal point is removed and then calculated. After the results are calculated, the decimal point is shifted to the left by n bits; (n is the sum of the decimal places);

Note: This method of logic and the previous method is actually the same, but out of the back of the division calculation, and instead of string processing, so that the accuracy of the results of some protection (the individual dare not too much believe that JS math calculation, was the nature of the pit to understand ).

Second, the computational results are automated as scientific counting methods

In fact, this is not a bug, but if you want to do string processing of data, there are some pits.

Attached: The scientific counting method is a function of ordinary counting.

Finally, do not know whether to submit the attachment, or upload a previously done JS math plug-in, the feeling is very useful

There are JS subtraction basic operations (guarantee accuracy), numerator, convention number common multiple, mathematical representation of the conversion, take the decimal point, rounding the various methods. If you are interested, you can take a peek.

NET disk Link: Http://pan.baidu.com/s/1bng2Yrd plug-in name: Com.snowActivity.math.js

On the problem of inaccurate and automatic scientific counting for JS mathematical calculation

Related Keywords:

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.