how do I make a portable isnan/isinf function

前端 未结 11 1461
遇见更好的自我
遇见更好的自我 2020-11-29 06:02

I\'ve been using isinf, isnan functions on Linux platforms which worked perfectly. But this didn\'t work on OS-X, so I decided to use std::is

相关标签:
11条回答
  • 2020-11-29 06:14

    Well, ideally, you'd wait until Intel fixes the bug or provides a workaround :-)

    But if you want to detect NaN and Inf from IEEE754 values, map it to an integer (32 or 64 bit depending on whether it's single or double precision) and check if the exponent bits are all 1. This indicates those two cases.

    You can distinguish between NaN and Inf by checking the high order bit of the mantissa. If it's 1, that's NaN otherwise Inf.

    +/-Inf is dictated by the sign bit.

    For single precision (32-bit values), the sign is the high-order bit (b31), exponent is the next eight bits (plus a 23-bit mantissa). For double precision, the sign is still the high-order bit but the exponent is eleven bits (plus 52 bits for the mantissa).

    Wikipedia has all the gory details.

    The following code shows you how the encoding works.

    #include <stdio.h>
    
    static void decode (char *s, double x) {
        long y = *(((long*)(&x))+1);
    
        printf("%08x ",y);
        if ((y & 0x7ff80000L) == 0x7ff80000L) {
            printf ("NaN  (%s)\n", s);
            return;
        }
        if ((y & 0xfff10000L) == 0x7ff00000L) {
            printf ("+Inf (%s)\n", s);
            return;
        }
        if ((y & 0xfff10000L) == 0xfff00000L) {
            printf ("-Inf (%s)\n", s);
            return;
        }
        printf ("%e (%s)\n", x, s);
    }
    
    int main (int argc, char *argv[]) {
        double dvar;
    
        printf ("sizeof double = %d\n", sizeof(double));
        printf ("sizeof long   = %d\n", sizeof(long));
    
        dvar = 1.79e308; dvar = dvar * 10000;
        decode ("too big", dvar);
    
        dvar = -1.79e308; dvar = dvar * 10000;
        decode ("too big and negative", dvar);
    
        dvar = -1.0; dvar = sqrt(dvar);
        decode ("imaginary", dvar);
    
        dvar = -1.79e308;
        decode ("normal", dvar);
    
        return 0;
    }
    

    and it outputs:

    sizeof double = 8
    sizeof long   = 4
    7ff00000 +Inf (too big)
    fff00000 -Inf (too big and negative)
    fff80000 NaN  (imaginary)
    ffefdcf1 -1.790000e+308 (normal)
    

    Just keep in mind that this code (but not the method) depends a great deal on the sizes of your longs which is not overly portable. But, if you have to bit-fiddle to get the information, you've already entered that territory :-)

    As an aside, I've always found Harald Schmidt's IEEE754 converter very useful for floating point analysis.

    0 讨论(0)
  • 2020-11-29 06:16

    isnan is part of C++11 now, included in GCC++ I believe, and Apple LLVM.

    Now MSVC++ has an _isnan function in <float.h>.

    Appropriate #defines and #includes should make a suitable workaround.

    However, I recommend preventing nan from ever occurring, instead of nan detection.

    0 讨论(0)
  • 2020-11-29 06:20

    this works on osx

    #include <math.h>
    

    also this might be portable,

    int isinf( double x ) { return x == x - 1; }
    

    edit:

    as Chris pointed out the above may fail with large x

    int isinf( double x ) { return x == x * 2; }
    
    0 讨论(0)
  • 2020-11-29 06:22

    Just use that super simple IEEE 754-1985-compliant code:

    static inline bool  ISINFINITE( float a )           { return (((U32&) a) & 0x7FFFFFFFU) == 0x7F800000U; }
    static inline bool  ISINFINITEPOSITIVE( float a )   { return (((U32&) a) & 0xFFFFFFFFU) == 0x7F800000U; }
    static inline bool  ISINFINITENEGATIVE( float a )   { return (((U32&) a) & 0xFFFFFFFFU) == 0xFF800000U; }
    static inline bool  ISNAN( float a )                { return !ISINFINITE( a ) && (((U32&) a) & 0x7F800000U) == 0x7F800000U; }
    static inline bool  ISVALID( float a )              { return (((U32&) a) & 0x7F800000U) != 0x7F800000U; }
    
    0 讨论(0)
  • 2020-11-29 06:24

    I've not tried this, but I would think

    int isnan(double x) { return x != x; }
    int isinf(double x) { return !isnan(x) && isnan(x - x); }
    

    would work. It feels like there should be a better way for isinf, but that should work.

    0 讨论(0)
  • 2020-11-29 06:28

    According to this, infinity is easy to check:

    • sign = either 0 or 1 bit indicating positive/negative infinity.
    • exponent = all 1 bits.
    • mantissa = all 0 bits.

    NaN is a bit more complicated because it doesn't have a unique representation:

    • sign = either 0 or 1.
    • exponent = all 1 bits.
    • mantissa = anything except all 0 bits (since all 0 bits represents infinity).

    Below is the code for double-precision floating-point case. Single-precision can be similarly written (recall that the exponent is 11-bits for doubles and 8-bits for singles):

    int isinf(double x)
    {
        union { uint64 u; double f; } ieee754;
        ieee754.f = x;
        return ( (unsigned)(ieee754.u >> 32) & 0x7fffffff ) == 0x7ff00000 &&
               ( (unsigned)ieee754.u == 0 );
    }
    
    int isnan(double x)
    {
        union { uint64 u; double f; } ieee754;
        ieee754.f = x;
        return ( (unsigned)(ieee754.u >> 32) & 0x7fffffff ) +
               ( (unsigned)ieee754.u != 0 ) > 0x7ff00000;
    }
    

    The implementation is pretty straightforward (I took those from the OpenCV header files). It uses a union over an equal-sized unsigned 64-bit integer which you might need to correctly declare:

    #if defined _MSC_VER
      typedef unsigned __int64 uint64;
    #else
      typedef uint64_t uint64;
    #endif
    
    0 讨论(0)
提交回复
热议问题