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
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: scott dot fagg at arup dot com
New email:
PHP Version: OS:

 

 [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: Mon Oct 26 03:01:24 2020 UTC