cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
pow() function
pow() function
Jan 27, 2013 at 4:32pm UTC
vrakas
(53)
this is a simple question. the pow function in math.h returns double. is there any problem using it with integer base,exponents like this: value=(int)pow(base,exponent);
I mean this is always accurate?(value is an int variable)
Jan 27, 2013 at 6:49pm UTC
mik2718
(347)
I would use
value = (int)(pow(base,exponent)+0.5);
The reason? casting to int does a truncate.
i.e, 3.9999999999999999 would become 3.
There could be a little inaccuracy in the returned double and if it is slightly under you will get the wrong number
Topic archived. No new replies allowed.