10 ms·
Fixed – Fixed-place decimal math library for Go
- speps 8y agoI'm not an expert but a finance library without a choice of rounding modes and using floating point for division and multiplication is a bit strange? Why those decisions?
- tyingq 8y agoIt appears to be using this: https://engineering.shopspring.com/decimal-an-arbitrary-precision-fixed-point-decimal-library-for-go-ff8b31ca7a5f https://engineering.shopspring.com/decimal-an-arbitrary-prec...
- robaho 8y agoNo, original go-trader used it, since quuckfixgo uses it, and during profiling it showed as a significant cost, so I wrote Fixed.Fixed
- chrisseaton 8y agoDespite what everyone says, I've spoken to several different people at several different highly-regarded banks who say that they do, in fact, use floating point for money amounts in some of their systems. What experience do people who work in banks have?
- christophilus 8y agoI worked in the credit union software space for a while, and using floating points for this does lead to trouble. There were several bugs in my time that were caused by using floats and were fixed by using integer math.
- segmondy 8y agoI work in Finance, I don't use float, and I suggest you never do either.
- chrisseaton 8y agoAs a concrete example, someone was running a massive online FFI for predicting the value of derivatives, and they were doing it in the real domain and using doubles on GPUs for monetary values. Where they doing it 'wrong' then? I don't know much about finance myself.
- frabert 8y agoIf you are predicting, chances are you don't care about all the decimals being correct and you'd rather get the result quickly.
- jimktrains2 8y agoPredicting a value is not the same as accounting.
- deleted 8y ago[deleted]
- OskarS 8y agoI think the problem here is that "financial computing" is too broad a term to apply a rule like that blindly. If you're making products like point-of-sale systems, ecommerce applications or account management, then it's basically malpractice to use floating point. The performance edge you get from them is so slight in the overall performance of the system and the damage bugs cause can be catastrophic. And it's very easy to get them wrong. It's pretty trivial in most languages to swap in decimal and/or rational types, so NOT doing so is just irresponsible. However, if you're doing things like high-frequency trading systems or machine learning stuff with massive amounts of data, then performance really does matter, and using floating point is entirely reasonable. If you're making systems like that, you're hopefully aware of the limitations of floating point and know how to use them safely.
- dragonwriter 8y agoPredicting is inherently approximate; accounting needs to be exact. They may use similar units, but they are different kinds of problems.
- mratsim 8y agoI used COBOL in a well known global American bank.
- atemerev 8y agoWe do use FP numbers for performance reasons occasionally, but we are well aware of their limitations, and when to normalize. Running option trading models with BigDecimal is unproductive. However, when the calculations are done, we store the results back as decimal representation.
- 75dvtwin 8y agoIn complex billing systems (that are also often responsible for computing non-trivial taxes), using fixed point math with configurable rounding rules -- is a must. There are requirement time discussions/rules that indicate if a computation has a rounding rule or not (and it is normally driven by whether a calculation involves division, or if a computation must match an amount in a different currency). Dividing amounts, as example, always comes about when there are mid-cycle cancelation/onboardings to a service. Currency translations cause that as well.
- nemetroid 8y agoI'm a bit surprised to see floating point being used for multiplication, division etc. I would have expected to see this implemented with integers as well (with each operation changing the position of the point).
- nickcw 8y agoFloating point multiplication can be faster than integer multiplication on modern CPUs. Check out this thread for lots of info! https://stackoverflow.com/q/2550281/164234 https://stackoverflow.com/q/2550281/164234
- dagss 8y agoWhen people consider float vs int, performance is seldom a relevant factor. You have to ask, what do you even mean if you use floating point for representing money? If you use floating point, the whole point is that you want the behaviour 1e100 = 1e100 + 1 This is exactly what you want for many analysis and decision making problems, but it is a dangerous default.
- tapirl 8y agoNo sqrt, sin, cos functions?
- OskarS 8y agoProbably not high priority in financial computing.
- tapirl 8y agoNo, this lib is not suitable for financial computing. It might be good for game development.
- OskarS 8y agoThe github page comes right out and says "[it] is ideally suited for high performance trading financial systems". Clearly, that's the intention behind the library. This would be essentially useless in game development (which is my field). In gamedev it's pretty much 32-bit floats all the way down, and very little else. Very small fixed point types are occasionally used in shaders (Cg provides a native 12-bit fixed point type) to represent colors, but a library like this is pretty much pointless.
- pjmlp 8y agoDear moderators it is called Go, from the github description, "fixed decimal place math library for Go".
- pstuart 8y agoGolang is the accepted label used for titles and other communications for disambiguations. Note the domain name that hosts the project.
- pjmlp 8y agoA domain name is not a programming language name, oh look this cool stuff I have done in ISOCPP. You can watch Rob Pike publicly correcting someone at the end of his "Go 2 Draft Specifications" session during Q&A. https://www.youtube.com/watch?v=RIvL2ONhFBI https://www.youtube.com/watch?v=RIvL2ONhFBI
- matt4077 8y agoGolang Golang Golang Golang ...and nothing bad happens. Maybe Bob should have thought of a better name instead of trying to become the least successful prescriptionist since that guy insisting it’s “thou”, not “you”.
- zapzupnz 8y agoOne wonders if the language had been invented at Apple, might the Rob have tried to name it 'Ap'.
- sigzero 8y ago"Go" was such a poor language name choice, it pretty much goes by "Golang" to make it easier to search, etc.
- null_content 8y agoFunny that you bring that up: https://en.wikipedia.org/wiki/Go_(programming_language)#Naming_dispute https://en.wikipedia.org/wiki/Go_(programming_language)#Nami...
- JoeAltmaier 8y agoIt's always struck me as strange than finance folk don't want correct math. They want math that matches the coins in their pocket. If I am owed 2.575% on $3425956.57 for 245 months, why not get the correct sum instead of some rounded monthly sum added up? Its just strange.
- jimktrains2 8y agoHow would you pay such a non-integer sum?
- JoeAltmaier 8y agoWith a bank balance on a computer.
- dmoy 8y agoCan computer bank balances store fractional cents right now? At least before, this was not possible.
- black-tea 8y agoThe question is can computers do arbitrary precision arithmetic. Technically you'd need infinite memory, but if you use fractions to represent amounts, as is correct, then practically speaking, yes you can. One accounting system that uses fractions is ledger[0]. [0] https://www.ledger-cli.org/ https://www.ledger-cli.org/
- JoeAltmaier 8y agoSix or seven digits of precision (like all floating point) is good enough, and far better than exactly 2 digits of decimal. Millions of times better. Fractions do little for interest rates, which are transcendental functions and sometimes even irrational.
- 8y ago
- black-tea 8y agoYou'll never get good performance without using assembly. GMP probably walks all over this. Even my own bignum library probably walks all over it for addition and subtraction.
- imaginenore 8y agoWould you like to show us a benchmark of your bignum library?
- PedroBatista 8y agoWhere can we find your bignum library?
- black-tea 8y agoYou can find it on github along with a hundred others. But you should use GMP. It's better than anything you or I could do alone.
- stephencanon 8y agoIt's not a bignum library. It's just 64b integer fixed-point with a scale of 10^-7. So, perf-wise it's not going to have any issues, but it's also pretty easy to exceed the representable bounds.
- chank 8y agoI work in the accounting space. In the current systems we develop today this would need to be able to handle trillions(up to 14 places) at least and most likely quadrillions(up to 17 spaces) These amounts happen already due to certain countries with high inflation levels.
- Finster 8y agoAnd in the cryptocurrency space, we need way more than 7 decimal places.
- strainer 8y agoThe ubiquitous float Double has 15 places of accuracy that is secure for summing integers up to that size. I think the core frustration is how values less than one are subject to about 1 million-billionth part of noise, which could be tolerated except that factors like inflation are recorded as fixed decimals. For example, if monthly inflation is standardized as 5 significant digits, then for 0.0012345 - the rounding to 5 sd actually introduced about one hundred-thousandth part of noise to whatever the real value was, but the number is then considered precise. Subsequent encoding of 0.0012345 to a float Double is ten billion times less imprecise than the writing of 0.00123447... to 5 sd was, but it is an unwanted inconsistency that is difficult to securely keep out of the fixed point results, even though they are kept to much lower precision.
- robsanchez1954 8y agowill try it
- robaho 8y agoAs the author of Fixed, I'd like to make a few comments: 1. If you are above 100 billion, you are probably dropping the the 'fractions' and dealing with whole numbers. Most databases don't have their columns configured as Decimal(64,64) for 128 digits. It's not practical. So I would store the amounts above 100 billion in another value - the billions unit. And if you are 100s of billion, I am pretty certain you're not concerned with .0000001 dollars. Most 'terminals/receipts' couldn't show/print a number that high without truncating or the UI/representation would be all messed up. 2. I don't believe the crypto-currency space needs more than 7 places on an exchange. The CME only supports 8 decimal places in their protocol. Most of these issues are handled by reinterpreting the scale & quantity. "The satoshi is currently the smallest unit of the bitcoin currency recorded on the block chain. It is a one hundred millionth of a single bitcoin (0.00000001 BTC)." and since there can only be 21 million BTC, it easily fits - although I would probably change the code to use 8 decimal places for ease of use. 3. I might add more rounding modes. They are trivial, but there are some pretty exotic ones - so implementing them all internally, rather than externally can be problematic. 4. For the gamers that say they need float32 - there are more digits of precision in Fixed that float32 - just change the number of places in the code... if you need less significand and more decimals. 5. I've added integer Mul. It is about 2x faster than using floating point. If you avoid limiting overflow, which it doesn't, it is closer to 10x.
- andreygrehov 8y agoAny ideas on handling Ethereum gas, which is uint256?
- miohtama 8y agoGas is limited by having per block gas usage limit. One block cannot consume more than 8M gas. Uint256 comes from the fact that it is native word size for EVM and that is because of crypto operations.
- robaho 8y agonot really, except that all of them are going to zero, so fixed.Fixed should be sufficient :)
- zerealshadowban 8y agoHypothesis: eventually all programming languages either add a library for fixed-place decimal maths, or revise the language to take care of it. Ada had proper fixed point types in 1983 already, and added additional requirements for the implementation of decimal fixed point types as part of the 1995 language revision. Other languages ... may benefit from its example.