php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54994 Weird float values on increment
Submitted: 2011-06-04 17:01 UTC Modified: 2011-06-04 17:09 UTC
From: technique at ircf dot fr Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.3SVN-2011-06-04 (snap) OS: Linux Ubuntu PHP 5.3.5-1ubuntu7
Private report: No CVE-ID: None
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:
MUST BE VALID
Solve the problem:
48 - 44 = ?
Subscribe to this entry?

 
 [2011-06-04 17:01 UTC] technique at ircf dot fr
Description:
------------
PHP produces much more decimals than expected when a variable is incremented in a 
for loop. On my computer, it happens around [54,66] and [84,100]. This might be 
related to the http://bugs.php.net/bug.php?id=50095 bug.

Test script:
---------------
for($i=0;$i<100;$i+=0.1) echo "$i\n";

Expected result:
----------------
0
0.1
0.2
...
99.8
99.9

Actual result:
--------------
0
0.1
0.2
...
54
54.1
54.200000000001
54.300000000001
...
66.300000000001
66.400000000001
66.5
66.6
...
83.9
84
84.099999999999
84.199999999999
...
99.899999999999
99.999999999999


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-06-04 17:09 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-06-04 17:09 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.

The number of expected decimals is given by your precision ini setting.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 07 01:01:29 2024 UTC