|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2005-11-21 23:48 UTC] sniper@php.net
[2005-11-21 23:56 UTC] ian at euona dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 07:00:01 2025 UTC |
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