Results 1 to 6 of 6

Thread: Is there any ways to check for nan inf and -inf in Qt?

  1. #1
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Is there any ways to check for nan inf and -inf in Qt?

    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

  2. #2
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is there any ways to check for nan inf and -inf in Qt?

    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.

  3. #3
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is there any ways to check for nan inf and -inf in Qt?

    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

  4. #4
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is there any ways to check for nan inf and -inf in Qt?

    Unfortunately not. It has to be done the usual way (whatever that might be; I'm avoiding floating point arithmetic deliberately).

  5. #5
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Is there any ways to check for nan inf and -inf in Qt?

    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:

    Qt Code:
    1. QString Approximator::ValidQReal(qreal value){
    2. if (value != value){
    3. return "NaN";
    4. }
    5. else if (value > std::numeric_limits<qreal>::max()){
    6. return "+Inf";
    7. }
    8. else if (value < -std::numeric_limits<qreal>::max()){
    9. return "-Inf";
    10. }
    11. else
    12. return "";
    13. }
    To copy to clipboard, switch view to plain text mode 

    In my particular case I return a QString that eventually cuts my thread and shows an errro message of what was found.

  6. #6
    Join Date
    Jun 2016
    Posts
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Is there any ways to check for nan inf and -inf in Qt?

    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.