开发者

What is causing this to behave strangely?

开发者 https://www.devze.com 2023-03-17 07:39 出处:网络
I\'m working in an environment with a mostly absent double/Math library (NETMF). I wrote this class to make things easier:

I'm working in an environment with a mostly absent double/Math library (NETMF). I wrote this class to make things easier:

public struct DoubleEx
{
    public const double NaN = 0.0d / 0.0d;
    public static bool IsNaN(double x)
    {
        return x != x;
    }
    ...
}

Seems like it should work, right?

Well, when I run this code:

Debug.Print("Method call: " + DoubleEx.IsNaN(DoubleEx.NaN));
Debug.Print("Method call: " + DoubleEx.NaN != DoubleEx.NaN);

I get this output:

False
True

Somehow, the act of putting it in a function breaks it! Is there some kind of optimization going on here? Or is the hardware misinterpreting the 开发者_高级运维instructions?


The following is based on the IEEE Standard 754:

// @struct IEEE_DOUBLEREP | allows bit access to 8 byte floats
//[StructLayout(LayoutKind.Sequential)]
//public struct ieee_doublerep
//{
//    ulong low_mantissa;       // @field low 16 bits of mantissa
//    ushort mid_mantissa;  // @field mid 16 bits of mantissa
//    uint high_mantissa:4;     // @field high 4 bits of mantissa
//    uint exponent:11;         // @field exponent of floating point number
//    uint sign:1;              // @field sign of floating point number
//};

public struct DoubleEx
{
    public const long NANMASK = 0x7FF0000000000000;
    public const long INFINITYMASK = 0x000FFFFFFFFFFFFF;

    public const double NaN = 0.0f / 0.0f;
    public const double NegativeInfinity = -1.0f / 0.0f;
    public const double PositiveInfinity = 1.0f / 0.0f;
    public static bool IsNaNBad(double x)
    {
        return x != x;
    }

    public unsafe static bool IsNaN(double value)        
    {
        long rep = *((long*)&value);
        return ((rep & NANMASK) == NANMASK &&
                ((rep & INFINITYMASK) != 0));
    }

    public unsafe static bool IsPositiveInfinity(double value)
    {
        double negInf = DoubleEx.PositiveInfinity;
        return *((long*)&value) == *((long*)&negInf);
    }

    public unsafe static bool IsNegativeInfinity(double value)
    {
        double posInf = DoubleEx.PositiveInfinity;
        return *((long*)&value) == *((long*)&posInf);
    }

    public unsafe static bool IsInfinite(double x)
    {
        long rep = *((long*)&x);
        return ((rep & NANMASK) == NANMASK &&
                ((rep & INFINITYMASK) == 0));
    }
}


You have an operator precedence problem, and putting it inside a function changes the expression.

Try:

Debug.Print("Method call: " + (DoubleEx.NaN != DoubleEx.NaN));

And what about:

static bool DoubleInequal(double a, double b) { return a != b; }
static bool IsNaN(double x) { return DoubleInequal(x, x + 0.0); }


Have you tried return x == NaN? It doesn't seem like good practice to me to assume x != x is synonymous with "IsNaN".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号