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.

Compiler/TMS320F28335: How should I check NaN with C2000 compiler v5.2.6

Part Number: TMS320F28335


Tool/software: TI C/C++ Compiler

Dear an expert engineer,

I would like to know how to check NaN on F28335.

nan and isnan() are not provide by math.h

Could you please let me know how to do that ?

-------- I tried to check like below

FLOAT32* test_NaN_pf;
UINT32 test_long;

test_long = 0xFFFFFFFF;
test_NaN_pf = (FLOAT32*)&test_long;

if( FLT_MIN > *test_NaN_pf) {

  judge_1 = 1;
}
if( FLT_MAX < *test_NaN_pf) {
  judge_2 = 1;
}

-> result

  judge_1 = 1, judge_2=0

--------

Best regards,

Hidehiko

  • Hi,

    NaN is when both are true: exponent = 0xFF and mantissa != 0.
    Infinity when exponent = 0xFF and mantissa = 0.
    So something like this:

    #define IsNan(x) (( *(UINT32*)&(x) & ~0x80000000) > 0x7F800000)

    drop sign bit, you get 0..0x7FFFFFFF. Then compare against positive infinity.


    C2000 compiler has predefined intrinsic to treat float as UINT32, __f32_bits_as_u32(x), so

    #define IsNan(x) (( __f32_bits_as_u32(x) & ~0x80000000) > 0x7F800000)



    Regards,
    Edward
  • Dear Edward,

    Thank you for your reply.

    I understand what you're meaning.

    Best regards,

    Hidehiko