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

后端 未结 9 546
粉色の甜心
粉色の甜心 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:40

    If your basic need is to have a floating point value which doesn't represent any number which could possibly have been received from the device, and if the device guarantees it will never return NaN, then it seems reasonable to me.

    Just remember that depending on your environment, you probably need a special way of detecting NaNs (don't just use if (x == float.NaN) or whatever your equivalent is.)

    0 讨论(0)
  • 2020-12-18 22:42

    I have used NaNs in similar situations just because of that: the usual default initialization value 0 is also a valid value. NaNs work fine so far.

    It's a good question, by the way, why the default initialization value is usually (for instance, in Java primitive types) 0 and not NaN. Couldn't it as well be 42 or whatever? I wonder what's the rationale of zeros.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-18 22:49

    Using NaN as a default value is reasonable.

    Note that some expressions, such as (0.0 / 0.0), return NaN.

    0 讨论(0)
  • 2020-12-18 22:54

    Just noticed this question.

    This is one of the uses of NaNs that the IEEE 754 committee has in mind (I was a committee member). The propagation rules for NaNs in arithmetic make this very attractive, because if you have a result from a long sequence of calculations that involve some initialized data, you will not mistake the result for a valid result. It can also make tracing back through your calculations to find where you are using the initialized data much more straightforward.

    That said, there are a few pitfalls that are outside of the 754 committee's control: as others have noted, not all hardware supports NaN values at speed, which can result in performance hazards. Fortunately, one does not often do a lot of operations on initialized data in a performance-critical setting.

    0 讨论(0)
  • 2020-12-18 22:54

    This sounds like a good use for nans to me. Wish I had thought of it...

    Sure, they are supposed to propagate like a virus, that is the point.

    I think I would use nan instead of one of the infinities. It might be nice to use a signaling nan and have it cause an event on the first use, but by then its too late it should go quiet on the first use.

    0 讨论(0)
提交回复
热议问题