Solution to the multiplication floating point error in JS

Source: Internet
Author: User
Tags mul

A floating point error occurs during the fractional Multiplication operation in JS. You can test the problem as follows:

<Script>
Alert (11*22.9)
</Script>

The result is 251.89999999999998 instead of 251.9.

This problem may be a headache for many people. How can this problem be solved? A solution is provided here.

1,
Copy codeThe Code is as follows:
<Script>
Alert (11*(22.9*10)/10 );
</Script>

The general idea of solving the problem is to first enlarge the factor into an integer and then divide it by the corresponding multiples to get the correct result.
2,
Copy codeThe Code is as follows:
<Script defer>
Number. prototype. rate = function (){
Var oStr = this. toString ();
If (oStr. indexOf (".") =-1)
Return 1;
Else
Return Math. pow (10, parseInt (oStr. length-oStr.indexOf (".")-1 ));
}

Function tran (){
Args = tran. arguments;
Var temp = 1;
For (I = 0; I <args. length; I ++)
Temp * = args [I] * args [I]. rate ();
For (I = 0; I <args. length; I ++)
Temp/= args [I]. rate ();
Return temp
}

Alert (tran (11, 22.9 ));

</Script>

This solution is troublesome, but it gives you a general idea about the actual process of solving this problem.

You can also use the rounding method. In js, you can use Math. round to rounding integers. If you need to implement the exact number of decimal places, you need to write a function.
Copy codeThe Code is as follows:
Function ForDight (Dight, How ){
Dight = Math. round (Dight * Math. pow (10, How)/Math. pow (10, How );
Return Dight;
}

// Division function, used to obtain accurate division results
// Note: the division result of javascript has an error, which is obvious when two floating point numbers are separated. This function returns a more precise division result.
// Call: accDiv (arg1, arg2)
// Return value: the exact result of dividing arg1 by arg2
Function accDiv (arg1, arg2 ){
Var t1 = 0, t2 = 0, r1, r2;
Try {t1 = arg1.toString (). split (".") [1]. length} catch (e ){}
Try {t2 = arg2.toString (). split (".") [1]. length} catch (e ){}
With (Math ){
R1 = Number (arg1.toString (). replace (".",""))
R2 = Number (arg2.toString (). replace (".",""))
Return (r1/r2) * pow (10, t2-t1 );
}
}

// Add a div Method to the Number type to facilitate calling.
Number. prototype. div = function (arg ){
Return accDiv (this, arg );
}

// Multiplication function, used to obtain accurate multiplication results
// Note: there is an error in the javascript multiplication result, which is obvious when two floating point numbers are multiplied. This function returns a more accurate multiplication result.
// Call: accMul (arg1, arg2)
// Return value: the exact result of multiplying arg1 by arg2
Function accMul (arg1, arg2)
{
Var m = 0, s1 = arg1.toString (), s2 = arg2.toString ();
Try {m + = s1.split (".") [1]. length} catch (e ){}
Try {m + = s2.split (".") [1]. length} catch (e ){}
Return Number (s1.replace (".", "") * Number (s2.replace (".", "")/Math. pow (10, m)
}

// Add a mul Method to the Number type to facilitate calling.
Number. prototype. mul = function (arg ){
Return accMul (arg, this );
}

// Addition function, used to obtain accurate addition results
// Note: The addition result of javascript has an error, which is obvious when two floating point numbers are added. This function returns a more accurate addition result.
// Call: accAdd (arg1, arg2)
// Return value: the exact result of adding arg2 to arg1
Function accAdd (arg1, arg2 ){
Var r1, r2, m;
Try {r1 = arg1.toString (). split (".") [1]. length} catch (e) {r1 = 0}
Try {r2 = arg2.toString (). split (".") [1]. length} catch (e) {r2 = 0}
M = Math. pow (10, Math. max (r1, r2 ))
Return (arg1 * m + arg2 * m)/m
}

// Add an add method to the Number type to facilitate calling.
Number. prototype. add = function (arg ){
Return accAdd (arg, this );
}

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.