Rounding errors
When doing some exercise in Java I notice that 30.7
may be printed as 30.700000000000003
. This is because floating-point numbers, such as double, are actually stored in binary form. That is, in zeros and ones using only a limited number of numbers. As the number of floating-point numbers is infinite — (in case you’re wondering how infinite?
, think how many floating-point or decimal values fit between the numbers 5
and 6
for instance). All of the floating-point numbers simply cannot be represented by a finite number of zeros and ones. Thus, the computer must place a limit on the accuracy of stored numbers.
Normally, account balances, for instance, are saved as integers such that, say, the value 1
represents one cent.
My site is free of ads and trackers. Was this post helpful to you? Why not
Disqus is great for comments/feedback but I had no idea it came with these gaudy ads.