int16_t a = -255; int16_t b = 409; int16_t c = 1000; int16_t result = a * b / c;
result: 26
expected: -104
What the heck is going on here?
Posted 09 October 2012 - 01:46 PM
Edited by Kaptein, 09 October 2012 - 01:50 PM.
Posted 09 October 2012 - 01:52 PM
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.i also believe (cant remember off the top of my head) that multiplication has presedence over division
which means that the multiplication happens first
Edited by Cornstalks, 09 October 2012 - 01:54 PM.
Posted 09 October 2012 - 01:56 PM
Huh??? That makes no sense.try refreshing your compiler
Posted 09 October 2012 - 01:57 PM
Posted 09 October 2012 - 02:07 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.
Posted 09 October 2012 - 02:17 PM
int16_t result = (int16_t)((int)a * (int)b / (int)c);On a side note, C++ does enforce integer promotion in this situation, so the result in C++ will be as you expected.
Posted 09 October 2012 - 02:18 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.
Could you please elaborate on that? What exactly is the factor that causes the calculation to be processed in 16-bit? Shouldn't the program detect that its running out of precision and automatically use a bigger (i.e. 32-bit) temporary buffer?
Which variables exactly should be increase in size?
EDIT: Is there a way to increase the temporary buffer size without changing the variable data types?
Edited by !Null, 09 October 2012 - 02:19 PM.
Posted 09 October 2012 - 02:30 PM
Posted 09 October 2012 - 02:32 PM
Actually, C has integer promotion rules as well (I'm pretty sure C++ got them from C in the first place). The only way that the result he's getting makes sense is if int on his compiler is 16-bits in which case manually casting to plain int isn't going to help either.On a side note, C++ does enforce integer promotion in this situation, so the result in C++ will be as you expected.
And on yet another side note, even when I tried to reproduce your results with VS2010 compiling the code as C code, I still get -104 so it appears it does integer promotion like C++ requires even in C.
Posted 09 October 2012 - 02:36 PM
Your variable a holds a negative value and you're casting it to an unsigned integer. Unsigned variables cannot hold negative values. And yes, it is technically enough to cast just a or b, because the other operands will be promoted automatically.Thank you for explaining the situation, this clears up my doubts. I am programming an application in AVR Studio 5.1 using gcc for an Atmega328p microcontroller. Due to limited RAM on a microcontroller (2 KB) I tend to stick to 8-bit and 16-bit variables. I did some tests and it seems that increasing at least one of the vars in the equation will produce the correct result.
This does not seem to work though.
int16_t result = (uint32_t)a * b / c; // does not work
I'll do some more tests.
Regards,
Bismuth
I was reading through both the C and C++ specification (the drafts only, but I doubt these details change) and the two were different in that C++ explicitly mentioned integer promotion for narrow integers and C did not; otherwise the wording was more or less identical. I did not read much more than that into it, so you may very well be right.Actually, C has integer promotion rules as well (I'm pretty sure C++ got them from C in the first place). The only way that the result he's getting makes sense is if int on his compiler is 16-bits in which case manually casting to plain int isn't going to help either.
On a side note, C++ does enforce integer promotion in this situation, so the result in C++ will be as you expected.
And on yet another side note, even when I tried to reproduce your results with VS2010 compiling the code as C code, I still get -104 so it appears it does integer promotion like C++ requires even in C.
Edited by Brother Bob, 09 October 2012 - 02:38 PM.
Posted 09 October 2012 - 02:44 PM
Eh, sorry it was a typo. I wonder why I typed uint anyway.Your variable a holds a negative value and you're casting it to an unsigned integer. Unsigned variables cannot hold negative values. And yes, it is technically enough to cast just a or b, because the other operands will be promoted automatically.
Posted 09 October 2012 - 02:49 PM
If I'm understanding that right, on a 32-bit system (that is, when `int` is 32-bits), then yes, you'd get the right result of -104 because each operand is implicitly promoted to an `int`. But on a system where `int` is smaller than 32-bits, (like if `int` is 16-bits), then you'll have overflow. Which is exactly why you need to either use bigger datatypes or use that cast.If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.
Posted 09 October 2012 - 03:40 PM
To clarify this a little bit, the C standard says this about evaluating an integer expression:
If I'm understanding that right, on a 32-bit system (that is, when `int` is 32-bits), then yes, you'd get the right result of -104 because each operand is implicitly promoted to an `int`. But on a system where `int` is smaller than 32-bits, (like if `int` is 16-bits), then you'll have overflow. Which is exactly why you need to either use bigger datatypes or use that cast.
If an int can represent all values of the original type, the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.
Posted 09 October 2012 - 07:29 PM
A.
Lotus RPG Engine - My Journal: http://www.gamedev.n...die-rpg-engine/ |
Action RPG In development using XNA 4.0. | Blog in English: en.lotusrpg.com.br |
Personal blog In Portuguese: lotuzgames.wordpress.com |
Posted 09 October 2012 - 07:40 PM
The OP explained:Just use 32 bit integers, I dont really see why trying so hard to use 16 bits, is there a reason for it?
I am programming an application in AVR Studio 5.1 using gcc for an Atmega328p microcontroller. Due to limited RAM on a microcontroller (2 KB) I tend to stick to 8-bit and 16-bit variables.