Browse Source
ConstantsComparator: reduced code duplications, fixed a potentially div by 0, and silenced a warning
ConstantsComparator: reduced code duplications, fixed a potentially div by 0, and silenced a warning
ConstantsComparator: The old code used `abs(x-y)/abs(x+y)<= epsilon` when comparing two numbers for equality (modulo relative precision). This is weird when x and y have different signs and potentially even causes a div by 0 whenever x=-y. Moreover the templating was awkward, causing a lot of code duplications and a Warning with clang. Distribution[WithReward]: replaced forward declarations of ConstantsComparator by actual includes as the forward declarations caused some ambiguity regarding the template parameters.tempestpy_adaptions
Tim Quatmann
4 years ago
No known key found for this signature in database
GPG Key ID: 6EDE19592731EEC3
4 changed files with 100 additions and 231 deletions
-
6src/storm/storage/Distribution.h
-
5src/storm/storage/DistributionWithReward.h
-
173src/storm/utility/ConstantsComparator.cpp
-
113src/storm/utility/ConstantsComparator.h
@ -1,108 +1,43 @@ |
|||
#ifndef STORM_UTILITY_CONSTANTSCOMPARATOR_H_ |
|||
#define STORM_UTILITY_CONSTANTSCOMPARATOR_H_ |
|||
#pragma once |
|||
|
|||
#include "storm/adapters/RationalFunctionAdapter.h" |
|||
#include <type_traits> |
|||
#include "storm/adapters/RationalNumberAdapter.h" |
|||
|
|||
namespace storm { |
|||
namespace utility { |
|||
|
|||
// A class that can be used for comparing constants. |
|||
template<typename ValueType> |
|||
template<typename ValueType, typename Enable = void> |
|||
class ConstantsComparator { |
|||
public: |
|||
// This needs to be in here, otherwise the template specializations are not used properly. |
|||
public: |
|||
ConstantsComparator() = default; |
|||
|
|||
bool isOne(ValueType const& value) const; |
|||
|
|||
bool isZero(ValueType const& value) const; |
|||
|
|||
bool isEqual(ValueType const& value1, ValueType const& value2) const; |
|||
|
|||
bool isConstant(ValueType const& value) const; |
|||
|
|||
bool isInfinity(ValueType const& value) const; |
|||
|
|||
bool isLess(ValueType const& value1, ValueType const& value2) const; |
|||
}; |
|||
|
|||
// For floats we specialize this class and consider the comparison modulo some predefined precision. |
|||
template<> |
|||
class ConstantsComparator<float> { |
|||
public: |
|||
ConstantsComparator(); |
|||
|
|||
ConstantsComparator(float precision); |
|||
|
|||
bool isOne(float const& value) const; |
|||
|
|||
bool isZero(float const& value) const; |
|||
|
|||
bool isEqual(float const& value1, float const& value2) const; |
|||
|
|||
bool isConstant(float const& value) const; |
|||
|
|||
bool isInfinity(float const& value) const; |
|||
|
|||
bool isLess(float const& value1, float const& value2) const; |
|||
|
|||
private: |
|||
// The precision used for comparisons. |
|||
float precision; |
|||
}; |
|||
|
|||
// For doubles we specialize this class and consider the comparison modulo some predefined precision. |
|||
template<> |
|||
class ConstantsComparator<double> { |
|||
public: |
|||
ConstantsComparator(); |
|||
|
|||
ConstantsComparator(double precision, bool relative = false); |
|||
|
|||
bool isOne(double const& value) const; |
|||
|
|||
bool isZero(double const& value) const; |
|||
|
|||
bool isInfinity(double const& value) const; |
|||
|
|||
bool isEqual(double const& value1, double const& value2) const; |
|||
|
|||
bool isConstant(double const& value) const; |
|||
|
|||
bool isLess(double const& value1, double const& value2) const; |
|||
|
|||
private: |
|||
// The precision used for comparisons. |
|||
double precision; |
|||
|
|||
// Whether to use relative comparison for equality. |
|||
bool relative; |
|||
}; |
|||
// Specialization for numbers where there can be a precision |
|||
template<typename ValueType> |
|||
using ConstantsComparatorEnablePrecision = |
|||
typename std::enable_if_t<std::is_same<ValueType, double>::value || std::is_same<ValueType, storm::RationalNumber>::value>; |
|||
|
|||
// For rational numbers we specialize this class and consider the comparison modulo some predefined precision. |
|||
template<> |
|||
class ConstantsComparator<storm::RationalNumber> { |
|||
public: |
|||
template<typename ValueType> |
|||
class ConstantsComparator<ValueType, ConstantsComparatorEnablePrecision<ValueType>> { |
|||
public: |
|||
ConstantsComparator(); |
|||
ConstantsComparator(ValueType const& precision, bool const& relative = false); |
|||
bool isOne(ValueType const& value) const; |
|||
bool isZero(ValueType const& value) const; |
|||
bool isEqual(ValueType const& value1, ValueType const& value2) const; |
|||
bool isConstant(ValueType const& value) const; |
|||
bool isInfinity(ValueType const& value) const; |
|||
bool isLess(ValueType const& value1, ValueType const& value2) const; |
|||
|
|||
ConstantsComparator(storm::RationalNumber precision, bool relative); |
|||
|
|||
bool isOne(storm::RationalNumber const& value) const; |
|||
|
|||
bool isZero(storm::RationalNumber const& value) const; |
|||
|
|||
bool isEqual(storm::RationalNumber const& value1, storm::RationalNumber const& value2) const; |
|||
|
|||
bool isConstant(storm::RationalNumber const& value) const; |
|||
|
|||
bool isInfinity(storm::RationalNumber const& value) const; |
|||
|
|||
bool isLess(storm::RationalNumber const& value1, storm::RationalNumber const& value2) const; |
|||
|
|||
private: |
|||
storm::RationalNumber precision; |
|||
private: |
|||
ValueType precision; |
|||
bool relative; |
|||
}; |
|||
} |
|||
} |
|||
|
|||
#endif /* STORM_UTILITY_CONSTANTSCOMPARATOR_H_ */ |
|||
} // namespace utility |
|||
} // namespace storm |
Write
Preview
Loading…
Cancel
Save
Reference in new issue