Search Unity

Entity translation NaN bug.

Discussion in 'Entity Component System' started by MintTree117, Apr 3, 2020.

  1. MintTree117

    MintTree117

    Joined:
    Dec 2, 2018
    Posts:
    340
    I have had this bug for a while now. For some reason, occasionally one of my entities translation will become NaN. I have checks in my code to detect this and they aren't triggered. But when I try to access the translation, it is NaN. Any ideas?

    Unity 2019.3.2f1
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    math.normalize is often a good culprit
     
    MintTree117 likes this.
  3. RichardWepner

    RichardWepner

    Joined:
    May 29, 2013
    Posts:
    33
    How do you check this? Keep in mind that, by definition, any comparison of
    NaN
    with any other value (or
    NaN
    ) will result in
    false
    . If you want to check if a value is
    NaN
    , you have to use either
    math.isnan
    or
    float.IsNaN
    .
     
  4. MintTree117

    MintTree117

    Joined:
    Dec 2, 2018
    Posts:
    340
    You are right. I wonder why they have normalize and normalize safe? Why would someone want to get NaN?
     
  5. MintTree117

    MintTree117

    Joined:
    Dec 2, 2018
    Posts:
    340

    I was using math.isnan but I did not even think to check math.normalize. I did not know it could return NaN. Issue solved!
     
  6. thebanjomatic

    thebanjomatic

    Joined:
    Nov 13, 2016
    Posts:
    36
    If you know that you don't have a zero-sized vector already (because you checked elsewhere or its impossible to get into that situation in your code) then you can avoid the extra operations required to handle that case. There is a good argument for having both options, but what the default should be is debatable.

    Right now we have normalize() and normalizesafe(), people will likely gravitate toward using normalize and not the safe version (even though both should show up in the type-ahead/intelliesense.

    If instead we had normalize() and normalizeunsafe() then people would get the safer alternative by default, but possibly at the expense of wasted performance.
     
  7. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    i suspect it's done how it is because it's a mathematics library and normalize is the mathematically correct version

    dividing by 0 is NaN