#35319 [NEW]: modulo operator is not coherent with mathematical definition
| From: | ian at euona dot com | Date: | Mon, 21 Nov 2005 21:12:20 +0000 |
| Subject: | #35319 [NEW]: modulo operator is not coherent with mathematical definition | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-88642@lists.php.net to get a copy of this message | ||
From: ian at euona dot com
Operating system: Win XP
PHP version: 5.0.5
PHP Bug Type: Math related
Bug description: modulo operator is not coherent with mathematical definition
Description:
------------
This is the same problem as stated in reports 20365 and 29822. This is
indeed not bogus, PHP, C and Javascript are NOT producing the mathematical
correct result. Per definition modulus arithmetics create a ring structure
containing the values [0,1,...,(n-1)]. THERE ARE NO NEGATIVE NUMBERS! The
current behavior is not intuitive and basically incorrect. An integer
typecast is not the same as floor.
This is a big problem when shifting table fields (e.g. weekdays). Take a
look at spreadsheet software.
//This is the way % should behave
function math_mod($a,$n){$n=abs($n);if($n===0){return NULL;}return
$a-$n*floor($a/$n);}
//this function emulates ($a%$n)
function php_mod($a,$n){return $a-$n*(int)($a/$n);}
Reproduce code:
---------------
echo -5%7 , ' ', fmod(-5,7),' ', 1%0,' ', 3%-2;
Expected result:
----------------
2 2 NULL 1
Actual result:
--------------
-5 -5 NULL 1
--
Edit bug report at http://bugs.php.net/?id=35319&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=35319&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=35319&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=35319&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=35319&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=35319&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=35319&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=35319&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=35319&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=35319&r=support
Expected behavior: http://bugs.php.net/fix.php?id=35319&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=35319&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=35319&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=35319&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=35319&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=35319&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=35319&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=35319&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=35319&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=35319&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=35319&r=mysqlcfg