#50824 [Opn->Bgs]: modulus (mod) returns wrong value
| From: | rasmus@php.net | Date: | Fri, 22 Jan 2010 15:07:26 +0000 |
| Subject: | #50824 [Opn->Bgs]: modulus (mod) returns wrong value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-146872@lists.php.net to get a copy of this message | ||
ID: 50824
Updated by: rasmus@php.net
Reported By: jancha at trave dot lv
-Status: Open
+Status: Bogus
Bug Type: Math related
Operating System: Gentoo
PHP Version: 5.2.12
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[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