4 ms·
Native decimal types, if your system has them. Many languages and databases used in financial contexts do.
by lxgr 4mo ago
Native decimal types, if your system has them. Many languages and databases used in financial contexts do.
- Maxatar 4mo agoBut native decimal libraries are almost always floating point. Do people not know what a floating point number is?
- lxgr 4mo agoThat’s alright, the important part here is that they’re decimal, not binary. You don’t want 0.1 + 0.2 to equal 0.300…004.
- Maxatar 4mo agoThere are pretty trivial ways to use binary floating point values that don't result in 0.1 + 0.2 producing 0.30000...4 and it saddens me when this topic comes up and people go to such extreme lengths to recreate a second hand buggy reimplementation of a subset of floating point numbers to do it.
- worik 4mo ago> There are pretty trivial ways to use binary floating point values that don't result in 0.1 + 0.2 producing 0.30000...4 Not across all architectures and operating systems there are not Listen to those who have done this. Use integers for finance.
- Maxatar 4mo agoYes across all architectures and OS's that use IEEE 754 floating points.
- FabHK 4mo agoWhat are those pretty trivial ways? And how are they better than storing 10 cents + 20 cents = 30 cents?
- Maxatar 4mo agoBy not thinking in terms of strict rules and dogmas and instead focusing on the actual problem you want to solve. If you want to guarantee that adding cents together results in an exact value without any loss of precision, and you also want a tiny memory footprint and very high performance, then use a binary floating point to represent cents, just as you would use an int. Also the question of how it's better depends on your use case and my argument is not that one representation is universally better than another, it's that money is used for such a diverse range of use cases that you need to actually understand what you're doing, what the goal is, what the potential issues are etc... in order to pick the right representation for your use case. At my trading firm we have three different Money classes optimized for three different use-cases (with functions to allow interoperability between them). For the use case where the representation needs to use little memory, is fast, and needs to be used to perform complex financial calculations at an enormous scale, then you use binary floating point where a value of 1.0d = 0.000001 dollars. This gives you exact precision when working with cents, and lets you perform all of the usual financial computations that most quantitative applications need to perform with excellent performance. We also have a use case optimized for I/O, where no calculations are expected to be performed but the data will be transmitted over a network or to/from a database etc... If this isn't your domain, maybe you're just writing a GUI application/web app, then by all means use a big decimal or use an integer... I don't know, but it's very sad seeing how many people who are presumably professionals don't take just the bare minimum amount of time to think things through and instead just reason in terms of strict dogmas.
- lxgr 4mo ago> you use binary floating point where a value of 1.0d = 0.000001 dollars Is that actually better than just defining one dollar as 1.0? In my (pretty limited) understanding of floats, this doesn't seem to add any precision or prevent any edge cases.
- ktimespi 4mo agoAren't decimal types BCD coded?