Usefulness of signaling NaN?

后端 未结 3 754
伪装坚强ぢ
伪装坚强ぢ 2020-12-04 21:09

I\'ve recently read up quite a bit on IEEE 754 and the x87 architecture. I was thinking of using NaN as a \"missing value\" in some numeric calculation code I\'m working on

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-04 22:04

    Using special values (even NULL) can make your data a lot muddier and your code a lot messier. It would be impossible to distinguish between a QNaN result and a QNaN "special" value.

    You might be better maintaining a parallel data structure to track validity, or perhaps having your FP data in a different (sparse) data structure to only keep valid data.

    This is fairly general advice; special values are very useful in certain cases (e.g. really tight memory or performance constraints), but as the context grows larger they can cause more difficulty than they're worth.

提交回复
热议问题