Doc #41802 [Com]: Float values are implicitly converted to int when using modulo operator
| From: | php at richardneill dot org | Date: | Mon, 18 Apr 2011 14:48:52 +0000 |
| Subject: | Doc #41802 [Com]: Float values are implicitly converted to int when using modulo operator | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-6250@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=41802&edit=1
ID: 41802
Comment by: php at richardneill dot org
Reported by: cosborne83 at hotmail dot com
Summary: Float values are implicitly converted to int when
using modulo operator
Status: Closed
Type: Documentation Problem
Package: Documentation problem
Operating System: Windows XP
PHP Version: 5.2.3
Block user comment: N
Private report: N
New Comment:
I think this warning needs to be much stronger - especially because % is
grouped with 4 other operators (*+-/) that do behave correctly with
floats. I'd suggest using a WARNING box to say:
"Operands of modulus are cast to int before processing. This means that
values exceeding PHP_INT_MAX will give the wrong result, EVEN when the
operand is has an exact integer representation in floating-point format.
For example, '0xffffffff %2' will evaluate to -1"
Imho the behaviour is also wrong, because PHP normally does integer
overflow (which is good), but % uses integer wraparound (bad).
Previous Comments:
------------------------------------------------------------------------
[2007-08-16 13:31:14] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation
better.
"Operands of modulus are converted to integers (by stripping the decimal
part) before processing. "
------------------------------------------------------------------------
[2007-06-25 14:35:25] tony2001@php.net
This is how it works in C, hence this is how it should work in PHP.
Reclassified as docu problem.
------------------------------------------------------------------------
[2007-06-25 14:18:36] cosborne83 at hotmail dot com
Description:
------------
When applying the % (modulo) operator to floats, the values are
implicitly converted to int, which causes problems in many instances.
This means that you can't use floats to store integer values larger than
the maximum int value if you wish to perform modulo operations.
Theis behaviour is also invalid since modulo is well defined on floats
as well as ints.
Reproduce code:
---------------
$m = 5000000000;
$n = 3.5;
// Integer modulo but with large int stored as float
print $m % 10;
print "\n";
// Non-integer modulo.
print $n % 1.5;
Expected result:
----------------
0
0.5
Actual result:
--------------
4
0
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=41802&edit=1