php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #53208 Modulo does not calc right (C99 not mathematical correct)
Submitted: 2010-10-30 19:45 UTC Modified: 2010-11-08 11:05 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: fibrefox at dynamicfiles dot de Assigned: aharvey (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: any
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: fibrefox at dynamicfiles dot de
New email:
PHP Version: OS:

 

 [2010-10-30 19:45 UTC] fibrefox at dynamicfiles dot de
Description:
------------
I found a bug-report that already noted it, but since this is VERY old, i think it should be re-opened or re-mentioned: http://bugs.php.net/bug.php?id=20365

Modulo operator DOES NOT calc right.

(-4) mod 10 should be 6, thats the mathematical definition.

modulo is NOT the distance to zero. modulo is NOT the remainder while division.

the problem is, that the underlying code is written in c/c++, but c99 does NOT have a real modulus, its a remainder-function.

here is a site which describes it:
http://www.alecjacobson.com/weblog/?p=1140

Test script:
---------------
<?php
// euclidean result: 6; php result: -4 .... due to the bug in c++
echo (-4)%10;
?>

Expected result:
----------------
6

Actual result:
--------------
-1

Patches

C99_ZEND_patch_euclidean_modulo (last revision 2010-10-30 17:46 UTC by fibrefox at dynamicfiles dot de)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-30 21:27 UTC] cataphract@php.net
-Type: Bug +Type: Documentation Problem
 [2010-10-30 21:27 UTC] cataphract@php.net
You are of course right, and this merits a mention in the documentation, but changing the meaning of such a basic operator is a huge BC break.
 [2010-10-30 22:40 UTC] fibrefox at dynamicfiles dot de
I see the problem in here, but as i see on the website there are changes anyway in meanings:
"Backwards incompatible change: Methods with the same name as the last element of a namespaced class name will no longer be treated as constructor."

The term "modulus" which the symbol "%" stands for is just wrong here. To gain the compatibility the documentation should be remade ... or the "real-modulus" has to be added in the core by offering a new function, maybe "euclid_mod" or something like that.

It is really important to NOT make programming in some kind of struggeling through the documentation or the code to find the problem, as you mentioned, of such a basic operator.

I really think there a lot of coders are wondering about this flaw in PHP (but it is a sign of C99) and are trying to make workarounds to "fix" this. Built-in functions should be a little bit faster in here, so there also will be some kind of performance-boost here.

Anyway, i think the operator should do what he says he would do ... producing the modulus, and not the remainder.
 [2010-10-30 23:57 UTC] cataphract@php.net
PHP doesn't have a formal specification; so the "correct" behavior is an fluid concept. What the documentation says has some weight, but it's not normative.

Definitions can be more or less consistent, but they are arbitrary. Considering only the case when the second operand n is positive, it is indeed more common in mathematics to define the modulus so it's a number between 0 and n - 1 (and that's indeed the behavior of e.g. gmp_mod).

In this case, it would certainly break a lot applications to make the change you propose and the benefits just aren't compelling enough, especially given that several other programming languages define the modulus operator in the same fashion.
 [2010-10-31 00:05 UTC] rasmus@php.net
Note also that the example you gave of a BC break was a very minor one.  It was a 
BC break for people who had written code specifically for PHP 5.3.x using an old-
style constructor.  The number of people who would write 5.3-specific code with a 
PHP4-style constructor is tiny.  It may in fact be the case that nobody had 
written code like that.  That's why we deemed it ok to break this to fix a 
mistake.  Changing the modulus operator is a completely different thing as it 
would touch every piece of PHP code ever written that uses modulus.
 [2010-11-03 21:51 UTC] kalle@php.net
-Package: *Math Functions +Package: Documentation problem
 [2010-11-07 05:04 UTC] kalle@php.net
-Type: Documentation Problem +Type: Feature/Change Request -Package: Documentation problem +Package: Math related
 [2010-11-08 10:42 UTC] aharvey@php.net
-Status: Open +Status: Assigned -Type: Feature/Change Request +Type: Documentation Problem -Package: Math related +Package: Documentation problem -Assigned To: +Assigned To: aharvey
 [2010-11-08 10:42 UTC] aharvey@php.net
Agreed; this is a documentation issue, since there's no way this is going to be changed at this point in PHP's life. There's already a minimal note about this on the arithmetic operators page; I'll expand it out to note that this is the same as the C99 implementation of %.
 [2010-11-08 11:05 UTC] aharvey@php.net
Automatic comment from SVN on behalf of aharvey
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=305195
Log: Expanded the discussion of the modulus operator's handling of negative operands
per doc bug #53208.
 [2010-11-08 11:05 UTC] aharvey@php.net
-Status: Assigned +Status: Closed
 [2010-11-08 11:05 UTC] aharvey@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 13:01:28 2024 UTC