Loading...
Searching...
No Matches
float_cmp.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
#ifndef DUNE_COMMON_FLOAT_CMP_HH
#define DUNE_COMMON_FLOAT_CMP_HH
namespace Dune {
namespace FloatCmp {
// basic constants
enum CmpStyle {
relativeWeak,
relativeStrong,
absolute,
defaultCmpStyle = relativeWeak
};
enum RoundingStyle {
towardZero,
towardInf,
downward,
upward,
defaultRoundingStyle = towardZero
};
template<class T> struct EpsilonType;
template<class T, CmpStyle style = defaultCmpStyle>
struct DefaultEpsilon {
static typename EpsilonType<T>::Type value();
};
// operations in functional style
template <class T, CmpStyle style /*= defaultCmpStyle*/>
bool eq(const T &first,
const T &second,
typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, style>::value());
template <class T, CmpStyle style /*= defaultCmpStyle*/>
bool ne(const T &first,
const T &second,
typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, style>::value());
template <class T, CmpStyle style /*= defaultCmpStyle*/>
bool gt(const T &first,
const T &second,
typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, style>::value());
template <class T, CmpStyle style /*= defaultCmpStyle*/>
bool lt(const T &first,
const T &second,
typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, style>::value());
template <class T, CmpStyle style /*= defaultCmpStyle*/>
bool ge(const T &first,
const T &second,
typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, style>::value());
template <class T, CmpStyle style /*= defaultCmpStyle*/>
bool le(const T &first,
const T &second,
typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, style>::value());
// rounding operations
template<class I, class T, CmpStyle cstyle /*= defaultCmpStyle*/, RoundingStyle rstyle /*= defaultRoundingStyle*/>
I round(const T &val, typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, cstyle>::value());
// truncation
template<class I, class T, CmpStyle cstyle /*= defaultCmpStyle*/, RoundingStyle rstyle /*= defaultRoundingStyle*/>
I trunc(const T &val, typename EpsilonType<T>::Type epsilon = DefaultEpsilon<T, cstyle>::value());
// group FloatCmp
} //namespace FloatCmp
// oo interface
template<class T, FloatCmp::CmpStyle cstyle_ = FloatCmp::defaultCmpStyle,
FloatCmp::RoundingStyle rstyle_ = FloatCmp::defaultRoundingStyle>
class FloatCmpOps {
typedef FloatCmp::CmpStyle CmpStyle;
typedef FloatCmp::RoundingStyle RoundingStyle;
public:
// record template parameters
static const CmpStyle cstyle = cstyle_;
static const RoundingStyle rstyle = rstyle_;
typedef T ValueType;
typedef typename FloatCmp::EpsilonType<T>::Type EpsilonType;
private:
EpsilonType epsilon_;
typedef FloatCmp::DefaultEpsilon<EpsilonType, cstyle> DefaultEpsilon;
public:
FloatCmpOps(EpsilonType epsilon = DefaultEpsilon::value());
EpsilonType epsilon() const;
void epsilon(EpsilonType epsilon__);
bool eq(const ValueType &first, const ValueType &second) const;
bool ne(const ValueType &first, const ValueType &second) const;
bool gt(const ValueType &first, const ValueType &second) const;
bool lt(const ValueType &first, const ValueType &second) const;
bool ge(const ValueType &first, const ValueType &second) const;
bool le(const ValueType &first, const ValueType &second) const;
template<class I>
I round(const ValueType &val) const;
template<class I>
I trunc(const ValueType &val) const;
};
} //namespace Dune
#include "float_cmp.cc"
#endif //DUNE_COMMON_FLOAT_CMP_HH
float_cmp.cc
Dune::FloatCmp::ne
bool ne(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for inequality using epsilon
Definition float_cmp.cc:151
Dune::FloatCmp::CmpStyle
CmpStyle
Definition float_cmp.hh:107
Dune::FloatCmp::eq
bool eq(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test for equality using epsilon
Definition float_cmp.cc:144
Dune::FloatCmp::round
I round(const T &val, typename EpsilonType< T >::Type epsilon)
round using epsilon
Definition float_cmp.cc:311
Dune::FloatCmp::trunc
I trunc(const T &val, typename EpsilonType< T >::Type epsilon)
truncate using epsilon
Definition float_cmp.cc:407
Dune::FloatCmp::RoundingStyle
RoundingStyle
Definition float_cmp.hh:119
Dune::FloatCmp::lt
bool lt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first lesser than second
Definition float_cmp.cc:165
Dune::FloatCmp::gt
bool gt(const T &first, const T &second, typename EpsilonType< T >::Type epsilon)
test if first greater than second
Definition float_cmp.cc:158
Dune::FloatCmp::ge
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
Dune::FloatCmp::le
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
Dune::FloatCmp::defaultCmpStyle
@ defaultCmpStyle
the global default compare style (relative_weak)
Definition float_cmp.hh:115
Dune::FloatCmp::relativeStrong
@ relativeStrong
|a-b|/|a| <= epsilon && |a-b|/|b| <= epsilon
Definition float_cmp.hh:111
Dune::FloatCmp::relativeWeak
@ relativeWeak
|a-b|/|a| <= epsilon || |a-b|/|b| <= epsilon
Definition float_cmp.hh:109
Dune::FloatCmp::absolute
@ absolute
|a-b| <= epsilon
Definition float_cmp.hh:113
Dune::FloatCmp::towardZero
@ towardZero
always round toward 0
Definition float_cmp.hh:121
Dune::FloatCmp::towardInf
@ towardInf
always round away from 0
Definition float_cmp.hh:123
Dune::FloatCmp::upward
@ upward
round toward
Definition float_cmp.hh:127
Dune::FloatCmp::downward
@ downward
round toward
Definition float_cmp.hh:125
Dune::FloatCmp::defaultRoundingStyle
@ defaultRoundingStyle
the global default rounding style (toward_zero)
Definition float_cmp.hh:129
Dune
Dune namespace
Definition alignedallocator.hh:13
Dune::FloatCmp
Various ways to compare floating-point numbers.
Definition float_cmp.cc:16
Dune::FloatCmp::EpsilonType::Type
T Type
The epsilon type corresponding to value type T.
Definition float_cmp.cc:25
Dune::FloatCmp::DefaultEpsilon
mapping from a value type and a compare style to a default epsilon
Definition float_cmp.hh:141
Dune::FloatCmp::DefaultEpsilon::value
static EpsilonType< T >::Type value()
Returns the default epsilon for the given value type and compare style.
Dune::FloatCmpOps::le
bool le(const ValueType &first, const ValueType &second) const
test if first lesser or equal second
Definition float_cmp.cc:486
Dune::FloatCmpOps::FloatCmpOps
FloatCmpOps(EpsilonType epsilon=DefaultEpsilon::value())
construct an operations object
Definition float_cmp.cc:431
Dune::FloatCmpOps::eq
bool eq(const ValueType &first, const ValueType &second) const
test for equality using epsilon
Definition float_cmp.cc:451
Dune::FloatCmpOps::cstyle
static const CmpStyle cstyle
How comparisons are done.
Definition float_cmp.hh:298
Dune::FloatCmpOps::lt
bool lt(const ValueType &first, const ValueType &second) const
test if first lesser than second
Definition float_cmp.cc:472
Dune::FloatCmpOps::ge
bool ge(const ValueType &first, const ValueType &second) const
test if first greater or equal second
Definition float_cmp.cc:479
Dune::FloatCmpOps::rstyle
static const RoundingStyle rstyle
How rounding is done.
Definition float_cmp.hh:300
Dune::FloatCmpOps::EpsilonType
FloatCmp::EpsilonType< T >::Type EpsilonType
Type of the epsilon.
Definition float_cmp.hh:307
Dune::FloatCmpOps::ne
bool ne(const ValueType &first, const ValueType &second) const
test for inequality using epsilon
Definition float_cmp.cc:458
Dune::FloatCmpOps::ValueType
T ValueType
Type of the values to compare.
Definition float_cmp.hh:302
Dune::FloatCmpOps::epsilon
EpsilonType epsilon() const
return the current epsilon
Definition float_cmp.cc:436
Dune::FloatCmpOps::round
I round(const ValueType &val) const
round using epsilon
Definition float_cmp.cc:495
Dune::FloatCmpOps::trunc
I trunc(const ValueType &val) const
truncate using epsilon
Definition float_cmp.cc:503
Dune::FloatCmpOps::gt
bool gt(const ValueType &first, const ValueType &second) const
test if first greater than second
Definition float_cmp.cc:465