Home  >  Article  >  Web Front-end  >  Solution to the loss of precision problem in js addition, subtraction, multiplication and division_javascript skills

Solution to the loss of precision problem in js addition, subtraction, multiplication and division_javascript skills

WBOY
WBOYOriginal
2016-05-16 16:48:101396browse

In JavaScript, when you use decimals for addition, subtraction, multiplication and division, you will find that the results are sometimes followed by a long period of decimals, which complicates the operation and affects the calculation results. I looked up the reasons online and found the following: In JavaScript, there are always many decimal places when calculating data with decimals. This is because the calculation of floating point numbers in JavaScript is based on binary calculations.

Copy code The code is as follows:

/**
* Addition operation to avoid multiple digits and calculation accuracy loss after the decimal point is added to the data.
*
* @param num1 addend 1 | num2 addend 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(10, Math.max(baseNum1, baseNum2));
return (num1 * baseNum num2 * baseNum) / baseNum;
};
/**
* Addition operation to avoid multiple digits and calculation accuracy loss after adding decimal point of data.
*
* @param num1 minuend | num2 minuend
*/
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( 10, Math.max(baseNum1, baseNum2));
precision = (baseNum1 >= baseNum2) ? baseNum1 : baseNum2;
return ((num1 * baseNum - num2 * baseNum) / baseNum).toFixed(precision );
};
/**
* Multiplication operation to avoid multiple digits and loss of calculation accuracy after multiplying data with decimal point.
*
* @param num1 multiplicand | 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(10, baseNum);
};
/**
* Division operation to avoid multiple digits and calculation accuracy loss after data is divided by decimal point.
*
* @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(10, baseNum2 - baseNum1);
}
};
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn