How do you do exponentiation in C?
pow only works on floating-point numbers (doubles, actually).If you want to take powers of integers, and the base isn''t known to be an exponent of 2, you''ll have to roll your own.. Usually the dumb way is good enough. int power(int base, unsigned int exp) { int i, result = 1; for (i = 0; i < exp; i++) result *= base; return result; }
阅读更多