Bill: JS Mathematical calculation accuracy correction

Source: Internet
Author: User
Tags mul

Problem description

If I ask you, 4330.61 times 100 equals how much, I guess you must say to me: "Must be 433061" Ah!
Yes, I am the same answer, come on, let's see what the browser says, like

The browser told me he was wrong.
Theodoros The browser tells me that he is wrong, if the customer to calculate money, the customer does not have the opinion ....
Why? Computer calculation is not as good as my own mental arithmetic!!!!
If you want to know what the reason, you see =>js floating point accuracy problem of the past life?

Solutions
;(function){functionMathService (){This.add=functionA, b) {var c, D, E;try {c = a.tostring (). Split (".") [1].length; }catch (f) {c =0; }try {d = b.tostring (). Split (".") [1].length; }catch (f) {d =0; }return e =Math.pow (10,Math.max (c, D)), (This.mul (A, E) +This.mul (b, E))/E; }This.mul=functionA, b) {var C =0, D = a.tostring (), E = b.tostring ();try {c + = D.split (".") [1].length; }catch (f) {}try {c + = E.split (".") [1].length; }catch (f) {}ReturnNumber (D.replace (".","")) *Number (E.replace (".","")) /Math.pow (, c); }this.sub=functionA, b) {var c, D, E;try {c = a.tostring (). Split (".") [1].length; }catch (f) {c =0; }try {d = b.tostring (). Split (".") [1].length; }catch (f) {d =0; }return e =Math.pow (10,Math.max (c, D)), (This.mul (A, E)-This.mul (b, E))/E; }this.div=functionA, b) {var c, d, E =0, F =0;try {e = a.tostring (). Split (".") [1].length;} catch (g) {} try {f = b.tostring (). Split (".") [1].length;} catch (g) {} return c = number (a.tostring (). Replace (".", "")), d = number (b.tostring (). Replace (".", ""), This.mul (C/D, Math.pow (f-e));}} window.mathservice=New MathService ()}) (window);                   

Come on, use it.

Image.png

Ok! Finish your homework!!!



Little Maple learns humor
Links: https://www.jianshu.com/p/c3374517b976
Source: Pinterest
The copyright of the book is owned by the author, and any form of reprint should be contacted by the author for authorization and attribution.

Bill: JS Mathematical calculation accuracy correction

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.