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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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 Dec 10 00:01:28 2024 UTC