note 59399 deleted from language.operators.arithmetic by mazzanet
| From: | mazzanet@php.net | Date: | Tue, 06 Dec 2005 07:45:40 +0000 |
| Subject: | note 59399 deleted from language.operators.arithmetic by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-99623@lists.php.net to get a copy of this message | ||
Note Submitter:
----
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.