How to check for inf (and | or) NaN in a double variable

  1. When using scanf() double should be read using %lf, not %f. %f will convert the input into a 32-bit float, so the first 32 bits of your variables will be filled with some invalid data, and the last 32 bits will be left as garbage.

  2. Yes. #include <limits>, then std::numeric_limits<double>::quiet_NaN(). Some compilers (e.g. gcc) also provides the NAN macro in <cmath>.

  3. There is no NaN or infinity for integer types. Divide-by-zero for integer will cause an exception (SIGFPE).

  4. #include <cmath>, then std::isinf(x). Use std::isfinite(x) to ensure x is not NaN or Infinity.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)