From: Greg Chicares (chicares_at_[hidden])
Date: 2001-07-26 22:59:14
I've uploaded 'round_to.zip' to the files area and solicit comments.
// rounding 1.234 to the nearest 2 decimals returns 1.23
round_to<double> const RoundToNearestHundredth(2, r_to_nearest);
double d = RoundToNearestHundredth(1.234);
This sounds almost too trivial to bother with, but as usual, the
devil's in the details. My work calls for various rounding modes
including unbiased (bankers) rounding. We round so frequently that
profiling showed a naive implementation of rounding was taking up
40% of the run time on a system that's uncomfortably slow.
This may be of interest to others with less exacting requirements:
- It grapples with some problems that other math libraries will
encounter when trying to work with nonconforming compilers.
- It's a problem that C99 can solve more efficiently than standard
C++, due to C99's IEC 60559 support (fesetround() in particular).
- It answers a question that comes up every month in some newsgroup
and is often answered less well than it might be.
I've striven to follow the draft coding guidelines throughout, and
found that surprisingly easy, but maybe I overlooked something.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk