3 ms·
browser console: (1.005).toFixed(2) (1.005).toFixed(20) reveals the problem. Math.round(1.005 * 100) // wrong In the end these conversion errors are not sol
by raxxorrax 7y ago
browser console:
(1.005).toFixed(2)
(1.005).toFixed(20) reveals the problem.
Math.round(1.005 * 100) // wrong
In the end these conversion errors are not solvable in any language, so you have to "cut off" somewhere. There are different approaches to this.
Wasn't there a case where programmers stole the "wrong" cent and wasn't The Office a persiflage on that?
- vivan 7y agoThat was the main plot of Office Space. https://www.imdb.com/title/tt0151804/ https://www.imdb.com/title/tt0151804/
- davedx 7y ago> In the end these conversion errors are not solvable in any language Not true at all. Ruby has BigDecimal: https://ruby-doc.org/stdlib-2.5.1/libdoc/bigdecimal/rdoc/BigDecimal.html https://ruby-doc.org/stdlib-2.5.1/libdoc/bigdecimal/rdoc/Big... .NET also has a Decimal type. I've worked on salary calculation applications and e-commerce platforms, and found that language choice makes a big difference.
- mark-r 7y agoIt's not that they're not solvable, it's just that you have to go out of your way to use the proper type or library. The default floating point type still has problems.
- daxelrod 7y agoImportant caveat: BigDecimal is arbitrary-precision, not infinite-precision. Using it still requires you to think about the precision of each number you operate on.
- raxxorrax 7y agoWith insolvable i meant just the technical restrictions of floating point numbers. There are of course solutions. But there is just the fact that binary numbers with discrete length can only be mapped to so many numbers. If you use numbers directly in exponential form in vanilla javascript, you already get better results.
- mark-r 7y agoThe story I heard is that the first time this was attempted in real life, the account balance grew so fast that it attracted attention and they were caught.