ID: 43053 Comment by: rbro at hotmail dot com Reported By: owner at dragon-hearts dot net Status: Verified Bug Type: Scripting Engine problem Operating System: Centos4 PHP Version: 5CVS-2007-10-25 New Comment:
Thanks for your help - that definitely seems to fix the problem. I manually applied your changes since I wanted to test it against the PHP 5.2.5 source, but I'm getting consistent output now when working with floats. Previous Comments: ------------------------------------------------------------------------ [2008-04-18 01:34:28] int-e at gmx dot de Here's a patch that fixes the problem. http://int-e.home.tlink.de/php/php-printf.patch There are three changes here: 1) get rid of a hack in zend_dtoa() that sometimes kept trailing zeros (which caused the inconsistent behaviour between printing 1200000 and 1300000) 2) change php_gcvt() to switch to E format for large numbers if the number wouldn't fit into precision digits. 3) update a few tests to reflect the change to the precision semantics. I believe that in each of these cases, the test was flawed. Note that a precision of 14 (the default) means that 15 digit numbers should be printed in E notation, at least that's how I read the sprintf documentation. ------------------------------------------------------------------------ [2008-04-15 22:37:33] rbro at hotmail dot com Your examples work correctly for me in PHP 5.2.1, so I'm not sure if the issue was caused by change you mentioned or something else, but it definitely happens for me in PHP 5.2.2 and later. ------------------------------------------------------------------------ [2008-04-15 20:43:16] marek dot chodor at gmail dot com on PHP Version 5.2.6RC1-pl1-gentoo and PHP 5.2.4 Fedora7 the same problem $ php -r "echo 120000000;" 120000000 $ php -r "echo (double)120000000;" 1.2E+8 $ php -r "echo (int)(double)120000000;" 120000000 It could be related to this change in PHP 5.2.1: # Changed double-to-string utilities to use BSD implementation. (Dmitry, Tony) ------------------------------------------------------------------------ [2008-04-15 09:08:48] ali at adcworks dot com I am receiving longs via SOAP responses which represent IDs of accounts. Due to some issue with PHP and long values when we retrieve accounts using the same IDs we receive, we get other peoples' accounts back - not good! I have found the core problem and the snippet of code below shows that the correct ID of 285000000003622757 is transformed into the incorrect ID of 285000000003622752 (last number goes down 5). <?php $long = 285000000003622757; function f0($long) { return $long; } function f1($long) { return sprintf ( "%.0f", $long ); } function f2($long) { return number_format($long, 0, '.', ''); } ?> <h1>F0 (no formatting) </h1> <?= f0($long) ?> <h1>F1 (sprintf) </h1> <?= f1($long) ?> <h1>F2 (number_format) </h1> <?= f2($long) ?> OUTPUT ====== F0 (no formatting) 2.85000000004E+17 F1 (sprintf) 285000000003622752 F2 (number_format) 285000000003622752 ------------------------------------------------------------------------ [2008-04-04 17:46:57] admin at vr-realty dot com I had a working database for my office Stock Trading Pool when AAAHHHHkkk 1.23E+007. The non programmers there were all freaked the hell out. They thought E meant Error and that we had lost all of our money. trying to tell 70+ people, that haven't seen Scientific notation since they were in 9th grade, that it's not a problem and we will fix it was no easy task. By the time it was brought to my attention people had already started trying to figure out how to sue me for losing their money, yeah it was a mess. And they didn't know that what they see is just a thing for them to see and reference and really could be forgotten and not updated for days. anyways I wen through and changed the MySql DB from floats to VarChars. and type casted the $vars I pulled from the DB to float using $curprice = (float)$price; and the ones that could be int like the number of shares $curshares = (int)$shares; then I did the math $total = (float)($price*$shares); now the General population is feeling at ease and the death threats have subsided. I'm still trying to understand why Scientific notation would come into play. maybe it sould be an obscure function like INT_TO_SCI($var) or num_to_sci($var) .. like the automatic and natural way to have the numbers over millions sould be preserved as a whole long string no matter what the length with the option to convert it to scientific notation if that suits your needs. my 2cents ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/43053 -- Edit this bug report at http://bugs.php.net/?id=43053&edit=1