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 this is not your bug, you can add a comment by following this link.
If this is your bug, but 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

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 Nov 25 09:01:23 2020 UTC