Page 1 of 1

simple problem i think.. modulus not working for me

Posted: Wed Mar 16, 2005 10:39 pm
by nilios
before i start pulling out my hair can someone help me with this simple piece of code you've probably all have seen in any php beginner book.

for some reason i am not getting the correct result:

<?php
$x=6;
$y=4;
$z=$x%$y;
print $z;
?>

correct answer -> 5
my answer -> 2

why? maybe someone can help me out here.

thanks.

Posted: Wed Mar 16, 2005 11:16 pm
by feyd
you got the correct answer.. where did the "correct answer" you found come from?

Posted: Wed Mar 16, 2005 11:17 pm
by timvw
x mod y will give you the remainder of the divsion.
so if you divide 6 by 4, you will have 2 as remainder... (6 = 1 * 4 + 2)

thus the correct answer is 2 (and every x that can be written as x * 4 + 2, because they are in the same equivalence class).

Posted: Wed Mar 16, 2005 11:20 pm
by nilios
hey guys,

thanks for the help, i figured it out afterwards and was doing something totally different. need sleep.

later