- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It's quaint how you think there is one thing called "mathematical rounding" that stands in opposition to "bankers rounding". There are more than just two ways to round, and all of them are mathematical, even random rounding.
Admin
FRACTION_NOT_FOUND
Admin
But the rest of the world doesn't have to go along with it. Most countries accept US paper money for some purposes, but most do not accept US coins.
Admin
You're replacing simple multiplication with a branch, or at best a conditional move. Not a terrible thing in and of itself, but in the context of pessimizing a small snippet of code, this is not making things better.
Admin
Admittedly I don't know if Delphi is as smart, but if not then you can assume that anyone using it doesn't care about that level of optimisation anyway.
[1] The only difference is that without optimisation, the sense of the branch and the corresponding "if" / "else" chunks are reversed.
Admin
Admin
Admin
Admin
Admin
Admin
Even after all this, neither rounding function works because they return a double which can only exactly represent 0, 25, 50 or 75 cents.
Admin
Admin
Admin
Yes, it's years later but I would like to add that Delphi (at least since Delphi 2007) has had a SimpleRoundTo function that implements the "Round half away from zero" algorithm. SimpleRoundTo(12.5, 0) = 13, SimpleRoundTo(11.5, 0) = 12.