php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50630 Modulo on strings/floats won't work on huge numbers
Submitted: 2010-01-01 22:25 UTC Modified: 2010-06-08 15:11 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: bananen-joe at bananen-joe dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.1 OS: win32 only - Windows XP SP3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: bananen-joe at bananen-joe dot de
New email:
PHP Version: OS:

 

 [2010-01-01 22:25 UTC] bananen-joe at bananen-joe dot de
Description:
------------
If you use the modulo operator on huge numbers (which cannot be converted into integers) you get a strange result.
Even not error is reported.

Reproduce code:
---------------
error_reporting(E_ALL);
$number = '9999999990';
echo "$number % 10 = ", $number % 10, "<br>";
$number = 9999999990;
echo "$number % 10 = ", $number % 10, "<br>";
$number = '99990';
echo "$number % 10 = ", $number % 10, "<br>";
$number = (float) 99990;
echo "$number % 10 = ", $number % 10, "<br>";

Expected result:
----------------
9999999990 % 10 = 0 (or even an error notice/warning)
9999999990 % 10 = 0 (or even an error notice/warning)
99990 % 10 = 0
99990 % 10 = 0

Actual result:
--------------
9999999990 % 10 = 7
9999999990 % 10 = 8
99990 % 10 = 0
99990 % 10 = 0

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-06 06:46 UTC] federico dot lebron at gmail dot com
The size of 9999999990 is larger than PHP_INT_MAX, so when converting to 
integer (since you asked for modulo, and it gets converted to integer), 
you're getting undefined behavior, as per 
http://php.net/manual/en/language.types.integer.php . No warnings or 
notices will be triggered in such a case.

Internally (and on this machine, with PHP compiled as 32 bit), 
strtol("9999999990", NULL, 10) is being called for the string case, 
which is 2147483647. This is noted in the intval function's 
documentation, so it's expected. For the float case, (long) (unsigned 
long) (long long int) 9999999990 is being casted, which is 1410065398, 
but this is undefined behavior.
 [2010-06-08 15:11 UTC] tony2001@php.net
-Status: Open +Status: Bogus
 [2010-06-08 15:11 UTC] tony2001@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed May 29 10:01:32 2024 UTC