When is it beneficial to use int's over floats? The way i understand it: a float can do everything an int can do but not vica versa. It appears to me that using int's is better memory wise but you are inviting a lot of problems when doing calculations. Is it a matter of "good form" to start with int's and replace them with float's where needed? I acknowledge that It's always better to have good plan/design of what you want to program before you start. I'm just curious about this. I only started this week with the mooc python course,
I believe it is all a matter of necessity, use what you need when you need it. Ints are used commonly in programming for uses other than math. If you need to form exact equations with numbers, floats are always better than ints, but in many programming cases, fractional numbers aren't needed.
While its true float can do everything as int and not vice versa but at least for one reason to me int is more logical: when you write code to count something (boxes, people etc) it more logical to see 1, 2, 3 then 1.0, 2.0, 3.0. :) and also for taking a range() in such matter is also more suitable that its type is int. :) Cheers
btw @NPAA77 we started at same day ;)
Floats are imprecise. Due to rounding errors in binary conversion, floating point math has some issues. These do not exist in integer math. Therefore, integers are more precise. Floats take more memory. For the development of software that is memory efficient it is best to avoid using floats if practical. For high memory efficiency, they even suggest avoiding ints and going to shorts if practical.
Aren't the size of floats and ints the same on most desktop architectures?
Not in Python. http://stackoverflow.com/questions/1331471/in-memory-size-of-python-stucture
Oh, I didn't know it was python.
Yah, they mentioned the MOOC Python course. =) For C++, yah, unless you use a short it is the same size most operating systems and architectures. http://www.cplusplus.com/doc/tutorial/variables/ I probably confused it some by bringing up shorts. Hehe.
Thank you for the clarification. On a side-note, I wasn't aware that students outside the MOOC Python course could view responses. Not that I mind :)
During the MMOOC, we'll see lots of exercises and problems where ints are required, and floats won't do. For example, we'll write code that refers to parts of strings (the 2nd to 5th characters, for example), and in this context, ints, rather than floats, are required to refer to the positions of the characters in the string that we wish to access.
@NPAA77 The way the MechMOOC works is by using lots of free resources. For a learning community, they use OpenStudy. MIT also uses OS for that for a few of their open courseware items. So the site is used by both, but that does not stop all the other user. On the advantage site, I know some Python and a couple of the other users have worked most if not all the MIT exercises. That makes for a great way to get your code checked.
Join our real-time social learning platform and learn together with your friends!