From 8a77e3238d83d3082823ea0ac39046d3c288af3c Mon Sep 17 00:00:00 2001 From: Tim Quatmann Date: Fri, 11 Sep 2020 17:03:26 +0200 Subject: [PATCH] Implemented isAlmostZero and isAlmostOne also for non-double value types. --- src/storm/utility/constants.cpp | 26 ++++++++++++++++++++++---- src/storm/utility/constants.h | 6 ++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/storm/utility/constants.cpp b/src/storm/utility/constants.cpp index 1b2bdff97..9172807ad 100644 --- a/src/storm/utility/constants.cpp +++ b/src/storm/utility/constants.cpp @@ -55,12 +55,14 @@ namespace storm { return std::isnan(value); } - bool isAlmostZero(double const& a) { - return a < 1e-12 && a > -1e-12; + template + bool isAlmostZero(ValueType const& a) { + return a < convertNumber(1e-12) && a > -convertNumber(1e-12); } - bool isAlmostOne(double const& a) { - return a < (1.0 + 1e-12) && a > (1.0 - 1e-12); + template + bool isAlmostOne(ValueType const& a) { + return a < convertNumber(1.0 + 1e-12) && a > convertNumber(1.0 - 1e-12); } template @@ -818,6 +820,16 @@ namespace storm { return std::move(value); } + template<> + bool isAlmostZero(storm::RationalFunction const& a) { + return a.isConstant() && isAlmostZero(convertNumber(a)); + } + + template<> + bool isAlmostOne(storm::RationalFunction const& a) { + return a.isConstant() && isAlmostOne(convertNumber(a)); + } + template<> std::pair minmax(std::vector const&) { STORM_LOG_THROW(false, storm::exceptions::InvalidArgumentException, "Minimum/maximum for rational functions is not defined."); @@ -885,6 +897,8 @@ namespace storm { template double infinity(); template bool isOne(double const& value); template bool isZero(double const& value); + template bool isAlmostZero(double const& value); + template bool isAlmostOne(double const& value); template bool isConstant(double const& value); template bool isInfinity(double const& value); template bool isInteger(double const& number); @@ -982,6 +996,8 @@ namespace storm { template bool isConstant(storm::ClnRationalNumber const& value); template bool isInfinity(storm::ClnRationalNumber const& value); template bool isNan(storm::ClnRationalNumber const& value); + template bool isAlmostZero(storm::ClnRationalNumber const& value); + template bool isAlmostOne(storm::ClnRationalNumber const& value); template storm::NumberTraits::IntegerType convertNumber(storm::NumberTraits::IntegerType const& number); template storm::ClnRationalNumber convertNumber(storm::ClnRationalNumber const& number); template storm::ClnRationalNumber simplify(storm::ClnRationalNumber value); @@ -1008,6 +1024,8 @@ namespace storm { template bool isConstant(storm::GmpRationalNumber const& value); template bool isInfinity(storm::GmpRationalNumber const& value); template bool isNan(storm::GmpRationalNumber const& value); + template bool isAlmostZero(storm::GmpRationalNumber const& value); + template bool isAlmostOne(storm::GmpRationalNumber const& value); template storm::NumberTraits::IntegerType convertNumber(storm::NumberTraits::IntegerType const& number); template storm::GmpRationalNumber convertNumber(storm::GmpRationalNumber const& number); template storm::GmpRationalNumber simplify(storm::GmpRationalNumber value); diff --git a/src/storm/utility/constants.h b/src/storm/utility/constants.h index ddd125edb..787ead089 100644 --- a/src/storm/utility/constants.h +++ b/src/storm/utility/constants.h @@ -88,9 +88,11 @@ namespace storm { template bool isNan(ValueType const& a); - bool isAlmostZero(double const& a); + template + bool isAlmostZero(ValueType const& a); - bool isAlmostOne(double const& a); + template + bool isAlmostOne(ValueType const& a); template bool isConstant(ValueType const& a);