Absolute hysteresis calculation in C++

Viewed 161

I want to implement a template function, which detects if the difference of ValueA and ValueB is bigger than a given hystersis. e.x.

  • ValueA=5, ValueB=7, Hystersis=1 -> true
  • ValueA=5, ValueB=7, Hystersis=3 -> false
  • ValueA=-5, ValueB=1, Hystersis=7 -> false

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.

3 Answers

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.

One way to detect possible overflow is to use some indicator of "how far" from limits is a value. I use a simple division, which wants to normalize vale values in the range [-1,1].
Then I substract both "positions" to get the range between them, and compare it with a valid range, this is, 1:

#include <limits>
#include <math.h>
#include <iostream>

template<typename T>
bool IsHysteresisExceeded(T ValueA, T ValueB, T Hysteresis) {
    long double posA = (long double) ValueA / std::numeric_limits<T>::max();
    long double posB = (long double) ValueB / std::numeric_limits<T>::max();

    if (std::fabs(posA - posB) > 1)
        return true; //ValueMax - ValueMin would overflow

    T ValueMax = std::max(ValueA, ValueB);
    T ValueMin = std::min(ValueA, ValueB);
    return (ValueMax - ValueMin) > Hysteresis;
}

int main()
{
    std::cout << (IsHysteresisExceeded<int>(-2147483648, 2147483647, 10) ? "Exceeded" : "In range") << std::endl;
}

I was hoping that this version would compile down efficiently, but alas, the C++ compiler on my machine is unable to merge the two branches. Posting anyway because it uses only +, -, < and a default constructor for 0.

#include <algorithm>
#include <tuple>

template <typename T> bool IsHysteresisExceeded(T a, T b, T h) {
  std::tie(a, b) = std::minmax(a, b);
  return a < T{} ? h + a < b : h < b - a;
}

bool test(int a, int b, int h) { return IsHysteresisExceeded(a, b, h); }
Related