php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34399 modulus operator truncates decimal operands
Submitted: 2005-09-06 23:33 UTC Modified: 2005-09-10 22:54 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: me at lucasoman dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.4.0 OS: Fedora Core 3
Private report: No CVE-ID: None
 [2005-09-06 23:33 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-07 04:18 UTC] 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-07 06:45 UTC] 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-10 22:54 UTC] sniper@php.net
In PHP modulus can only be used on integers.

 [2021-06-04 14:01 UTC] sjaakdewaal at gmail dot com
Y THO?
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sat Jul 02 11:05:44 2022 UTC