View Full Version : Is there any ways to check for nan inf and -inf in Qt?
aarelovich
25th February 2009, 11:16
Hi:
Simple question for today. Does Qt provide any function, macro or something that checks if a qreal has a value of Inf -Inf or nan?
I know c++ has a way using certain libraries, but I thought Qt might have something like this too.
If there is I can't find it anywhere?
Thanks
Boron
25th February 2009, 16:54
No.
qreal is just a simple typedef double qreal (float on ARM architecture).
So you can handle it as if it is a double/float.
aarelovich
25th February 2009, 17:08
I did.
I've just thought that since there were functions like qBound qAbs, qMax there would be something to detect the nans and Ifs.
Thanks
Boron
25th February 2009, 17:40
Unfortunately not. It has to be done the usual way (whatever that might be; I'm avoiding floating point arithmetic deliberately).
aarelovich
25th February 2009, 17:43
To check for NaN is relatively easy as it is the only case where
value == value is false.
To check for Inifinity, seeing as it is a sort of "valid value" is a bit more complex and requires the use of the <limits> c++ library. Here is the code:
QString Approximator::ValidQReal(qreal value){
if (value != value){
return "NaN";
}
else if (value > std::numeric_limits<qreal>::max()){
return "+Inf";
}
else if (value < -std::numeric_limits<qreal>::max()){
return "-Inf";
}
else
return "";
}
In my particular case I return a QString that eventually cuts my thread and shows an errro message of what was found.
AndyArmitage
2nd June 2016, 12:29
Of course this thread is quite old, but these are now available: qIsInf(), qIsNan(), qIsFinite.
I followed the above advice before a colleague pointed out that this is now available, at least in Qt5.4
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.