Numerical data in Atoti Server
NaN (Not a Number)
The danger of NaN
In Java, NaN
has the property that NaN != NaN
.
This is a problem when storing NaN
in a numeric dictionary, as the dictionary uses ==
to compare values.
This means that the value will never be found and each instance of NaN
will be mapped to a new value in the dictionary.
It leads to an ever-growing dictionary, wasting memory, and potential bad result in queries.
How to handle NaN:
Do not dictionarize a field containing NaN
It is ok to have some NaN
values in the datastore as long as the field storing the value is not dictionarized.
For instance, such field can be used as a measure.
Try to avoid using NaN
If you need to dictionarize the field to use it as a hierarchy, then try to avoid using NaN
:
- Use another special value as the marker for “no value”, for instance
null
,Double.NEGATIVE_INFINITY
,Double.POSITIVE_INFINITY
,Double.MAX_VALUE
... - Use a String column with
N/A
and parse the value when you need it as double (parseN/A
the same way you would useNaN
) - Use 2 columns: one
String
and onedouble
(you can have one column calculated based on the other) and use theString
column as the hierarchy and the numeric column for measures.
Type the column as Object
You can also type the column as Object instead of Double, so it uses an object dictionary which compares values with Object.equals(...)
.
As NaN.equals(Nan)
is true
, the dictionary will be able to correctly match the NaN
.