# Arduino can't multiply?

I have a value I'm taking from a sensor module, which is 2500 when the sensor is facing a light source. This reads to my code and can be saved perfectly fine. When I try to find the weighted average of a set of values (2500*1000+2500*2000+2500*3000 etc), the Arduino comes out with very weird answers. For example

2500*1000=9632

If I just compute 2500*1000 on its own, the answer still comes out the same! The results come out negative if I use int so I switched to using double (just in case the values created were too big), this stops producing negatives as expected but the result is still like it is above.. I have no idea why.. any ideas?

• 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 '16 at 20:12
• No. double × int → double. Actually, “doubles” are just floats on Arduino (32 bit, i.e. single precision). Jan 26 '16 at 20:21
• 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 '16 at 20:22
• Show your code -- it could be how the results or intermediate results are stored. Jan 26 '16 at 20:25
• 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 '16 at 20:36

``````2500L * 1000L == 2500000L
``````2500.0 * 1000.0 == 2500000.0