-
When using
scanf()doubleshould be read using%lf, not%f.%fwill convert the input into a 32-bitfloat, 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. -
Yes.
#include <limits>, thenstd::numeric_limits<double>::quiet_NaN(). Some compilers (e.g. gcc) also provides theNANmacro in<cmath>. -
There is no NaN or infinity for integer types. Divide-by-zero for integer will cause an exception (SIGFPE).
-
#include <cmath>, thenstd::isinf(x). Usestd::isfinite(x)to ensurexis not NaN or Infinity.