How to solve floating-point arithmetic bugs in JS

Source: Internet
Author: User
Tags arithmetic pow split tostring

  This article is mainly on the floating-point operator in JS solution to solve the problem, the need for friends can come to the reference, I hope to help you.

Once the project used to find this code on the Internet before, but under certain conditions Division and addition operations will still appear bugs individuals to this slightly optimized       code as follows://Division function, to get the exact division result// Description: JavaScript division results will be error, the two floating-point numbers are more obvious when dividing. This function returns a more precise division result. Call: Accdiv (ARG1,ARG2)//return value: Arg1 divided by arg2 exact result function Accdiv (arg1, arg2) {    var t1 = 0, t2 = 0, R1, R2; &nbsp ;   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 Accmul ((R1/R2), pow (T2-T1));    }}      code is as follows://multiplication function, to get the exact result of multiplication//description: JavaScript multiplication results will be error, two floating-point numbers will be more obvious when multiplying. This function returns a more accurate result of the multiplication. Call: Accmul (ARG1,ARG2)//return value: Arg1 times Arg2 's exact result 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 (M }      Code is as follows://addition Operation Intermediate solution function Accadd (arg1, arg2) {       var r1, R2, M, C; &nbsp ;   try {r1 = arg1.tostring (). Split (".") [1].length} catch (e) {r1 = 0}     try {r2 = arg2.tostring (). Split (".") [1].length} catch (e) {r2 = 0}       c = Math.Abs (R1-R2);        m = Math.pow (Math.max (R1, r2))        if (C > 0) {   &nbs P       var cm = Math.pow (c);            if (R1 > R2) {               ARG1 = N Umber (Arg1.tostring (). Replace (".", ""));                ARG2 = number (arg2.tostring (). Replace (".", "")) * cm;           }    &nbsP       Else {               ARG1 = number (arg1.tostring (). Replace (" . "," ")) * cm;                ARG2 = number (arg2.tostring (). Replace (".", ""));            {      }        else {   &NB Sp       ARG1 = number (arg1.tostring (). Replace (".", ""));            ARG2 = number (arg2.tostring (). Replace (".", ""));       }        return Accdiv (Arg1 + arg2), m); }   

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.