# Modulo Function returns incorrect results

- Thread: https://externals.io/message/11949
- Original archive: https://news-web.php.net/php.internals/11949
- Started by Matthew Boehm on 2004-08-06T01:02:04+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #11949 — Matthew Boehm — 2004-08-06T01:02:04+00:00

URL: https://externals.io/message/11949#11949

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!"; ?

---

## #11950 — George Schlossnagle — 2004-08-06T01:11:26+00:00

URL: https://externals.io/message/11949#11950 · In reply to: #11949

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




> 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!"; ?

---

## #11951 — Thomas Goyne — 2004-08-06T01:50:08+00:00

URL: https://externals.io/message/11949#11951 · In reply to: #11949

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/

---

## #11952 — Jason Garber — 2004-08-06T02:04:09+00:00

URL: https://externals.io/message/11949#11952 · In reply to: #11949

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

---

## #11962 — Lester Caine — 2004-08-06T07:33:29+00:00

URL: https://externals.io/message/11949#11962 · In reply to: #11949

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
