int RowsToEnable[] = {1, 3};
int ArraySize = sizeof(RowsToEnable) / sizeof(int);
enableLEDRows( RowsToEnable, ArraySize);
In the function I am looping over the array
void enableLEDRows(int RowArray[], int RowArraySize)
{
for (int aktSpalte = 0; aktSpalte < RowArraySize; aktSpalte++) {
int intPower = pow(2, (RowArray[aktSpalte]));
Serial.println(RowArray[aktSpalte]);
Serial.println(intPower);
delay(1000);
For the 1st value = 1 --> I am getting power = 2; this is OK because 2 Power 1 = 2
For the 2nd value = 3 --> I am getting power = 7; this is not OK because 2 Power 3 = 8
When I put the values hard coded (instead of the arrayValues), everythis ins fine
From that thread, and considering that you are always wanting powers of 2, you may be better off simply using the method explained there by halley (Integer powers of two are ridiculously easy, since all computations are done in binary (a numbering system based on integer powers of two). If you want 2 to the 4th power, then say (1<<4). It's fast and accurate for integers.)
When floating point numbers are converted to integers, they are TRUNCATED, not rounded, and floating point numbers are not perfectly accurate; pow() returns a floating point number, but because it's floating point, it will be either a tiny bit higher or tiny bit lower than the actual value; when that
The solution is not to use floats when you don't absolutely need them (and when you think you do, pause a moment and consider if you really do). Floating point numbers are undesirable on Arduino platforms - they pull in bulky software implementations of floating point math operations if you do arithmatic with them, a 32-bit floating point number isn't accurate enough for you to ignore that inaccuracy, and it's very easy to introduce bugs when converting them to integers (as you have above). There are correct times to use floats, but this isn't one of them.
(Compare the flash used by your current code vs converting it to what JaBa said)
pow() returns a floating point number, but because it's floating point, it will be either a tiny bit higher or tiny bit lower than the actual value
It MAY, not WILL, be a bit off. The Arduino is perfectly capable of representing 8 exactly as a float. It is the process that pow() uses that results in a value like 7.999998, instead of the expected 8.000000.
When printing 7.999998, the Arduino will print 8, because 7.999998, to 2 decimal places is 8.