php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52787 intval() returns incorrect int
Submitted: 2010-09-07 01:17 UTC Modified: 2010-09-07 05:44 UTC
From: mrdanielbang at gmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.3.3 OS: Windows & Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mrdanielbang at gmail dot com
New email:
PHP Version: OS:

 

 [2010-09-07 01:17 UTC] 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)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-07 05:44 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2010-09-07 05:44 UTC] aharvey@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.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jun 14 13:01:30 2024 UTC