Dune Core Modules (2.9.0)

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 (C) 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 
97 namespace Dune {
100  namespace FloatCmp {
101  // basic constants
104  enum CmpStyle {
113  };
127  };
128 
129  template<class T> struct EpsilonType;
130 
132 
137  template<class T, CmpStyle style = defaultCmpStyle>
138  struct DefaultEpsilon {
140  static typename EpsilonType<T>::Type value();
141  };
142 
143  // operations in functional style
144 
147 
149 
156  template <class T, CmpStyle style /*= defaultCmpStyle*/>
157  bool eq(const T &first,
158  const T &second,
161 
169  template <class T, CmpStyle style /*= defaultCmpStyle*/>
170  bool ne(const T &first,
171  const T &second,
174 
185  template <class T, CmpStyle style /*= defaultCmpStyle*/>
186  bool gt(const T &first,
187  const T &second,
190 
201  template <class T, CmpStyle style /*= defaultCmpStyle*/>
202  bool lt(const T &first,
203  const T &second,
206 
217  template <class T, CmpStyle style /*= defaultCmpStyle*/>
218  bool ge(const T &first,
219  const T &second,
222 
233  template <class T, CmpStyle style /*= defaultCmpStyle*/>
234  bool le(const T &first,
235  const T &second,
237 
238  // rounding operations
240 
253  template<class I, class T, CmpStyle cstyle /*= defaultCmpStyle*/, RoundingStyle rstyle /*= defaultRoundingStyle*/>
254  I round(const T &val, typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, cstyle>::value());
255  // truncation
257 
270  template<class I, class T, CmpStyle cstyle /*= defaultCmpStyle*/, RoundingStyle rstyle /*= defaultRoundingStyle*/>
271  I trunc(const T &val, typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, cstyle>::value());
272 
274  // group FloatCmp
275  } //namespace FloatCmp
276 
277 
278  // oo interface
280 
286  template<class T, FloatCmp::CmpStyle cstyle_ = FloatCmp::defaultCmpStyle,
288  class FloatCmpOps {
291 
292  public:
293  // record template parameters
295  static const CmpStyle cstyle = cstyle_;
297  static const RoundingStyle rstyle = rstyle_;
299  typedef T ValueType;
301 
305 
306  private:
307  EpsilonType epsilon_;
308 
310 
311  public:
313 
317 
319  EpsilonType epsilon() const;
321  void epsilon(EpsilonType epsilon__);
322 
324  bool eq(const ValueType &first, const ValueType &second) const;
326 
329  bool ne(const ValueType &first, const ValueType &second) const;
331 
335  bool gt(const ValueType &first, const ValueType &second) const;
337 
341  bool lt(const ValueType &first, const ValueType &second) const;
343 
347  bool ge(const ValueType &first, const ValueType &second) const;
349 
353  bool le(const ValueType &first, const ValueType &second) const;
354 
356 
365  template<class I>
366  I round(const ValueType &val) const;
367 
369 
378  template<class I>
379  I trunc(const ValueType &val) const;
380 
381  };
382 
383 } //namespace Dune
384 
385 #include "float_cmp.cc"
386 
387 #endif //DUNE_COMMON_FLOAT_CMP_HH
Class encapsulating a default epsilon.
Definition: float_cmp.hh:288
bool le(const ValueType &first, const ValueType &second) const
test if first lesser or equal second
Definition: float_cmp.cc:486
FloatCmpOps(EpsilonType epsilon=DefaultEpsilon::value())
construct an operations object
Definition: float_cmp.cc:431
bool eq(const ValueType &first, const ValueType &second) const
test for equality using epsilon
Definition: float_cmp.cc:451
static const CmpStyle cstyle
How comparisons are done.
Definition: float_cmp.hh:295
bool lt(const ValueType &first, const ValueType &second) const
test if first lesser than second
Definition: float_cmp.cc:472
bool ge(const ValueType &first, const ValueType &second) const
test if first greater or equal second
Definition: float_cmp.cc:479
static const RoundingStyle rstyle
How rounding is done.
Definition: float_cmp.hh:297
FloatCmp::EpsilonType< T >::Type EpsilonType
Type of the epsilon.
Definition: float_cmp.hh:304
bool ne(const ValueType &first, const ValueType &second) const
test for inequality using epsilon
Definition: float_cmp.cc:458
T ValueType
Type of the values to compare.
Definition: float_cmp.hh:299
EpsilonType epsilon() const
return the current epsilon
Definition: float_cmp.cc:436
I round(const ValueType &val) const
round using epsilon
Definition: float_cmp.cc:495
I trunc(const ValueType &val) const
truncate using epsilon
Definition: float_cmp.cc:503
bool gt(const ValueType &first, const ValueType &second) const
test if first greater than second
Definition: float_cmp.cc:465
bool ne(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for inequality using epsilon
Definition: float_cmp.cc:151
CmpStyle
Definition: float_cmp.hh:104
bool eq(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for equality using epsilon
Definition: float_cmp.cc:144
I round(const T &val, typename EpsilonType< T >::Type epsilon)
round using epsilon
Definition: float_cmp.cc:311
I trunc(const T &val, typename EpsilonType< T >::Type epsilon)
truncate using epsilon
Definition: float_cmp.cc:407
RoundingStyle
Definition: float_cmp.hh:116
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 T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition: float_cmp.cc:158
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 le(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first lesser or equal second
Definition: float_cmp.cc:179
@ defaultCmpStyle
the global default compare style (relative_weak)
Definition: float_cmp.hh:112
@ relativeStrong
|a-b|/|a| <= epsilon && |a-b|/|b| <= epsilon
Definition: float_cmp.hh:108
@ relativeWeak
|a-b|/|a| <= epsilon || |a-b|/|b| <= epsilon
Definition: float_cmp.hh:106
@ absolute
|a-b| <= epsilon
Definition: float_cmp.hh:110
@ towardZero
always round toward 0
Definition: float_cmp.hh:118
@ towardInf
always round away from 0
Definition: float_cmp.hh:120
@ upward
round toward
Definition: float_cmp.hh:124
@ downward
round toward
Definition: float_cmp.hh:122
@ defaultRoundingStyle
the global default rounding style (toward_zero)
Definition: float_cmp.hh:126
Dune namespace.
Definition: alignedallocator.hh:13
mapping from a value type and a compare style to a default epsilon
Definition: float_cmp.hh:138
static EpsilonType< T >::Type value()
Returns the default epsilon for the given value type and compare style.
T Type
The epsilon type corresponding to value type T.
Definition: float_cmp.cc:25
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 1, 22:29, 2024)