Bug #52906 [PATCH]: gmp_mod returns negative result when non-negative is expected
| From: | cataphract@php.net | Date: | Wed, 22 Sep 2010 15:43:53 +0000 |
| Subject: | Bug #52906 [PATCH]: gmp_mod returns negative result when non-negative is expected | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-154570@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=52906&edit=1
ID: 52906
Patch added by: cataphract@php.net
Reported by: kkcagg at gmail dot com
Summary: gmp_mod returns negative result when non-negative is
expected
Status: Analyzed
Type: Bug
Package: GNU MP related
Operating System: Linux
PHP Version: 5.3.3
Block user comment: N
New Comment:
The following patch has been added/updated:
Patch Name: gmp_mod
Revision: 1285170233
URL:
http://bugs.php.net/patch-display.php?bug=52906&patch=gmp_mod&revision=1285170233
Previous Comments:
------------------------------------------------------------------------
[2010-09-22 17:21:09] cataphract@php.net
This is a bug in the PHP extension. Worse, the result differs if you
pass a GMP resource instead of an integer:
$ php -r "echo gmp_intval(gmp_mod(-2, gmp_add(6,1)));"
5
The problem is that gmp_zval_binary_ui_op_ex changes the sign of the
result if it uses the unsigned integer variant of the GMP API and the
first operand is negative. This may be the correct for other binary
functions (I haven't checked), but it isn't for mod.
The documentation mirrors the upstream documentation, which says:
«
â Function: void mpz_mod (mpz_t r, mpz_t n, mpz_t d)
â Function: unsigned long int mpz_mod_ui (mpz_t r, mpz_t n, unsigned
long int d)
Set r to n mod d. The sign of the divisor is ignored; the result is
always non-negative.
»
http://gmplib.org/manual/Integer-Division.html#Integer-Division
------------------------------------------------------------------------
[2010-09-22 11:48:29] kkcagg at gmail dot com
Description:
------------
GMP version 4.2.2
Running the function in test script returns the output of -5 when 5 is
expected.
Documentation (http://us.php.net/manual/en/function.gmp-mod.php) asserts
that the
result is always non-negative. This is not the case in actuality. Bug
type could
be either a bug or documentation problem.
Test script:
---------------
<?php
echo gmp_intval(gmp_mod(-2,7));
?>
Expected result:
----------------
5
Actual result:
--------------
-5
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=52906&edit=1