Bug #64682 [Opn->Nab]: failing to add 0.001 multiple times
| From: | johannes@php.net | Date: | Sat, 20 Apr 2013 07:50:18 +0000 |
| Subject: | Bug #64682 [Opn->Nab]: failing to add 0.001 multiple times | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-178553@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=64682&edit=1
ID: 64682
Updated by: johannes@php.net
Reported by: easteregg at verfriemelt dot org
Summary: failing to add 0.001 multiple times
-Status: Open
+Status: Not a bug
Type: Bug
Package: Math related
Operating System: Linux and Windows
PHP Version: 5.4.14
Block user comment: N
Private report: N
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.
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.
.
Previous Comments:
------------------------------------------------------------------------
[2013-04-20 07:42:15] easteregg at verfriemelt dot org
Description:
------------
Hi,
first some informations: vanilla php 5.4.14 without any changes from your
website, and a php 5.4.13 on a linux host.
C:\Users\Administrator>php -v
PHP 5.4.12 (cli) (built: Feb 19 2013 21:26:17)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
root@verfriemelt:~# php -v
PHP 5.4.13-1~dotdeb.1 (cli) (built: Mar 21 2013 08:29:56)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
------------
i have a number like 8 and add 0.001 while this number reaches 11. i suspected
the numbers in between should look like 8.999 and 10.743 but instead i got some
like this:
8.9899999999999
and i noticed a gap between some numbers.
eg:
8.09 + 0.001 equals 8.090999999999
i suspected a problem with my linux box and tested it with my windows
workstation, same result. so i guess its a php internal error.
this doest not occur, when i simple add 0.001 to 8.09 so i guess it has
something to do with the for()
Test script:
---------------
<?php
for ($i = 8; $i< 11; $i += 0.001) {
echo $i . "\n";
}
Expected result:
----------------
[...]
8.08
8.081
8.082
8.083
8.084
8.085
8.086
8.087
8.088
8.089
8.09
8.091
8.092
8.093
8.094
8.095
8.096
8.097
8.098
8.099
8.1
[...]
Actual result:
--------------
8.08
8.081
8.082
8.083
8.084
8.085
8.086
[...]
8.087
8.088
8.089
8.09
8.0909999999999
8.0919999999999
8.0929999999999
8.0939999999999
8.0949999999999
8.0959999999999
8.0969999999999
8.0979999999999
8.0989999999999
8.0999999999999
8.1009999999999
[...]
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=64682&edit=1