Calculation of JS floating-point data type

Source: Internet
Author: User
Tags mul pow return split tostring

JS floating-point data type calculation, the following example:

 
 
  1. Description: JavaScript multiplication results will be error, in two floating-point numbers are more obvious when multiplying. This function returns a more accurate result of the multiplication.
  2. Call: Accmul (ARG1,ARG2)
  3. return value: Arg1 times Arg2 's exact result
  4. function Accmul (ARG1,ARG2)
  5. {
  6. var m=0,s1=arg1.tostring (), s2=arg2.tostring ();
  7. Try{m+=s1.split (".") [1].length}catch (e) {}
  8. Try{m+=s2.split (".") [1].length}catch (e) {}
  9. Return number (S1.replace (".", "")) *number (S2.replace (".", ""))/math.pow (10,m)
  10. }
  11. Adding a Mul method to the number type is more convenient to call.
  12. Number.prototype.mul = function (ARG) {
  13. Return Accmul (ARG, this);
  14. }
  15. 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.
  16. Call: Accdiv (ARG1,ARG2)
  17. Return value: Arg1 divided by Arg2 's exact result
  18. function Accdiv (ARG1,ARG2) {
  19. var t1=0,t2=0,r1,r2;
  20. Try{t1=arg1.tostring (). Split (".") [1].length}catch (e) {}
  21. Try{t2=arg2.tostring (). Split (".") [1].length}catch (e) {}
  22. With (Math) {
  23. R1=number (Arg1.tostring (). Replace (".", ""))
  24. R2=number (Arg2.tostring (). Replace (".", ""))
  25. Return (R1/R2) *pow (10,T2-T1);
  26. }
  27. }
  28. Adding a Div method to the number type is more convenient to call.
  29. Number.prototype.div = function (ARG) {
  30. Return Accdiv (this, ARG);
  31. }
  32. Description: JavaScript addition results will be error, in two floating-point number added when it is more obvious. This function returns a more precise addition result.
  33. Call: Accadd (ARG1,ARG2)
  34. return value: Arg1 plus arg2 's exact result
  35. function Accadd (ARG1,ARG2) {
  36. var r1,r2,m;
  37. Try{r1=arg1.tostring (). Split (".") [1].length}catch (e) {r1=0}
  38. Try{r2=arg2.tostring (). Split (".") [1].length}catch (e) {r2=0}
  39. M=math.pow (10,math.max (R1,R2))
  40. Return (Accmul (arg1,m) +accmul (arg2,m))/M
  41. }
  42. Adding an Add method to the number type is more convenient to call.
  43. Number.prototype.add = function (ARG) {
  44. Return Accadd (Arg,this);
  45. }
  46. Description: JavaScript subtraction results will be error, in two floating-point numbers added when it is more obvious. This function returns a more accurate subtraction result.
  47. Call: Accsub (ARG1,ARG2)
  48. Return value: arg1 The exact result of reducing ARG2
  49. function Accsub (ARG1,ARG2) {
  50. Return Accadd (ARG1,-ARG2);
  51. }
  52. Adding a sub method to the number type is more convenient to call.
  53. Number.prototype.sub = function (ARG) {
  54. Return Accsub (THIS,ARG);
  55. }


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.