Rounding up or down accordingly...

This is a discussion on Rounding up or down accordingly... within the C Programming forums, part of the General Programming Boards category; how do you do it? I want to round up if the value is .5+, and down if .49 or ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    51

    Rounding up or down accordingly...

    how do you do it?

    I want to round up if the value is .5+, and down if .49 or less.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    preferably in an elegant way...

    short code looks pretty.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    round().

    Unless you're on some compiler that hasn't gotten up to 1999, in which case you have to trunc(number+0.5).

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    how do you use it?

    is there a guide here on the forum?

    thnkx

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you have a C book, or reference of some kind? They're "built-in" functions (library functions, really, but close enough).

    Anyway, they're in <math.h>.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by tabstop View Post
    Anyway, they're in <math.h>.
    Not according to cplusplus.com

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by ISO C99
    7.12.9.6 The round functions
    Synopsis
    1 #include <math.h>
    double round(double x);
    float roundf(float x);
    long double roundl(long double x);
    Description
    2 The round functions round their argument to the nearest integer value in floating-point
    format, rounding halfway cases away from zero, regardless of the current rounding
    direction.
    Returns
    3 The round functions return the rounded integer value.
    And I guess trunc is C99 only, too, it's floor that's in C89.
    Quote Originally Posted by ISO C99
    7.12.9.8 The trunc functions
    Synopsis
    1 #include <math.h>
    double trunc(double x);
    float truncf(float x);
    long double truncl(long double x);
    Description
    2 The trunc functions round their argument to the integer value, in floating format,
    nearest to but no larger in magnitude than the argument.
    Returns
    3 The trunc functions return the truncated integer value.

  8. #8
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    Alternatively, you could use ceil and floor..

    Code:
    double n = 23.45, floorDiff = n - floor(n), ceilDiff = ceil(n) - n;
    
    if(floorDiff < ceilDiff)
         n = floor(n);
    else
         n = ceil(n);
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And if you do this "a lot", you could optimize the calls to ceil/floor by doing:
    Code:
    double n = 23.45, nFloor = floor(n), nCeil = ceil(n);
    double floorDiff = n - nFloor, ceilDiff = nCeil - n;
    
    if(floorDiff < ceilDiff)
         n = nFloor;
    else
         n = nCeil;
    Whether that is worth it or not depends a bit on the actual implementation of floor() and ceil(), as they may not be "true functions", in which case the overhead is much smaller than if they are real functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by IceDane View Post
    Alternatively, you could use ceil and floor..
    True, but wouldn't this be better:
    Code:
    float n = 23.45
    n = (float) ((int) (n+0.5));
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Rounding off a double
    By C-Dummy in forum C Programming
    Replies: 3
    Last Post: 06-23-2008, 11:45 AM
  2. setprecision() - can I count on it rounding or not?
    By major_small in forum C++ Programming
    Replies: 2
    Last Post: 11-23-2005, 01:26 PM
  3. Rounding errors
    By Buckshot in forum C++ Programming
    Replies: 15
    Last Post: 08-16-2005, 09:11 PM
  4. preventing rounding problems with doubles
    By mccoz in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2004, 08:23 AM
  5. Help with rounding a number
    By nickk in forum C Programming
    Replies: 3
    Last Post: 06-02-2004, 11:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21