Summary of JavaScript floating-point number and operation precision Adjustment

Source: Internet
Author: User
Tags mul pow

Summary of JavaScript floating-point number and operation precision Adjustment

JavaScript has only one numeric type number, and all the numbers in JavaScript are represented in the IEEE-754 standard format. The accuracy of floating-point numbers is not specific to JavaScript, because some decimals are in binary notation and the number of bits is infinite.

Source: thewalker|2015-12-02 10:21
Mobile collection Sharing
"Technology salon" AI developer Combat Camp-7 minutes to build 1 custom skills. July 22, we wait for you together!

JavaScript has only one numeric type number, and all the numbers in JavaScript are represented in the IEEE-754 standard format. the accuracy of floating-point numbers is not specific to JavaScript, because some decimals are in binary notation and the number of bits is infinite .

  1. Decimal binary binary
  2. 0.1 0.0001 1001 1001 1001 ...
  3. 0.2 0.0011 0011 0011 0011 ...
  4. 0.3 0.0100 1100 1100 1100 ...
  5. 0.4 0.0110 0110 0110 0110 ...
  6. 0.5 0.1
  7. 0.6 0.1001 1001 1001 1001 ...

So for example, 1.1, its program actually can not really represent ' 1.1′, but only to a certain degree of accuracy, which is unavoidable loss of precision: 1.09999999999999999

The problem in JavaScript is even more complicated, and here are just a few things to test in chrome:

  1. Console.log (1.0-0.9 = = 0.1) //false
  2. Console.log (1.0-0.8 = = 0.2) //false
  3. Console.log (1.0-0.7 = = 0.3) //false
  4. Console.log (1.0-0.6 = = 0.4) //true
  5. Console.log (1.0-0.5 = = 0.5) //true
  6. Console.log (1.0-0.4 = = 0.6) //true
  7. Console.log (1.0-0.3 = = 0.7) //true
  8. Console.log (1.0-0.2 = = 0.8) //true
  9. Console.log (1.0-0.1 = = 0.9) //true

So how to avoid this kind of 1.0-0.9! = 0.1 Non-bug problem occurs? Here is a more current solution, before judging the results of floating-point calculations to reduce the accuracy of the results, because in the process of precision reduction will always automatically rounded:

  1. (1.0-0.9). toFixed (digits) //toFixed () precision parameter digits must be between 0 and 20
  2. Console.log (parsefloat (1.0-0.9) toFixed ()= = = 0.1 ) //true
  3. Console.log (parsefloat (1.0-0.8) toFixed ()= = = 0.2 ) //true
  4. Console.log (parsefloat (1.0-0.7) toFixed ()= = = 0.3 ) //true
  5. Console.log (parsefloat (11.0-11.8) toFixed ()= = =-0.8) //true

Write a method:

  1. Determine if the values are equal by using the IsEqual tool method
  2. function IsEqual (number1, number2, digits) {
  3. digits = digits = = undefined? 10:digits; //default accuracy is ten
  4. return number1.tofixed (digits) = = = Number2.tofixed (digits);
  5. }
  6. Console.log (isequal (1.0-0.7, 0.3)); //true
  7. Prototype extension mode, prefer object-oriented style
  8. Number.prototype.isEqual = function (number, digits) {
  9. digits = digits = = undefined? 10:digits; //default accuracy is ten
  10. return this.tofixed (digits) = = = Number.tofixed (digits);
  11. }
  12. Console.log ((1.0-0.7). IsEqual (0.3)); //true

Next, try floating-point arithmetic,

    1. Console.log (1.79+0.12) //1.9100000000000001
    2. Console.log (2.01-0.12) //1.8899999999999997
    3. Console.log (1.01*1.3) //1.3130000000000002
    4. Console.log (0.69/) //0.06899999999999999

