Rounding to next highest number?

Discussion in 'C Programming' started by NoKetch, Dec 15, 2003.

  1. NoKetch

    NoKetch Guest

    I have a simple program to output log2(x) but the result is not whole numbers.
    Here is the code:

    ----------------------
    #include <stdio.h>
    #include <math.h>

    int main()
    {
    double x;

    printf("Enter a Number: ");
    scanf( "%lf", &x);

    if ( x >= 0 )
    printf("\nResult: %lf\n", log2(x));

    system("PAUSE");
    return 0;
    }
    ----------------------

    I get results like this:

    Result: 4.321928
    Result: 5.459432

    How do i round these numbers to the next highest whole number so that the
    output would be:

    Result: 5
    Result: 6

    I'm not a very experienced programmer, so the explanation should be fairly
    simple. :)

    Thanks for any tips


    Dave
     
    NoKetch, Dec 15, 2003
    #1
    1. Advertising

  2. (NoKetch) wrote in
    news::

    > How do i round these numbers to the next highest whole number so that
    > the output would be:
    >
    > Result: 5
    > Result: 6
    >
    > I'm not a very experienced programmer, so the explanation should be
    > fairly simple. :)


    Try ceil() for up, and floor() for down. See:

    http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.7.html#ceil

    --
    - Mark ->
    --
     
    Mark A. Odell, Dec 15, 2003
    #2
    1. Advertising

  3. NoKetch <> spoke thus:

    > How do i round these numbers to the next highest whole number so that the
    > output would be:


    Use ceil(), from the math library. #include math.h and do whatever
    may be necessary on your system to link to the math library. Check
    your friendly documentation or man page for more details.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
     
    Christopher Benson-Manica, Dec 15, 2003
    #3
  4. "Mark A. Odell" <> wrote in
    news:Xns945271BBC622CCopyrightMarkOdell@130.133.1.4:

    > (NoKetch) wrote in
    > news::
    >
    >> How do i round these numbers to the next highest whole number so that
    >> the output would be:
    >>
    >> Result: 5
    >> Result: 6
    >>
    >> I'm not a very experienced programmer, so the explanation should be
    >> fairly simple. :)

    >
    > Try ceil() for up, and floor() for down. See:
    >
    > http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.7.html#ceil


    Or you could at 0.5 to the result and then cast to int. E.g.

    (int) (4.732 + 0.5) is 5.

    --
    - Mark ->
    --
     
    Mark A. Odell, Dec 15, 2003
    #4
  5. Mark A. Odell wrote:

    > "Mark A. Odell" <> wrote in
    > news:Xns945271BBC622CCopyrightMarkOdell@130.133.1.4:
    >>Try ceil() for up, and floor() for down. See:
    >>
    >>http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.7.html#ceil

    >
    >
    > Or you could at 0.5 to the result and then cast to int. E.g.
    >
    > (int) (4.732 + 0.5) is 5.


    That depends on if you want to round away from zero or in a positive
    direction. What you have rounds in a positive direction

    --
    Thomas.
     
    Thomas Stegen, Dec 15, 2003
    #5
  6. "osmium" <> wrote in
    news:brkqrr$4grmj$-berlin.de:

    >> >>Try ceil() for up, and floor() for down. See:
    >> >>
    >> >>http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.7.html#ceil
    >> >
    >> >
    >> > Or you could at 0.5 to the result and then cast to int. E.g.
    >> >
    >> > (int) (4.732 + 0.5) is 5.

    >>
    >> That depends on if you want to round away from zero or in a positive
    >> direction. What you have rounds in a positive direction

    >
    > Rather than stew about what happens here, I think you should use ceil().
    > After all that IS what you want to do. Why make some other poor soul
    > figure out what you were up to? Eschew obfuscation!


    I agree. On a small 8-bit micro, I might do this instead of ceil() if I
    hadn't used any FP library functions anywhere else. Ceil() is the better
    choice in most cases I would think.

    --
    - Mark ->
    --
     
    Mark A. Odell, Dec 15, 2003
    #6
  7. NoKetch

    osmium Guest

    Thomas Stegen writes:

    > Mark A. Odell wrote:
    >
    > > "Mark A. Odell" <> wrote in
    > > news:Xns945271BBC622CCopyrightMarkOdell@130.133.1.4:
    > >>Try ceil() for up, and floor() for down. See:
    > >>
    > >>http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.7.html#ceil

    > >
    > >
    > > Or you could at 0.5 to the result and then cast to int. E.g.
    > >
    > > (int) (4.732 + 0.5) is 5.

    >
    > That depends on if you want to round away from zero or in a positive
    > direction. What you have rounds in a positive direction


    Rather than stew about what happens here, I think you should use ceil().
    After all that IS what you want to do. Why make some other poor soul figure
    out what you were up to? Eschew obfuscation!
     
    osmium, Dec 15, 2003
    #7
  8. On 15 Dec 2003 18:11:36 GMT, in comp.lang.c , "Mark A. Odell"
    <> wrote:

    >"osmium" <> wrote in
    >news:brkqrr$4grmj$-berlin.de:
    >
    >>> >>Try ceil() for up, and floor() for down. See:
    >>> >>
    >>> >>http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.7.html#ceil
    >>> >
    >>> >
    >>> > Or you could at 0.5 to the result and then cast to int. E.g.
    >>> >
    >>> > (int) (4.732 + 0.5) is 5.
    >>>
    >>> That depends on if you want to round away from zero or in a positive
    >>> direction. What you have rounds in a positive direction

    >>
    >> Rather than stew about what happens here, I think you should use ceil().
    >> After all that IS what you want to do. Why make some other poor soul
    >> figure out what you were up to? Eschew obfuscation!

    >
    >I agree. On a small 8-bit micro, I might do this instead of ceil() if I
    >hadn't used any FP library functions anywhere else. Ceil() is the better
    >choice in most cases I would think.


    ceil() can lead to unexpected results when reading in data from
    external sources, due to FP inaccuracies. Well I reckon it always
    surprises to see ceil(5.0f) == 6 anyway.

    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>


    ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
     
    Mark McIntyre, Dec 15, 2003
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Deniz Bahar
    Replies:
    2
    Views:
    481
    Andrey Tarasevich
    Mar 9, 2005
  2. Jaspreet

    Second Highest number in an array

    Jaspreet, Sep 23, 2005, in forum: C Programming
    Replies:
    21
    Views:
    1,380
    Keith Thompson
    Feb 1, 2006
  3. sort a number highest to lowest

    , Dec 10, 2006, in forum: C Programming
    Replies:
    17
    Views:
    615
    santosh
    Dec 11, 2006
  4. noydb

    Rounding up to the next 100

    noydb, Jan 21, 2010, in forum: Python
    Replies:
    4
    Views:
    715
  5. Replies:
    5
    Views:
    353
    casevh
    Jan 22, 2010
Loading...

Share This Page