ID: 47168
Updated by: [email protected]
Reported By: exploringbinary at gmail dot com
-Status: Bogus
+Status: Open
Bug Type: Math related
Operating System: Windows
PHP Version: 5.2.8
-Assigned To:
+Assigned To: rasmus
New Comment:
I agree with Rick here. The 40-digit limit is strangely arbitrary and
doesn't match the size of the double. We are hiding available precision
here.
Previous Comments:
------------------------------------------------------------------------
[2009-01-30 21:51:56] exploringbinary at gmail dot com
Since I didn't find anything in the documentation, I poked around in
the source code. In php-5.2.8\ext\standard\formatted_print.c I found
this: #define MAX_FLOAT_PRECISION 40
There is no explanation as to why it's capped at 40.
What's interesting is that this code prints only 40 decimal places
(0.0000000000004547473508864641189575195312), without a warning:
<?php
/* Print 2^-41 */
$dyadic = 0.00000000000045474735088646411895751953125;
printf ("%1.2147483646f",$dyadic);
?>
While this code prints ``Warning: printf() [function.printf]: Precision
must be greater than zero and less than 2147483647 ...''
<?php
/* Print 2^-41 */
$dyadic = 0.00000000000045474735088646411895751953125;
printf ("%1.2147483647f",$dyadic);
?>
If you're going to warn about a maximum, why not warn about the real
maximum -- 40, not 2147483646?
I wrote an article about how this issue is handled in PHP and 7 other
languages at
http://www.exploringbinary.com/print-precision-of-dyadic-fractions-varies-by-language/
------------------------------------------------------------------------
[2009-01-20 22:26:19] exploringbinary at gmail dot com
Hi,
I read the documentation and the "how to submit a bug report" and
googled as well before (and now again after) submitting the bug report.
I cannot see discussion of this limit. Could you please give me a more
specific link?
I would also like to know the rationale for the limit. Why doesn't
javascript or gcc have this limit? Why did Microsoft accept my bug
report for a similar problem in VS? It seems like something PHP should
consider, or at least give rationale for why not.
Thanks.
------------------------------------------------------------------------
[2009-01-20 18:04:24] [email protected]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php
PHP string formatting functions will not permit float precision beyond
40 characters, this is a hard limit. A notice was however added to
indicate that this is happening.
------------------------------------------------------------------------
[2009-01-20 14:55:45] exploringbinary at gmail dot com
Description:
------------
printf will not print more than 40 decimal places, even if the format
specifier asks for more. For example,
<?php
$value = 0.00000000000000011102230246251565404236316680908203125; //
2^-53
printf ("%1.53f",$value); // Prints only 40 decimal places:
0.0000000000000001110223024625156540423632
?>
Contrast this with javascript:
<script type="text/javascript">
var value = 0.00000000000000011102230246251565404236316680908203125;
// 2^-53
var result = value.toFixed(53);
document.write (result); // Prints all 53 decimal places
</script>
I reported a similar bug in Visual Studio, which Microsoft acknowledged
as an error:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=329278
Reproduce code:
---------------
<?php
$value = 0.00000000000000011102230246251565404236316680908203125; //
2^-53
printf ("%1.53f",$value); // Prints only 40 decimal places:
0.0000000000000001110223024625156540423632
?>
Expected result:
----------------
I expect to see all 53 decimal places printed:
0.00000000000000011102230246251565404236316680908203125
Actual result:
--------------
Only 40 decimal places are printed:
0.0000000000000001110223024625156540423632
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=47168&edit=1