php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65676 position inaccurate a.xyz
Submitted: 2013-09-15 16:31 UTC Modified: 2013-09-15 19:58 UTC
From: pcmadnessltd at yahoo dot co dot uk Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.4.19 OS: ubuntu 10.04 pae
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: pcmadnessltd at yahoo dot co dot uk
New email:
PHP Version: OS:

 

 [2013-09-15 16:31 UTC] pcmadnessltd at yahoo dot co dot uk
Description:
------------
position inaccurate when adding 0.001 to a value

possible solution round($num, 3);

Test script:
---------------
   	echo '<table>';
    	$ii =0;
    	for($i =1; $i<= 10000; $i++)
    		   	{
     		   		echo '<tr>';
    		   		echo '<td>';
    		   		echo $ii;
    		   		echo '</td>';
    		   		echo '</tr>';
    		   		$ii = $ii +0.001;
    		   	}
    	   	echo '</table>';

Expected result:
----------------
1.459		
1.46		
1.461		
1.462
1.463	
1.464
1.465
1.466
...
2.369	
2.370
2.371
2.372
2.373
2.374
2.375

...
3.278	
3.279
3.280
3.281

Actual result:
--------------
1.459		
1.46		
1.4609999999999		
1.4619999999999		
1.4629999999999		
1.4639999999999		
1.4649999999999		
1.4659999999999		
...
2.3689999999999		
2.3699999999999		
2.3709999999998		
2.3719999999998		
2.3729999999998		
2.3739999999998		
2.3749999999998	
...
3.2769999999998		
3.2779999999998		
3.2789999999997		
3.2799999999997		
	
and so on

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-09-15 19:58 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2013-09-15 19:58 UTC] nikic@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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 15:01:33 2024 UTC