php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36730 int and intval produce wrong results
Submitted: 2006-03-14 01:20 UTC Modified: 2006-03-14 02:04 UTC
From: scott dot fagg at arup dot com Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 4.4.2 OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
14 + 10 = ?
Subscribe to this entry?

 
 [2006-03-14 01:20 UTC] scott dot fagg at arup dot com
Description:
------------
(int) and intval() generate wrong values.

Output of (int) and intval() is affected by how the values passed to them, were calculated.

Reproduce code:
---------------
$x = 0.57 ;

$a = (int) ($x * 100);
$b = intval($x * 100);

# produce incorrect results , $b = 56 
print "$x $a $b\n";

$x = 0.56 + 0.01;

$a = (int) ($x * 100);
$b = intval($x * 100);

# produce expected results , $b = 57 
print "$x $a $b\n";

Expected result:
----------------
(int) and intval() should produce correct answers and should not be affected by how the values passed to them are calculated.

Workaround seems to be to use round() instead of intval().

Actual result:
--------------
Above code produces :

0.57 56 56
0.57 57 57



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-14 02:04 UTC] rasmus@php.net
This is not a bug.  Please read the short note at http://php.net/float

 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Dec 02 01:01:24 2020 UTC