math.NaN() not taken into account

When one computes a Mean in a sample, 0.00 can mean either that there is no information, or that the measure is correctly 0.00 (like in temperature measurement for example). In math.stats package, presence of one math.NaN() data forbid to find a correct answer, (that would be excluding the data and compute mean on n-1 population). Same thing with package, and marshalling to json. How do you deal with the problem of “null”, or “not available” data ?

Your problem is not clearly stated as well as the result/response you expect.

Do you have a slice of floats with some NaN values in it ? Is it an array of values in JSON encoded data ?
Do you want to compute a mean value ?
If you want an answer, you will have to clarify your question.

Let us take the example of the speed of a fan, transmitted through a database. I am supposed to receive data each minute. The data is either correctly 0.0, the data can be missing, or, for example, 402.9 r/m.
If I build a []float64 with 2 missing data in it, the mean should compute the mean on a population (n-2). The use of math.Nan gives NaN as a result. I can obviously not use 0.0 when the data is missing. There is no “nil” value for float64. Not only the stats.Mean does not compute a mean with math.nan in the array, but json.Marshal will generate an Error with type math.Nan. It looks like being 2 separate problems, but it is really one : how can i keep the trace of such an infomation in Go ? It is a basic problem in IoT, but I could not find a way to deal with it (not could I find a clue on internet)

Thank you :blush:

You can use your own mean loop that skips NaNs, or you can avoid recording missing values in your slice?

Sorry. I still don’t understand your question.

If data is missing, what is the problem ? I have nothing to deal with. If the data is invalid (NaN), I can remove it from my data set or skip it when processing the data.

In Go there is no “null” value for a float, but you could replace it with a NaN which is a special value.

If you need to marshall to JSON a floating point value that can be a NaN, I would suggest to define your own type and associated marshalling and unmarshal functions that will convert NaN <-> null.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.