0
why need to use int? why not just float everything since float must containt and accept integer?
5 Answers
+ 4
Actually int and float both use 4 bytes, 32 bits each. The only simple type that uses 2 bytes, 16 bits, is short and ushort. However, doubles are 8 bytes, 64 bits and decimals are 16 bytes, 128 bits. So doubles and decimal use more memory than integers and floats, but floats do not use more memory than integers and vice versa. Since they require the same size in bits.
Now if your whole number is in the -32768 to 32767 range, you can use short instead of int to save memory space. Same goes with ushort 0-65535 .
Â
Also using a real data type no matter the memory size requires more time to calculate than an int. So if speed is the name of the game for your program and you use a real number data type when an int could be used. Your program is not living up to its true potiental, expecially if you are facing fierce competition.
Another example is if you want the user to enter their age or house number. The user could accidently type that they are 33.2 years and 11 months old, which would make no sense, or that they live at 45.37 street. It opens up the possibility for errors that would not happen when using int.
There are many more reasons why you would not want to do this. Such as int holds a vastly larger integer value range (roughly -2 billion to 2 billion), compared to float. So if you wanted to use large whole numbers, using float would not work. I could go on and on, but basically int is used for whole numbers (within their intended value range) and you just got to deal with it.
0
with int (integer) u only can use numbers like 3, 6 , 241 etc. with float u can use 3.56 or 37.06
- 1
In order to save memory we need to use int because when we declare variable of int data type it occupy 2 byte while when we decare it as float it occupy 4 byte .
- 2
both the data type occupies same memory. but use of each data type is different for different scenario.
Such as you can't allow user to input their age in 11.9 or street no as 16.2 etc in such scenario use of float is not appropriate
also while defining temperature or percentage we need exact decimal because it affect the whole number so here we should not use int.
- 5
U just need to accept it for what it is. High level programming is magic