Bug #51108 [Fbk->NoF]: Remainder operator (%) FAILS with two specific numbers
| From: | php-bugs at lists dot php dot net | Date: | Wed, 03 Mar 2010 17:34:46 +0000 |
| Subject: | Bug #51108 [Fbk->NoF]: Remainder operator (%) FAILS with two specific numbers | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-148225@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=51108&edit=1
ID: 51108
Updated by: php-bugs@lists.php.net
Reported by: nonsqtr at hotmail dot com
Summary: Remainder operator (%) FAILS with two specific numbers
-Status: Feedback
+Status: No Feedback
Type: Bug
Package: Math related
Operating System: Linux 2.6.18-164.6.1.el5 #1 SMP
PHP Version: 5.2.12
New Comment:
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
Previous Comments:
------------------------------------------------------------------------
[2010-02-22 10:25:30] jani@php.net
Where's the real reproducing script? So far your examples are fail at
best..
------------------------------------------------------------------------
[2010-02-22 03:04:20] nonsqtr at hotmail dot com
It gets even more interesting. The value passed into a function is
incorrect too.
So for instance, here's some test code:
$val = 3330;
$rc = foo($val);
function foo($amount)
{
if($amount == 3330 OR $amount == "3330")
{
// this line of code is never reached
// and it doesn't matter if you typecast $amount in the test
}
else if($amount > 3329 AND $amount < 3331)
{
// this line of code is reached
}
// ...
}
Again, this only happens with the two specific number 3330 and 6660, as
near as I can tell and according to my testing so far.
------------------------------------------------------------------------
[2010-02-22 02:08:17] nonsqtr at hotmail dot com
Description:
------------
This line of code
$tag = $amount % 100
gives an incorrect result when "amount" is set to one of two specific
numbers: 3330 and 6660
Any other number seems to work fine - for example, 60, 660, 66660,
666660, .... work fine. Only 6660 fails, it gives the incorrect result
59.
And, 3330 gives the incorrect result 29.
It doesn't matter if you typecast amount before calculating.
And it doesn't matter if it's a string or an int being passed in, the
result is still 29 or 59.
I ran a whole series of numbers against this, and 3330 and 6660 are the
only ones that fail (at least, that I found).
I'm on a hosted system, so I can't tell you how PHP was compiled. But
I've reproduced this on three hosted systems so far, with different PHP
versions, going all the way back to 5.2.5, so it looks like it's just a
hidden bug that's been lurking around for a while.
Once again, casting does not resolve this problem, and using the round()
function doesn't resolve it either. The operator % is what's failing
here.
Reproduce code:
---------------
---
From manual page: http://www.php.net/function.round#Description
---
$tag = $amount % 100
When $amount is an int, a string, or a float
Expected result:
----------------
When $amount is set to 6660, I expect to see a 60 come back
Actual result:
--------------
I get a 59 instead of a 60
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51108&edit=1