php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51108 Remainder operator (%) FAILS with two specific numbers
Submitted: 2010-02-22 02:08 UTC Modified: 2010-03-03 18:34 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: nonsqtr at hotmail dot com Assigned:
Status: No Feedback Package: Math related
PHP Version: 5.2.12 OS: Linux 2.6.18-164.6.1.el5 #1 SMP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
24 + 13 = ?
Subscribe to this entry?

 
 [2010-02-22 02:08 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-22 03:04 UTC] 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 10:25 UTC] jani@php.net
Where's the real reproducing script? So far your examples are fail at best..
 [2010-03-03 18:34 UTC] php-bugs at lists dot php dot net
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".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 18:01:28 2024 UTC