Boost logo

Boost :

From: Rob Stewart (stewart_at_[hidden])
Date: 2004-07-19 11:34:16


From: "Matt Verona" <mattverona_at_[hidden]>
>
> I was reading the Boost documentation, and couldn't find a function to round
> floating point numbers (up or down) to the nearest integer.
>
> #include <math.h>
>
> double round(double number)
> {
> return number < 0.0 ? ceil(number-0.5) : floor(number+0.5);
> }
>
> This round function produces the following output:
>
> round(0.0) = 0
> round(1.49) = 1
> round(1.5) = 2
> round(-1.49) = -1
> round(-1.5) = -2

Your function implements only one of several rounding schemes.
If you named it "round_half_up," or something else unique and
descriptive, you could also have round_to_nearest_even() and
others.

> Does Boost contain a simple rounding function like this?

I don't think so.

-- 
Rob Stewart                           stewart_at_[hidden]
Software Engineer                     http://www.sig.com
Susquehanna International Group, LLP  using std::disclaimer;

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk