Warn on invalid fp-to-int conversions?

Hi,
Consider this program:

#include <math.h>
int foo() {
   int a = (int)INFINITY;
   int b = (int)NAN;
   return a+b;
}

It compiles without warnings (using -Wall), however neither of the conversions will result in anything useful. Would it be reasonable to have a warning about these kinds of conversions?

-Krzysztof

Hi,
Consider this program:

#include <math.h>
int foo() {
  int a = (int)INFINITY;
  int b = (int)NAN;
  return a+b;
}

It compiles without warnings (using -Wall), however neither of the conversions will result in anything useful. Would it be reasonable to have a warning about these kinds of conversions?

As I recall, these conversions, as well as any out-of-range conversions, are UB, and we should warn.

  -Hal

A little while ago, we replaced some of the overloaded stuff from math.h with _Generic in FreeBSD and found quite a few bugs in programs (including a number of configure scripts) calling math.h functions with integer arguments when they intended to provide floating point values. For example, no one intentionally calls isnan() with an int argument (unless via a load of template / macro instantiations). It would have been nice to have a warning for these conversions as well, but it’s not clear exactly when you should warn.

David