Tim Hunter
2/18/2007 2:15:00 PM
Derek Teixeira wrote:
> i tried to get a better understanding of the % method, so i just set up
> a program to show me the results of the % of a number ...
>
> so i tried out the number 4.
>
> puts 4%1000
> puts 4%500
> puts 4%100
> puts 4%50
> puts 4%10
> puts 4%5
>
> and for everyline i got "4" as the remainder.. but when i tried these
> with a calulator .. i only got 4 as a remainder for the 1000. when i did
> the 4/500 i got a .008 .. so shouldn't the % be an 8?
>
>
When I was in elementary school they taught us these words for division.
Suppose you have 9 divided by 4. 4 is the "divisor". 9 is the
"dividend". 4 goes into 9 2 times, so 2 is the "quotient", but since 4
times 2 is only 8, there is 1 left over. 1 is called the "remainder".
The modulus operator returns the remainder after dividing two integers.
That is, 9 % 4 = 1.
So what is 4 % 5? 5 goes into 4 0 times. 0 times 5 is 0, so there is 4
left over. 4 % 5 = 4. For 4 % 10, 10 goes into 4 0 times, with 4 left
over. 4 % 10 = 4. Same for 4 % 50 and all your other examples.
For interesting experiments, use a divisor that is smaller than the
dividend. 10 % 4 = 2. 12 % 3 = 0.
For more interesting experiments, use negative numbers.