I have a seemingly simple goal: I want to convert a float into an integer-type that is exactly 32 bit wide. The code I am posting works on my windows machine, but not on the Linux device I am trying to run the code in the end. I have tried this first:
float a;
a = /*some real number, for example 1.08825e+11*/;
if (!isfinite(a)){
 return -1;
}
int32_t b;
b = static_cast<int32_t> (a);
Which does not work -the programs just crashes with this information in the command window:
Runtime received SIGFPE (address: 0xb62cd3b7 reason: floating-point invalid operation)
Then I have found a function in another question on stack overflow that safely converts float into int-data types and have tried to adopt the function for my needs. This is my version:
template <class FloatType>
int32_t safeFloatToDINT(const FloatType& num) {
    //check if float fits into int32_t
    if (std::numeric_limits<int32_t>::digits < std::numeric_limits<FloatType>::digits) {
        // check if float is smaller than max int32_t
        if ((num < static_cast<FloatType>(std::numeric_limits<int32_t>::max())) &&
            (num > static_cast<FloatType>(std::numeric_limits<int32_t>::min()))) {
            return static_cast<int32_t>(num); //safe to cast
        }
        else {
            std::cerr << "Unsafe conversion of value:" << num << std::endl;
            //NaN is not defined for int32_t return the largest int32_t value
            return std::numeric_limits<int32_t>::max();
        }
    }
    else {
        //It is safe to cast
        return static_cast<int32_t>(num);
    }
}
Unfortunately, b = safeFloatToDINT(a);
throws the FPU exception, too. I have also just tried writing b = (int32_t)a; but that does not work either. I would like to know how I can take my float variable and safely convert it into an integer type that spans 32 bit.
Thank you in advance.
