JS Precision Arithmetic

Source: Internet
Author: User
Tags mul

Because JS multiplication and add and subtract operations sometimes have the problem of loss of precision, at the same time, two non-numeric addition will also appear the problem of type conversion, so specifically wrote a arithmetic JS not lost precision version to deal with JS arithmetic.

    /** * Exact addition * * @param {String | Number} Arg1 * @param {String | Number} arg2 * * @returns {number} arg1 + arg2 */Exports.add = function add(arg1, arg2) {        //Digitization        varNUM1 =parsefloat(ARG1);varnum2 =parsefloat(ARG2);varR1, R2, M;Try{r1 = num1.tostring (). Split (".")[1].length; }Catch(e) {r1 =0; }Try{r2 = num2.tostring (). Split (".")[1].length; }Catch(e) {r2 =0; } m =Math. POW (Ten,Math. Max (R1, r2));return(NUM1 * m + num2 * m)/m; };/** * Exact subtraction * * @param {number | String} arg1 * @param {number | String} arg2 * * @returns {number} arg1-arg2 */Exports.sub = function sub(arg1, arg2) {        //Digitization        varNUM1 =parsefloat(ARG1);varnum2 =parsefloat(ARG2);varR1, R2, M, N;Try{r1 = num1.tostring (). Split (".")[1].length; }Catch(e) {r1 =0; }Try{r2 = num2.tostring (). Split (".")[1].length; }Catch(e) {r2 =0; } m =Math. POW (Ten,Math. Max (R1, r2));return((NUM1 * m-num2 * m)/m); };/** * Exact multiplication * * @param {number | String} arg1 * @param {number | String} arg2 * @returns {number} arg1 * arg2s * *Exports.mul = function mul(arg1, arg2) {        //Digitization        varNUM1 =parsefloat(ARG1);varnum2 =parsefloat(ARG2);varm =0, S1 = num1.tostring (), S2 = num2.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 (Ten, m); };/** * Exact Division * * @param {number | String} arg1 * @param {number | String} arg2 * @returns {Number} */Exports.div = function div(arg1, arg2) {        //Digitization        varNUM1 =parsefloat(ARG1);varnum2 =parsefloat(ARG2);varT1 =0, t2 =0, R1, R2;Try{T1 = num1.tostring (). Split (".")[1].length; }Catch(e) {}Try{t2 = num2.tostring (). Split (".")[1].length; }Catch(e) {} R1 = Number(Num1.tostring (). Replace (".","")); r2 = Number(Num2.tostring (). Replace (".",""));return(R1/R2) *Math. POW (Ten, T2-T1); };

Students who need it can get their own.

For more information, please see Zakwu's small station.

JS Precision Arithmetic

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.