Timeline for Arduino can't multiply?
Current License: CC BY-SA 3.0
9 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Jan 27, 2016 at 15:59 | comment | added | Gerben | (2500*1000) % 2^16 = 9632 | |
Jan 26, 2016 at 20:36 | comment | added | Edgar Bonet | Makes little sense to me. If sensorValues[j] is int, (sensorValues[j] * 1000) will be computed as an int, no matter what you have in the next multiplication. You should probably show a little more of your code (at least the relevant declarations with the types). Add that to the question, indented by four spaces. | |
Jan 26, 2016 at 20:27 | comment | added | Chris Collins | for (int j = 0; j < NUM_SENSORS; j++) { sensorTotal+=sensorValues[j]; //This works sensorWeighted+=sensorValues[j]*1000*(j+1); //This dosent } problem fixed by casting the j inside (j+1) to a double. | |
Jan 26, 2016 at 20:25 | comment | added | Dave X | Show your code -- it could be how the results or intermediate results are stored. | |
Jan 26, 2016 at 20:22 | comment | added | Chris Collins | Hmm, but then why did this happen: val(double)*weight(double)*(i(int)+1) = the integer result and not the double? Even though it was stored in a variable defined as a double.. Im trying to describe the problem in a way that will help people stuck with the same issue because this had me confused for ages | |
Jan 26, 2016 at 20:21 | comment | added | Edgar Bonet | No. double × int → double. Actually, "doubles" are just floats on Arduino (32 bit, i.e. single precision). | |
Jan 26, 2016 at 20:12 | comment | added | Chris Collins | Fixed it! The problem is that if you multiply a double by an int it turns into an int... I was using doubles for everything but then my weight was using the counter variable, i (e.g. 1000*(i+1)).. This turned the end result back into an int! | |
Jan 26, 2016 at 20:08 | answer | added | Edgar Bonet | timeline score: 4 | |
Jan 26, 2016 at 20:03 | history | asked | Chris Collins | CC BY-SA 3.0 |