I want to implement a template function, which detects if the difference of ValueA and ValueB is bigger than a given hystersis. e.x.
So I implemented this function:
template<typename T>
bool MyClass::IsHysteresisExceeded(T ValueA, T ValueB, T Hysteresis) {
T ValueMax = std::max(ValueA, ValueB);
T ValueMin = std::min(ValueA, ValueB);
return (ValueMax - ValueMin) > Hysteresis;
}
But with the following parameters this function returns false when I expected true as result.
IsHysteresisExceeded<int>(-2147483648, 2147483647, 10)
I know that a integer overflow occurs while subtracting, but I did not find an elegant solution yet.
I have the following solution for integers:
template<typename T>
bool IsHysteresisExceeded(T ValueA, T ValueB, T Hysteresis) {
T ValueMax = std::max(ValueA, ValueB);
T ValueMin = std::min(ValueA, ValueB);
assert(Hysteresis >= 0);
T underflowRange = std::numeric_limits<T>::min() + Hysteresis;
bool underflow = underflowRange > ValueMax;
return !underflow && (ValueMax - Hysteresis > ValueMin);
}
The trick is to detect the underflow. If it happens you may be sure ValueMin is in range <ValueMax,std::numeric_limits<T>::min()> and
(ValueMax - Hysteresis) < std::numeric_limits<T>::min() <= ValueMin
I posted the code on godbolt.org
Edit: My previous answer used a very popular approach and was also wrong. I proposed to detect the underflow like:
T lowBound = ValueMax - Hysteresis;
bool underflow = lowBound > ValueMax;
Although it produces expected results on the architectures i know, it is an undefined behavior.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With