...

# C++ rounding

Dalziel
12-02-2002, 10:18 PM
Is there a C++ equivalent to Javascript's Math.round?

jkd
12-02-2002, 11:41 PM
Just add 0.5 and typecast to int:

int round(double a) {
return int(a + 0.5);
}

double x = 1.234;
double y = 1.567;

round(x) == 1;
round(y) == 2;

There is probably some round() function in a common file though, like math.h or something, but I don't know about that for sure.

Bosko
12-03-2002, 06:49 PM
There is no round function in math.h,but there are two functions that you can use for this:
double ceil(double x);
Returns the smallest integer value greater than or equal to x.

double floor(double x);
Returns the largest integer value less than or equal to x.

Note that these functions don't round the number,so ceil(.4) would return 1 and floor(.6) 0 .Heres a little function which rounds the number using ceil and floor:

double round(double x)
{
if(x>=0.5){return ceil(x);}else{return floor(x);}
}

Wichetael
12-04-2002, 01:07 PM
Bosko, first of all, have you ever hear of the ?: operator... much cleaner in this case. Secondly your code has a flaw, it only works for number between 0 and 1, if the number for instance were 2.3 it would be bigger than 0.5 and according to your code it would then be rounded up to 3.0 which is of course not right.

But anyway why use such comlex and unoptimized code when you can do it much simpler, do it as jkd suggested. That's they way it has been done for ages and that's how built in round functions work. It really is the best way.

Bosko
12-04-2002, 04:21 PM
Originally posted by Wichetael
Bosko, first of all, have you ever hear of the ?: operator... much cleaner in this case. Secondly your code has a flaw, it only works for number between 0 and 1, if the number for instance were 2.3 it would be bigger than 0.5 and according to your code it would then be rounded up to 3.0 which is of course not right.

But anyway why use such comlex and unoptimized code when you can do it much simpler, do it as jkd suggested. That's they way it has been done for ages and that's how built in round functions work. It really is the best way.

Yes,I have.But with the ? operator the statement must look like this:
expression ? expression2 : expression3
So this wont work here,unless you add one more statement,which would make it as long as the if-statement.
I know that,but the short code was only meant to show how to do this with functions from math.h (as jkd was not sure if a round() function existed in math.h).

Wichetael
12-06-2002, 01:36 PM
Originally posted by Bosko
Yes,I have.But with the ? operator the statement must look like this:
expression ? expression2 : expression3
So this wont work here,unless you add one more statement,which would make it as long as the if-statement.

Well, I just think that this code:
return (x > 0.5) ? ceil(x) : floor(x);
is still a whole lot better looking than what you had...

And it's still wrong of course, you'd need

though I don't know if the frac function exists.

The whole point is moot though since you don't need it.

I know that,but the short code was only meant to show how to do this with functions from math.h (as jkd was not sure if a round() function existed in math.h).

But that was exactly the point, you don't even need the round function since you can easily make one yourself (in fact any round function in any decent implementation would do it the same way), simply take the code jdk supplied, ie

[code]int double(double x) {
return (int)(x + 0.5);
}

and there you have your round function.

Derek777
09-02-2006, 11:36 AM
Jdk's response was good for positive numbers. I usually use:

inline int round(double x)
{
return int(x > 0.0 ? x + 0.5 : x - 0.5);
}

kccarter
10-30-2007, 06:39 AM
Well, I just think that this code:
return (x > 0.5) ? ceil(x) : floor(x);
is still a whole lot better looking than what you had...

And it's still wrong of course, you'd need

though I don't know if the frac function exists.

The whole point is moot though since you don't need it.

But that was exactly the point, you don't even need the round function since you can easily make one yourself (in fact any round function in any decent implementation would do it the same way), simply take the code jdk supplied, ie

[code]int double(double x) {
return (int)(x + 0.5);
}

and there you have your round function.

in this code you want to do the following:

double round(double x) {
return ((x - floor(x)) >= 0.5) ? ceil(x) : floor(x);
}

this way you operate on the the value to the right of the decimal regardless of the value of x.

ralph l mayo
10-30-2007, 10:09 PM
It's in the standard library.

#include <cmath>
// ...
std::cout << round(some_double);

deXo
07-22-2009, 10:24 PM
Bosko, please be smart enough to realize that both jkd and wichetael are way out of your league. jkd posseses an intelligence rate that you will most likely only experience while watching television, and wichetael is the proud owner of the same rate, only his confirms his programming experience.

if(x>=0.5){return ceil(x);}else{return floor(x);}

could be written as

return ((x >= 0.5) ? ceil(x) : floor(x));

I used an unnecessary pair or parentheses, and still it's shorter than your if-else.