pow
returns a double, and with doubles you must always worry about precision. The product number * pow(...)
may not return exactly 1. It could be 0.99 or something else that's almost 1 but not quite. When this value gets assigned back to the unsigned integer number
, it would get demoted to become an int, and rounded down to 0.
To get around this, you can always implement your own pow
function with integers. I'd recommend implementing with longs, though, because your integers can overflow fast (e.g. base = 50, exponent = 6 easily exceeds 32-bits typically allotted to ints).
See answers to this SO question for more details on the pow
issue.
Wait, what's precision?
Many numbers cannot be expressed with a finite binary representation. The float
type is one such finite binary representation. A float can only store a certain number of bits of information (which you can think of as precision), so any bits that don't fit are discarded. This loss of information is why floating point numbers can have errors like these. You can read more about this here.