Arduino calculation returns 0: integer division and float values

Two integers divided give an integer. 1 / 2 is 0. Make one side a float, for example 1.0 / 2.

What the message means

The compiler uses the types of the numbers, not the type of the variable that receives the answer.

Fix it in this order

  1. Write one number with a decimal point, such as 5.0.
  2. Or cast: (float)a / b.
  3. Store the result in a float variable.
  4. Multiply before dividing when you want to stay in integers, as in a * 100 / b.
  5. Watch for overflow in int, which is 16 bit on an Uno. Use long for big products.

Why it happens

When both numbers are integers, the result is also an integer, and the fraction is thrown away: 1 / 2 is 0. This surprises people when scaling a sensor value.

Make one value a float, for example 1.0 / 2, or cast it: (float)a / b. Do the multiplication first when you need to keep precision with integers.

If you see this, do this

You seeDo this
Result is 0 or 1 onlyInteger division.
Result wraps to negativeOverflow. Use long.

map() function, serial monitor shows nothing.

Last reviewed: November 2026. These are the usual causes and the order that finds them fastest. Test on your own board; clones and cables differ.

Why this happens

C++ divides integers with integer rules: the fractional part is thrown away (truncated toward zero). So 1 / 2 is 0 and 5 / 2 is 2. The decision is made from the operand types before the answer is stored anywhere, which is why float result = 1 / 2 still gives 0.0. A floating-point operand makes the whole division floating point, so 1.0 / 2 is 0.5.

A second trap sits next to it. A percentage calculation such as raw * 100 / 1023 multiplies first. On an Uno that product is a 16-bit int, which tops out at 32767, so any raw above 327 overflows before the division. Both bugs give results that look plausible but wrong.

How to confirm the cause

  • Print each step separately: the numerator, the denominator and the result. A numerator that is negative or smaller than expected shows overflow.
  • Check the types in the declarations; any int / int division is integer division.
  • Test with simple numbers like 1 / 2 and 7 / 2 in a short sketch and compare with what you expect.
  • Look for the classic formula mistake 5 / 9 * (F – 32). The 5 / 9 is 0, so the answer is always 0.

Worked example

Worked numbers: 512 x 100 = 51200, which wraps to -14336 on a 16-bit int, and -14336 / 1023 is about -14. With a long, 51200 / 1023 is 50 (50.05 as a float).

int raw = 512;
int bad   = raw * 100 / 1023;     // overflows on an Uno: -14
int good  = raw * 100L / 1023;    // long maths: 50
float f   = raw * 100.0 / 1023;   // 50.05 (approx.)

// temperature trap
float c1 = 5 / 9 * (100 - 32);    // 0
float c2 = (100 - 32) * 5.0 / 9;  // 37.78

Common mistakes

  • Declaring the result as float and expecting the division itself to become float.
  • Casting the result instead of an operand: (float)(a / b) divides first and casts after.
  • Using int for intermediate products on 8-bit boards.
  • Forgetting that map() also uses integers, so small ranges lose steps.

If this did not fix it

  • Check that float values are printed with decimals, for example Serial.println(f, 2).
  • Check rounding: integer division truncates, so add half the divisor if you need rounding.
  • Check a sensor scale factor that is written as a whole number but should be fractional.