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


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

1 Answer 1


2500 * 1000 = 9632 is exactly what you expect with 16-bit integers. You should use long integers instead:

2500L * 1000L == 2500000L

Floats work also, but are more expensive:

2500.0 * 1000.0 == 2500000.0

although I do not recommend testing for exact equality of floats (it works here because these numbers are exactly representable).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.