Solution:

  1. An addition function that is used to obtain accurate addition results
  2. Note: the addition of JavaScript will have an error, and it will be more obvious when the two floating-point numbers are added. This function returns a more accurate addition result.
  3. Call: Accadd (ARG1,ARG2)
  4. return value: Arg1 plus arg2 's exact result
  5. function Accadd (ARG1,ARG2) {
  6. var r1,r2,m;
  7. try{r1=arg1.tostring (). Split (".") [1].length} catch (E) {r1=0}
  8. try{r2=arg2.tostring (). Split (".") [1].length} catch (E) {r2=0}
  9. M=math.pow (10,math.max (R1,R2))
  10. return (arg1*m+arg2*m)/M
  11. }
  12. Adding an Add method to the number type makes it more convenient to call.
  13. Number.prototype.add = function (ARG) {
  14. return Accadd (ARG, this);
  15. }
  16. Subtraction function, which is used to get the exact subtraction result
  17. Note: the addition of JavaScript will have an error, and it will be more obvious when the two floating-point numbers are added. This function returns a more accurate subtraction result.
  18. Call: Accsub (ARG1,ARG2)
  19. return value: Arg1 minus arg2 's exact result
  20. function Accsub (ARG1,ARG2) {
  21. var r1,r2,m,n;
  22. try{r1=arg1.tostring (). Split (".") [1].length} catch (E) {r1=0}
  23. try{r2=arg2.tostring (). Split (".") [1].length} catch (E) {r2=0}
  24. M=math.pow (10,math.max (R1,R2));
  25. //last Modify by Deeka
  26. //Dynamic control accuracy length
  27. N= (R1>=R2) r1:r2;
  28. return ((arg1*m-arg2*m)/m). ToFixed (n);
  29. }
  1. The Division function, which is used to get accurate division results.
  2. Description: The result of the division of JavaScript will be error, which will be obvious when dividing two floating-point numbers. This function returns a more accurate division result.
  3. Call: Accdiv (ARG1,ARG2)
  4. Return value: Arg1 divided by the exact result of arg2
  5. function Accdiv (ARG1,ARG2) {
  6. var t1=0,t2=0,r1,r2;
  7. try{t1=arg1.tostring (). Split (".") [1].length} catch (e) {}
  8. try{t2=arg2.tostring (). Split (".") [1].length} catch (e) {}
  9. With (Math) {
  10. R1=number (Arg1.tostring (). Replace (".",""))
  11. R2=number (Arg2.tostring (). Replace (".",""))
  12. return (R1/R2) *pow (10,t2-t1);
  13. }
  14. }
  15. Adding a Div method to the number type makes it easier to call.
  16. Number.prototype.div = function (ARG) {
  17. return Accdiv (this, arg);
  18. }
  19. multiplication function to get the exact multiplication result
  20. Description: JavaScript multiplication results are error-evident when multiplying two floating-point numbers. This function returns a more accurate multiplication result.
  21. Call: Accmul (ARG1,ARG2)
  22. return value: Arg1 times the exact result of arg2
  23. function Accmul (ARG1,ARG2) {
  24. var m=0,s1=arg1.tostring (), s2=arg2.tostring ();
  25. try{m+=s1.split (".") [1].length} catch (e) {}
  26. try{m+=s2.split (".") [1].length} catch (e) {}
  27. return Number (S1.replace (".", "")) *number (s2.replace (".", ""))/math.pow (10,m)
  28. }
  29. Adding a Mul method to the number type makes it more convenient to call.
  30. Number.prototype.mul = function (ARG) {
  31. return Accmul (ARG, this );
  32. }
  33. <br>//verify:
  34. Console.log (Accadd (1.79, 0.12)); //1.91
  35. Console.log (Accsub (2.01, 0.12)); //1.89
  36. Console.log (Accdiv (0.69, 10));   //0.069<br>console.log (Accmul (1.01, 1.3)); 1.313

After the transformation, you can happily carry out the floating point subtraction operation ~

"Editor's recommendation"

    1. 25 free resources that are useful to JavaScript novice programmers
    2. JavaScript modularization and Seajs source analysis
    3. JavaScript design pattern Theory and actual combat: Observer pattern
    4. The problem of JavaScript prototype chain and scope during interview
    5. WordPress.com Open source, discard PHP instead of JavaScript

Summary of JavaScript floating-point number and operation precision Adjustment

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.