Edit report at http://bugs.php.net/bug.php?id=51547&edit=1
ID: 51547 Updated by: ahar...@php.net Reported by: acollins at liv dot ac dot uk Summary: Type casting from int to int after log changes value Status: Bogus Type: Bug Package: *Math Functions PHP Version: 5.2.13 New Comment: Exactly right: the first value is being rendered as 3 because of the fact that PHP limits the significant digits shown when converting a float to a string. If you look at the value of serialize(log(1000, 10)), you'll end up with something like the following: d:2.999999999999999555910790149937383830547332763671875; Which demonstrates that the internal representation is slightly less than 3. The explicit conversion to an integer causes the value to be rounded down, hence why you get 2. Previous Comments: ------------------------------------------------------------------------ [2010-04-13 11:24:06] degeb...@php.net Floating point values have a limited precision. Hence a value might not have the same string representation after any processing. That also includes writing a floating point value in your script and directly printing it without any mathematical operations. If you would like to know more about "floats" and what IEEE 754 is, read this: http://docs.sun.com/source/806-3568/ncg_goldberg.html Thank you for your interest in PHP. Try having a look at this: echo serialize(log(1000, 10)); When you cast to int you just truncate the fractional part. ------------------------------------------------------------------------ [2010-04-13 11:04:47] acollins at liv dot ac dot uk Note: I assume this is because log() returns a floating point value. I.e. it's a precision issue? ------------------------------------------------------------------------ [2010-04-13 11:03:32] acollins at liv dot ac dot uk Description: ------------ When performing a log(), and immediately type casting afterwards, the value changes. Test script: --------------- <?php $val = 1000; echo log($val, 10) .' == '. (int) log($val, 10); ?> Expected result: ---------------- 3 == 3 Actual result: -------------- 3 == 2 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=51547&edit=1