Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61662 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 44856 invoked from network); 24 Jul 2012 08:50:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Jul 2012 08:50:59 -0000 Authentication-Results: pb1.pair.com header.from=robin@kingsquare.nl; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=robin@kingsquare.nl; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain kingsquare.nl from 212.54.42.167 cause and error) X-PHP-List-Original-Sender: robin@kingsquare.nl X-Host-Fingerprint: 212.54.42.167 smtpq4.tb.mail.iss.as9143.net Linux 2.6 Received: from [212.54.42.167] ([212.54.42.167:57100] helo=smtpq4.tb.mail.iss.as9143.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F2/F9-17533-2F16E005 for ; Tue, 24 Jul 2012 04:50:58 -0400 Received: from [212.54.42.135] (helo=smtp4.tb.mail.iss.as9143.net) by smtpq4.tb.mail.iss.as9143.net with esmtp (Exim 4.71) (envelope-from ) id 1StapT-0005bP-1L for internals@lists.php.net; Tue, 24 Jul 2012 10:50:55 +0200 Received: from 5351426c.cm-6-2b.dynamic.ziggo.nl ([83.81.66.108] helo=[10.0.0.110]) by smtp4.tb.mail.iss.as9143.net with esmtpsa (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1StapS-0000Ve-Mu for internals@lists.php.net; Tue, 24 Jul 2012 10:50:55 +0200 Message-ID: <500E61F1.9040904@kingsquare.nl> Date: Tue, 24 Jul 2012 10:50:57 +0200 Organization: Kingsquare Information Services User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:14.0) Gecko/20120713 Thunderbird/14.0 MIME-Version: 1.0 To: internals@lists.php.net References: <500E566C.7040206@kingsquare.nl> In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Authenticated-Sender: kingsquare@ziggo.nl (via SMTP) X-Ziggo-spambar: / X-Ziggo-spamscore: -0.7 X-Ziggo-spamreport: ALL_TRUSTED=-1,BAYES_00=-1.9,CM_META_TB_NOARR=0.5,FF_IHOPE_YOU_SINK=1.699 X-Ziggo-Spam-Status: No X-Spam-Status: No X-Spam-Flag: No Subject: Re: [PHP-DEV] Working with floats From: robin@kingsquare.nl ("Kingsquare.nl - Robin Speekenbrink") Thanks for the reply. I know of the fact that converting the key to a string would be the 'correct' way of using it, i was more curious as to the difference in the var_dump-s... But the precision setting is new to me, i'll have a look into that... That would indeed make the differences in the resulting $i visible and thus explainable to others. Regards, Robin Speekenbrink Op dinsdag 24 juli 2012 10:42:45, Galen Wright-Watson schreef: > > > On Tue, Jul 24, 2012 at 1:01 AM, Kingsquare.nl - Robin Speekenbrink > > wrote: > > Hi all, > [...] > > Why does the last of the following examples lower my key to 18 BUT > does the var dump of the float clearly state 'float(19)' as the > actual value: > > [...] > $i =(float) 1.19; > $i -= 1; > $i *= 100; > var_dump($i); > $arr = array($i=>1); > var_dump($arr); > ?> > > > When displaying floating point numbers, PHP uses the precision config > option (http://php.net/precision) so as to shorten output (and to hide > rounding errors in some circumstances). If you increase precision to > 17 digits or so (64-bit IEEE floating point numbers have a decimal > precision of around 15 to 17 digits), the var_dump will reveal that $i > is actually 18.999999999999993.... When you use it as an array index, > it ends up converted to an integer, which is done by truncation. If > instead you were to convert $i to a string when using it as an index: > > array("$i" => 1); > array((string)$i => 1); > > you'd get a result more along the lines that you expect. > > I do know this is not really an internals thing, but after > fiddling with this for some time, i gave up (bug 32671 might > relate to this) > > > It's central to what you see, but note that it was decided that the > behavior was correct; the bug was in the documentation. > > Again, if i'm to be regarded as a traditional n00b, i understand > as i've seen float / casting discussions before. > > > I think those were more of the "how should PHP handle this" rather > than the "why does it do this" variety. There is probably a more > suitable venue for your question than the internals list; perhaps the > general usage list. I can be a bit grumpy about these things, but from > what I've seen, the PHP community likes to be inclusive, so I doubt > it's a big deal.