I actually have around of 4-5 years of professional experience in finance and at some point I've got added to a project which actually used 64 bit floats and it was just broken in corner cases and the unholy shit we've had to do to fix past mistakes, because nobody was willing to spend the time to fix the entire code base.
@karolherbst Is it better to do operations with a fractional field (e.g. $128.64 becomes 128 in one var and 64 in the other), or just do everything in one field (e.g. $128.64 becomes 12864 in a single var)?
@karolherbst Sure, but in e.g. C, one would need to implement that themselves if they were unable to use any external libraries. I'm just curious about the upsides and downsides to each approach.
@serebit I think that's kinda the wrong way to go about it.
Especially with VAT calculation you'd have to multiply two decimals. if your VAT rate is e.g. 0.19, and prices are also have two digit in the fraction part, you can get away with using an internal format with 4 digits.
But then some lawmaker goes "yeah, but a VAT rate of 0.195 sounds fun" and you are basically screwed, because now you need another digit.
So value + scale is kinda what you want to do instead.
@shironeko@darkdragon@serebit yeah, but also rounding problems. You can't just have fractions of the value being lost due to adding a small and a big float and other corner cases.
Decimal floats are way better with this, but having your custom value + scale layout might allow you to be more explicit about the value ranges you care about.
@karolherbst@darkdragon@serebit good point, though the big number thing can be mitigated with 128 bit float (or 64 bit if you don't handle trillions of a currency) but the base-2 problem is fundamental. Imagine telling your bank you want to transact exactly 1/3 of a base-3 currency, it doesn't matter how many bits their base-10 number system has.
@shironeko@karolherbst@serebit Thanks for the explanation. I didn't realize before that already for common decimals you receive periodic representations in base 2.
Do I see it correctly that every fractional number which can be represented in base 2 can be represented non-periodic in base 10 as well as 10 is a multiple of 2?
@shironeko@karolherbst@serebit and the problem with adding a small and a big number won't go away with value+scale (significand + base10 exponent): When your value doesn't have enough precision to represent the distance between the small and the big number, you will also loose precision.
I deeply hate using floats or doubles for anything, but have to for a scientific database where numbers vary over a large number of orders of magnitude and there are too many billions of records to hold them as decimals
@richpuchalsky yeah one could argue, that as long as the value doesn't end up in accounting/invoicing/whatever customer facing situation it might be fine to use it for analytic approaches or something, but... yeah...