Rounding to next highest number?

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

1. NoKetchGuest

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

2. Mark A. OdellGuest

(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

3. Christopher Benson-ManicaGuest

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
4. Mark A. OdellGuest

"Mark A. Odell" <> wrote in

> (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
5. Thomas StegenGuest

Mark A. Odell wrote:

> "Mark A. Odell" <> wrote in
>>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
6. Mark A. OdellGuest

"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
7. osmiumGuest

Thomas Stegen writes:

> Mark A. Odell wrote:
>
> > "Mark A. Odell" <> wrote in
> >>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
8. Mark McIntyreGuest

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.

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>