Velocity Reviews > Rounding to next highest number?

Rounding to next highest number?

NoKetch
Guest
Posts: n/a

 12-15-2003
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

Mark A. Odell
Guest
Posts: n/a

 12-15-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (NoKetch) wrote in
news:(E-Mail Removed):

> 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/b.../2.7.html#ceil

--
- Mark ->
--

Christopher Benson-Manica
Guest
Posts: n/a

 12-15-2003
NoKetch <(E-Mail Removed)> 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.

Mark A. Odell
Guest
Posts: n/a

 12-15-2003
"Mark A. Odell" <(E-Mail Removed)> wrote in

> (E-Mail Removed) (NoKetch) wrote in
> news:(E-Mail Removed):
>
>> 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/b.../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 ->
--

Thomas Stegen
Guest
Posts: n/a

 12-15-2003
Mark A. Odell wrote:

> "Mark A. Odell" <(E-Mail Removed)> wrote in
>>Try ceil() for up, and floor() for down. See:
>>
>>http://www.acm.uiuc.edu/webmonkeys/b.../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.

Mark A. Odell
Guest
Posts: n/a

 12-15-2003
"osmium" <(E-Mail Removed)> wrote in
news:brkqrr\$4grmj\$(E-Mail Removed)-berlin.de:

>> >>Try ceil() for up, and floor() for down. See:
>> >>
>> >>http://www.acm.uiuc.edu/webmonkeys/b.../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 ->
--

osmium
Guest
Posts: n/a

 12-15-2003
Thomas Stegen writes:

> Mark A. Odell wrote:
>
> > "Mark A. Odell" <(E-Mail Removed)> wrote in
> >>Try ceil() for up, and floor() for down. See:
> >>
> >>http://www.acm.uiuc.edu/webmonkeys/b.../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!

Mark McIntyre
Guest
Posts: n/a

 12-15-2003
On 15 Dec 2003 18:11:36 GMT, in comp.lang.c , "Mark A. Odell"
<(E-Mail Removed)> wrote:

>"osmium" <(E-Mail Removed)> wrote in
>news:brkqrr\$4grmj\$(E-Mail Removed)-berlin.de:
>
>>> >>Try ceil() for up, and floor() for down. See:
>>> >>
>>> >>http://www.acm.uiuc.edu/webmonkeys/b.../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>

----== 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 =---