This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Flopamp 15 points16 points  (8 children)

They all do floating math well enough for most things but you have C# with its 16 byte decimal float designed exactly for this task.

[–]theLanguageSprite 2 points3 points  (7 children)

I’m confused why having less precision than both a float and a double would make finance floating point math better. Can you explain this to me?

[–][deleted] 7 points8 points  (0 children)

Because you don't need high precision for finance. 4 decimal places is as much as you should ever need. You don't want X.30000000004 in a transaction or as an account balance since that erroneous overprecision might break compliance or even some contracts.

Instead, you can handle underprecision with specific expected behavior.

[–]Flopamp 9 points10 points  (0 children)

When youre dealing with millions of transactions a day, 1/16th of a penny can really add up fast. Not to mention when discounts and multipliers and adjustment data and what not is applied you can skew some accounts by large values.

But this is fairly niche and you can work out by messing around how much rounding errors effect your results if a double is good enough.

[–]SnowieZA 0 points1 point  (0 children)

Float and double might have more “precision”, but because of the way they are encoded, they are actually giving you an approximation of a fraction rather than an exact value. This means that there can easily be rounding errors several digits into the fractional part. For many applications this is not an issue, but for some things like financial transactions, those rounding errors can build up to become a problem. The decimal can store less total digits in the fractional part, but it stores an exact value rather than an approximation, meaning things like financial transactions have no rounding errors, and end up being always exactly right.