php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #41886 int added by float ended as not equal to float equavalent value
Submitted: 2007-07-03 11:42 UTC Modified: 2007-07-03 14:07 UTC
From: c00lways at gmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.3 OS: windows xp prof sp 2
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: c00lways at gmail dot com
New email:
PHP Version: OS:

 

 [2007-07-03 11:42 UTC] 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>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-07-03 11:46 UTC] tony2001@php.net
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.


 [2007-07-03 13:52 UTC] c00lways at gmail dot com
Hi,

if what you states is true,
how come:

//this output 1
//echo 5.23 == (1+4.23);
 [2007-07-03 14:07 UTC] tony2001@php.net
.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Mar 12 11:01:32 2025 UTC