php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54458 Comparison with intval()
Submitted: 2011-04-03 17:31 UTC Modified: 2011-04-03 20:49 UTC
From: cs dot luizeduardo at hotmail dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.3SVN-2011-04-03 (SVN) OS: Windows
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: cs dot luizeduardo at hotmail dot com
New email:
PHP Version: OS:

 

 [2011-04-03 17:31 UTC] cs dot luizeduardo at hotmail dot com
Description:
------------
comparisons with intval() function with real values between 1 and 2 (like 1.2 1.3 
etc).
Every return any fail;

Test script:
---------------
$numero = "1.1";
if (intval($numero * 100) == $numero * 100) 
{
        echo intval($numero * 100) . ":" . $numero * 100 . "<br/>"; 
	echo "Number OK!";
    } else {
	echo intval($numero * 100) . ":" . $numero * 100 . "<br/>"; //Return 110:110 (same) but the comparison has fail
	echo "The number shoud contains 2 decimal places";
}

Expected result:
----------------
110:110
Number OK!

OBS: With the variable with the 2.1 for example, the return result are the 
expected.

Actual result:
--------------
110:110
The number shoud contains 2 decimal places

Patches

numeric-test.txt (last revision 2011-04-03 15:36 UTC by cs dot luizeduardo at hotmail dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-03 20:49 UTC] cataphract@php.net
-Status: Open +Status: Bogus
 [2011-04-03 20:49 UTC] cataphract@php.net
In[1]:= SetAccuracy[(1.1*100.), Infinity] - 110 // N[#, 20] &
Out[1]= 1.4210854715202003717*10^-14
In[2]:= SetAccuracy[110., Infinity] - 110 // N[#, 20] &
Out[2]= 0

110 is exactly representable in an IEEE 754 double precision number; 1.1 is not. When 1.1 is multiplied by 100, the error in the representation of 1.1 is propagated and we don't end up with the exact 110. In PHP:

echo sprintf("%.20f %.20f\n", 110., 1.1 * 100);

110.00000000000000000000 110.00000000000001421085

Closing as bogus.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 21 19:01:34 2024 UTC