php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52689 ban number of loops on float number
Submitted: 2010-08-24 18:46 UTC Modified: 2010-08-24 22:41 UTC
From: jakubmatas at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.3 OS: Linux
Private report: No CVE-ID: None
 [2010-08-24 18:46 UTC] jakubmatas at gmail dot com
Description:
------------
for different float values in loop (while or for) wrong number of loops. When 
number si bigger then 4.4 in condition, then one more loop is doing.

Test script:
---------------
for ($index = 3.1; $index < 4.0; $index += 0.1) {
   echo $index.'<br />';
}
for ($index = 3.1; $index < 4.4; $index += 0.1) {
   echo $index.'<br />';
}



Expected result:
----------------
number is 4.0
= OUTPUT ==============
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9

end is 3.9

number is 4.4
= OUTPUT ==============
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4
4.1
4.2
4.3

end is 4.3

Actual result:
--------------
number is 4.0
= OUTPUT ==============
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9

end is 3.9

number is 4.4
= OUTPUT ==============
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
4
4.1
4.2
4.3
4.4 < WRONG

end is 4.4 < WRONG

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-08-24 22:41 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-08-24 22:41 UTC] johannes@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.

.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sat Mar 14 14:00:02 2026 UTC