php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50824 modulus (mod) returns wrong value
Submitted: 2010-01-22 14:46 UTC Modified: 2010-01-22 21:49 UTC
From: jancha at trave dot lv Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.12 OS: Gentoo
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: jancha at trave dot lv
New email:
PHP Version: OS:

 

 [2010-01-22 14:46 UTC] jancha at trave dot lv
Description:
------------
We came accorss following  case:

<?php echo 72.6 * 100 % 100;

You would expect: 60
But you get: 59

We did further analysis and got following results:
[OK] 72.0:0
[FAIL] 72.1:9 
[OK] 72.2:20 
[OK] 72.3:30
[OK] 72.4:40
[OK] 72.5:50
[FAIL] 72.6:59
[OK] 72.7:70
[OK] 72.8:80
[OK] 72.9:90

That makes us think that bug happens, if last digit is either 1 or 6. Also, according to manual, operands of modulus are converted to integers by stripping decimal part away - in our case that is not reason, as 72.6 * 100 = 7260 (float). And conversion to int should result with 7260.

Reproduce code:
---------------
<?php echo 72.6 * 100 % 100;

Expected result:
----------------
60

Actual result:
--------------
59

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-01-22 15:07 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://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.

72.6 * 100 is not exactly 7260
 [2010-01-22 21:49 UTC] jancha at trave dot lv
temporary patch would be:

<?php echo round(72.6 * 100) % 100;

That would indeed result with: 60
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 23:01:29 2024 UTC