#50095 [Opn->Bgs]: Unexpected behavior of precision of float numbers
| From: | rasmus@php.net | Date: | Thu, 05 Nov 2009 21:22:52 +0000 |
| Subject: | #50095 [Opn->Bgs]: Unexpected behavior of precision of float numbers | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-144307@lists.php.net to get a copy of this message | ||
ID: 50095
Updated by: rasmus@php.net
Reported By: majkl578 at gmail dot com
-Status: Open
+Status: Bogus
Bug Type: Scripting Engine problem
Operating System: Linux Debian
PHP Version: 5.2.11
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://docs.sun.com/source/806-3568/ncg_goldberg.html
Thank you for your interest in PHP.
The output is exactly as it should be. 20.2 at a high precision shows
you the actual value that the computer is using to try to represent
20.2. There is no such thing as precisely 20.2 on a computer. It
cannot be represented using the floating point mechanism computers use.
Previous Comments:
------------------------------------------------------------------------
[2009-11-05 20:12:17] majkl578 at gmail dot com
Description:
------------
When dumping a float number, it is represented incorrectly. Generally,
in some cases, PHP handles float numbers in invalid format.
It is related to a setting of php.ini directive "precision". When a
float number with one decimal place is dumped and high precision is set,
it is not dumped in an original format, but in damaged one.
Additional info:
32-bit system
CPU: AMD Athlon64
Reproduce code:
---------------
foreach (range(0, 30) as $precision) {
ini_set('precision', $precision);
echo $precision . ': ';
var_dump(20.2);
}
Expected result:
----------------
0: float(20)
1: float(20.2)
2: float(20.2)
3: float(20.2)
4: float(20.2)
5: float(20.2)
6: float(20.2)
7: float(20.2)
8: float(20.2)
9: float(20.2)
10: float(20.2)
11: float(20.2)
12: float(20.2)
13: float(20.2)
14: float(20.2)
15: float(20.2)
16: float(20.2)
17: float(20.2)
18: float(20.2)
19: float(20.2)
20: float(20.2)
21: float(20.2)
22: float(20.2)
23: float(20.2)
24: float(20.2)
25: float(20.2)
26: float(20.2)
27: float(20.2)
28: float(20.2)
29: float(20.2)
30: float(20.2)
Actual result:
--------------
0: float(2.0E+1)
1: float(2.0E+1)
2: float(20)
3: float(20.2)
4: float(20.2)
5: float(20.2)
6: float(20.2)
7: float(20.2)
8: float(20.2)
9: float(20.2)
10: float(20.2)
11: float(20.2)
12: float(20.2)
13: float(20.2)
14: float(20.2)
15: float(20.2)
16: float(20.2)
17: float(20.199999999999999)
18: float(20.1999999999999993)
19: float(20.19999999999999929)
20: float(20.199999999999999289)
21: float(20.1999999999999992895)
22: float(20.19999999999999928946)
23: float(20.199999999999999289457)
24: float(20.1999999999999992894573)
25: float(20.19999999999999928945726)
26: float(20.199999999999999289457264)
27: float(20.1999999999999992894572642)
28: float(20.19999999999999928945726424)
29: float(20.19999999999999928945726424)
30: float(20.1999999999999992894572642399)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=50095&edit=1