Bug #52787 [NEW]: intval() returns incorrect int
| From: | mrdanielbang at gmail dot com | Date: | Mon, 06 Sep 2010 23:17:28 +0000 |
| Subject: | Bug #52787 [NEW]: intval() returns incorrect int | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-154127@lists.php.net to get a copy of this message | ||
From:
Operating system: Windows & Linux
PHP version: 5.3.3
Package: Math related
Bug Type: Bug
Bug description:intval() returns incorrect int
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 bug report at http://bugs.php.net/bug.php?id=52787&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=52787&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=52787&r=trysnapshot53
Try a snapshot (trunk): http://bugs.php.net/fix.php?id=52787&r=trysnapshottrunk
Fixed in SVN: http://bugs.php.net/fix.php?id=52787&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=52787&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=52787&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=52787&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=52787&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=52787&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=52787&r=support
Expected behavior: http://bugs.php.net/fix.php?id=52787&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=52787&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=52787&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=52787&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=52787&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=52787&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=52787&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=52787&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=52787&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=52787&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=52787&r=mysqlcfg