karolherbst , Englisch
@karolherbst@chaos.social avatar

The answer to the question of "do I use a floating point arithmetic for currency" was, is and always will be: No

Please just don't do this to yourself.

goedelchen ,

@karolherbst That's why every sane programming language (like COBOL) has a proper fix point decimal type as a built-in primitive type! 😜

avis_jay ,
@avis_jay@squawk.social avatar

@karolherbst but that makes hiding my penny-shaving attack harder...

Nulhomme ,
@Nulhomme@mastodon.social avatar

@karolherbst if i had a penny for every time i had to explain that to other Dev i would be rich by now and that kinda sad.

karolherbst OP ,
@karolherbst@chaos.social avatar

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.

serebit ,
@serebit@floss.social avatar

@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 OP ,
@karolherbst@chaos.social avatar

@serebit That kind depends on preference. Some programming languages e.g. Java has a BigDecimal class which abstracts that for you anyway.

serebit ,
@serebit@floss.social avatar

@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.

karolherbst OP ,
@karolherbst@chaos.social avatar

@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.

darkdragon ,

@karolherbst @serebit Isn't float exactly value + scale?

shironeko ,
@shironeko@fedi.tesaguri.club avatar

@darkdragon @karolherbst @serebit I think the issue with float is it's base-2 but our financial system is base-10

karolherbst OP ,
@karolherbst@chaos.social avatar

@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.

shironeko ,
@shironeko@fedi.tesaguri.club avatar

@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.

darkdragon ,

@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?

darkdragon ,

@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.

karolherbst OP ,
@karolherbst@chaos.social avatar

@darkdragon @shironeko @serebit it's deterministic and you can do something about it though.

But it's also something you can plan ahead of time or verify what the legal requirements are, etc...

The issue with floats mostly is, that it's hard to know that any operation is causing any numeric errors.

richpuchalsky ,
@richpuchalsky@mastodon.social avatar

@karolherbst

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

karolherbst OP ,
@karolherbst@chaos.social avatar

@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...

richpuchalsky ,
@richpuchalsky@mastodon.social avatar

@karolherbst

I certainly understand and agree with "never use them for money" but it's also very bad when scientific calculations are subtly unrepeatable

  • Alle
  • Abonniert
  • Moderiert
  • Favoriten
  • random
  • haupteingang
  • Alle Magazine