warning: comparing floating point with == or != is unsafe

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

warning: comparing floating point with == or != is unsafe

Mateusz Loskot
Administrator
Folks,

What's out approach to float-point comparisons across the library?
I'm kidding :-)

Just for comparisons with Zero, I wonder if we could stick to absolute
epsilon comparisons [1].

There are some places, i.e. in the index, with such cases and
TODO comments too, so perhaps we could decide on it soon.

[1] http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
_______________________________________________
Geometry mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/geometry
--
Mateusz Loskot
http://mateusz.loskot.net
Reply | Threaded
Open this post in threaded view
|

Re: warning: comparing floating point with == or != is unsafe

Adam Wulkiewicz
Hi,

Mateusz Loskot wrote:

> Folks,
>
> What's out approach to float-point comparisons across the library?
> I'm kidding :-)
>
> Just for comparisons with Zero, I wonder if we could stick to absolute
> epsilon comparisons [1].
>
> There are some places, i.e. in the index, with such cases and
> TODO comments too, so perhaps we could decide on it soon.
>
> [1] http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
>

Which place in the Index do you have in mind?

Regards,
Adam
_______________________________________________
Geometry mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/geometry
Reply | Threaded
Open this post in threaded view
|

Re: warning: comparing floating point with == or != is unsafe

Mateusz Loskot
Administrator
Adam Wulkiewicz wrote
Mateusz Loskot wrote:
> Folks,
>
> What's out approach to float-point comparisons across the library?
> I'm kidding :-)
>
> Just for comparisons with Zero, I wonder if we could stick to absolute
> epsilon comparisons [1].
>
> There are some places, i.e. in the index, with such cases and
> TODO comments too, so perhaps we could decide on it soon.
>
> [1] http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
>

Which place in the Index do you have in mind?
For example,
/trunk/boost/geometry/index/detail/rtree/linear/redistribute_elements.hpp
lines 98-106

Best regards,
--
Mateusz Loskot
http://mateusz.loskot.net
Reply | Threaded
Open this post in threaded view
|

Re: warning: comparing floating point with == or != is unsafe

Adam Wulkiewicz
Mateusz Loskot wrote:

> Adam Wulkiewicz wrote
>> Mateusz Loskot wrote:
>>> Folks,
>>>
>>> What's out approach to float-point comparisons across the library?
>>> I'm kidding :-)
>>>
>>> Just for comparisons with Zero, I wonder if we could stick to absolute
>>> epsilon comparisons [1].
>>>
>>> There are some places, i.e. in the index, with such cases and
>>> TODO comments too, so perhaps we could decide on it soon.
>>>
>>> [1]
>>> http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
>>>
>>
>> Which place in the Index do you have in mind?
>
> For example,
> /trunk/boost/geometry/index/detail/rtree/linear/redistribute_elements.hpp
> lines 98-106
>

Ah, I've forgotten to handle this after porting it from the old version.
Thanks for digging this up. Yes, there should probably be comparison
with std::numeric_limits<coordinate_type>::epsilon().

Regards,
Adam
_______________________________________________
Geometry mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/geometry
Reply | Threaded
Open this post in threaded view
|

Re: warning: comparing floating point with == or != is unsafe

Mateusz Loskot
Administrator
On 7 June 2013 01:36, Adam Wulkiewicz <[hidden email]> wrote:

> Mateusz Loskot wrote:
>> Adam Wulkiewicz wrote
>>> Mateusz Loskot wrote:
>>>>
>>>> Folks,
>>>>
>>>> What's out approach to float-point comparisons across the library?
>>>> I'm kidding :-)
>>>>
>>>> Just for comparisons with Zero, I wonder if we could stick to absolute
>>>> epsilon comparisons [1].
>>>>
>>>> There are some places, i.e. in the index, with such cases and
>>>> TODO comments too, so perhaps we could decide on it soon.
>>>>
>>>> [1]
>>>>
>>>> http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
>>>>
>>>
>>> Which place in the Index do you have in mind?
>>
>>
>> For example,
>> /trunk/boost/geometry/index/detail/rtree/linear/redistribute_elements.hpp
>> lines 98-106
>>
>
> Ah, I've forgotten to handle this after porting it from the old version.
> Thanks for digging this up. Yes, there should probably be comparison with
> std::numeric_limits<coordinate_type>::epsilon().

You may try GCC/clang with -Wfloat-equal, there is a few more, but those
need to be addressed individually, I think.

So, we may discuss unobvious cases.

Best regards,
--
Mateusz Loskot, http://mateusz.loskot.net
_______________________________________________
Geometry mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/geometry
--
Mateusz Loskot
http://mateusz.loskot.net
Reply | Threaded
Open this post in threaded view
|

Re: warning: comparing floating point with == or != is unsafe

Adam Wulkiewicz
Mateusz Loskot wrote:

> On 7 June 2013 01:36, Adam Wulkiewicz <[hidden email]> wrote:
>>
>> Ah, I've forgotten to handle this after porting it from the old version.
>> Thanks for digging this up. Yes, there should probably be comparison with
>> std::numeric_limits<coordinate_type>::epsilon().
>
> You may try GCC/clang with -Wfloat-equal, there is a few more, but those
> need to be addressed individually, I think.
>
> So, we may discuss unobvious cases.
>

In the Index there are comparisons in:

/boost/geometry/index/detail/rtree/visitors/insert.hpp:70
/boost/geometry/index/detail/rtree/linear/redistribute_elements.hpp:326
/boost/geometry/index/detail/rtree/quadratic/redistribute_elements.hpp:189
/boost/geometry/index/detail/rtree/rstar/redistribute_elements.hpp:93
/boost/geometry/index/detail/rtree/rstar/choose_next_node.hpp:112

but they're ok.

And I've detected also the one in:

/boost/geometry/util/math.hpp:54

Regards,
Adam
_______________________________________________
Geometry mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/geometry
Reply | Threaded
Open this post in threaded view
|

Re: warning: comparing floating point with == or != is unsafe

Barend
Hi,

On 7-6-2013 4:46, Adam Wulkiewicz wrote:

> Mateusz Loskot wrote:
>> On 7 June 2013 01:36, Adam Wulkiewicz <[hidden email]> wrote:
>>>
>>> Ah, I've forgotten to handle this after porting it from the old
>>> version.
>>> Thanks for digging this up. Yes, there should probably be comparison
>>> with
>>> std::numeric_limits<coordinate_type>::epsilon().
>>
>> You may try GCC/clang with -Wfloat-equal, there is a few more, but those
>> need to be addressed individually, I think.
>>
>> So, we may discuss unobvious cases.
>>
>
> In the Index there are comparisons in:
>
> /boost/geometry/index/detail/rtree/visitors/insert.hpp:70
> /boost/geometry/index/detail/rtree/linear/redistribute_elements.hpp:326
> /boost/geometry/index/detail/rtree/quadratic/redistribute_elements.hpp:189
>
> /boost/geometry/index/detail/rtree/rstar/redistribute_elements.hpp:93
> /boost/geometry/index/detail/rtree/rstar/choose_next_node.hpp:112
>
> but they're ok.
>
> And I've detected also the one in:
>
> /boost/geometry/util/math.hpp:54

That one first compares with == indeed. If that is true, it returns true
(whatever the epsilon is, if == returns true, it will always return
true). If == is false, it does a more sophisticated comparison, still
intended to be replaced by some Boost utility. See the comments. There
are still some issues with this (not exactly with this, but with FP in
general).

Regards, Barend

_______________________________________________
Geometry mailing list
[hidden email]
http://lists.boost.org/mailman/listinfo.cgi/geometry