简体   繁体   中英

JavaScript Integer division resulting in float, is rounding error possible?

I know that examples such as 1.005*100 / 100 produce a slightly inaccurate result (100.49999999999999) in JavaScript, and this is intrinsic to floating point design.

My question is to see whether I can produce a reliable decimal round routine without the use of string manipulation.

Where x is an integer, will x / 100 (int/int=float) ever produce a visual inaccuracy? (ie ..000001 or ..99999 ) An example proving this would be preferable.

If the answer is yes then the only way to reliably move the decimal point towards the left in JavaScript is with string manipulation (ie MDN's Math.round10 ), and to take the modest performance hit involved.

According to this test, there are no inaccuracies from division of integers by integers.

 var multipliers = [10, 100, 1000, 10000, 100000, 1000000] for(var i = 0; i < 100000; i++) { multipliers.forEach(function (mul) { var x = i / mul if(x != x.toFixed(6)) document.write('found one! ' + i + ' / ' + mul + ' = ' + x + '<br/>') }) } document.write('done!')

Nice. This means a robust round-to-decimal routine can be created without string manipulation. :-)

To precise my comments, the fractional parts remain the same only in infinite precision, which you do not have with JS IEEE-754-compliant floating-point arithmetic.

In finite precision you may not be able to represent an integer exactly in floating-point (eg many integers > 2^53). When dividing x by 100 you can actually end up with a number that is not mathematically equal to x / 100 .

eg let x = 1.0e23 In IEEE 754 double precision, x rounds to 99999999999999991611392 Then x / 100 = 999999999999999916113.92

But as this number is not representable in double precision it rounds to 999999999999999868928

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM