Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61661 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 43323 invoked from network); 24 Jul 2012 08:43:30 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 24 Jul 2012 08:43:30 -0000 Authentication-Results: pb1.pair.com smtp.mail=ww.galen@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ww.galen@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.170 as permitted sender) X-PHP-List-Original-Sender: ww.galen@gmail.com X-Host-Fingerprint: 209.85.212.170 mail-wi0-f170.google.com Received: from [209.85.212.170] ([209.85.212.170:42680] helo=mail-wi0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3D/99-17533-1306E005 for ; Tue, 24 Jul 2012 04:43:30 -0400 Received: by wibhq12 with SMTP id hq12so2997560wib.5 for ; Tue, 24 Jul 2012 01:43:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=17QiCxYFXafu+raAYusW7hXrduePHTaA0pg2fKvwbdw=; b=iCC1vW3mREDI6KRpNFTqslugWUKGS1cF+RzZ0M6mVOmrvGzMjNAWEsHOUfoxSjfLyo u8IBMacHXxWlm/77mMT9dtN1OYMA8ZQWcJbo1g9G/hUsD7ZlMm/ptnkq6ZL0rsTHbozY arNnnj3Ki3tugrFDu3rvM+s+84br04p1WBwzXDiPJWok0FRlIyOnCZR2pbKh5yrv0w2u fM0Zwgb1fKn4MJ1oWbgGx/9AlYxYSNreAueCFDuSwA4Pgd8FmPIeUWzGnPXLSt9q02iq O7dOFy8e/1TD4sth+JprbBKytBWIZnOuICEhqpvM6NYMsYNU4DZYVUVkEIOJXBStGfBZ ThQA== Received: by 10.216.184.196 with SMTP id s46mr9332017wem.66.1343119406266; Tue, 24 Jul 2012 01:43:26 -0700 (PDT) MIME-Version: 1.0 Received: by 10.194.17.100 with HTTP; Tue, 24 Jul 2012 01:42:45 -0700 (PDT) In-Reply-To: <500E566C.7040206@kingsquare.nl> References: <500E566C.7040206@kingsquare.nl> Date: Tue, 24 Jul 2012 01:42:45 -0700 Message-ID: To: "Kingsquare.nl - Robin Speekenbrink" Cc: internals@lists.php.net Content-Type: multipart/alternative; boundary=0016e649863e19fb5704c58f5b3c Subject: Re: [PHP-DEV] Working with floats From: ww.galen@gmail.com (Galen Wright-Watson) --0016e649863e19fb5704c58f5b3c Content-Type: text/plain; charset=ISO-8859-1 On Tue, Jul 24, 2012 at 1:01 AM, Kingsquare.nl - Robin Speekenbrink < robin@kingsquare.nl> 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. --0016e649863e19fb5704c58f5b3c--