How to check if a float number is a true number? That is: it is not infinity, negative infinity, NaN ...
float f;
???
Simpler than std::fpclassify()
is to use std::isfinite()
Determines if the given floating point number arg has finite value i.e. it is normal, subnormal or zero, but not infinite or NaN.
std::isnormal() does what you want but it also checks for 0.0. So you might check that case extra:
float f;
bool isNumber = (std::isnormal(f) || f == 0.0);
Edit: as pointed out by user2079303 isnormal
also returns false for a subnormal number which the OP probably does not want.
However, maybe std::isfinite does the right thing.
float f;
bool isNumber = std::isfinite(f) ;
it returns false
for NaN
and Inf
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With