php.net |  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
 [2012-03-31 12:32 UTC] hesam at kolahan dot com
Description:
------------
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)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-31 15:49 UTC] rasmus@php.net
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:
http://www.floating-point-gui.de/

Thank you for your interest in PHP.

.
 [2012-03-31 15:49 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 18:01:29 2024 UTC