#28542 [Opn->Bgs]: Floating point error while adding values to a float var

From: Date: Thu, 27 May 2004 13:15:09 +0000
Subject: #28542 [Opn->Bgs]: Floating point error while adding values to a float var
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-60071@lists.php.net to get a copy of this message
ID: 28542 Updated by: derick@php.net Reported By: tny at eyefi dot nl -Status: Open +Status: Bogus Bug Type: Variables related Operating System: Linix / BSDs PHP Version: 4.3.6 New Comment: 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. . Previous Comments: ------------------------------------------------------------------------ [2004-05-27 15:01:29] 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 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=28542&edit=1

« previous php.bugs (#60071) next »