#50824 [Bgs]: modulus (mod) returns wrong value
ID: 50824
User updated by: jancha at trave dot lv
Reported By: jancha at trave dot lv
Status: Bogus
Bug Type: Math related
Operating System: Gentoo
PHP Version: 5.2.12
New Comment:
temporary patch would be:
<?php echo round(72.6 * 100) % 100;
That would indeed result with: 60
Previous Comments:
------------------------------------------------------------------------
[2010-01-22 15:07:25] 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 14:46:10] 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
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50824&edit=1
Thread (3 messages)