1. ## Data Types

Hello. Struggling with getting my head around data types. Clearly a newcomers question, but why does this produce 4.2*10^9?
Code:
```// playing with variables

#include <iostream>
using namespace std;

int main()
{
int a(2);
unsigned int b(3);
float result;
result=a-b;
cout<<result;
return 0;
}```

Thanks. Feel free to insult the sheer ignorance.

2. When you do the a-b, a is promoted to unsigned int, and so the expression result of -1 overflows to INT_MAX. Your platform has 32-bit ints, so this is 4,294,967,296. Assigned to a float, this becomes 4.29497e9 (you only get 6 significant digits with float).

3. nice one, thanks.

4. 3 things here.
Firstly,
int a(2);
unsigned int b(3);
While correct syntax, to some it can be a little confusing, so it's better to do
int a = 2;
unsigned int b = 3;
I think most agree on this.

Second, it is unwise to do operations on one signed and one unsigned.
Preferably, both types should be the same, otherwise you can use a cast to make one signed or unsigned, depending on what you want. static_cast should work fine for this.

Thirdly, you are doing integer subtraction, so the result will be an integer and not a float, so using a float to store the answer is really unnecessary and can actually be a bad thing since your answer might not be what you want due to floating point inconsistencies.

5. Originally Posted by Elysia
Second, it is unwise to do operations on one signed and one unsigned.
Preferably, both types should be the same, otherwise you can use a cast to make one signed or unsigned, depending on what you want. static_cast should work fine for this.

Thirdly, you are doing integer subtraction, so the result will be an integer and not a float, so using a float to store the answer is really unnecessary and can actually be a bad thing since your answer might not be what you want due to floating point inconsistencies.
QFE, except the word you were looking for is "inaccuracies", not "inconsistencies".

Turn your compiler's warning level up to the max and leave it there. Pay attention to everything it tells you.

6. Right you are.

7. Originally Posted by iMalc
QFE, except the word you were looking for is "inaccuracies", not "inconsistencies".

Turn your compiler's warning level up to the max and leave it there. Pay attention to everything it tells you.
"QFE"? There's one I haven't seen before. What's the long form?

8. Quoted for emphasis. Closely related to QFT.