1

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.

dda
1,5951 gold badge12 silver badges17 bronze badges
asked Nov 27, 2017 at 20:55
5
  • Are you trying to encode a bool array into an integer? What whilecycle are you talking about? Commented Nov 27, 2017 at 21:13
  • Your code doesn't compile. Some variables are missing. Commented Nov 27, 2017 at 21:16
  • yes of course it won't compile.. this is only a part of evolutionary algoritm. Commented Nov 27, 2017 at 21:29
  • while is used in main....imagine it as a while and inside of that is this Fitness function Commented Nov 27, 2017 at 21:32
  • 4
    Imagine doesn't work. Post a complete, minimal, working sketch. We can't speculate on unseen code. Commented Nov 27, 2017 at 21:55

2 Answers 2

0

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.

answered Nov 27, 2017 at 21:55
4
  • 1
    Where did you see that pow(2, 3) gives 7? My Arduino tells me (int) pow(2, 3) == 8. Commented Nov 27, 2017 at 22:04
  • 1
    Oh, wait, you're right. It gives 8 if 3 is a constant, but 7 if it's a variable. Commented Nov 27, 2017 at 22:17
  • 1
    I'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. Commented Nov 27, 2017 at 22:21
  • 1
    this helped me :)... now it gives me good answers :).. thanks Commented Nov 28, 2017 at 20:58
0

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.

answered Nov 27, 2017 at 21:08
1
  • yes I saw this already. But this doesn't solve my problem Commented Nov 27, 2017 at 21:30

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.