3 ms·
Yes, every time someone categorically declaims that 'floats and money should never mix', I question whether they've ever met a real accountant. Accountants fre
by smallnamespace 6y ago
Yes, every time someone categorically declaims that 'floats and money should never mix', I question whether they've ever met a real accountant.
Accountants frequently spend all day in Excel. Excel uses floats for all computations (doubles, to be precise) [1].
Now, in all fairness, an accountant and developer's relationship to the numbers is rather different.
For an accountant, the risk of floats blowing up is largely mitigated by the fact that they have a close, intimate relationship with the actual numbers.
The responsible accountant should always deliver numbers they have personally reviewed, while a developer is usually automating a process, generating numbers that have yet to touch a human eye, so there is rather less room for error.
Still, it's simply untrue that one should never use floats for money. Many of the cases where floats would generate bad results are also problematic for simple alternatives. For example, fixnums are simply 'floats that can't float', so you need to be able to guarantee a fixed range ahead of time.
Understanding basic numerical analysis is unavoidable to writing correct code.
[1] https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel https://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft...