By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 947 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

Rounding to next highest number?

P: n/a
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

Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
no*****@aol.com (NoKetch) wrote in
news:20***************************@mb-m04.aol.com:
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 ->
--
Nov 14 '05 #2

P: n/a
NoKetch <no*****@aol.com> 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.
Nov 14 '05 #3

P: n/a
"Mark A. Odell" <no****@embeddedfw.com> wrote in
news:Xn********************************@130.133.1. 4:
no*****@aol.com (NoKetch) wrote in
news:20***************************@mb-m04.aol.com:
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 ->
--
Nov 14 '05 #4

P: n/a
Mark A. Odell wrote:
"Mark A. Odell" <no****@embeddedfw.com> wrote in
news:Xn********************************@130.133.1. 4:
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.

Nov 14 '05 #5

P: n/a
Thomas Stegen writes:
Mark A. Odell wrote:
"Mark A. Odell" <no****@embeddedfw.com> wrote in
news:Xn********************************@130.133.1. 4:
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!
Nov 14 '05 #6

P: n/a
"osmium" <r1********@comcast.net> wrote in
news:br************@ID-179017.news.uni-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 ->
--
Nov 14 '05 #7

P: n/a
On 15 Dec 2003 18:11:36 GMT, in comp.lang.c , "Mark A. Odell"
<no****@embeddedfw.com> wrote:
"osmium" <r1********@comcast.net> wrote in
news:br************@ID-179017.news.uni-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.


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 =---
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.