php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28542 Floating point error while adding values to a float var
Submitted: 2004-05-27 15:01 UTC Modified: 2004-05-27 15:14 UTC
From: tny at eyefi dot nl Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.3.6 OS: Linix / BSDs
Private report: No CVE-ID: None
 [2004-05-27 15:01 UTC] tny at eyefi dot nl
Description:
------------
When adding a float value to a variable (starting with a negative value) in a loop the result that should be zero (0) is nearly zero, but not exact zero.

Reproduce code:
---------------
<?php

for ($i = -1.0; $i <= 1.0; $i += 0.1) {
  echo $i . "\n";
}

?>

Expected result:
----------------
-1
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1

Actual result:
--------------
-1
-0.9
-0.8
-0.7
-0.6
-0.5
-0.4
-0.3
-0.2
-0.1
-1.38777878078E-16
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-27 15:14 UTC] derick@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.
 
Thank you for your interest in PHP.

.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 23:01:29 2024 UTC