3 ms·
Floating point cannot represent money.
by parfe 16y ago
Floating point cannot represent money.
- btilly 16y agoFloating point cannot represent money. It shouldn't. But you would be amazed how much bad code there is out there. A lot of it just uses floating point arithmetic because it is easy. (Actually for Monte Carlo simulations it isn't even a bad idea. There speed matters over precision.) (Furthermore you get fun issues when you add in the complication of having multiple currencies that vary against each other.)
- tzs 16y agoWhat would you use to represent money?
- binarysoul 16y agowhole numbers, ie cents 100 cents instead of 1.00 dollars
- tzs 16y agoSo basically integers. Floating point can represent integers, and floating point add, subtract, and multiply on integers is exact, so why not use floating point to represent cents? On a 32-bit machine, using native integers limits you to $40 million dollars or less ($20 million or less if you have to use signed integers). Double precision floating point gives you something like 52-bit integers. When dividing, you would have to explicitly use floor, round, or ceil to make the result be an integer.