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
 [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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 19:01:31 2024 UTC