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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: technique at ircf dot fr
New email:
PHP Version: OS:

 

 [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-2025 The PHP Group
All rights reserved.
Last updated: Thu Jan 02 18:01:29 2025 UTC