Boost logo

Boost :

Subject: Re: [boost] Fwd: [Boost-users] [test] floating point comparison
From: Paul A. Bristow (pbristow_at_[hidden])
Date: 2013-10-31 14:45:59


> -----Original Message-----
> From: Boost [mailto:boost-bounces_at_[hidden]] On Behalf Of Steven Watanabe
> Sent: Thursday, October 31, 2013 5:11 PM
> To: boost_at_[hidden]
> Subject: Re: [boost] Fwd: [Boost-users] [test] floating point comparison
>
> AMDG
>
> On 10/31/2013 06:18 AM, Matthijs Möhlmann wrote:
> > I've the following test program:
> >
> > #define BOOST_TEST_DYN_LINK
> > #define BOOST_TEST_MAIN
> > #define BOOST_TEST_MODULE Master test suite #include
> > <boost/test/unit_test.hpp>
> >
> > BOOST_AUTO_TEST_CASE(TestCase) {
> >
> > BOOST_REQUIRE_CLOSE_FRACTION(45.00f, 45.17f, 0.004f); }
> >
> > When I compile it with the following line:
> > g++ -o main.o -c -Werror -Wall -std=c++11 -fvisibility=hidden
> > -fvisibility-inlines-hidden -DBOOST_SPIRIT_USE_PHOENIX_V3 -DUTEST -g
> > main.cpp
> > g++ -o main main.o -lboost_unit_test_framework
> >
> > I get the following output:
> > Running 1 test case...
> >
> > *** No errors detected
> >
> > If I lower the tolerance to 0.0004f then I'll get the following:
> > Running 1 test case...
> > main.cpp(8): fatal error in "TestCase": difference{0.00376352} between
> > 45.00f{45} and 45.17f{45.1699982} exceeds 0.00039999999
> >
> > *** 1 failure detected in test suite "Master test suite"
> >
> > Why does it say that the difference is 0.00376352 and not 0.17....... ?
> >
>
> I suppose the output is a bit misleading.
> The difference printed (0.0037...) is
> approximately 0.17/45.

Comparing floating-point is tricky. It may be helpful to understand the method used in Boost.Test
to read this article by

Alberto Squassabia

Comparing Floats: How To Determine if
Floating Quantities Are Close Enough Once a Tolerance Has Been Reached

http://adtmag.com/articles/2000/03/16/comparing-floats-how-to-determine-if-floating-quantities-are-c
lose-enough-once-a-tolerance-has-been.aspx

On the hand, you may discover more than you want to know ;-)

Paul

---
Paul A. Bristow,
Prizet Farmhouse, Kendal LA8 8AB  UK
+44 1539 561830  07714330204
pbristow_at_[hidden]

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk