php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #50095 Unexpected behavior of precision of float numbers
Submitted: 2009-11-05 20:12 UTC Modified: 2009-11-05 21:22 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: majkl578 at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.11 OS: Linux Debian
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: majkl578 at gmail dot com
New email:
PHP Version: OS:

 

 [2009-11-05 20:12 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-11-05 21:22 UTC] rasmus@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://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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri May 09 20:01:27 2025 UTC