dune-common  2.11
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 // SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
5 #ifndef DUNE_COMMON_FLOAT_CMP_HH
6 #define DUNE_COMMON_FLOAT_CMP_HH
7 
96 namespace Dune {
103  namespace FloatCmp {
104  // basic constants
107  enum CmpStyle {
116  };
130  };
131 
132  template<class T> struct EpsilonType;
133 
135 
140  template<class T, CmpStyle style = defaultCmpStyle>
141  struct DefaultEpsilon {
143  static typename EpsilonType<T>::Type value();
144  };
145 
146  // operations in functional style
147 
150 
152 
159  template <class T, CmpStyle style /*= defaultCmpStyle*/>
160  bool eq(const T &first,
161  const T &second,
164 
172  template <class T, CmpStyle style /*= defaultCmpStyle*/>
173  bool ne(const T &first,
174  const T &second,
177 
188  template <class T, CmpStyle style /*= defaultCmpStyle*/>
189  bool gt(const T &first,
190  const T &second,
193 
204  template <class T, CmpStyle style /*= defaultCmpStyle*/>
205  bool lt(const T &first,
206  const T &second,
209 
220  template <class T, CmpStyle style /*= defaultCmpStyle*/>
221  bool ge(const T &first,
222  const T &second,
225 
236  template <class T, CmpStyle style /*= defaultCmpStyle*/>
237  bool le(const T &first,
238  const T &second,
240 
241  // rounding operations
243 
256  template<class I, class T, CmpStyle cstyle /*= defaultCmpStyle*/, RoundingStyle rstyle /*= defaultRoundingStyle*/>
257  I round(const T &val, typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, cstyle>::value());
258  // truncation
260 
273  template<class I, class T, CmpStyle cstyle /*= defaultCmpStyle*/, RoundingStyle rstyle /*= defaultRoundingStyle*/>
274  I trunc(const T &val, typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, cstyle>::value());
275 
277  // group FloatCmp
278  } //namespace FloatCmp
279 
280 
281  // oo interface
283 
289  template<class T, FloatCmp::CmpStyle cstyle_ = FloatCmp::defaultCmpStyle,
291  class FloatCmpOps {
294 
295  public:
296  // record template parameters
298  static const CmpStyle cstyle = cstyle_;
300  static const RoundingStyle rstyle = rstyle_;
302  typedef T ValueType;
304 
308 
309  private:
310  EpsilonType epsilon_;
311 
313 
314  public:
316 
320 
322  EpsilonType epsilon() const;
324  void epsilon(EpsilonType epsilon__);
325 
327  bool eq(const ValueType &first, const ValueType &second) const;
329 
332  bool ne(const ValueType &first, const ValueType &second) const;
334 
338  bool gt(const ValueType &first, const ValueType &second) const;
340 
344  bool lt(const ValueType &first, const ValueType &second) const;
346 
350  bool ge(const ValueType &first, const ValueType &second) const;
352 
356  bool le(const ValueType &first, const ValueType &second) const;
357 
359 
368  template<class I>
369  I round(const ValueType &val) const;
370 
372 
381  template<class I>
382  I trunc(const ValueType &val) const;
383 
384  };
385 
386 } //namespace Dune
387 
388 #include "float_cmp.cc"
389 
390 #endif //DUNE_COMMON_FLOAT_CMP_HH
bool eq(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for equality using epsilon
Definition: float_cmp.cc:144
the global default rounding style (toward_zero)
Definition: float_cmp.hh:129
bool lt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first lesser than second
Definition: float_cmp.cc:165
bool gt(const ValueType &first, const ValueType &second) const
test if first greater than second
Definition: float_cmp.cc:465
T Type
The epsilon type corresponding to value type T.
Definition: float_cmp.cc:25
T ValueType
Type of the values to compare.
Definition: float_cmp.hh:302
round toward
Definition: float_cmp.hh:125
bool lt(const ValueType &first, const ValueType &second) const
test if first lesser than second
Definition: float_cmp.cc:472
FloatCmp::EpsilonType< T >::Type EpsilonType
Type of the epsilon.
Definition: float_cmp.hh:307
bool ne(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for inequality using epsilon
Definition: float_cmp.cc:151
I trunc(const T &val, typename EpsilonType< T >::Type epsilon)
truncate using epsilon
Definition: float_cmp.cc:407
|a-b|/|a| <= epsilon || |a-b|/|b| <= epsilon
Definition: float_cmp.hh:109
bool ne(const ValueType &first, const ValueType &second) const
test for inequality using epsilon
Definition: float_cmp.cc:458
static const CmpStyle cstyle
How comparisons are done.
Definition: float_cmp.hh:298
|a-b| <= epsilon
Definition: float_cmp.hh:113
round toward
Definition: float_cmp.hh:127
Dune namespace
Definition: alignedallocator.hh:12
static const RoundingStyle rstyle
How rounding is done.
Definition: float_cmp.hh:300
Class encapsulating a default epsilon.
Definition: float_cmp.hh:291
static EpsilonType< T >::Type value()
Returns the default epsilon for the given value type and compare style.
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:158
bool eq(const ValueType &first, const ValueType &second) const
test for equality using epsilon
Definition: float_cmp.cc:451
RoundingStyle
Definition: float_cmp.hh:119
always round toward 0
Definition: float_cmp.hh:121
mapping from a value type and a compare style to a default epsilon
Definition: float_cmp.hh:141
bool ge(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater or equal second
Definition: float_cmp.cc:172
bool ge(const ValueType &first, const ValueType &second) const
test if first greater or equal second
Definition: float_cmp.cc:479
always round away from 0
Definition: float_cmp.hh:123
I round(const ValueType &val) const
round using epsilon
Definition: float_cmp.cc:495
I round(const T &val, typename EpsilonType< T >::Type epsilon)
round using epsilon
Definition: float_cmp.cc:311
the global default compare style (relative_weak)
Definition: float_cmp.hh:115
bool le(const ValueType &first, const ValueType &second) const
test if first lesser or equal second
Definition: float_cmp.cc:486
CmpStyle
Definition: float_cmp.hh:107
bool le(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first lesser or equal second
Definition: float_cmp.cc:179
EpsilonType epsilon() const
return the current epsilon
Definition: float_cmp.cc:436
FloatCmpOps(EpsilonType epsilon=DefaultEpsilon::value())
construct an operations object
Definition: float_cmp.cc:431
|a-b|/|a| <= epsilon && |a-b|/|b| <= epsilon
Definition: float_cmp.hh:111
I trunc(const ValueType &val) const
truncate using epsilon
Definition: float_cmp.cc:503