Precision of JavaScript floating point operations

Source: Internet
Author: User

Js Code Copy codeThe Code is as follows: <script type = "text/javascript" language = "javascript">
Alert (1/3); // displayed: 0.3333333333333333
Alert (0.09999999 + 0.00000001); // displayed: 0.09999999999999999
Alert (-0.09999999-0.00000001); // pop-up:-0.09999999999999999
Alert (0.012345*0.000001); // displayed: 1.2344999999999999e-8
Alert (0.000001/0.0001); // displayed: 0.009999999999999998
</Script>
<Script type = "text/javascript" language = "javascript">
Alert (1/3); // displayed: 0.3333333333333333
Alert (0.09999999 + 0.00000001); // displayed: 0.09999999999999999
Alert (-0.09999999-0.00000001); // pop-up:-0.09999999999999999
Alert (0.012345*0.000001); // displayed: 1.2344999999999999e-8
Alert (0.000001/0.0001); // displayed: 0.009999999999999998
</Script>
[Code]
In normal calculation, except for the first line (because it cannot be used apart), the rest should get accurate results, from the pop-up results, we found that it was not the correct result we wanted. To solve the problem of inaccuracy in floating-point calculation, before the calculation, we first upgrade the number involved in the calculation (10 to the power of X) to an integer, after the computation is complete, downgrade (the power of X of 0.1 ). Now we collect and paste it here for future use.
Addition
Js Code
[Code]
// 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 );
}
// 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 );
}
Subtraction
Js Code
[Code]
// Note: the subtraction result of javascript has an error, which is obvious when two floating point numbers are added. This function returns a more precise subtraction result.
// Call: accSub (arg1, arg2)
// Return value: the exact result of arg1 minus arg2
Function accSub (arg1, arg2 ){
Return accAdd (arg1,-arg2 );
}
// Add a sub Method to the Number type to facilitate calling.
Number. prototype. sub = function (arg ){
Return accSub (this, arg );
}
// Note: the subtraction result of javascript has an error, which is obvious when two floating point numbers are added. This function returns a more precise subtraction result.
// Call: accSub (arg1, arg2)
// Return value: the exact result of arg1 minus arg2
Function accSub (arg1, arg2 ){
Return accAdd (arg1,-arg2 );
}
// Add a sub Method to the Number type to facilitate calling.
Number. prototype. sub = function (arg ){
Return accSub (this, arg );
}

Multiplication
Js CodeCopy codeThe Code is as follows: // 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 );
}
// 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 );
} Division
Js Code
// 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 );
}
// 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 );
}

Test
Js CodeCopy codeThe Code is as follows: <script type = "text/javascript" language = "javascript">
/*
Alert (0.09999999 + 0.00000001); // displayed: 0.09999999999999999
Alert (-0.09999999-0.00000001); // pop-up:-0.09999999999999999
Alert (0.012345*0.000001); // displayed: 1.2344999999999999e-8
Alert (0.000001/0.0001); // displayed: 0.009999999999999998
*/
Alert (0.09999999.add (0.00000001); // displayed: 0.1
Alert (-0.09999999.sub (0.00000001); // pop-up:-0.09999998
Alert (0.012345.mul (0.000001); // displayed: 1.2345e-8
Alert (0.000001.div (0.0001); // displayed: 0.01
</Script>

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.