# Working with floats

- Thread: https://externals.io/message/61657
- Original archive: https://news-web.php.net/php.internals/61657
- Started by Kingsquare.nl - Robin Speekenbrink on 2012-07-24T08:01:48+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #61657 — Kingsquare.nl - Robin Speekenbrink — 2012-07-24T08:01:48+00:00

URL: https://externals.io/message/61657#61657

Hi all,

First of all, let me state that the following question is probably going 
to shun me forever due to the 'basic' nature of the question and the 
probable misunderstanding of floats in general, but still this got me 
baffled and i'd like to post this here:

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:
<?php
$i = (float)19;
var_dump($i);
$arr = array($i=>1);
var_dump($arr);

$i = (float)19.000000000000000000;
var_dump($i);
$arr = array($i=>1);
var_dump($arr);

$i = (float)18.99999999999999999;
var_dump($i);
$arr = array($i=>1);
var_dump($arr);

$i =(float) .19;
$i *= 100;
var_dump($i);
$arr = array($i=>1);
var_dump($arr);

$i =(float) 0.19;
$i *= 100;
var_dump($i);
$arr = array($i=>1);
var_dump($arr);

$i =(float) 1.19;
$i -= 1;
$i *= 100;
var_dump($i);
$arr = array($i=>1);
var_dump($arr);
?>

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)

Again, if i'm to be regarded as a traditional n00b, i understand as i've 
seen float / casting discussions before.

---

## #61659 — Ronald Chmara — 2012-07-24T08:33:39+00:00

URL: https://externals.io/message/61657#61659 · In reply to: #61657

Your CompSci education (or lack thereof) has failed you.

Use the source.

Floats behave differently under different conditions.

-Ronabop

---

## #61661 — Galen Wright-Watson — 2012-07-24T08:42:45+00:00

URL: https://externals.io/message/61657#61661 · In reply to: #61657

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:

    <?php
>     [...]
>     $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.

---

## #61662 — Kingsquare.nl - Robin Speekenbrink — 2012-07-24T08:50:57+00:00

URL: https://externals.io/message/61657#61662 · In reply to: #61661

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
> <robin@kingsquare.nl <mailto: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:
>
>         <?php
>         [...]
>         $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.
