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.
I am using an TMS320F2837xS cpu (C28x DSP core + FPU + CLA). I am trying to test floats for nan using isnan(). The macro implementation for this in math.h (compiler version 6.2.5 and 6.4.9) is defined as: isnan(x) ((x) != (x)). This is technically correct for nan's but it fails the test in the compiled code. I have looked at the assembly code that this produces and the code executes a CMPF32. In the instruction set manual (sprueo2b.pdf) the description for this instruction states that "NaN will be treated as infinity". If I change my code to test the float using isinf() then the test is true for a nan, but the definition for isinf() in math.h is comparing the float against +-INFINITY and INFINITY and NaN definitions differ in a binary sense. Is this the expected behavior? If this is the case why is the definition in math.h invalid?
So currently, the implementation of isinf() returns true for a NaN (because the FPU HW is treating NaN as +Inf in the CMPF32 instruction). Is it reasonable to assume this will remain true with the current HW and future compiler implementations? ie is a suitable workaround (albeit confusing)?
So a macro defined as:
#define ISNAN(x) (isinf(x) || isnan(x))
would return true for the state of the compiler/HW now and in the future where either the compiler or the HW change to properly handle NaN. This assumes that isinf() does not change independently of isnan().