Consider the following code:
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
template<class T>
bool IsNaN(T t)
{
return t != t;
}
int main(int argc, char**argv)
{
double d1, d2;
sscanf(argv[1], "%f", &d1);
sscanf(argv[2], "%f", &d2);
double dRes = d1/d2;
cout << "dRes = " << dRes << "\n";
if(IsNaN(dRes))
cout << "Is NaN\n";
else
cout << "Not NaN\n";
}
Couple of questions:
- When I pass 0 and 0 as arguments, it outputs
dRes = inf
. But I was expectingdRes = NaN
or something like that. - Is NaN representable in double variables? For that matter, any variable?
- When I changed the data type of d1,d2,dRes to int and passed 0 and 0, I got a
Floating exception
. What is the difference? - How to check if a variable's value is equal to
inf
?