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

Thread: C++ rounding

  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    161
    Thanks
    2
    Thanked 0 Times in 0 Posts

    C++ rounding

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

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Netherlands
    Posts
    217
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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);}
    }

  • #4
    New Coder
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    The Netherlands
    Posts
    217
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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).

  • #6
    New Coder
    Join Date
    Nov 2002
    Location
    Netherlands
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    New to the CF scene
    Join Date
    Aug 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

  • #8
    New to the CF scene
    Join Date
    Oct 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Wichetael View Post
    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.

  • #9
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    It's in the standard library.

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

  • #10
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    When my browser first loaded this page and I read your post, I really didn't care a whole lot as I was but immediately convinced that you were quite ordinary in terms of intelligence, but when I found out that jkd had posted his message before you had posted yours, I began to realize the maginute of your lack of at birth given gifts. Your post was so unnecessary and stupid that it made me euforic about my IQ-result of 144, which a few days ago I thought to be shamefully low compared to certain idols of mine.

    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.
    Last edited by deXo; 07-22-2009 at 09:27 PM.

  • #11
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    WTF? Thread digging an ancient thread to rant about intelligence? Thread closed.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •