Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## C++ rounding

Is there a C++ equivalent to Javascript's Math.round?

• 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.

• 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);}
}

• 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.

• 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).

• 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:
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
[code[return (frac(x) > 0.5) ? ceil(x) : floor(x);[/code]
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.

• ## A better round function...

Jdk's response was good for positive numbers. I usually use:

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

• Originally Posted by Wichetael
Well, I just think that this code:
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
[code[return (frac(x) > 0.5) ? ceil(x) : floor(x);[/code]
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:

Code:
``` 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.

• It's in the standard library.

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

• 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.

I am but certain that I have written nothing of what you want to hear, but the truth has mysterious ways of deploying itself. Though it could this time be explained by my adrenaline aneurism or my autistic behaviour, I think you should gather all your mental and physical resources and repeatedly attempt to increase thy brain acitivity.

And by the wizzle my mizzle, english is more than far from my primary language, so misspellings and bad grammar is selfexplanatory for those who need not gather mental and physical resources.