Bug #34399 [Com]: modulus operator truncates decimal operands
| From: | sjaakdewaal at gmail dot com | Date: | Fri, 04 Jun 2021 14:01:46 +0000 |
| Subject: | Bug #34399 [Com]: modulus operator truncates decimal operands | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-234211@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=34399&edit=1
ID: 34399
Comment by: sjaakdewaal at gmail dot com
Reported by: me at lucasoman dot com
Summary: modulus operator truncates decimal operands
Status: Not a bug
Type: Bug
Package: Math related
Operating System: Fedora Core 3
PHP Version: 4.4.0
Block user comment: N
Private report: N
New Comment:
Y THO?
Previous Comments:
------------------------------------------------------------------------
[2005-09-10 22:54:49] sniper@php.net
In PHP modulus can only be used on integers.
------------------------------------------------------------------------
[2005-09-07 06:45:14] me at lucasoman dot com
I realize that this may not be considered a bug, per se, because the software behaves as intended.
I'm simply questioning your intentions. As a developer, myself, I enjoy hiding behind the
it-works-as-intended-so-shut-up excuse, also.
"In PHP modulus can only be used on integers."
I'm well aware of this. PHP does not exist in its own mathematical cosmos. You wouldn't
change the addition operator to integer-only because that would be mathematically inconsistent and
incorrect. The operator would only be able to be used in a small subset of its space. The
restriction is arbitrary.
------------------------------------------------------------------------
[2005-09-07 04:18:08] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
In PHP modulus can only be used on integers.
------------------------------------------------------------------------
[2005-09-06 23:33:55] me at lucasoman dot com
Description:
------------
This issue has been brought up before, but not in so many words.
The modulus operator (%) truncates float operands and always returns an integer result. Some
languages behave similarly, some do not. Java, for one, correctly performs the float calculation.
I see no reason--besides ease of coding or perhaps for the sake of efficient code--that such an
arbitrary limitation would be placed on a general mathematical operator. Some limitations, like
disallowing division by zero, are not arbitrary; these are required mathematically. However,
restricting modulus to integer operands is not mathematically logical.
You may make the suggestion that I write my own float modulus function that performs recursive
subtractions. However, if your reason for coding mod as an integer operator is efficiency, writing
<i>script</i> code to perform this calculation--especially on large numbers--is most
certainly not more efficient.
I (humbly) request that you reconsider the way you've coded the mod operator.
Reproduce code:
---------------
print(4.5 % 2);
Expected result:
----------------
.5
Actual result:
--------------
0
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=34399&edit=1