I'll start with the usual apology for such a basic question, but I can't get my head around this.
Here is my code from the tutorial I am using:
float firstNumber;
float secondNumber;
float floatAnswer;
int integerAnswer;
firstNumber = 10.5F;
secondNumber = 32.5F;
integerAnswer = firstNumber + secondNumber;
MessageBox.Show(integerAnswer.ToString());
43 doesn't have a decimal place so it is an integer, right? What am I missing?
Can anybody simplify this for me, or should I just give up now lol?
Many Thanks
Lee
VulpesPosted Mar 26, 2011, 7:27 AM
The questions you've asked so far have been sensible ones which show that you're thinking about what you're doing. This is a good thing so don't give up!
LeePosted Mar 26, 2011, 11:40 AM
VulpesPosted Mar 26, 2011, 10:29 AM
The reason why integers are used is that they more efficient (i.e. they produce faster code) than floating point numbers if you only need to work with whole numbers.
Also an int can store numbers with up to 9 (possibly 10) digits whereas a float, whilst having a much larger range, is only accurate up to 7 digits.
On the other hand a double has an even larger range and is accurate to 15 or 16 digits. However, it requires 8 bytes of storage compared to 4 bytes for an int or float.
LeePosted Mar 26, 2011, 9:49 AM
Why can't my tutorial have put it like that! If I understand you correctly, what you are saying is that because I have declared the 2 numbers in the sum as Floats, the fact that the answer has no decimal places is irrelevent. It MIGHT have had a decimal place so the code doesn't like it.
That I do understand, thank you.
However this begs another question. You can't store a float value in an integer value, but you CAN store an integer in a float.
Therefore why bother with Int variables? Why not simply declare all number variables as Float or Double? 43.0 will work as a float, so why declare it as an integer? If you're using numbers that may or may not have decimal places, Float is safer. Where is the advantage of using integer?
Many Thanks
Lee