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
Preface: Use debugbar to view document code in iet...
Table of contents Experimental environment Instal...
1. Requirements description Display the delete ic...
I wrote a test program before, in which adding and...
I recently used the ssm framework when doing a pr...
Table of contents What is FormData? A practical e...
It is recommended to use the sudo su command to s...
Adding a network interface to the container 1 Run...
I searched for three-level linkage on the Interne...
Simple use of Vue bus Scenario description: Compo...
Preface Relational databases are more likely to b...
Table of contents Preface: System Requirements: I...
Table of contents 1. Introduction 2. Actual Cases...
Table of contents 1. What is syntactic sugar? 2. ...
Moreover, an article website built with a blog pro...