I'm debugging a "big" program in Arduino. At some point it started to throw strange results, so I followed it to an operation that shouldn't be giving negative numbers. The result should be 248,625 and as it is stored in an int variable (giving me the same -79). I've already tried casting to int and applying ceil, floor, without success. Any help will be appreciated.
Debug prints:
B -> (255*195)/200+0 = -79
The code:
// the setup routine runs once when you press reset:
void setup() {
Serial.begin(115200);
}
// the loop routine runs over and over again forever:
void loop() {
int _num_steps = 200;
int _current_step = 195;
int _delta_B = 255;
int currentValues_B = 0;
Serial.print(" B -> ");
Serial.print("(");
Serial.print(_delta_B);
Serial.print("*");
Serial.print(_current_step);
Serial.print(")/");
Serial.print(_num_steps);
Serial.print("+");
Serial.print(currentValues_B);
Serial.print(" = ");
Serial.println(((_delta_B*_current_step) / _num_steps) + currentValues_B);
while(true){}
}