Boost.Test: floating point comparison using absolute diference

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Boost.Test: floating point comparison using absolute diference

Boost - Users mailing list
Hi all,

I am trying to find a way to make the boost unit test framework use an absolute difference for floating point comparison instead of the relative difference. 
I searched the documentation to try to find a suitable decorator but couldn’t find anything. Any help is much appreciated!
#define BOOST_TEST_MODULE example
#include <boost/test/included/unit_test.hpp>
// boost::unit_test::tolerance uses relative difference and hence this test will pass; 
// is there anything I can use here to indicate absolute difference?
BOOST_AUTO_TEST_CASE(my_test, * boost::unit_test::tolerance(0.1) ) 
{
  BOOST_TEST(10.1 == 10.3);
}







_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost.Test: floating point comparison using absolute diference

Boost - Users mailing list
Le 29.12.17 à 17:33, Georgios Sermaidis via Boost-users a écrit :

> Hi all,
>
> I am trying to find a way to make the boost unit test framework use an
> absolute difference for floating point comparison instead of the
> relative difference. 
> I searched the documentation to try to find a suitable decorator but
> couldn’t find anything. Any help is much appreciated!
>
> #defineBOOST_TEST_MODULEexample
>
> #include<boost/test/included/unit_test.hpp>
>
>
> //boost::unit_test::toleranceusesrelativedifference and hence this test
> will pass; 
>
> // isthereanythingIcanusehereto indicateabsolutedifference?
>
> BOOST_AUTO_TEST_CASE(my_test,*boost::unit_test::tolerance(0.1))
>
> {
>
>
> BOOST_TEST(10.1==10.3);
>
> }

What you want to do is discussed here in the doc:
http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point/floating_points_comparison_impl.html

You may just use

BOOST_TEST(left - right == .0, boost::unit_test::tolerance(0.1));

and I think it should use absolute tolerance on the absolute value of
left-right.

If nothing works, there is always the good old comparison:
BOOST_TEST((std::abs(left - right) < 0.1))

Note the double parenthesis, in this case BOOST_TEST will see only the
evaluation of the expression inside, which is cast to bool (less
elegant, but should work as last resort).

Raffi

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost.Test: floating point comparison using absolute diference

Boost - Users mailing list
Thank you. Is there a way to achieve this when comparing collections, i.e. when using boost::test_tools::per_element()?


On 29 Dec 2017, at 19:37, Raffi Enficiaud via Boost-users <[hidden email]> wrote:

Le 29.12.17 à 17:33, Georgios Sermaidis via Boost-users a écrit :
Hi all,

I am trying to find a way to make the boost unit test framework use an
absolute difference for floating point comparison instead of the
relative difference. 
I searched the documentation to try to find a suitable decorator but
couldn’t find anything. Any help is much appreciated!

#defineBOOST_TEST_MODULEexample

#include<boost/test/included/unit_test.hpp>


//boost::unit_test::toleranceusesrelativedifference and hence this test
will pass; 

// isthereanythingIcanusehereto indicateabsolutedifference?

BOOST_AUTO_TEST_CASE(my_test,*boost::unit_test::tolerance(0.1))

{


BOOST_TEST(10.1==10.3);

}

What you want to do is discussed here in the doc:
http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point/floating_points_comparison_impl.html

You may just use

BOOST_TEST(left - right == .0, boost::unit_test::tolerance(0.1));

and I think it should use absolute tolerance on the absolute value of
left-right.

If nothing works, there is always the good old comparison:
BOOST_TEST((std::abs(left - right) < 0.1))

Note the double parenthesis, in this case BOOST_TEST will see only the
evaluation of the expression inside, which is cast to bool (less
elegant, but should work as last resort).

Raffi

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost.Test: floating point comparison using absolute diference

Boost - Users mailing list
Le 30.12.17 à 14:41, Georgios Sermaidis via Boost-users a écrit :

