Is it a good idea to use IEEE754 floating point NaN for values which are not set?

后端 未结 9 559
粉色の甜心
粉色の甜心 2020-12-18 22:17

Is it a good idea to use IEEE754 floating point NaN (not-a-number) for values which are undefined for non-mathematical reasons?

In our case they are not yet set beca

9条回答
  •  一向
    一向 (楼主)
    2020-12-18 22:42

    I think it is a bad idea in general. One thing to keep in mind is that most CPU treat Nan much slower then "usual" float. And it is hard to guarantee you will never have Nan in usual settings. My experience in numerical computing is that it often brings more trouble than it worths.

    The right solution is to avoid encoding "absence of value" in the float, but to signal it in another way. That's not always practical, though, depending on your codebase.

提交回复
热议问题