Quote:
Originally Posted by JBotAlan
Hey all,
I feel really dumb asking this question, but since I don't have a programming mentor I must rely on what I read, and the community. Since I don't know what to search for ("division" brought up tons of non-related posts...what else should I search for?) I am just going to post this. Sorry if it's a repeat.
If I'm using all integers, how will the RC calculate these:
30/7 = (It rounds to 4, so I assume I'd get 4)
70/6 = (This is where I'm confused...it's 11.6666..., but will the robot round it, or take the floor? Meaning, do I get 12, or 11? I don't have the hardware in front of me to experiment with...)
Thanks for helping with a n00b question...
JBot
|
Good question... it becomes much of a shocker when you start investigating computer science, you just got to grill it into you head a bit.
type int: The computer will basically divide it as many times evenly into whole number, not part of a whole, and not one more. Since all the computer knows is zeros and ones, at least for type int, it sticks to its whole number interpretation, and fills what it can.
Thus your 70/6 goes to 11... along with 66/6, 67, 68, 69, and 71/6. 72/6 will of course yield 12 though. (Truncation, not rounding... be careful about this difference)
Once you get into type double and float (i think), then the computer believes it can handle decimals, and it will give you decimal answers.
Side note somewhat related: The modulus (%) operator is
amazingly useful if you want to find the remainder, for that is what it does. Its even more useful for determining if a given integer is odd or even given you divide by 2 (Example: 66 % 2 gives you 0 since it is even, 65 % 2 gives you 1 since it is odd).
So if I were to run through that above problem again, this time with modulus:
66 % 6 = 0
67........= 1
68........= 2
69........= 3
70........= 4
71........= 5
72........= 0
Have fun... and never be afraid to ask questions after a search goes nowhere.
-Joe