0

Which variable type is best for this?

Basically, the module I'm in is giving an example of a simple addition code and calling it a calculator (which it is, just limited to one function). But it uses integers, so that got me thinking: wouldn't it be better to use either floats or doubles? In which case, in a more complete calculator (lets go with addition, subtraction, multiplication, and division), would a float or a double be better? Actually, maybe my question would be better asked as "What's the difference between a double and a float?"

11th Jul 2016, 4:31 PM
Greg
5 Respostas
+ 2
Far as I remember, you can't trust the floats. For high precision calculation (financial), you should stick to decimals. Then again,for simple finance calculations, you can stick with just int. int breadCostInCents = 120; and only adjust on output.
11th Jul 2016, 7:02 PM
Roberts L
Roberts L - avatar
0
double and float both contain -ve and decimal no.s but double has slight edge over float because of greater range . So its better to use double over float if u dont have a memory constraint in program .
11th Jul 2016, 4:35 PM
shubham sharma
0
@shubham sharma Thank you!
11th Jul 2016, 4:38 PM
Greg
0
you better take double. float and double are similar type data types...but d dfrnce is double can hold more values than float
11th Jul 2016, 6:59 PM
Santhosh
0
float and double are almost the same as float can be pretty precise but double is even more precise although either one works fine as they can both contain a large amount of precision
12th Jul 2016, 7:37 AM
Supernova 70
Supernova 70 - avatar