Dumb Question - Casting

--00163683351c90d872047fe1e3cf
Content-Type: text/plain; charset=ISO-8859-1

Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad
rusty.

Can someone explain why the second expression in this code snippet evaluates
to 7 and not 8?

$a = (int) (0.1 +0.7);

echo "$a\n";

$x = (int) ((0.1 + 0.7) * 10);

echo "$x\n";

$y = (int) (8);

echo "$y\n";

--00163683351c90d872047fe1e3cf--
Chuck Carson [ Do, 18 Februar 2010 16:47 ] [ ID #2032730 ]

Re: Dumb Question - Casting

--=-xaSqGDr5Js3wnyrJG+/c
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Thu, 2010-02-18 at 09:47 -0600, Chuck wrote:

> Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad
> rusty.
>
> Can someone explain why the second expression in this code snippet evaluates
> to 7 and not 8?
>
> $a = (int) (0.1 +0.7);
>
> echo "$a\n";
>
> $x = (int) ((0.1 + 0.7) * 10);
>
> echo "$x\n";
>
> $y = (int) (8);
>
> echo "$y\n";


It works as expected if you take out the int() parts in each line. I'm
not sure why, but the use of int() seems to be screwing around with the
results. That's why the first line outputs a 0.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-xaSqGDr5Js3wnyrJG+/c--
Ashley Sheridan [ Do, 18 Februar 2010 16:50 ] [ ID #2032731 ]

Re: Dumb Question - Casting

On Thu, Feb 18, 2010 at 10:50 AM, Ashley Sheridan
<ash [at] ashleysheridan.co.uk> wrote:
> On Thu, 2010-02-18 at 09:47 -0600, Chuck wrote:
>
>> Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad
>> rusty.
>>
>> Can someone explain why the second expression in this code snippet evaluates
>> to 7 and not 8?
>>
>> $a = (int) (0.1 +0.7);
>>
>> echo "$a\n";
>>
>> $x = (int) ((0.1 + 0.7) * 10);
>>
>> echo "$x\n";
>>
>> $y = (int) (8);
>>
>> echo "$y\n";
>
>
> It works as expected if you take out the int() parts in each line. I'm
> not sure why, but the use of int() seems to be screwing around with the
> results. That's why the first line outputs a 0.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>

Another fine example of floating point math.

<?php

$x = ((0.1 + 0.7) * 10);

echo "((0.1 + 0.7) * 10) === $x\n";
// ((0.1 + 0.7) * 10) === 8


var_dump(8 == $x);
// bool(false)

var_dump($x - (int) $x);
// float(1)

var_dump(8 - $x);
// float(8.8817841970013E-16)

?>

Andrew

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Andrew Ballard [ Do, 18 Februar 2010 17:03 ] [ ID #2032732 ]

Re: Dumb Question - Casting

On Thu, Feb 18, 2010 at 16:47, Chuck <chuck.carson [at] gmail.com> wrote:
> Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad
> rusty.
>
> Can someone explain why the second expression in this code snippet evaluates
> to 7 and not 8?
>
> $a = (int) (0.1 +0.7);
>
> echo "$a\n";
>
> $x = (int) ((0.1 + 0.7) * 10);
>
> echo "$x\n";
>
> $y = (int) (8);
>
> echo "$y\n";
>

The reason why you get 7 instead of 8 is because you are using
floating point arithmetic. 0.1 (i.e. the fraction 1/10) does not have
a finite representation in base 2 (like you cannot finitely represent
1/3 in base 10). So the number 0.1 is represented in the computer as a
number that is strictly less than 0.1 so when you do 0.1+0.7=x then
you have x<0.8 in the computer (think 7.9999999...). When you cast to
int you just truncate the number, i.e. you chop off the fractional
part leaving you with 7.

--
Daniel Egeberg

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Daniel Egeberg [ Do, 18 Februar 2010 17:03 ] [ ID #2032733 ]

Re: Dumb Question - Casting

--------------000707080504020206050703
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

According to the PHP manual using the same expression, "Never cast an
unknown fraction to integer, as this can sometimes lead to unexpected
results". My guess is that since it is an expression of floating
points, that the result is not quite 8 (for whatever reason).
Therefore, it is rounded towards 0. Of course, that is only a guess,
and I have no true documentation on it.

Ashley Sheridan wrote:
> On Thu, 2010-02-18 at 09:47 -0600, Chuck wrote:
>
>
>> Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad
>> rusty.
>>
>> Can someone explain why the second expression in this code snippet evaluates
>> to 7 and not 8?
>>
>> $a = (int) (0.1 +0.7);
>>
>> echo "$a\n";
>>
>> $x = (int) ((0.1 + 0.7) * 10);
>>
>> echo "$x\n";
>>
>> $y = (int) (8);
>>
>> echo "$y\n";
>>
>
>
> It works as expected if you take out the int() parts in each line. I'm
> not sure why, but the use of int() seems to be screwing around with the
> results. That's why the first line outputs a 0.
>
> Thanks,
> Ash
> http://www.ashleysheridan.co.uk
>
>
>
>

--------------000707080504020206050703--
Joseph Thayne [ Do, 18 Februar 2010 17:04 ] [ ID #2032734 ]

Re: Dumb Question - Casting

Daniel Egeberg wrote:
> On Thu, Feb 18, 2010 at 16:47, Chuck <chuck.carson [at] gmail.com> wrote:
>> Sorry, been doing heavy perl and haven't written any PHP in 3 years so a tad
>> rusty.
>>
>> Can someone explain why the second expression in this code snippet evaluates
>> to 7 and not 8?
>>
>> $a = (int) (0.1 +0.7);
>>
>> echo "$a\n";
>>
>> $x = (int) ((0.1 + 0.7) * 10);
>>
>> echo "$x\n";
>>
>> $y = (int) (8);
>>
>> echo "$y\n";
>>
>
> The reason why you get 7 instead of 8 is because you are using
> floating point arithmetic. 0.1 (i.e. the fraction 1/10) does not have
> a finite representation in base 2 (like you cannot finitely represent
> 1/3 in base 10). So the number 0.1 is represented in the computer as a
> number that is strictly less than 0.1 so when you do 0.1+0.7=x then
> you have x<0.8 in the computer (think 7.9999999...). When you cast to
> int you just truncate the number, i.e. you chop off the fractional
> part leaving you with 7.
>

yup as Daniel pointed out; this is correct - love floats & casting!

see:

$y = ((0.1 + 0.7) * 10);
$x = (int)$y;
$z = intval($y);
var_dump($y);
var_dump(serialize($y));
var_dump($x);
var_dump($z);

outputs:
float(8)
string(55) "d:7.99999999999999911182158029987476766109466552734375;"
int(7)
int(7)

lovely! - note: serializing gives us the exact value of the float

regards,

Nathan



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Nathan Rixham [ Do, 18 Februar 2010 18:06 ] [ ID #2032735 ]
PHP » gmane.comp.php.general » Dumb Question - Casting

Vorheriges Thema: PHPDoc (not PhD) question.
Nächstes Thema: Problem retrieving CN with ldap_search()