JavaScript floating-point arithmetic correction

Source: Internet
Author: User
Tags pow

As we all know, JavaScript has always had problems with floating-point numbers, such as the 0.2+0.1 result is 0.30000000000000004.

Here is my solution, put the code first:

varCalmath = (function() {    varIsfloat =function(a) {varReg =/\d.\d+/greturnreg.test (a)}varGetfloatdigit =function(a) {varDigit, Len a=a.tostring () digit= A.split (".") Len= digit[1] = = undefined? 0:digit[1].lengthreturnLen}varAllarithmetic =function(type, a, b) {varC, Gfd_a, Gfd_b, Baselen, BasemultivarA =Number (a), B=Number (b)if(Isfloat (a) | |Isfloat (b)) {gfd_a=Getfloatdigit (a) gfd_b=Getfloatdigit (b) Baselen= Gfd_a >= gfd_b?gfd_a:gfd_b Basemulti= Math.pow (10, Baselen) a= Type! = "Add"? Number (a.tostring (). Replace (".", "")): a B= Type! = "Add"? Number (b.tostring (). Replace (".", "")): bif(Type = = "Add") {C= ((A * basemulti + b * basemulti)/basemulti). toFixed (Baselen)}Else if(Type = = "Multi") {C= (A * b)/Math.pow (gfd_a +gfd_b)} Else if(Type = = "Divi") {C= ((A/b) * MATH.POW (Gfd_b-gfd_a)) }        } Else {            if(Type = = "Add") {C= A +B}Else if(Type = = "Multi") {C= A *B}Else if(Type = = "Divi") {C= A/B}}returnC}return{add:function(A, b) {returnAllarithmetic ("Add", A, B)}, sub:function(A, b) {returnAllarithmetic ("Add", A,-b)}, Multi:function(A, b) {returnAllarithmetic ("Multi", A, B)}, Divi:function(A, b) {returnAllarithmetic ("Divi", A, B)} }})()

The idea is:

1. Determine if it is a floating-point number.

2. If it is not a floating-point number, select the general operation method.

3. In the case of floating-point numbers, first take the number of floating-point numbers (that is, the number of decimal places).

4. If the addition and subtraction of floating-point numbers, then compare their digits after the decimal point, take the larger one, for the moment called N, what is the use of this number? is to let our floating-point number into an integer (decimal times 10 of the N-square), and then re-become a floating point number (the result is divided by 10 of the N-square), the integer do add and subtract total OK? Actually not necessarily, in some cases, the floating-point number multiplied by 10 of the time, the numbers will also have a "tail", but the "tail" error is relatively small, so we use Tofixed (n) to dispose of it is OK.

5. If it is the multiplication of floating-point numbers, in fact, the idea is similar to the above, the number of digits after their decimal point is added, the obtained value continues to call him small n bar, small n regardless of him, we here to change the way floating point number into an integer, is directly to remove the decimal point, the reason is just in addition and If you use 10 of the N to let the decimal variable integer, in fact, there may be very small error floating point number, that in addition and subtraction we can have remedial measures, but in multiplication, a point 0.00000000x of decimals appeared, then recovery is impossible. What's the use of our little n here, of course, to change the integers back to decimals.

6. If it is a floating-point division, the idea is basically consistent with multiplication, look at the code to think for yourself.

Console.log (Calmath.add (0.2,0.1))//result is 0.3 oh

JavaScript floating-point arithmetic correction

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.