JavaScript floating-point number and operation accuracy Adjustment _javascript skill

Source: Internet
Author: User
Tags numeric mul pow

JavaScript has only one numeric type number, and all numbers in JavaScript are represented in the IEEE-754 standard format. The problem with the precision of floating-point numbers is not JavaScript-specific, because some decimals are infinite in binary representations of digits.

Decimal binary
0.1 0.0001 1001 1001 1001 ...
0.2 0.0011 0011 0011 0011 ...
0.3 0.0100 1100 1100 1100 ...
0.4 0.0110 0110 0110 0110 ...
0.5 0.1
0.6 0.1001 1001 1001 1001 ...

So for example 1.1, the program can actually not really represent ' 1.1′, but only to a certain extent accurate, which is unavoidable precision loss: 1.09999999999999999

The problem in JavaScript is even more complicated, and here's just some test data in chrome:

Console.log (1.0-0.9 = = 0.1)  //false 
console.log (1.0-0.8 = = 0.2)  //false console.log 
(1.0-0.7 = 0.3)  //false 
console.log (1.0-0.6 = = 0.4)  //true console.log 
(1.0-0.5 = = 0.5) 
//true Console.log (1.0-0.4 = = 0.6)  //true 
console.log (1.0-0.3 = = 0.7)  //true 
console.log (1.0-0.2 = 0.8 )  //true 
console.log (1.0-0.1 = = 0.9)  //true 

So how do you avoid this type of 1.0-0.9!= 0.1 bug-type problem? The following is a more current solution, which reduces the precision of the results before judging the results of floating-point operations, because the process of narrowing the precision will always be rounded automatically:

(1.0-0.9). toFixed (digits)//toFixed () precision parameter digits must be between 0 and 20 
console.log ((parsefloat). 1.0-0.9 (10)) = = = 0.1)  //true 
console.log (parsefloat (1.0-0.8) toFixed () = = 0.2)  //true Console.log 
( Parsefloat ((1.0-0.7). toFixed ()) = = 0.3)  //true 
Console.log (parsefloat ((11.0-11.8). toFixed (10)) = = =- 0.8)  

Write a method:

The IsEqual tool method is used to determine whether numeric values are equal 
to function isequal (number1, number2, digits) {digits = digits = undefined 
 ? 10:digits; The default precision is 
 number1.tofixed (digits) = = = Number2.tofixed (digits) 
; 
Console.log (IsEqual (1.0-0.7, 0.3)); True 
//prototype extension way, prefer object-oriented style 
Number.prototype.isEqual = function (number, digits) { 
 digits = digits = = Undefined? 10:digits; The default precision is 
 this.tofixed (digits) = = = Number.tofixed (digits) 
; 
Console.log ((1.0-0.7). IsEqual (0.3)); True 

Next, let's try the floating-point numbers,

Console.log (1.79+0.12)//1.9100000000000001 
console.log (2.01-0.12)  //1.8899999999999997 
Console.log (1.01*1.3)  //1.3130000000000002 
console.log (0.69/10)   //0.06899999999999999 

Solution:

/addition function, which is used to get the exact addition result//description: JavaScript additive result will have error, it will be more obvious when two floating-point numbers are added. 
This function returns a more precise addition result. 
 Call: Accadd (ARG1,ARG2)//return value: Arg1 plus arg2 exact result 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)///+ Adds an Add method to the number type, which is easier to invoke. 
Number.prototype.add = function (ARG) {return accadd (arg,this); The//subtraction function, which is used to get the exact subtraction result//Description: The addition of JavaScript will have errors, it will be more obvious when the two floating-point numbers are added. 
This function returns a more accurate subtraction result. 
 Call: Accsub (ARG1,ARG2)//return value: Arg1 minus arg2 's exact result function accsub (arg1,arg2) {var r1,r2,m,n; 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)); 
 Last modify by Deeka//dynamic Control precision length n= (R1>=R2) r1:r2; 
Return ((arg1*m-arg2*m)/m). ToFixed (n); } 
The Division function, which is used to get the exact division result//Description: JavaScript division results will have errors, it is more obvious when dividing two floating-point numbers. 
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; 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, which is more convenient to call. 
Number.prototype.div = function (ARG) {return Accdiv (this, ARG); A//multiplication function to get the exact result of multiplication//description: JavaScript multiplication results will have errors, the 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 (10,M)}// 
Adding a Mul method to the number type is more convenient to call. Number.prototype.mul = function (ARG) {returnAccmul (ARG, this); <br>//Verify: Console.log (Accadd (1.79, 0.12)); 1.91 Console.log (Accsub (2.01, 0.12));  1.89 Console.log (Accdiv (0.69, 10));  0.069<br>console.log (Accmul (1.01, 1.3)); 
 1.313

After the transformation, you can be happy to carry out floating point subtraction operation ~ above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.