Arduino calculation problem

fiddler:
Can you please explain to me how an integer has decimal points ?

I think AWOL is making a bit of a jest there. Integers are, like, 1, 2, 3, 4.

I suppose you could say that is 1.0, 2.0, 3.0, 4.0 where the numbers are "to the left of the decimal point". The implied decimal point.

As has been pointed out earlier, you can multiply first and then divide. For example, to express 3/4 as a percent:

3 * 100 / 4 = 75

That works because the expression is evaluated left-to-right.

However (with integers):

3 / 4 * 100 = 0

Because 3/4 is 0, and multiply 0 by 100 and you still get 0.

Multiplying first has its own hazards. eg. using signed 2-byte integers:

3000 * 100 / 4000 = -6

That's because 3000 * 100 is 30000 (0x493E0) which is truncated to 93E0, effectively becoming -6C20 (-27680) because the high-order bit (the sign bit) is set, divided by 4000, giving -6.

Example sketch, for anyone that doesn't believe me:

void setup ()
  {
  Serial.begin (115200);
  int a = 3000 * 100 / 4000;
  Serial.println (a);
  }
  
void loop () {}

Output:

-6