note 59399 added to language.operators.arithmetic

From: Date: Mon, 05 Dec 2005 23:55:45 +0000
Subject: note 59399 added to language.operators.arithmetic
Groups: php.notes 
Request: Send a blank email to php-notes+get-99612@lists.php.net to get a copy of this message
Basically a mistake is in the name of the function. They should have called it REMAINDER and not MODULUS. The classic schoolboy misunderstanding of modular arithmetic is to confuse remainders and equivalence classes. Let me give an example using modula 5 arithmetic. Mathematically speaking, 7 does not equal 2, even if we are talking about modulo 5 arithmetic. What would be equal is the equivalence classes, [2]=[7]. They are equal, because they are the SAME equivalence class, eg [0]={..,-5,0,5,10,15,20,...} eg [2]={...,-3,2,7,12,17,...} eg [7]={...,-3,2,7,12,17,...} To have a computer representation of the equivalence classes, it is important that comparison of the equivalence classes provides the answer TRUE if the equivalence classes are compared and the classes are the same. For this reason, the equivalence classes are usually identified by the remainder after division, and this is the source of the schoolboy error of identifying the remainer and the equivalence class as one and the same thing. The distiction between the equivalence class [2] and the number two is not helped by common language amonst mathematicians themselves, who will typically say things like 7 mod 5 is 2, meaning [7] is [2] (in a modulo 5 context). But note also that [7] is [12] so the statement 7 mod 5 is 12 is just as valid to a mathematician though perhaps a little confusing to a schoolchild. [7]=[2] since 7 and 2 differ by a multiple of 5. [-8]=[2] since -3 and 2 differ by a multiple of 5. If the PHP function is to be called MODULUS, it is important that [-8]=[2] ie 7 % 5 should be 2 2 % 5 should be 2 -3 % 5 should be two = Let's take an easy to understand example, days of the week. There are 7 possible values. Today is Monday. In 7 days time it will be Monday. 7 days ago it was Monday. Tomorrow will be Tuesday. In 8 days time, it will be Tuesday. 6 days ago it was Tuesday. To do modulo arithmetic and not get one of the seven expected values is ridiculous. To say that 6 days ago was "Negative Sunday" instead of Tuesday is clearly stupid. I have come here to find the PHP syntax since I need modular arithmetic for a PHP webpage related to engines, crankshafts, valve timing and stuff like that. I shall be using the suggested function from above. function mod($num, $base) { return ($num - $base*floor($num/$base)); } The existing PHP function 'MODULUS' should be fixed or renamed, depending on the original intention. Renaming is probably better since working programs that break when a new software release is issued are a pain to everybody. ---- Manual Page -- http://www.php.net/manual/en/language.operators.arithmetic.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+59399 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+59399&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+59399&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+59399&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+59399&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+59399&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+59399&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+59399&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+59399&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#99612) next »