Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to adjust JavaScript floating point number and operation precision

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article shows you how to adjust JavaScript floating point numbers and operation precision. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

JavaScript has only one number type, Number, and all numbers in Javascript are represented in the IEEE-754 standard format. The precision problem of floating-point numbers is not specific to JavaScript because some decimals represent infinite digits in binary.

Decimal binary 0.1 0.0001 1001 1001 1001... 0.2 0.0011 0011 0011 0011... 0.3 0.0100 1100 1100 1100... 0.4 0.0110 0110 0110 0110... 0.5 0.1 0.6 0.1001 1001 1001 1001...

So, for example, 1.1, its program can not really express' 1.1', but can only achieve a certain degree of accuracy, which is an inevitable precision loss: 1.09999999999999999

The problem is more complicated in JavaScript, here is only some test data in Chrome:

Console.log / / false console.log (1.0-0.8 = = 0.2) / / false console.log (1.0-0.7 = = 0.3) / / false console.log (1.0-0.6 = = 0.4) / / true console.log (1.0-0.5 = = 0.5) / / true console.log (1.00.4 = = 0.6) ) / / true console.log (1. 0-0. 3 = = 0. 7) / / true console.log (1. 0-0. 2 = = 0. 8) / / true console.log (1-0. 1 = = 0. 9) / / true

So how to avoid this kind of non-bug problem of 1.0-0.9! = 0.1? Here is a solution that is widely used at present, which reduces the accuracy of the calculation result before judging the floating point operation result, because it is always automatically rounded in the process of precision reduction:

The precision parameter digits must be between 0 and 20: console.log (parseFloat (1.00.9) .toFixed (10)) / / true console.log (parseFloat (1.00.8) .toFixed (10)) = = 0.2) / / true console.log (parseFloat (1.00.7) .toFixed (10)) = = 0.3 ) / / true console.log (parseFloat (11.0-11.8) .toFixed (10)) = =-0.8) / / true

Write it as a method:

/ / determine whether the values are equal by isEqual tool method: function isEqual (number1, number2, digits) {digits = digits = = undefined? 10: digits; / / the default precision is 10 return number1.toFixed (digits) = = number2.toFixed (digits);} console.log (isEqual (1.0,0.7,0.3)) / / true / / prototype extension, preferring the object-oriented style Number.prototype.isEqual = function (number, digits) {digits = digits = = undefined? 10: digits; / / default precision is 10 return this.toFixed (digits) = = number.toFixed (digits);} console.log (1.00.7) .isEqual (0.3); / / true

Next, let's try the operation of floating point numbers.

Console.log (1.790.12) / / 1.910000000000000001 console.log (2.01-0.12) / / 1.889999999999999997 console.log (1.01-1.3) / / 1.31300000000002 console.log (0.69) / 0.06899999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999

Solution:

/ / addition function, used to get accurate addition results / / description: the addition results of javascript will have errors, which will be more obvious when two floating-point numbers are added together. This function returns a more accurate addition result. / / call: accAdd (arg1,arg2) / / return value: arg1 plus the exact result of arg2 function accAdd (arg1,arg2) {var R1 Try {r1=arg1.toString (). Split (".") [1] .length} catch (e) {r1prag0} try {r2=arg2.toString () .length (".") [1] .length} catch (e) {r2q0} m=Math.pow (10Magnum (r1MagerR2)) return (arg1*m+arg2*m) / m} / add an add method to the Number type, which is more convenient to call. Number.prototype.add = function (arg) {return accAdd (arg,this);} / subtraction function, used to get accurate subtraction results / / description: the addition results of javascript will have errors, which will be more obvious when the two floating point numbers are added together. This function returns a more accurate result of subtraction. / / call: accSub (arg1,arg2) / / return value: the exact result of arg1 minus arg2: function accSub (arg1,arg2) {var R1 r1=arg1.toString (). Split (".) [1] .length} catch (e) {r1mm 0} try {r2=arg2.toString (). Split (". ") [1] .length} catch (e) {r2mm 0} m=Math.pow (10m Math. Max (r 1m r 2)) / / last modify by deeka / / dynamic control precision length n = (R1 > = R2)? R1 arg1*m-arg2*m R2; return ((R1) / m) .tofixed (n);} / / division function, used to get accurate division results / / description: there will be errors in the division results of javascript, which will be more obvious when two floating point numbers are divided. This function returns a more accurate result of division. / / call: accDiv (arg1,arg2) / / return value: the exact result of arg1 divided by arg2 function accDiv (arg1,arg2) {var t1mem0rect r1memr2 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) * pow (10Lines t2melt1) }} / / add a div method to the Number type to make it easier to call. Number.prototype.div = function (arg) {return accDiv (this, arg);} / / multiplication function, used to get accurate multiplication results / / explanation: the multiplication results of javascript will have errors, which will be more obvious when two floating-point numbers are multiplied. This function returns a more accurate multiplication result. / / call: accMul (arg1,arg2) / / return value: the exact result of arg1 multiplied by arg2 function accMul (arg1,arg2) {var mould0.s1roomarg1.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 (10m)} / add a mul method to the Number type to make it easier to call. Number.prototype.mul = function (arg) {return accMul (arg, this);}

/ / verify: console.log (accAdd (1.79,0.12)); / / 1.91 console.log (accSub (2.01,0.12)); / / 1.89 console.log (accDiv (0.69,10)); / / 0.069

Console.log (accMul (1.01,1.3)); / / 1.313

After the transformation, floating-point addition, subtraction, multiplication and division can be carried out happily.

The above content is how to adjust the JavaScript floating point number and operation precision. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report