note 128385 added to language.operators.arithmetic
| From: | fahadmm7 at hotmail dot com | Date: | Wed, 12 Apr 2023 12:01:31 +0000 |
| Subject: | note 128385 added to language.operators.arithmetic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-220341@lists.php.net to get a copy of this message | ||
It is time for PHP to distinguish between the Reminder and the Modulus and define them correctly
mathematically.
Let us assume that we have two integers a and b. Then, the reminder (denoted by r) of a (the
dividend) divided by b (the divisor) should merely satisfy the equation a = q*b + r, where q is the
quotient.
In case of modulus, however, it refers to a finite set originated by b, that is {0, 1, 2, ..., b-1}.
The result of the modulus operation must be an element of that finite set.
To illustrate, let us assume that a = 5, and b = 3. This means we have a finite set {0, 1, 2}. The
modulus operation, a mod b or 5 mod 3, must return 2. Think about it as a loop on the elements of
the set for a times, in this case 5 times.
If a = 0, then it returns 0.
If a = 1, then it returns 1.
If a = 2, then it returns 2.
If a = 3, then it returns 0.
If a = 4, then it returns 1.
If a = 5, then it returns 2.
And so on so forth. It is forward loop.
If a is negative, then it means backward or reverse loop.
If a = -1, then it returns 2.
If a = -2, then it returns 1.
If a = -3, then it returns 0.
If a = -4, then it returns 2.
If a = -5, then it returns 1.
And so on so forth.
If b is negative, then the set is constructed as follows {0, b+1, b+2, b+3, ..., -1}. So, if b = -4,
then the set is {0, -3, -2, -1}. a is still the circular index of the loop, so:
If a is positive, then forward loop:
If a = 0, then it returns 0.
If a = 1, then it returns -3.
If a = 2, then it returns -2.
If a = 3, then it returns -1.
If a = 4, then it returns 0.
If a = 5, then it returns -3.
If a = 6, then it returns -2.
If a = 7, then it returns -1.
If a = 8, then it returns 0.
And so on so forth.
If a is negative, then reverse loop.
If a = -1, then it returns -1.
If a = -2, then it returns -2.
If a = -3, then it returns -3.
If a = -4, then it returns 0.
If a = -5, then it returns -1.
If a = -6, then it returns -2.
If a = -7, then it returns -3.
If a = -8, then it returns 0.
If a = -9, then it returns -1.
And so on so forth.
Therefore, the result of the modulo operator % has the same sign as the divisor, not the dividend.
And the reminder has the same sign as the dividend. Moreover, though it is good to mind the sign,
minding the results is far more important.
The table below summaries the differences:
<?php
a | b | a mod b | a rem b
------+-----+-----------+------------
13 | 4 | 1 | 1
------+-----+-----------+------------
13 | -4 | -3 | 1
------+-----+-----------+------------
-13 | 4 | 3 | -1
------+-----+-----------+------------
-13 | -4 | -1 | -1
------+-----+-----------+------------
?>
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.13.130 (proxied: 5.36.213.35)
----
Manual Page -- https://php.net/manual/en/language.operators.arithmetic.php
Edit -- https://main.php.net/note/edit/128385
Del: integrated -- https://main.php.net/note/delete/128385/integrated
Del: useless -- https://main.php.net/note/delete/128385/useless
Del: bad code -- https://main.php.net/note/delete/128385/bad+code
Del: spam -- https://main.php.net/note/delete/128385/spam
Del: non-english -- https://main.php.net/note/delete/128385/non-english
Del: in docs -- https://main.php.net/note/delete/128385/in+docs
Del: other reasons-- https://main.php.net/note/delete/128385
Reject -- https://main.php.net/note/reject/128385
Search -- https://main.php.net/manage/user-notes.php