31 template<
class T,
typename A>
42 template<
class T,
int n>
52 {
return std::numeric_limits<typename EpsilonType<T>::Type>::epsilon()*8; }
57 {
return std::numeric_limits<typename EpsilonType<T>::Type>::epsilon()*8; }
67 template<
class T, CmpStyle style = defaultCmpStyle>
71 static bool eq(
const T &first,
74 {
return std::abs(first - second) <= epsilon*std::max(std::abs(first), std::abs(second)); }
78 static bool eq(
const T &first,
81 {
return std::abs(first - second) <= epsilon*std::min(std::abs(first), std::abs(second)); }
85 static bool eq(
const T &first,
88 {
return std::abs(first-second) <= epsilon; }
90 template<
class T, CmpStyle cstyle>
92 static bool eq(
const std::vector<T> &first,
93 const std::vector<T> &second,
95 unsigned int size = first.size();
96 if(size != second.size())
return false;
97 for(
unsigned int i = 0; i < size; ++i)
103 template<
class T,
int n, CmpStyle cstyle>
108 for(
int i = 0; i < n; ++i)
117 template <
class T, CmpStyle style>
118 bool eq(
const T &first,
124 template <
class T, CmpStyle style>
125 bool ne(
const T &first,
129 return !eq<T, style>(first, second, epsilon);
131 template <
class T, CmpStyle style>
132 bool gt(
const T &first,
136 return first > second && ne<T, style>(first, second, epsilon);
138 template <
class T, CmpStyle style>
139 bool lt(
const T &first,
143 return first < second && ne<T, style>(first, second, epsilon);
145 template <
class T, CmpStyle style>
146 bool ge(
const T &first,
150 return first > second || eq<T, style>(first, second, epsilon);
152 template <
class T, CmpStyle style>
153 bool le(
const T &first,
157 return first < second || eq<T, style>(first, second, epsilon);
162 bool eq(
const T &first,
166 return eq<T, defaultCmpStyle>(first, second, epsilon);
169 bool ne(
const T &first,
173 return ne<T, defaultCmpStyle>(first, second, epsilon);
176 bool gt(
const T &first,
180 return gt<T, defaultCmpStyle>(first, second, epsilon);
183 bool lt(
const T &first,
187 return lt<T, defaultCmpStyle>(first, second, epsilon);
190 bool ge(
const T &first,
194 return ge<T, defaultCmpStyle>(first, second, epsilon);
197 bool le(
const T &first,
201 return le<T, defaultCmpStyle>(first, second, epsilon);
206 template<
class I,
class T, CmpStyle cstyle = defaultCmpStyle, RoundingStyle rstyle = defaultRoundingStyle>
208 template<
class I,
class T, CmpStyle cstyle>
216 if(eq<T, cstyle>(T(lower), val, epsilon))
return lower;
217 if(T(lower) > val) { upper = lower; lower--; }
218 else upper = lower+1;
219 if(le<T, cstyle>(val - T(lower), T(upper) - val, epsilon))
224 template<
class I,
class T, CmpStyle cstyle>
232 if(eq<T, cstyle>(T(lower), val, epsilon))
return lower;
233 if(T(lower) > val) { upper = lower; lower--; }
234 else upper = lower+1;
235 if(lt<T, cstyle>(val - T(lower), T(upper) - val, epsilon))
240 template<
class I,
class T, CmpStyle cstyle>
250 template<
class I,
class T, CmpStyle cstyle>
260 template<
class I,
class T, CmpStyle cstyle, RoundingStyle rstyle>
261 struct round_t<
std::vector<I>, std::vector<T>, cstyle, rstyle> {
262 static std::vector<I>
265 unsigned int size = val.size();
266 std::vector<I> res(size);
267 for(
unsigned int i = 0; i < size; ++i)
272 template<
class I,
class T,
int n, CmpStyle cstyle, RoundingStyle rstyle>
278 for(
int i = 0; i < n; ++i)
284 template<
class I,
class T, CmpStyle cstyle, RoundingStyle rstyle>
289 template<
class I,
class T, CmpStyle cstyle>
292 return round<I, T, cstyle, defaultRoundingStyle>(val, epsilon);
294 template<
class I,
class T, RoundingStyle rstyle>
297 return round<I, T, defaultCmpStyle, rstyle>(val, epsilon);
299 template<
class I,
class T>
302 return round<I, T, defaultCmpStyle>(val, epsilon);
307 template<
class I,
class T, CmpStyle cstyle = defaultCmpStyle, RoundingStyle rstyle = defaultRoundingStyle>
309 template<
class I,
class T, CmpStyle cstyle>
315 if(!std::numeric_limits<I>::is_signed)
317 if(eq<T, cstyle>(val, T(0), epsilon))
return I(0);
321 if(T(lower) > val) lower--;
323 if(eq<T, cstyle>(T(lower+1), val, epsilon))
328 template<
class I,
class T, CmpStyle cstyle>
334 if(ne<T, cstyle>(T(upper), val, epsilon)) ++upper;
338 template<
class I,
class T, CmpStyle cstyle>
347 template<
class I,
class T, CmpStyle cstyle>
356 template<
class I,
class T, CmpStyle cstyle, RoundingStyle rstyle>
357 struct trunc_t<
std::vector<I>, std::vector<T>, cstyle, rstyle> {
358 static std::vector<I>
361 unsigned int size = val.size();
362 std::vector<I> res(size);
363 for(
unsigned int i = 0; i < size; ++i)
368 template<
class I,
class T,
int n, CmpStyle cstyle, RoundingStyle rstyle>
374 for(
int i = 0; i < n; ++i)
380 template<
class I,
class T, CmpStyle cstyle, RoundingStyle rstyle>
385 template<
class I,
class T, CmpStyle cstyle>
388 return trunc<I, T, cstyle, defaultRoundingStyle>(val, epsilon);
390 template<
class I,
class T, RoundingStyle rstyle>
393 return trunc<I, T, defaultCmpStyle, rstyle>(val, epsilon);
395 template<
class I,
class T>
398 return trunc<I, T, defaultCmpStyle>(val, epsilon);
403 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
408 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
415 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
419 epsilon_ = epsilon__;
423 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
427 return Dune::FloatCmp::eq<ValueType, cstyle>(first, second, epsilon_);
430 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
434 return Dune::FloatCmp::ne<ValueType, cstyle>(first, second, epsilon_);
437 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
441 return Dune::FloatCmp::gt<ValueType, cstyle>(first, second, epsilon_);
444 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
448 return Dune::FloatCmp::lt<ValueType, cstyle>(first, second, epsilon_);
451 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
455 return Dune::FloatCmp::ge<ValueType, cstyle>(first, second, epsilon_);
458 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
462 return Dune::FloatCmp::le<ValueType, cstyle>(first, second, epsilon_);
466 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
471 return Dune::FloatCmp::round<I, ValueType, cstyle, rstyle_>(val, epsilon_);
474 template<
class T, FloatCmp::CmpStyle cstyle_, FloatCmp::RoundingStyle rstyle_>
479 return Dune::FloatCmp::trunc<I, ValueType, cstyle, rstyle_>(val, epsilon_);
Implements a vector constructed from a given type representing a field and a compile-time given size.
Various ways to compare floating-point numbers.
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 T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for equality using epsilon
Definition float_cmp.cc:118
I round(const T &val, typename EpsilonType< T >::Type epsilon)
round using epsilon
Definition float_cmp.cc:285
I trunc(const T &val, typename EpsilonType< T >::Type epsilon)
truncate using epsilon
Definition float_cmp.cc:381
bool lt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first lesser than second
Definition float_cmp.cc:139
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition float_cmp.cc:132
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
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
@ relativeStrong
|a-b|/|a| <= epsilon && |a-b|/|b| <= epsilon
Definition float_cmp.hh:106
@ relativeWeak
|a-b|/|a| <= epsilon || |a-b|/|b| <= epsilon
Definition float_cmp.hh:104
@ absolute
|a-b| <= epsilon
Definition float_cmp.hh:108
@ towardZero
always round toward 0
Definition float_cmp.hh:116
@ towardInf
always round away from 0
Definition float_cmp.hh:118
@ upward
round toward
Definition float_cmp.hh:122
@ downward
round toward
Definition float_cmp.hh:120
Dune namespace.
Definition alignment.hh:11
vector space out of a tensor product of fields.
Definition fvector.hh:93
Mapping of value type to epsilon type.
Definition float_cmp.cc:21
T Type
The epsilon type corresponding to value type T.
Definition float_cmp.cc:23
EpsilonType< T > Type
The epsilon type corresponding to value type std::vector<T, A>
Definition float_cmp.cc:34
EpsilonType< T > Type
The epsilon type corresponding to value type Dune::FieldVector<T, n>
Definition float_cmp.cc:45
static EpsilonType< T >::Type value()
Definition float_cmp.cc:51
static EpsilonType< T >::Type value()
Definition float_cmp.cc:56
static EpsilonType< T >::Type value()
Definition float_cmp.cc:61
Definition float_cmp.cc:68
static bool eq(const T &first, const T &second, typename EpsilonType< T >::Type epsilon=DefaultEpsilon< T >::value())
Definition float_cmp.cc:71
static bool eq(const T &first, const T &second, typename EpsilonType< T >::Type epsilon=DefaultEpsilon< T >::value())
Definition float_cmp.cc:78
static bool eq(const T &first, const T &second, typename EpsilonType< T >::Type epsilon=DefaultEpsilon< T >::value())
Definition float_cmp.cc:85
static bool eq(const std::vector< T > &first, const std::vector< T > &second, typename EpsilonType< T >::Type epsilon=DefaultEpsilon< T >::value())
Definition float_cmp.cc:92
static bool eq(const Dune::FieldVector< T, n > &first, const Dune::FieldVector< T, n > &second, typename EpsilonType< T >::Type epsilon=DefaultEpsilon< T >::value())
Definition float_cmp.cc:105
Definition float_cmp.cc:207
static I round(const T &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:211
static I round(const T &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:227
static I round(const T &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:243
static I round(const T &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:253
static std::vector< I > round(const T &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:263
static Dune::FieldVector< I, n > round(const T &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:275
Definition float_cmp.cc:308
static I trunc(const T &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:312
static I trunc(const T &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:331
static I trunc(const T &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:341
static I trunc(const T &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:350
static std::vector< I > trunc(const std::vector< T > &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:359
static Dune::FieldVector< I, n > trunc(const Dune::FieldVector< T, n > &val, typename EpsilonType< T >::Type epsilon=(DefaultEpsilon< T, cstyle >::value()))
Definition float_cmp.cc:371
mapping from a value type and a compare style to a default epsilon
Definition float_cmp.hh:136
static EpsilonType< T >::Type value()
Returns the default epsilon for the given value type and compare style.
bool le(const ValueType &first, const ValueType &second) const
test if first lesser or equal second
Definition float_cmp.cc:460
FloatCmpOps(EpsilonType epsilon=DefaultEpsilon::value())
construct an operations object
Definition float_cmp.cc:405
bool eq(const ValueType &first, const ValueType &second) const
test for equality using epsilon
Definition float_cmp.cc:425
bool lt(const ValueType &first, const ValueType &second) const
test if first lesser than second
Definition float_cmp.cc:446
bool ge(const ValueType &first, const ValueType &second) const
test if first greater or equal second
Definition float_cmp.cc:453
FloatCmp::EpsilonType< T >::Type EpsilonType
Type of the epsilon.
Definition float_cmp.hh:302
bool ne(const ValueType &first, const ValueType &second) const
test for inequality using epsilon
Definition float_cmp.cc:432
T ValueType
Type of the values to compare.
Definition float_cmp.hh:297
EpsilonType epsilon() const
return the current epsilon
Definition float_cmp.cc:410
I round(const ValueType &val) const
round using epsilon
Definition float_cmp.cc:469
I trunc(const ValueType &val) const
truncate using epsilon
Definition float_cmp.cc:477
bool gt(const ValueType &first, const ValueType &second) const
test if first greater than second
Definition float_cmp.cc:439