Could someone tell me why ceil() isn't working?

Jul 4, 2011 at 6:44pm
I've got this function

double round (double n)
{
n = n * 10;
ceil(n);
n = n / 10;
return n;
}

It's working except for the ceil(n); part. When I step through it, ceil() does nothing to n. For example, if I pass 12.04195 to it, the first line makes n = 120.4195, ceil() does nothing, and the third line makes n = 12.04159 again. I have #include <cmath> in the header, and pow() is working fine.

Thanks.
Last edited on Jul 4, 2011 at 6:48pm
Jul 4, 2011 at 6:56pm
http://www.cplusplus.com/reference/clibrary/cmath/ceil/

Google is your friend. Basically you need to either use the return value, or save it because that's the result you need.
Jul 4, 2011 at 7:15pm
Seriously webJose, I've been on that page. You think I'd come bother you without having hit the cmath reference and Google?? ;-)

I'm in the Beginners section for a good reason. I was confused about how ceil works. I was thinking it used the variable you pass it as a reference for some reason. But like you said, it returns a value and you need to stick it somewhere. Thanks.

Here's what I have now:

double round (double n)
{
n = n * 10000;
n = ceil(n + .00005);
n = n / 10000;
return n;
}

My intention is to round a number to 4 decimal places.
Last edited on Jul 4, 2011 at 7:21pm
Topic archived. No new replies allowed.