> Thank you. Is there a way to achieve this when comparing collections,
> i.e. when using boost::test_tools::per_element()?
>
>
>> On 29 Dec 2017, at 19:37, Raffi Enficiaud via Boost-users
>> <[hidden email] <mailto:[hidden email]>> wrote:
>>
>> Le 29.12.17 à 17:33, Georgios Sermaidis via Boost-users a écrit :
>>> Hi all,
>>>
>>> I am trying to find a way to make the boost unit test framework use an
>>> absolute difference for floating point comparison instead of the
>>> relative difference. 
>>> I searched the documentation to try to find a suitable decorator but
>>> couldn’t find anything. Any help is much appreciated!
>>>
>>> #defineBOOST_TEST_MODULEexample
>>>
>>> #include<boost/test/included/unit_test.hpp>
>>>
>>>
>>> //boost::unit_test::toleranceusesrelativedifference and hence this test
>>> will pass; 
>>>
>>> // isthereanythingIcanusehereto indicateabsolutedifference?
>>>
>>> BOOST_AUTO_TEST_CASE(my_test,*boost::unit_test::tolerance(0.1))
>>>
>>> {
>>>
>>>
>>> BOOST_TEST(10.1==10.3);
>>>
>>> }
>>
>> What you want to do is discussed here in the doc:
>> http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point/floating_points_comparison_impl.html
>>
>> You may just use
>>
>> BOOST_TEST(left - right == .0, boost::unit_test::tolerance(0.1));
>>
>> and I think it should use absolute tolerance on the absolute value of
>> left-right.
>>
>> If nothing works, there is always the good old comparison:
>> BOOST_TEST((std::abs(left - right) < 0.1))
>>
>> Note the double parenthesis, in this case BOOST_TEST will see only the
>> evaluation of the expression inside, which is cast to bool (less
>> elegant, but should work as last resort).
>>
>> Raffi

Apart from creating a collection of differences that you compare with a
collection of zeros (that can be wrap up in helper functions), I do not
see a way to use this facility as is. Looping over the elements would
certainly be the most maintainable and easy to understand choice.

Raffi


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: Boost.Test: floating point comparison using absolute diference

Boost - Users mailing list
Got it, thank you so much

Regards,
Georgios

> On 30 Dec 2017, at 22:28, Raffi Enficiaud via Boost-users <[hidden email]> wrote:
>
>> Le 30.12.17 à 14:41, Georgios Sermaidis via Boost-users a écrit :
>> Thank you. Is there a way to achieve this when comparing collections,
>> i.e. when using boost::test_tools::per_element()?
>>
>>
>>> On 29 Dec 2017, at 19:37, Raffi Enficiaud via Boost-users
>>> <[hidden email] <mailto:[hidden email]>> wrote:
>>>
>>>> Le 29.12.17 à 17:33, Georgios Sermaidis via Boost-users a écrit :
>>>> Hi all,
>>>>
>>>> I am trying to find a way to make the boost unit test framework use an
>>>> absolute difference for floating point comparison instead of the
>>>> relative difference.
>>>> I searched the documentation to try to find a suitable decorator but
>>>> couldn’t find anything. Any help is much appreciated!
>>>>
>>>> #defineBOOST_TEST_MODULEexample
>>>>
>>>> #include<boost/test/included/unit_test.hpp>
>>>>
>>>>
>>>> //boost::unit_test::toleranceusesrelativedifference and hence this test
>>>> will pass;
>>>>
>>>> // isthereanythingIcanusehereto indicateabsolutedifference?
>>>>
>>>> BOOST_AUTO_TEST_CASE(my_test,*boost::unit_test::tolerance(0.1))
>>>>
>>>> {
>>>>
>>>>
>>>> BOOST_TEST(10.1==10.3);
>>>>
>>>> }
>>>
>>> What you want to do is discussed here in the doc:
>>> http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/testing_tools/extended_comparison/floating_point/floating_points_comparison_impl.html
>>>
>>> You may just use
>>>
>>> BOOST_TEST(left - right == .0, boost::unit_test::tolerance(0.1));
>>>
>>> and I think it should use absolute tolerance on the absolute value of
>>> left-right.
>>>
>>> If nothing works, there is always the good old comparison:
>>> BOOST_TEST((std::abs(left - right) < 0.1))
>>>
>>> Note the double parenthesis, in this case BOOST_TEST will see only the
>>> evaluation of the expression inside, which is cast to bool (less
>>> elegant, but should work as last resort).
>>>
>>> Raffi
>
> Apart from creating a collection of differences that you compare with a
> collection of zeros (that can be wrap up in helper functions), I do not
> see a way to use this facility as is. Looping over the elements would
> certainly be the most maintainable and easy to understand choice.
>
> Raffi
>
>
> _______________________________________________
> Boost-users mailing list
> [hidden email]
> https://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users