Seeking Boost round() function

I was reading the Boost documentation, and couldn't find a function to round floating point numbers (up or down) to the nearest integer. I have my own implementation of this algorithm as follows: #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 Does Boost contain a simple rounding function like this? Thanks in advance, ~Matt Verona matt_verona@acm.org Houston, Texas USA

From: "Matt Verona" <mattverona@houston.rr.com>
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@sig.com Software Engineer http://www.sig.com Susquehanna International Group, LLP using std::disclaimer;

"Matt Verona" <mattverona@houston.rr.com> wrote in message news:004c01c46c5f$76ee7b10$6601a8c0@adminmd41gg0jc...
I was reading the Boost documentation, and couldn't find a function to round floating point numbers (up or down) to the nearest integer.
I have my own implementation of this algorithm as follows:
#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
Does Boost contain a simple rounding function like this?
Thanks in advance,
Check out Fernando Cacciola's numeric conversion library in CVS. (See libs/numeric/conversion/doc) It contains the definition of a function object type converter parameterized by a source and target type and several traits and policy classes, including one called Float2IntRounder. There are four built in float to int rounding policies: Trunc (default), RoundEven, Ceil and Floor. Jonathan
participants (3)
-
Jonathan Turkanis
-
Matt Verona
-
Rob Stewart