ID:               51108
 Updated by:       j...@php.net
 Reported By:      nonsqtr at hotmail dot com
-Status:           Open
+Status:           Feedback
 Bug Type:         Math related
 Operating System: Linux 2.6.18-164.6.1.el5 #1 SMP
 PHP Version:      5.2.12
 New Comment:

Where's the real reproducing script? So far your examples are fail at
best..


Previous Comments:
------------------------------------------------------------------------

[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/?id=51108&edit=1

Reply via email to