tags:

views:

81

answers:

2

Hi,

I'm reading in a csv file of time-series data into a C++ program. My data however contains some NaN's. For example:

1-Jul-2010,   1.0 
2-Jul-2010,   2.0
3-Jul-2010,   NaN
4-Jul-2010,   3.0

To deal with this I wrote a short script in Matlab which replaces all the NaN's with 0.0 - I then read in the new file without the NaN's. Is there an easy way or avoiding this pre-processing?

Thanks!

A: 

Do you want the result to be a float (or double) NaN - then use the stdlib strtod() function.

If you want to deal with it in your own way (set it to 0 etc) then read each line into a string first, check for NaN with strcmp() then parse the string depending on what you find - easier if NaN can only appear in one column.

Martin Beckett
Ideally I'd like the result to be a double NaN. I'll have a look at the strtod() function now.
Wawel100
+4  A: 

As David Given already mentioned, you do not have to pre-process the file at all. strtof() and strtod() are both able to convert the NaN string to the NaN float/double value.

If you want to replace the values with 0.0 in your dataset, you can do so using the isnan() function.

if (isnan(val))
{
    val = 0.0;
}
On my machine (VC++) the function call: strtod("NaN",NULL) returns 0.0 (which I take to mean no valid conversion could be performed) rather then a double NaN. Am I using it correctly?
Wawel100
You're using it correctly and the IBM compiler returns NaN. I just tried with the MS VC++ compiler and indeed, it does return 0.0. So probably you should test yourself using stricmp() to be sure. A definition for NaN is in std::numeric_limits<double>::quiet_NaN();
Thanks! I'm now using strcmp() to test for the "NaN" strings and if so then call std::numeric_limits<double>::quiet_NaN(). But is there a function similar to strtod that will work (correctly) in MS VC++?
Wawel100
I don't think so. Every implementation I've seen so far uses some kind of workaround.