#34399 [Opn->Bgs]: modulus operator truncates decimal operands
ID: 34399
Updated by: sniper@php.net
Reported By: me at lucasoman dot com
-Status: Open
+Status: Bogus
Bug Type: Math related
Operating System: Fedora Core 3
PHP Version: 4.4.0
New Comment:
In PHP modulus can only be used on integers.
Previous Comments:
------------------------------------------------------------------------
[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 http://bugs.php.net/?id=34399&edit=1
Thread (6 messages)