Edit report at http://bugs.php.net/bug.php?id=52787&edit=1
ID: 52787 Updated by: ahar...@php.net Reported by: mrdanielbang at gmail dot com Summary: intval() returns incorrect int -Status: Open +Status: Bogus Type: Bug Package: Math related Operating System: Windows & Linux PHP Version: 5.3.3 Block user comment: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2010-09-07 01:17:27] mrdanielbang at gmail dot com Description: ------------ I was working on a way to calculate the LIMIT and OFFSET for a blog in reverse but still with ORDER BY date_create DESC and I kept running into flaws until i did a print on the mysql_query string,.. in order to keep the urls correct on the page,.. $postoffset = (((14 / 10) - 1) * 10); // 4 var_dump($postoffset); // returns float(4) var_dump(intval($postoffset)); // returns int(3) 14/10 = 1.4 1.4-1 = 0.4 0.4*10 = 4 (this is happening on many different php version, so i almost wanted to put the php version select on "Irrelavant") Test script: --------------- <?php header("Content-Type: text/plain"); $limit = (int) 10; $count = (int) 14; // mysql_query("select count(*)..."); $pages = (int) ceil($count/$limit); $page = (int) 1; $offset = ((($count / $limit) - $page) * $limit); var_dump($offset); var_dump(intval($offset)); $offset = (((14 / 10) - 1) * 10); // 4 var_dump($offset); var_dump(intval($offset)); var_dump(ceil(((14 / 10) - 1) * 10)); var_dump(floor(((14 / 10) - 1) * 10)); var_dump(round(((14 / 10) - 1) * 10)); print "- step by step -\n"; $pages = $count / $limit; var_dump($pages); // 1.4 var_dump(intval($pages)); // 1 $_page = $pages - $page; var_dump($_page); // 0.4 var_dump(intval($_page)); // 0 $offset = $_page * $limit; var_dump($offset); // 4 var_dump(intval($offset)); // 3 incorrect $offset = 0.4 * 10; var_dump($offset); // 4 var_dump(intval($offset)); // 4 correct ?> Expected result: ---------------- float(4) int(4) float(4) int(4) float(4) float(4) float(4) - step by step - float(1.4) int(1) float(0.4) int(0) float(4) int(4) float(4) int(4) Actual result: -------------- float(4) int(3) float(4) int(3) float(4) float(3) float(4) - step by step - float(1.4) int(1) float(0.4) int(0) float(4) int(3) float(4) int(4) ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=52787&edit=1