9

I've scoured the LANGUAGE REFERENCE in the Arduino web-site, and I can't find a non-Float equivalent to pow() I've got to be missing something big, but for the life of me, I'm stumped! I found pow() in the FUNCTIONS column under the Math heading (like I would expect), but it says that both parameters, [base] and [exponent] are both (float). And there are only six other entries under the Math heading; none of them seem to be seem to be an integer version. All I want to do is generate the powers of 2 using the exponents from 0 to 10. Like 2^0=1 then 2^1=2 then 2^2=4 then 2^3=8 then 2^4=16 then 2^5=32 then 2^6=64 then 2^7=128 then 2^8=256 then 2^9=512 then 2^10 is 1024

Is using floats the only way I can do this? I'm starting to feel like I'm at odds with reality, and have actually counted my medication, but I'm right where I should be. Let me apologize in advance for this egregious oversight that I've wasted your time with, but I've gone through all 9 pages of tags and have done ever search I could think of. I'll admit that I haven't spent all THAT much time, but I was sure this was only going to be like a five minute thing!

8

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available.

Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally shifts the digits of the binary representation of the number (i.e., bits) leftward. In base two, shifting bits one place to the left is the same as multiplying by 2, just as in base 10 shifting digits one place to the left is the same as multiplying by 10. For a full explanation of the left shift operator in C++, see this answer on Stack Overflow.

It is important to note that left shifting can lose information; bits shifted off the end are lost. Because you need powers of 2 up to 10, you are safe when working with signed integers, which have a max value of 2^15-1 on Arduino Uno.

Given those caveats, here's a function to calculate powers of two within these constraints. This is very fast code because the left shift operation is a very low level operation, and no multiplication is actually performed.

int pow2(int p){
    return 1 << p;
}
  • Error: It can go up to 2^32 - 1 if you use an unsigned long. – Dat Ha Nov 14 '16 at 2:31
  • @DatHa thanks, I seemed to have lost the word "signed" during editing. Fixed. – Jason Clark Nov 14 '16 at 2:33
  • 1
    It can go past 2^32 - 1 if you use an implementation of arbitrary precision integer arithmetic – Dat Han Bag Nov 14 '16 at 2:34
  • I'd like, specifically to know why the results of an integer conversion on the results of pow() DOES NOT work for powers of 2. For me, pow(2,3) returns 8.00, but while int(8.00) returns 8, int(pow(2,3)) returns 7! – KDM Jan 29 '18 at 22:23
1

It works with int, double, long and float. unsigned long and unsigned int should also work. You are not required to use ONLY floats.

Hope it helped!

  • The reason the answer above answer works is because the set of real numbers (which contain floats) contains the set of integers – Dat Han Bag Nov 14 '16 at 2:28
  • @DatHanBag: And more importantly, every 32-bit integer is exactly representable by a double. Actually, since IEEE floating point is based on a binary mantissa/exponent representation, every power of 2 should be exactly representable even beyond 2^53 (the point where double can't represent every arbitrary integer, an 1 unit in the last place of the mantissa is greater than 1.0). – Peter Cordes Nov 14 '16 at 2:41
  • @PeterCordes Yes I did know that. Maybe I should have said "bounded sets" when referring to float and integer sets for arduino in my comment about the answer of – Dat Han Bag Nov 14 '16 at 2:44
  • 4
    This is a somewhat valid answer to the general question of using pow() for integers, but AFAICT arduino doesn't even have hardware floating point, so it's a terrible answer. An integer pow() implementation like this one that runs in log2(n) time multiplying and adding to accumulate a result would almost certainly probably perform better, and failing to mention that bit shifts work for powers of 2 just makes this a terrible answer to this question. – Peter Cordes Nov 14 '16 at 2:45
  • 1
    @PeterCordes "so it's a terrible answer". -agreed that it is kind of a low quality simplistic answer. pow() is certainly computable in log2(n)- the simple method learned in school (multiply number by itself to the power is not so efficient). You can do it better with a Fourier transform for really big integers -for example. But maybe the OP will accept and like it. – Dat Han Bag Nov 14 '16 at 2:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy