You check your local applicable law to see what rounding mode you need to apply, and apply that to get back to full cents.
-
-
Replying to @andreasdotorg @jxxf and
Note that actually knowing the fractional part (up to a certain precision) allows you to apply the correct rounding mode, unlike what happens when calculating using integers, where truncation always happens.
1 reply 0 retweets 0 likes -
In my experience, "use floating-point, manage the bookkeeping yourself, and pray you don't get unlucky with error propagation" is much more prone to errors than "use a decimal type".
1 reply 0 retweets 1 like -
Until you hit an integer overflow, and things go belly up in pretty dramatic ways.
1 reply 0 retweets 0 likes -
Replying to @andreasdotorg @jxxf and
But my entire point here is that using floats is perfectly fine for money, if you follow a small number of rules, whereas using regexes for HTML parsing is bound to fail due to HTML being a recursive language, and regexes recognizing regular languages.
2 replies 0 retweets 2 likes -
I think that's fair. My point is that it's not necessarily obvious you're getting burned by FPEP until you're in deep, whereas it's usually clear if a regex isn't returning what you think it should. A lot of F500s have lost a lot of money to very silly FP errors!
1 reply 0 retweets 1 like -
I've never heard of any such case. Even if you use floats incorrectly, the accumulated error tends to be small.
1 reply 0 retweets 0 likes -
In financial calculations, it's common to iterate over a large number of periods (e.g., mortgages, bond payments, and so on) or a large number of transactions (e.g., all the payroll checks). Adding or subtracting iteratively easily leads to catastrophic cancellations with FPs.
2 replies 0 retweets 0 likes -
For such calculations, when you store back to an integer in every step, you accumulate a much bigger error in total. I don't see how floats would hurt you there in a worse way.
1 reply 0 retweets 0 likes -
Replying to @andreasdotorg
The alternative is not "use integers"; it's "use the right tool for the job, which is usually your language's Money or BigDecimal type".
1 reply 0 retweets 2 likes
If your language has such a thing, this is sane advice.
-
-
Replying to @andreasdotorg @jxxf
I would add Rational to that list. You can't go wrong with rational numbers.
0 replies 0 retweets 0 likesThanks. Twitter will use this to make your timeline better. UndoUndo
-
Loading seems to be taking a while.
Twitter may be over capacity or experiencing a momentary hiccup. Try again or visit Twitter Status for more information.