From 641a55ace26db038b07e2ae2c99a60eca8910727 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Bl=C3=BChdorn?= Date: Tue, 29 Aug 2023 17:29:21 +0200 Subject: [PATCH] Adapt the behaviour of CheckExpressionArguments for pow. Warn about negative bases for non-integral exponents. --- include/codi/expressions/real/binaryOperators.hpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/include/codi/expressions/real/binaryOperators.hpp b/include/codi/expressions/real/binaryOperators.hpp index ce4af4f3..cf75d8f3 100644 --- a/include/codi/expressions/real/binaryOperators.hpp +++ b/include/codi/expressions/real/binaryOperators.hpp @@ -576,7 +576,7 @@ namespace codi { static CODI_INLINE Real gradientA(ArgA const& argA, ArgB const& argB, Real const& result) { CODI_UNUSED(result); - checkArguments(argA); + checkArguments(argA, argB); if (RealTraits::getPassiveValue(argA) <= 0.0 && 1 <= RealTraits::MaxDerivativeOrder()) { // Special case for higher order derivatives. Derivative will be wrong since the argB part is not evaluated. return RealTraits::getPassiveValue(argB) * pow(argA, argB - 1.0); @@ -590,7 +590,7 @@ namespace codi { static CODI_INLINE Real gradientB(ArgA const& argA, ArgB const& argB, Real const& result) { CODI_UNUSED(argB); - checkArguments(argA); + checkArguments(argA, argB); if (RealTraits::getPassiveValue(argA) > 0.0) { return log(argA) * result; } else { @@ -599,11 +599,14 @@ namespace codi { } private: - template - static CODI_INLINE void checkArguments(ArgA& argA) { + template + static CODI_INLINE void checkArguments(ArgA const& argA, ArgB const& argB) { if (Config::CheckExpressionArguments) { - if (RealTraits::getPassiveValue(argA) < 0.0) { - CODI_EXCEPTION("Negative base for active exponent in pow function. (Value: %0.15e)", + RealTraits::PassiveReal integralPart = 0.0; + std::modf(RealTraits::getPassiveValue(argB), &integralPart); + + if (RealTraits::getPassiveValue(argA) < 0.0 && RealTraits::getPassiveValue(argB) != integralPart) { + CODI_EXCEPTION("Negative base for non-integral exponent in pow function. (Value: %0.15e)", RealTraits::getPassiveValue(argA)); } }