Can a conversion from double to int be written in portable C
Can a conversion from double to int be written in portable C I need to write function like double_to_int(double val, int *err) which would covert double val to integer when it's possible; otherwise report an error (NAN/INFs/OUT_OF_RANGE). double_to_int(double val, int *err) so pseudo code implementation would look like: if isnan(val): err = ERR_NAN return 0 if val < MAX_INT: err = ERR_MINUS_INF return MIN_INT if ... return (int)val There are at least two similar questions on SO: in this answer it's solved in enough clean way, though it's C++ solution - in C we do not have portable digits for signed int. In this answer, it's explained why we cannot just check (val > INT_MAX || val < INT_MIN) . (val > INT_MAX || val < INT_MIN) So the only possible clean way i see is to use floating point environment, but it's stated as implementation-defined feature. So my question: is there any way to implement double_to_int function in cross-platform...
