• Create Account

### #ActualCornstalks

Posted 09 October 2012 - 01:54 PM

Actually, you're running out of precision in that operation. -255 * 409 is -104,295, but int16_t only has 16 bits of storage, which means it can only store values in the range −32,768 to 32,767. So what happens? Your intermediate result gets chomped down to 16 bits *before* the divide happens, and the end result is a weird value due to overflow.

What should you do about it? Use a bigger data type.

i also believe (cant remember off the top of my head) that multiplication has presedence over division
which means that the multiplication happens first

Not quite. Multiplication, division, and modulo all have the same precedence. I can't remember if the standard guarantees left to right evaluation in this case or not though.

### #2Cornstalks

Posted 09 October 2012 - 01:52 PM

Actually, you're running out of precision in that operation. -255 * 409 is -104,295, but int16_t only has 16 bits of storage, which means it can only store values in the range −32,768 to 32,767. So what happens? Your intermediate result gets chomped down to 16 bits *before* the divide happens, and the end result is a weird value due to overflow.

What should you do about it? Use a bigger data type.

### #1Cornstalks

Posted 09 October 2012 - 01:52 PM

Actually, you're running out of precision in that operation. -255 * 409 is -104,295, but int16_t only has 16 bits of storage, which means it can only store values in the range −32,768 to 32,767. So what happens? Your intermediate result gets chomped down to 16 bits, and the end result is a weird value due to overflow.

What should you do about it? Use a bigger data type.

PARTNERS