This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

float NaN comparison in C-code

Genius 4170 points
Other Parts Discussed in Thread: MSP430F5529

Hello,


I have a quick question: How do I get an easy solution for finding out wheather a float number is NaN?

I am using the MSP430F5529 and I do store some values in the internal InfoFlash segments. Now when erased or when writing to a completly new one the Flash segments are always 0XFFFF so 1s everywhere, this translates into NaN when I read out the flash and put it into my float variable.

    f32_x = 0.0;
    f32_x /= f32_x;

    if (f32_x != f32_x ){
        f32_x = 1;
    }

This is my minimal code as I found out in the world wide web, this should result in NaN when compared to itself, but it does not work for me.

Anyone got ideas why this doesnt work or how it works properly.

If noone helps I think I have to do the workaround of  not filling a float but rather fill a long variable and compare with a hex number, this will work, but I dont wanna do it like that since it is even more complicated for me I want to do it the nice elegant way.

Best wishes,

Seb

  • Use the macro isnan from math.h.

    Thanks and regards,

    -George

  • What version of the MSP compiler are you using?  It is different than the CCS version.  What command-line options are you using?

  • Try using <math.h>'s isnan.  e.g.:

    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
        float f = 0;
        f /= f;
        
        if(f != f)
        {
            printf("Can use self-comparison for NaN detection\n");
        }
        
        if(isnan(f) != 0)
        {
            printf("Can us isnan for NaN detection\n");
        }
        
        return 0;
    }
    

  • isnan is a C99 macro and is not provided in older MSP compiler versions.  Even where it is available, older versions of the TI MSP compiler would perform incorrect optimizations on floating point expressions such as x/x; this expresison appears to be 1, but could be NaN.  I need to know the version of the compiler in use.

  • Hi and thanks for the replies,

    I am working with CCS 5.5.0 right now compiler is 4.2.1

    So any ideas now?

    In stackoverflow they suggested the x /=x version should be working in every C code, that is why I asked here why it is not working in proprietary Texas code :)

  • For MSP430 compiler version 4.2.1, if the optimizer is used, it correctly folds 0.0F/0.0F into NaN, but the comparison function fs_cmp does not consider the possibility of NaN and believes f==f.  If the optimizer is not used, fs_div calculates 0.0F/0.0F as NaN, but again fs_cmp doesn't get the comparison right.  When the isnan macro is used, it turns into a function call that picks apart the bits to look for NaN directly, so it doesn't go through fs_cmp.  The bottom line is that the compiler is working correctly in this case, but the RTS implementation falls short.

    Please see this general statement about the compiler's adherence to IEEE-754.

    I have submitted SDSCM00051198 to track the issue with fs_cmp.