Modulo Function returns incorrect results

php.internals

Matthew Boehm

22 years ago
Found some weird behavior in the built in mod function (%): $a = 4; $b = 3; print ( $a % $b ); (returns 1 as it should) Now swap $a and $b: $a = 3; $b = 4; print ( $a % $b ); This returns 3. 3?! 3/4 is 0.75. Shouldn't this return 0? Try this one: $a = 20; $b = 10; print ( $a % $b ); Shouldn't that return 2? It returns 0; $a = 11; $b = 21; print ( $a % $b ); Returns 11. What is going on? Was my C.S. Professor wrong in telling us that the % function returns the left side of the decimal in a division? This is PHP 4.3.4 Any help would be appreciated. Thanks, Matthew
-- ---------------------------------------------------------------------------- Matthew Boehm dr_mac@mail.utexas.edu The University of Texas at Austin, Department of Geography "Why did the prison use Windows2K as a guard? Because it always locks up!" <?PHP echo "PHP kicks ASP!"; ?> ----------------------------------------------------------------------------

George Schlossnagle

22 years ago
Hi. I think you don't understand what modulo does. The examples you give all suggest you think it is simply integer divide. a % b returns the remainder when the integer a is divided by the integer b. All your 'examples' illustrate correct behavior of the modulo operator. George On Aug 5, 2004, at 9:02 PM, Matthew Boehm wrote:

Thomas Goyne

22 years ago
On Thu, 05 Aug 2004 20:02:04 -0500, Matthew Boehm <dr_mac@mail.utexas.edu> wrote:
> Returns 11. What is going on? Was my C.S. Professor wrong in telling us > that the % function returns the left side of the decimal in a division?
I suggest you demand a refund for all courses you paid for with that professor.
-- Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

Jason Garber

22 years ago
Hello Matthew, 3/4 = (0*4)+3 --> 3%4 = 3 9/4 = (2*4)+1 --> 9%4 = 1 20/10 = (2*10)+0 --> 20%10 = 0 4 goes into 3 a total of ZERO times with a remainder of 3. 10 goes into 20 a total of TWO times with a remainder of 0. Take a look at a standard (integer) long division problem with remainders.
-- Best regards, Jason mailto:jason@ionzoft.com Thursday, August 5, 2004, 9:02:04 PM, you wrote: MB> Found some weird behavior in the built in mod function (%): MB> $a = 4; MB> $b = 3; MB> print ( $a % $b ); (returns 1 as it should) MB> Now swap $a and $b: MB> $a = 3; MB> $b = 4; MB> print ( $a % $b ); MB> This returns 3. 3?! 3/4 is 0.75. Shouldn't this return 0? MB> Try this one: MB> $a = 20; MB> $b = 10; MB> print ( $a % $b ); MB> Shouldn't that return 2? It returns 0; MB> $a = 11; MB> $b = 21; MB> print ( $a % $b ); MB> Returns 11. What is going on? Was my C.S. Professor wrong in telling us MB> that the % function returns the left side of the decimal in a division? MB> This is PHP 4.3.4 MB> Any help would be appreciated. MB> Thanks, MB> Matthew MB> -- MB> ---------------------------------------------------------------------------- MB> Matthew Boehm MB> dr_mac@mail.utexas.edu MB> The University of Texas at Austin, Department of Geography MB> "Why did the prison use Windows2K as a guard? Because it always locks up!" MB> <?PHP echo "PHP kicks ASP!"; ?> MB> ----------------------------------------------------------------------------

Lester Caine

22 years ago
Matthew Boehm wrote:
> Returns 11. What is going on? Was my C.S. Professor wrong in telling us > that the % function returns the left side of the decimal in a division?
Try RIGHT side, but as an integer remainder ( fraction * divisor )
-- Lester Caine ----------------------------- L.S.Caine Electronic Services