JS Subtraction Loss Accuracy problem Solving method _javascript Skills

Source: Internet
Author: User
Tags pow
In JavaScript, when you use decimal to perform a subtraction operation, you will find that the resulting results are sometimes followed by a long fractional fraction, which complicates the operation and affects the results of the calculation. The Internet query for the reasons, roughly as follows: in JavaScript, with decimal data operations, there will always be a number of decimal places. This is because the calculation of floating-point numbers in JavaScript is calculated in 2.
Copy Code code as follows:

/**
* Addition operation, to avoid the data added decimal point after the number of digits and computational precision loss.
*
* @param num1 addends 1 | Num2 Addends 2
*/
function Numadd (NUM1, num2) {
var basenum, BaseNum1, baseNum2;
try {
BASENUM1 = Num1.tostring (). Split (".") [1].length;
catch (e) {
BASENUM1 = 0;
}
try {
baseNum2 = Num2.tostring (). Split (".") [1].length;
catch (e) {
baseNum2 = 0;
}
Basenum = Math.pow (Math.max (BASENUM1, baseNum2));
Return (NUM1 * basenum + num2 * basenum)/basenum;
};
/**
* Addition operation, to avoid the data phase reduction points after the number of digits and computational precision loss.
*
* @param num1 Bing | Num2 meiosis
*/
function Numsub (NUM1, num2) {
var basenum, BaseNum1, baseNum2;
var precision;//precision
try {
BASENUM1 = Num1.tostring (). Split (".") [1].length;
catch (e) {
BASENUM1 = 0;
}
try {
baseNum2 = Num2.tostring (). Split (".") [1].length;
catch (e) {
baseNum2 = 0;
}
Basenum = Math.pow (Math.max (BASENUM1, baseNum2));
Precision = (baseNum1 >= baseNum2)? basenum1:basenum2;
Return ((NUM1 * basenum-num2 * basenum)/basenum). toFixed (precision);
};
/**
* Multiplication operations, to avoid data multiplication decimal point after the number of digits and computational precision loss.
*
* @param num1 by multiplier | Num2 Multiplier
*/
function Nummulti (NUM1, num2) {
var basenum = 0;
try {
Basenum + = Num1.tostring (). Split (".") [1].length;
catch (e) {
}
try {
Basenum + = Num2.tostring (). Split (".") [1].length;
catch (e) {
}
Return number (num1.tostring (). Replace (".", "")) * Number (num2.tostring (). Replace (".", ""))/Math.pow (basenum);
};
/**
* Division operations, to avoid dividing the decimal point after the number of digits and computational precision loss.
*
* @param NUM1 Dividend | NUM2 Divisor
*/
function Numdiv (NUM1, num2) {
var baseNum1 = 0, baseNum2 = 0;
var baseNum3, BaseNum4;
try {
BASENUM1 = Num1.tostring (). Split (".") [1].length;
catch (e) {
BASENUM1 = 0;
}
try {
baseNum2 = Num2.tostring (). Split (".") [1].length;
catch (e) {
baseNum2 = 0;
}
With (Math) {
BASENUM3 = number (num1.tostring (). Replace (".", ""));
BASENUM4 = number (num2.tostring (). Replace (".", ""));
Return (BASENUM3/BASENUM4) * POW (BASENUM2-BASENUM1);
}
};

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.