CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2004
    Posts
    223

    how to compare to float number?

    Hi, all

    I have a question about how to compare two float.
    the follows is my code
    Code:
    template < class T >
    class MyPoint
    {
    public:
        typedef typename MyPoint<T> Point;
    public:
        Point(T x,T y) { this->x = x; this->y = y; };
        friend bool operator < ( const Point & left, const Point & right )
        {
            if ( left.x != right.x )
            {
                return ( left.x < right.x );
            }
            else
            {
                return ( left.y < right.y );
            }
        }
    public:
        T x,y;
    };
    int main()
    {
        typedef MyPoint<float> POINT;
    
        set < POINT > point;
        point.insert( POINT(1.0,1.0) );
        point.insert( POINT(0.0,0.0) );
    
       
        set < POINT > :: iterator it;
    
        it = point.find( POINT(1.00000005,1.0) );
        if ( it == point.end() )
        {
            cout << "not found\n";
        }
        else
        {
            cout << it->x<< " " << it->y << "\n";
        }
        return 0;
    }
    I found that POINT(1.00000005,1.0) is the equal to POINT(1.0,1.0) ,
    but some times I want to get the result POINT(1.000001,1.0) == POINT(1.0,1.0) be true.

    what is the better way to do this?

  2. #2
    Join Date
    Sep 2004
    Posts
    561

    Re: how to compare to float number?

    There isn't a way to accurately represent floating point numbers in binary.

    Here is a good way to compare floating point numbers.

    http://www.codeguru.com/forum/showthread.php?t=323835

  3. #3
    Join Date
    Feb 2004
    Posts
    223

    Re: how to compare to float number?

    thank you Rigel for so quickly reply!

    the link that you provide is very helpful.

    but I hope to set the two point equal according to some given numerical error.

    I change the MyPoint class like follows:
    Code:
    template < class T >
    class MyPoint
    {
    public:
        typedef typename MyPoint<T> Point;
    public:
        Point(T x,T y) { this->x = x; this->y = y; };
        friend bool operator < ( const Point & left, const Point & right )
        {
            double eps = 1.0e-5;
            double dx,dy;
            dx = abs( left.x - right.x );
            dy = abs( left.y - right.y );
            double maxx = max( abs(left.x), abs(right.x) ) + 1.0e-30;
            double maxy = max( abs(left.y), abs(right.y) ) + 1.0e-30;
    
            if ( !( ( dx < eps ) || ( dx / maxx < eps ) ) )
            {
                return ( left.x < right.x );
            }
            else if ( !( ( dy < eps ) || ( dy/maxy < eps ) ) )
            {
                return ( left.y < right.y );
            }
            else
            {
                return false;
            }
        }
    public:
        T x,y;
    };
    but i have to set the eps in function operator <
    I hope i can set the error eps without change the codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured