dune-common  2.5.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
float_cmp.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_COMMON_FLOAT_CMP_HH
4 #define DUNE_COMMON_FLOAT_CMP_HH
5 
94 namespace Dune {
98  namespace FloatCmp {
99  // basic constants
102  enum CmpStyle {
111  };
125  };
126 
127  template<class T> struct EpsilonType;
128 
130 
135  template<class T, CmpStyle style = defaultCmpStyle>
136  struct DefaultEpsilon {
138  static typename EpsilonType<T>::Type value();
139  };
140 
141  // operations in functional style
142 
145 
147 
154  template <class T, CmpStyle style /*= defaultCmpStyle*/>
155  bool eq(const T &first,
156  const T &second,
159 
167  template <class T, CmpStyle style /*= defaultCmpStyle*/>
168  bool ne(const T &first,
169  const T &second,
172 
183  template <class T, CmpStyle style /*= defaultCmpStyle*/>
184  bool gt(const T &first,
185  const T &second,
188 
199  template <class T, CmpStyle style /*= defaultCmpStyle*/>
200  bool lt(const T &first,
201  const T &second,
204 
215  template <class T, CmpStyle style /*= defaultCmpStyle*/>
216  bool ge(const T &first,
217  const T &second,
220 
231  template <class T, CmpStyle style /*= defaultCmpStyle*/>
232  bool le(const T &first,
233  const T &second,
235 
236  // rounding operations
238 
251  template<class I, class T, CmpStyle cstyle /*= defaultCmpStyle*/, RoundingStyle rstyle /*= defaultRoundingStyle*/>
252  I round(const T &val, typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, cstyle>::value());
253  // truncation
255 
268  template<class I, class T, CmpStyle cstyle /*= defaultCmpStyle*/, RoundingStyle rstyle /*= defaultRoundingStyle*/>
269  I trunc(const T &val, typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, cstyle>::value());
270 
272  // group FloatCmp
273  } //namespace FloatCmp
274 
275 
276  // oo interface
278 
284  template<class T, FloatCmp::CmpStyle cstyle_ = FloatCmp::defaultCmpStyle,
286  class FloatCmpOps {
289 
290  public:
291  // record template parameters
293  static const CmpStyle cstyle = cstyle_;
295  static const RoundingStyle rstyle = rstyle_;
297  typedef T ValueType;
299 
303 
304  private:
305  EpsilonType epsilon_;
306 
308 
309  public:
311 
315 
317  EpsilonType epsilon() const;
319  void epsilon(EpsilonType epsilon__);
320 
322  bool eq(const ValueType &first, const ValueType &second) const;
324 
327  bool ne(const ValueType &first, const ValueType &second) const;
329 
333  bool gt(const ValueType &first, const ValueType &second) const;
335 
339  bool lt(const ValueType &first, const ValueType &second) const;
341 
345  bool ge(const ValueType &first, const ValueType &second) const;
347 
351  bool le(const ValueType &first, const ValueType &second) const;
352 
354 
363  template<class I>
364  I round(const ValueType &val) const;
365 
367 
376  template<class I>
377  I trunc(const ValueType &val) const;
378 
379  };
380 
381 } //namespace Dune
382 
383 #include "float_cmp.cc"
384 
385 #endif //DUNE_COMMON_FLOAT_CMP_HH
bool gt(const ValueType &first, const ValueType &second) const
test if first greater than second
Definition: float_cmp.cc:439
bool ne(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for inequality using epsilon
Definition: float_cmp.cc:125
bool eq(const ValueType &first, const ValueType &second) const
test for equality using epsilon
Definition: float_cmp.cc:425
|a-b| &lt;= epsilon
Definition: float_cmp.hh:108
FloatCmpOps(EpsilonType epsilon=DefaultEpsilon::value())
construct an operations object
Definition: float_cmp.cc:405
T Type
The epsilon type corresponding to value type T.
Definition: float_cmp.cc:23
bool le(const ValueType &first, const ValueType &second) const
test if first lesser or equal second
Definition: float_cmp.cc:460
bool lt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first lesser than second
Definition: float_cmp.cc:139
round toward
Definition: float_cmp.hh:120
the global default compare style (relative_weak)
Definition: float_cmp.hh:110
bool eq(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for equality using epsilon
Definition: float_cmp.cc:118
T ValueType
Type of the values to compare.
Definition: float_cmp.hh:297
bool le(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first lesser or equal second
Definition: float_cmp.cc:153
bool ge(const ValueType &first, const ValueType &second) const
test if first greater or equal second
Definition: float_cmp.cc:453
|a-b|/|a| &lt;= epsilon &amp;&amp; |a-b|/|b| &lt;= epsilon
Definition: float_cmp.hh:106
always round toward 0
Definition: float_cmp.hh:116
bool ge(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater or equal second
Definition: float_cmp.cc:146
static EpsilonType< T >::Type value()
Returns the default epsilon for the given value type and compare style.
CmpStyle
Definition: float_cmp.hh:102
I round(const T &val, typename EpsilonType< T >::Type epsilon)
round using epsilon
Definition: float_cmp.cc:285
Class encapsulating a default epsilon.
Definition: float_cmp.hh:286
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:132
always round away from 0
Definition: float_cmp.hh:118
round toward
Definition: float_cmp.hh:122
FloatCmp::EpsilonType< T >::Type EpsilonType
Type of the epsilon.
Definition: float_cmp.hh:302
I trunc(const T &val, typename EpsilonType< T >::Type epsilon)
truncate using epsilon
Definition: float_cmp.cc:381
bool lt(const ValueType &first, const ValueType &second) const
test if first lesser than second
Definition: float_cmp.cc:446
static const RoundingStyle rstyle
How rounding is done.
Definition: float_cmp.hh:295
the global default rounding style (toward_zero)
Definition: float_cmp.hh:124
I trunc(const ValueType &val) const
truncate using epsilon
Definition: float_cmp.cc:477
bool ne(const ValueType &first, const ValueType &second) const
test for inequality using epsilon
Definition: float_cmp.cc:432
static const CmpStyle cstyle
How comparisons are done.
Definition: float_cmp.hh:293
mapping from a value type and a compare style to a default epsilon
Definition: float_cmp.hh:136
RoundingStyle
Definition: float_cmp.hh:114
|a-b|/|a| &lt;= epsilon || |a-b|/|b| &lt;= epsilon
Definition: float_cmp.hh:104
I round(const ValueType &val) const
round using epsilon
Definition: float_cmp.cc:469
EpsilonType epsilon() const
return the current epsilon
Definition: float_cmp.cc:410