[PHP] Dumb Question - Casting
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;
Re: [PHP] Dumb Question - Casting
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
Re: [PHP] Dumb Question - Casting
On Thu, Feb 18, 2010 at 10:50 AM, Ashley Sheridan a...@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
Re: [PHP] Dumb Question - Casting
On Thu, Feb 18, 2010 at 16:47, Chuck chuck.car...@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 x0.8 in the computer (think 7.999...). 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
Re: [PHP] Dumb Question - Casting
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
Re: [PHP] Dumb Question - Casting
Daniel Egeberg wrote: On Thu, Feb 18, 2010 at 16:47, Chuck chuck.car...@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 x0.8 in the computer (think 7.999...). 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.99911182158029987476766109466552734375; 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