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
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: 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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 11:01:27 2024 UTC