Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 16 Online
OpenStudy (anonymous):

Working my way through Rec 1. Why do we have floats and ints? Why not just make everything a float?

OpenStudy (anonymous):

I also would like to know this.

OpenStudy (e.mccormick):

Accuracy. Floats are not accurate.

OpenStudy (e.mccormick):

Let me show a decimal example. \(\dfrac{2}{3}\) is an accurate number. Now put it in a calculator. With the Windows calculator, I get 0.66666666666666666666666666666667. Now that is close to \(\dfrac{2}{3}\) but not the same as \(\dfrac{2}{3}\). And I can't tell if a person that enters 0.66666666666666666666666666666667 really wants 0.66666666666666666666666666666667 or if they want \(\dfrac{2}{3}\). The same sort of issues happen between floats and ints.

OpenStudy (rsmith6559):

There's also a matter of cost. Integer math can be done very fast and with little processor power. Floats, internally, are basically scientific notation in binary. In some ways this is a plus. It allows for huge numbers in a small amount of memory, but the precision and numbers that don't work too well with binary, less than perfect.

OpenStudy (e.mccormick):

Ooh, yes, I should have mentioned the resources too. Quite right rsmith6559. And to be honest, depending on what you consider an int and a float (their bit size can be changed,) that can be a big deal for both CPU use and memory.

OpenStudy (anonymous):

Actually for maths problem float is more likely to use for exact evaluation, i.e: in case of integer 5/2 evaluation does not give accurate answer and float does not have modules operation ( % ).

OpenStudy (e.mccormick):

@miron012 You seem to have misunderstood my example completely. See, where the real problems in floats are is in the fact that things are done in binary. My example was done in base ten decimal numbers rather than try and explain the whole binary issues, which tends to take a lot more setup.

OpenStudy (anonymous):

Actually I wrote a python program a bit ago that demonstrated this well. Note that this program is basically doing the same thing as 1+1 but it is using floating point (and decimals) instead of integers. I added the comments just now for your sake. it's in .txt format so just go into python and run it after taking a look.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!