Working with floats

From: Date: Tue, 24 Jul 2012 08:01:48 +0000
Subject: Working with floats
Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
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.



Thread (4 messages)

« previous php.internals (#61657) next »