#41886 [Opn->Bgs]: int added by float ended as not equal to float equavalent value
ID: 41886
Updated by: tony2001@php.net
Reported By: c00lways at gmail dot com
-Status: Open
+Status: Bogus
Bug Type: Math related
Operating System: windows xp prof sp 2
PHP Version: 5.2.3
New Comment:
Floating point values have a limited precision. Hence a value might
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly
printing it without any mathematical operations.
If you would like to know more about "floats" and what IEEE
754 is read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
Thank you for your interest in PHP.
Previous Comments:
------------------------------------------------------------------------
[2007-07-03 11:42:22] c00lways at gmail dot com
Description:
------------
int added by float ended as not equal to float equavalent value
Reproduce code:
---------------
//this output <empty>, not 1
echo isValidRange( 5.23, 1, 10, 0.01 );
//this output 1
//echo 5.23 == (1+4.23);
function isValidRange( $p_value, $p_start, $p_end, $p_step=1 )
{
//reverse step
if( $p_end < $p_start )
{
$p_step = -($p_step);
}
if( $p_step == 0.000000 )
{
raiseError(1, "Invalid step: " . $p_step);
}
for( $c = $p_start; $c <= $p_end; $c+=$p_step )
{
//this part caused the problem
if( $p_value == $c )
{
return true;
}
//if change it to
//if( $p_value."" == $c."" )
// then it works! return true
}
return false;
}
Expected result:
----------------
1
Actual result:
--------------
<empty>
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=41886&edit=1
Thread (5 messages)