php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18157 precision ini value affects internal calculations too
Submitted: 2002-07-04 04:31 UTC Modified: 2002-07-04 12:42 UTC
From: cnovak at gmx dot net Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.2.1 OS: Windows XP Pro
Private report: No CVE-ID: None
 [2002-07-04 04:31 UTC] cnovak at gmx dot net
By default precision in php.ini is set to 14 significant digits. I would assume, that this is for printing float results only. Instead, the precision argument is used for storing values in databases too. Check the following code:

01 for ($x = 0; $x < 64; $x++) {
02    $group_bitfield = pow(2, $x);
03    print " $group_bitfield<br>";
04 }

The above yields precision errors at 2^48, 2^49, 2^51, 2^52, 2^54. If this is for output only, fine. But the issue is that these values are also _incorrectly_ stored in an INT(20) field on MySQL. Explicit type casting does not help either. Using arbitrary math is not an option for speed and availabilty at various ISP's.

Configuration:
WINNT 
Apache/1.3.26 (Win32) mod_ssl/2.8.10 OpenSSL/0.9.6d PHP version: 4.2.1 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-04 04:38 UTC] sander@php.net
PHP can't handle numbers larger than 2^32. Use the gmp or bcmath extension.
(the ini setting precision is only for floats (both calculating and printing), you're talking about integers).
 [2002-07-04 05:25 UTC] cnovak at gmx dot net
I know that the variable is an integer and that the float/double cast is only an internal way of PHP handling larger integers. The manual (http://www.php.net/manual/en/language.types.integer.php) states the following:

Integer overflow
If you specify a number beyond the _bounds_ of the integer type, it will be interpreted as a float instead. Also, if you perform an operation that results in a number beyond the bounds of the integer type, a float will be returned instead. 
--

I would assume from the above detail that PHP would cast my variable from int to float when reaching the 32bit boundary. I understand that beyond 2^63 that my number will be incorrect. The strange issue is that this works correctly until 2^47. When precision is set to 20, it works as expected. Since the pow(2, int) does not create any fractions I would assume that the casting works as expected. Same happens using a left bit shift.
 [2002-07-04 07:17 UTC] cnovak at gmx dot net
chg. status
 [2002-07-04 12:42 UTC] sander@php.net
AFAIK, all the numeric operations are done by the OS (or the OS' libraries), not by PHP itself.
Precision, boundaries, all depend on the OS and system.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC