php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22527 Modulus returned negative value
Submitted: 2003-03-03 22:29 UTC Modified: 2003-03-04 01:23 UTC
From: jaypedhskr at aol dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.2.3 OS: Unix
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jaypedhskr at aol dot com
New email:
PHP Version: OS:

 

 [2003-03-03 22:29 UTC] jaypedhskr at aol dot com
Hi,

I had the following result from a PHP script, as seen from printing output variables:

    -27 % 7 == -6

My understanding of the modulus function is that it returns the remainder from division, and that the remainder can never be negative.

I believe that is the standard mathematical definition.

So I think what should have been returned is 1 instead of -6.

That is

  -27 = 7*(-4) + 1

instead of

  -27 = 7*(-3) -6

I got around this by adding the the modulus value if the result was less than zero.  But I think a result of less than zero shouldn't occur.

Code snippet that ran into this:

      $T5 = $D + $T1 + $Y + $T2 + $T3 - $T4;
      $weekday = $T5 % 7;

Added code to handle this:
      if ($weekday < 0) {
          $weekday += 7;  // -27 % 7 = -6 case
      }

Specific problem case:
    $T5 = 3 + 2 + 3 + 5 + 0 - 40; // -27
    $weekday = %T5 % 7; // -27 % 7 = -6

Thanks!

-Jay Pedersen

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-04 01:23 UTC] rasmus@php.net
Modulus has never been well-defined for negative values in computer languages.  It all comes down to whether the language truncates towards zero or towards negative infinity.  Computers have traditionally truncated towards zero, whereas mathematicians tend to truncate towards negative infinity.  Some languages like Fortran and Ada actually have two different modulus operators for this reason.  PHP just has one, and it truncates towards zero as has been the traditional and expected thing for programming languages to do.

So, given that, let's look at your numbers.  Modulus has to satisfy the relation: (a/b)*b + a%b = a
where a/b is an integer division where our truncation direction comes in.

  (-27/7) * 7 + -27%7 = -27
  ( -3  ) * 7 + -27%7 = -27
        -21 + -27%7   = -27
              -27%7   = -27+21
              -27%7   = -6

In fact, the ISO standard for the C programming language, in which PHP is written, defines integer division and modulus operators to perform truncation towards 0 and not towards negative infinity.  We don't really have a strict language definition for PHP, but if we did, we would most likely follow the lead of languages like Fortran, C and C++ and specify truncation towards zero to be as consistent as possible with other languages.

 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 11:01:24 2019 UTC