3 ms·
> I consider running from “amounts as integer” systems these days (but usually unfortunately can’t). In the context of Fintech, how do you otherwise resolve fl
by lawlorino 3mo ago
> I consider running from “amounts as integer” systems these days (but usually unfortunately can’t).
In the context of Fintech, how do you otherwise resolve floating point rounding issues if not representing amounts with integers?
- lxgr 3mo agoNative decimal types, if your system has them. Many languages and databases used in financial contexts do.
- Maxatar 3mo agoBut native decimal libraries are almost always floating point. Do people not know what a floating point number is?
- lxgr 3mo 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 3mo 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 3mo 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 3mo agoYes across all architectures and OS's that use IEEE 754 floating points.
- FabHK 3mo agoWhat are those pretty trivial ways? And how are they better than storing 10 cents + 20 cents = 30 cents?
- Maxatar 3mo 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 3mo 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 3mo agoAren't decimal types BCD coded?
- simpsond 3mo agoAn integer for the value (scaled by number of decimals) and an integer value for the number of decimals. Different systems may use different values, even for the same currency or asset.
- Maxatar 3mo agoWhat exactly do you think a floating point number is?
- lxgr 3mo agoUsually that, but in binary, which causes lots of headaches for financial math.
- Maxatar 3mo agoFor those who don't understand what they're doing, yes. But those people end up having headaches and causing trouble no matter what.
- CyberDildonics 3mo agoSo all the people who write financial software and avoid floats don't know what they're doing? That's would end up being just about everyone. You should teach them, you know so much more than they do.
- dgunay 3mo agoAll of the data we control (in the database, in our apis, etc) is integer cents. When we have to interface with a system that represents money using JSON numbers as dollars.cents, we parse or serialize it into an arbitrary precision decimal type. Hasn't been much of a problem.