4 ms·
Thanks for the suggestion. And do you know where I could read about the underlying theory? I'd like to implement the most I could from scratch, for learning pu
by myth2018 2y ago
Thanks for the suggestion.
And do you know where I could read about the underlying theory? I'd like to implement the most I could from scratch, for learning purposes.
- Galanwe 2y agoThe underlying theory is primary school math if you stay in a decimal fixed point format. All you have to do is choose the number of decimals you want. Say, half the number of digits of the biggest number that fits your desired size. Half these digits will be for the integral part, the other half for the decimal part. For instance, if you want to store signed fixed point on 32 bits, the highest number in base 10 is 2 billion. That's 9 zeros for you. Say you pick 4 for the decimal part, just multiply your numbers by 10000. So 1 is 10000. You can use all the regular operators you want on these numbers, just remember to x10000 when storing them, and /10000 when extracting from them. You're really just "computing on tenths of thousands" instead of "computing on ones". This is obviously wasteful to do that in a decimal base, but it's also trivial, so unless you cannot afford to lose that many digits, stick with it. When confortable, consider moving to a binary base so that you don't waste as much digits. Note that it is not _stricly_ mandatory to use fixed point numbers when doing financial computations. Floating point can work just fine, it all depends on what kind of number and computation you have to deal with. Typically, the need for fixed point arises when 1) You work with really small and large numbers together, these are the most susceptible to be misrepresented as floating points. The best example is if you need to deal with exchange rates to convert between currencies that have widely different bases. 2) You need to chain a lot of operations, which could compound imprécision. 3) You need to store exact numbers of arbitrary precision (e.g. market data)
- teo_zero 2y ago> You can use all the regular operators you want on these numbers, just remember to x10000 when storing them, and /10000 when extracting from them. ??? You can only use addition and subtraction in this way. As soon as you touch multiplication and division the rules don't hold anymore!