I have a small problem with conversion from bool array to int. It is working fine until it comes to the 12th cycle of while. I don't know why but this is really weird.
I try to paste here a code where I am doing this conversion.
int* BoolToInt(bool* set) {
int* ret = (int*)malloc(3*sizeof(int));
int sum0 = 0;
int sum1 = 0;
int sum2 = 0;
//bool test[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};
for(int m=0; m<24; m++) {
if(m<8) {
sum0 += (int)set[m] * (pow(2,m));
}
if(m>7 && m<16) {
sum1 += (int)set[m] * (pow(2,(m-8)));
}
if(m>15 && m<24) {
sum2 += (int)set[m] * (pow(2,(m-16)));
}
}
ret[0] = sum0;
ret[1] = sum1;
ret[2] = sum2;
Serial.print("\n ret[0] = ");
Serial.print(ret[0]);
Serial.print("\n ret[1] = ");
Serial.print(ret[1]);
Serial.print("\n ret[2] = ");
Serial.print(ret[2]);
return ret;
}
And this is where I am using this function:
void Fitness(bool** set) {
int maX = 0;
int index = 0;
int* temp;
for(int i=0; i<10; i++) {
int* phenotype = BoolToInt(set[i]);
_fitness[i] = 1000/((phenotype[0] - _colours[0])*(phenotype[0] - _colours[0]) + (phenotype[1] - _colours[1])*(phenotype[1] - _colours[1]) + (phenotype[2] - _colours[2])*(phenotype[2] - _colours[2]) + 1);
Serial.print("\n phenotype[0] = ");
Serial.print(phenotype[0]);
Serial.print("\n phenotype[1] = ");
Serial.print(phenotype[1]);
Serial.print("\n phenotype[2] = ");
Serial.print(phenotype[2]);
free(phenotype);
}
for(int i=0;i<10;i++) {
temp = BoolToInt(set[i]);
if(_fitness[i]>maX) {
maX = _fitness[i];
index = i;
}
}
setColorOnSecond(temp[index]);
free(temp);
}
As seen in the Fitness function, I have some debugging prints to see if everything is fine. My main is in the while cycle. Everything is fine and "ret" from BoolToInt function is the same as "phenotype" in Fitness, which is a good thing and this is what I want. But then the 12th cycle comes and phenotype[1] gives me 1200 as int. And of course everything in ret[] is fine(0-255). Am I doing something wrong?
NOTE: bool* set in BoolToInt is 24 bool array (yes 3 numbers as you can see).
NOTE 2: every previous cycle before 12th cycle is good and the result are equal.
2 Answers 2
Your problem is using pow. It works in floats. It may come as some surprise, but pow(2,3) gives 7 when you put it into an int.
Don't waste time on pow for powers of 2. Just use a bitshift. It's way smaller and faster.
-
1Where did you see that pow(2, 3) gives 7? My Arduino tells me
(int) pow(2, 3) == 8
.Edgar Bonet– Edgar Bonet2017年11月27日 22:04:35 +00:00Commented Nov 27, 2017 at 22:04 -
1Oh, wait, you're right. It gives 8 if
3
is a constant, but 7 if it's a variable.Edgar Bonet– Edgar Bonet2017年11月27日 22:17:13 +00:00Commented Nov 27, 2017 at 22:17 -
1I've seen enough threads on Arduino.cc where someone is shocked that Arduino gets powers of 2 wrong to know that one pretty well. I'm guessing that when it is done with constants the calculation is being optimized away and the compiler is smart enough to put 8 in there.Delta_G– Delta_G2017年11月27日 22:21:04 +00:00Commented Nov 27, 2017 at 22:21
-
1this helped me :)... now it gives me good answers :).. thanksApuna12– Apuna122017年11月28日 20:58:27 +00:00Commented Nov 28, 2017 at 20:58
Let me the first one to say that 1. Boolean to int conversion shouldn't be this complicated and 2. Much simpler ways to do what you are trying to do.
Like this.
If (bool) boolint |= mask; Mask = mask << 1; ...
You get the idea.
-
yes I saw this already. But this doesn't solve my problemApuna12– Apuna122017年11月27日 21:30:22 +00:00Commented Nov 27, 2017 at 21:30
while
cycle are you talking about?