source: NonGTP/Boost/boost/test/floating_point_comparison.hpp @ 857

Revision 857, 6.2 KB checked in by igarcia, 19 years ago (diff)
Line 
1//  (C) Copyright Gennadiy Rozental 2001-2005.
2//  Distributed under the Boost Software License, Version 1.0.
3//  (See accompanying file LICENSE_1_0.txt or copy at
4//  http://www.boost.org/LICENSE_1_0.txt)
5
6//  See http://www.boost.org/libs/test for the library home page.
7//
8//  File        : $RCSfile: floating_point_comparison.hpp,v $
9//
10//  Version     : $Revision: 1.23 $
11//
12//  Description : defines algoirthms for comparing 2 floating point values
13// ***************************************************************************
14
15#ifndef BOOST_TEST_FLOATING_POINT_COMPARISON_HPP_071894GER
16#define BOOST_TEST_FLOATING_POINT_COMPARISON_HPP_071894GER
17
18#include <boost/limits.hpp>  // for std::numeric_limits
19
20#include <boost/test/utils/class_properties.hpp>
21
22#include <boost/test/detail/suppress_warnings.hpp>
23
24//____________________________________________________________________________//
25
26namespace boost {
27
28namespace test_tools {
29
30using unit_test::readonly_property;
31
32// ************************************************************************** //
33// **************        floating_point_comparison_type        ************** //
34// ************************************************************************** //
35
36enum floating_point_comparison_type { FPC_STRONG, FPC_WEAK };
37
38// ************************************************************************** //
39// **************                    details                   ************** //
40// ************************************************************************** //
41
42namespace tt_detail {
43
44template<typename FPT>
45inline FPT
46fpt_abs( FPT arg )
47{
48    return arg < 0 ? -arg : arg;
49}
50
51//____________________________________________________________________________//
52
53// both f1 and f2 are unsigned here
54template<typename FPT>
55inline FPT
56safe_fpt_division( FPT f1, FPT f2 )
57{
58    return  (f2 < 1 && f1 > f2 * (std::numeric_limits<FPT>::max)())               ? (std::numeric_limits<FPT>::max)()
59            : ((f2 > 1 && f1 < f2 * (std::numeric_limits<FPT>::min)() || f1 == 0) ? 0
60                                                                                  : f1/f2 );
61}
62
63//____________________________________________________________________________//
64
65} // namespace tt_detail
66
67// ************************************************************************** //
68// **************             close_at_tolerance               ************** //
69// ************************************************************************** //
70
71template<typename FPT, typename PersentType = FPT >
72class close_at_tolerance {
73public:
74    // Public typedefs
75    typedef bool result_type;
76
77    // Constructor
78    explicit    close_at_tolerance( PersentType percentage_tolerance, floating_point_comparison_type fpc_type = FPC_STRONG )
79    : p_fraction_tolerance( static_cast<FPT>(0.01)*percentage_tolerance ), p_strong_or_weak( fpc_type ==  FPC_STRONG ) {}
80
81    bool        operator()( FPT left, FPT right ) const
82    {
83        FPT diff = tt_detail::fpt_abs( left - right );
84        FPT d1   = tt_detail::safe_fpt_division( diff, tt_detail::fpt_abs( right ) );
85        FPT d2   = tt_detail::safe_fpt_division( diff, tt_detail::fpt_abs( left ) );
86       
87        return p_strong_or_weak ? (d1 <= p_fraction_tolerance.get() && d2 <= p_fraction_tolerance.get())
88                                : (d1 <= p_fraction_tolerance.get() || d2 <= p_fraction_tolerance.get());
89    }
90
91    // Public properties
92    readonly_property<FPT>  p_fraction_tolerance;
93    readonly_property<bool> p_strong_or_weak;
94};
95
96//____________________________________________________________________________//
97
98// ************************************************************************** //
99// **************               check_is_close                 ************** //
100// ************************************************************************** //
101
102struct check_is_close_t {
103    // Public typedefs
104    typedef bool result_type;
105
106    template<typename FPT, typename PersentType>
107    bool
108    operator()( FPT left, FPT right, PersentType percentage_tolerance, floating_point_comparison_type fpc_type = FPC_STRONG )
109    {
110        close_at_tolerance<FPT,PersentType> pred( percentage_tolerance, fpc_type );
111
112        return pred( left, right );
113    }
114};
115
116namespace {
117check_is_close_t check_is_close;
118}
119
120//____________________________________________________________________________//
121
122// ************************************************************************** //
123// **************               check_is_small                 ************** //
124// ************************************************************************** //
125
126struct check_is_small_t {
127    // Public typedefs
128    typedef bool result_type;
129
130    template<typename FPT>
131    bool
132    operator()( FPT fpv, FPT tolerance )
133    {
134        return tt_detail::fpt_abs( fpv ) < tolerance;
135    }
136};
137
138namespace {
139check_is_small_t check_is_small;
140}
141
142//____________________________________________________________________________//
143
144} // namespace test_tools
145
146} // namespace boost
147
148//____________________________________________________________________________//
149
150#include <boost/test/detail/enable_warnings.hpp>
151
152// ***************************************************************************
153//  Revision History :
154// 
155//  $Log: floating_point_comparison.hpp,v $
156//  Revision 1.23  2005/05/29 08:54:57  rogeeff
157//  allow bind usage
158//
159//  Revision 1.22  2005/02/21 10:21:40  rogeeff
160//  check_is_small implemented
161//  check functions implemented as function objects
162//
163//  Revision 1.21  2005/02/20 08:27:05  rogeeff
164//  This a major update for Boost.Test framework. See release docs for complete list of fixes/updates
165//
166//  Revision 1.20  2005/02/01 06:40:06  rogeeff
167//  copyright update
168//  old log entries removed
169//  minor stilistic changes
170//  depricated tools removed
171//
172//  Revision 1.19  2005/01/22 19:22:12  rogeeff
173//  implementation moved into headers section to eliminate dependency of included/minimal component on src directory
174//
175// ***************************************************************************
176
177#endif // BOOST_FLOATING_POINT_COMAPARISON_HPP_071894GER
Note: See TracBrowser for help on using the repository browser.