|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #61580 wrong calculation of Modulus operator
Submitted: 2012-03-31 12:32 UTC Modified: 2012-03-31 15:49 UTC
From: hesam at kolahan dot com Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.3.10 OS: CentOS
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 !
Your email address:
Solve the problem:
48 - 12 = ?
Subscribe to this entry?

 [2012-03-31 12:32 UTC] hesam at kolahan dot com
The operator % can not calculate the following calculation:

Wrong calculation:
echo (16.49*100)%100; // prints 48  (should be 49)

Right calculations:
echo (13.49*100)%100; // prints 49
echo (14.49*100)%100; // prints 49
echo (15.49*100)%100; // prints 49

Test script:
echo "A wrong calculation:";
echo "<br>(16.49*100)%100 = ".((16.49*100)%100)." *By PHP";
echo "<br>(16.49*100)%100 = 1649%100 = 49 *By Hand";

echo "<br><br>A right calculation:";
echo "<br>(15.49*100)%100 = ".((15.49*100)%100)." *By PHP";
echo "<br>(15.49*100)%100 = 1549%100 = 49 *By Hand";

Expected result:
All results should be the same value (by hand == by PHP)


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-31 15:49 UTC]
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:

Thank you for your interest in PHP.

 [2012-03-31 15:49 UTC]
-Status: Open +Status: Not a bug
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue Oct 03 07:01:24 2023 UTC