Thread: isnan() and isinf() in C++

  1. #16
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    If it wasn't optimized, you wouldn't need the volatile temp.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Well, GCC has a pragma (or attribute?) that can enable/disable various command line options per function. This includes the optimizer attributes, so you could apply -O0 to a specific function.

    Of course, GCC will also always have a working isnan(), so that doesn't help.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by cpjust View Post
    Maybe it would be good to add a #pragma to the C/C++ standard to do something like this:
    Code:
    #pragma no_optimize_begin
    bool myisnan(double var)
    {
        volatile double temp = var;
        return temp != temp;
    }
    #pragma no_optimize_end
    By definition, pragmas are compiler and/or operating system specific, and therefore no particular particular pragmas will ever be added to the C/C++ standards.

  4. #19
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Actually, C99 has standard pragmas in the form

    #pragma STDC pragma_name

    However, a no-optimization directive would imply that there is a standard implementation technique for each platform.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Another possibility is of course to use integer bit operations to do exactly what I expect the isnan function actually does:
    Check if all the bits in exponent is 1 (although in real life, since the exponent is offset, it actually has the highest bit cleared).

    http://en.wikipedia.org/wiki/IEEE_fl...point_standard
    http://en.wikipedia.org/wiki/NaN

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed