php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37202 Iterations with bad precision
Submitted: 2006-04-25 20:42 UTC Modified: 2006-04-25 20:43 UTC
From: ruben_ruben at yahoo dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.1.2 OS: win xp pro sp2
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: ruben_ruben at yahoo dot com
New email:
PHP Version: OS:

 

 [2006-04-25 20:42 UTC] ruben_ruben at yahoo dot com
Description:
------------
In loops like While and For with increases with point decimal fails the precision.

Reproduce code:
---------------
$i=71;
$i2 = 82;
$increases = .1;
for($i; $i <= $i2; $i = $i + $increases)
	{
	echo "$i<br>";
	}


//**** or ******

$i=1.5;
$i2 = 2;
$increases = .001;
while($i <= $i2)
	{
	echo "$i<br>";
	$i = $i + $increases;
	}

Expected result:
----------------
with
$i=71;
$i2 = 82;
$increases = .1;
it must be:
71
71.1
.
.
.
81.9
82
***************
with
$i=1.5;
$i2 = 2;
$increases = .001;
it must be:
1.5
1.501
1.502
.
.
.
1.999
2

Actual result:
--------------
both with
$i=71;
$i2 = 82;
generates:
71
71.1
.
.
79.8
79.899999999999
79.999999999999
.
.
81.899999999999
81.999999999999
***************
both with
$i=1.5;
$i2 = 2;
$increases = .001;
generates:
1.5
1.501
1.502
.
.
1.954
1.9549999999999
1.9559999999999
.
.
1.9989999999999
1.9999999999999

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-25 20:43 UTC] tony2001@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-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 29 16:00:03 2025 UTC