var numA = 0.1; var numB = 0.2; alert( numA + numB ); 0.1 + 0.2 = 0.30000000000000004. For the four arithmetic operations of floating-point numbers, almost all programming languages will have similar precision error problems, but in languages such as C++/C#/Java, methods have been encapsulated to avoid precision problems. JavaScript is a weakly typed language, and from the design concept, there is no strict data type for floating-point numbers, so the problem of precision error is particularly prominent. Let's convert 0.1 and 0.2 to binary:
The decimal part of a double-precision floating-point number supports up to 52 bits, so after adding the two together, we get a string of 0.0100110011001100110011001100110011001100110011001100110011001100, which is a binary number truncated due to the decimal limit of the floating-point number. At this time, when we convert it to decimal, it becomes 0.30000000000000004. How to solve it? First, multiply the number by a power of 10 and remove the decimal places to get an integer that can be converted to binary, and then restore it after calculation. /** ** Division function, used to get accurate division results** Note: JavaScript's division results will have errors, which will be more obvious when dividing two floating-point numbers. This function returns a more accurate division result. ** Call: accdiv(arg1,arg2) ** Return value: the exact result of dividing arg1 by arg2 **/ function accdiv(arg1, arg2) { var t1 = 0, t2 = 0, r1, r2; try { t1 = arg1.toString().split(".")[1].length; } catch (e) { } try { t2 = arg2.toString().split(".")[1].length; } catch (e) { } with (Math) { r1 = Number(arg1.toString().replace(".", "")); r2 = Number(arg2.toString().replace(".", "")); return (r1 / r2) * Math.pow(10, t2 - t1); } } /** ** Addition function, used to get accurate addition results** Note: JavaScript addition results will have errors, which will be more obvious when adding two floating point numbers. This function returns a more accurate addition result. ** Call: accAdd(arg1,arg2) ** Return value: the exact result of arg1 plus arg2 **/ function accAdd(arg1, arg2) { var r1, r2, m, c; try { r1 = arg1.toString().split(".")[1].length; } catch (e) { r1 = 0; } try { r2 = arg2.toString().split(".")[1].length; } catch (e) { r2 = 0; } c = Math.abs(r1 - r2); m = Math.pow(10, Math.max(r1, r2)); if (c > 0) { var cm = Math.pow(10, c); if (r1 > r2) { arg1 = Number(arg1.toString().replace(".", "")); arg2 = Number(arg2.toString().replace(".", "")) * cm; } else { arg1 = Number(arg1.toString().replace(".", "")) * cm; arg2 = Number(arg2.toString().replace(".", "")); } } else { arg1 = Number(arg1.toString().replace(".", "")); arg2 = Number(arg2.toString().replace(".", "")); } return (arg1 + arg2) / m; } /** ** Multiplication function, used to get accurate multiplication results** Note: JavaScript's multiplication results will have errors, which will be more obvious when multiplying two floating-point numbers. This function returns a more accurate multiplication result. ** Call: accMul(arg1,arg2) ** Return value: the exact result of multiplying arg1 by arg2 **/ function accMul(arg1, arg2) { var m = 0, s1 = arg1.toString(), s2 = arg2.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(10, m); } The above is the detailed content of js precise calculation. For more information about js precise calculation, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
>>: Installation tutorial of MySQL 5.1 and 5.7 under Linux
mysql set to case insensitive Windows Go to the d...
Table of contents Index Model B+Tree Index select...
This article introduces the command statements fo...
In this article, we will use the libudev library ...
Using the official MySQL image requires some modi...
When using the font-family property in CSS to ref...
Friends who have some basic knowledge of SQL must...
When you feel that there is a problem with MySQL ...
When configuring web.xml for tomcat, servlet is a...
Wildcard categories: %Percent wildcard: indicates...
<br />In HTML language, you can automaticall...
Pure js implements a single-click editable table ...
There are two ways to configure multiple projects...
There is no need to say much about the difference...
Preface Because of project needs, the storage fie...