How to check the float variable contain Real Numbers or not in C++ ?
ex: -1.#IND000 my value
How to determine whether it is Real numbers or like above numbers .
How to check the float variable contain Real Numbers or not in C++ ?
ex: -1.#IND000 my value
How to determine whether it is Real numbers or like above numbers .
converting into bool (explicitly o by mean of whatever logical operator) will give "false" for all values that are not "real" or that are 0, so
bool is_number(double d)
{ return d || d==0; }
should be fine.
There are functions like std::isnan
in header <cmath>
.
These following returns true
if it's a number, false
otherwise:
bool test1(double d) { return d == d; }
bool test2(double d) { return d * 0.0 == 0.0; }
These's a good discussion on Checking if a double (or float) is nan in C++.
A very simple way..
float a=3.9;
long b;
b=a;
if ((float)b==a)
cout<<"Non-real, i.e. integer";
else
cout<<"REAL";
You can use '_isnan' in Visual Studio (it is included in float.h):
float T;
T=std::numeric_limits<double>::quiet_NaN(); //YOUR CALCS HERE!!
if (_isnan(T)) {
printf("error\n");
}