Walt Mankowski on 17 Aug 2004 20:55:04 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

[PLUG] How to specify NaN in C programs?


In a program I'm working on, I've got an array of floating point
numbers.  There will be situations where I want some of the elements
to just be placeholder, and when I go to print then out I want to
print "N/A" instead of an actual number.

Obviously there are any number of ways to implement this.  If I were
writing this in Perl, for instance, I could just set the invalid
elements to undef.  Since I'm writing this in C (well, really C++, but
in this case I don't think it makes any difference) I thought I'd use
NaN, which is a special value allowed by the IEEE floating point spec
to indicate not-a-number.

It's easy to test for NaN with isnan(3).  However, I can't find any
documentation on how to set a floating point variable to NaN.  "NAN"
works on linux and OSX, but it fails on cygwin.  I haven't tried it
yet on VC++.

Does anyone know if there's a standard way of indicating NaN?

Thanks.

Walt

Attachment: signature.asc
Description: Digital signature