Search Unity

Getting NaN when math.normalize(zeroFloat3) or math.cross(zeroFloat3,zeroFloat3)

Discussion in 'Entity Component System' started by isbdnt, May 16, 2018.

  1. isbdnt

    isbdnt

    Joined:
    May 16, 2017
    Posts:
    35
    this happens even if the value of float3 is just close to zero. Meanwhile, Vector3 handles zero vectors well.
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    This seems like the correct behaviour you'd expect from a maths library for normalize.

    Though I'm not sure about cross product. I thought (0,0,0)x(0,0,0) was actually 0. Been a while though.

    -edit- yeah wiki seems to say that as well

    If the cross product of two vectors is the zero vector (i.e. a × b = 0), then either one or both of the inputs is the zero vector

    Normalizing zero vector is undefined though and returning NaN seems to be the correct behaviour. Whether it is more useful to return 0 is another matter.
     
    Last edited: May 16, 2018
  3. isbdnt

    isbdnt

    Joined:
    May 16, 2017
    Posts:
    35
    Indeed, math.cross of two vectors is zero vector. sorry for my mistake.
     
  4. mike_acton

    mike_acton

    Unity Technologies

    Joined:
    Nov 21, 2017
    Posts:
    110
    There exists math.safeNormalize.
     
    Sab_Rango likes this.
  5. isbdnt

    isbdnt

    Joined:
    May 16, 2017
    Posts:
    35
    It's neither in the package source code nor on github. is it in the next release?
     
  6. Prastiwar

    Prastiwar

    Joined:
    Jul 29, 2017
    Posts:
    125
  7. MintTree117

    MintTree117

    Joined:
    Dec 2, 2018
    Posts:
    340
    I know this is 2 years late, but I spent 2 weeks looking for an answer to this lol. Maybe add this to the documentation?
     
    PrimalCoder and R0man like this.
  8. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    The math library is pretty well documented. Here's the snippet.

    Code (CSharp):
    1. /// <summary>Returns a normalized version of the float2 vector x by scaling it by 1 / length(x).</summary>
    2. public static float2 normalize(float2 x) { return rsqrt(dot(x, x)) * x; }
    3.  
    4. /// <summary>Returns a normalized version of the double2 vector x by scaling it by 1 / length(x).</summary>
    5. public static double2 normalize(double2 x) { return rsqrt(dot(x, x)) * x; }
    6.  
    7. /// <summary>
    8. /// Returns a safe normalized version of the float2 vector x by scaling it by 1 / length(x).
    9. /// Returns the given default value when 1 / length(x) does not produce a finite number.
    10. /// </summary>
    11. static public float2 normalizesafe(float2 x, float2 defaultvalue = new float2())
    12. {
    13.     float len = math.dot(x, x);
    14.     return math.select(defaultvalue, x * math.rsqrt(len), len > FLT_MIN_NORMAL);
    15. }
     
  9. R0man

    R0man

    Joined:
    Jul 10, 2011
    Posts:
    90
    I can't for the life of me find that.
     
    PrimalCoder likes this.
  10. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    I hit F12 (Go to Declaration or Usages) in rider to open the source code, and it's all there

    upload_2020-7-29_18-2-35.png
     
  11. SenseEater

    SenseEater

    Joined:
    Nov 28, 2014
    Posts:
    84
  12. R0man

    R0man

    Joined:
    Jul 10, 2011
    Posts:
    90
    Damn, now I have to get Rider, just for this.

    I'm not sure why you linked to a page that doesn't have any documentation about safeNormalize. I think that was the whole point of contention of martygrof3708.

    Strange that Visual Studio doesn't link to source, but a codegen meta version of it. Very strange.
     
  13. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
  14. Ozego

    Ozego

    Joined:
    Apr 26, 2015
    Posts:
    10
    If you get math through the package manager it is precompiled as a dll in Library/ScriptAssemblies and you don't have access to the source. You have to get it from the git in order to read the source code.
    https://github.com/Unity-Technologies/Unity.Mathematics

    I hope they give the option of downloading the source through the package manager in future when it is available, the convenience of having access to it far outweighs the added build time.

    F12 works in Visual Studio Code as well. Visual Studio and MonoDevelop should have similar functions. It may be called definition instead of declaration.
     
    Last edited: Sep 1, 2020
  15. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,761
    Source is all there mate
    Library\PackageCache\com.unity.mathematics@1.2.1\
     
  16. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,271
    To further elaborate, Library/ScriptAssemblies is just the compiled assemblies in the editor. Unity does actually compile package assemblies from source on users' machines.
     
  17. R0man

    R0man

    Joined:
    Jul 10, 2011
    Posts:
    90
    VS Code doesn't link to those files. What a shame.
     
  18. Ozego

    Ozego

    Joined:
    Apr 26, 2015
    Posts:
    10
    It is! I wonder if there is a way to get visual studio code to refer to it.
     
  19. Lieene-Guo

    Lieene-Guo

    Joined:
    Aug 20, 2013
    Posts:
    547
    VS Code Do link to math.
    1. Add "Visual Studio Code Editor" package to you project
    2. In Preferences=>External Tools. Select Visual Studio Code as External Script Editor.
    3. Click the "Regenerate project files" button
    4. change this line in your ".vscode/settings.json" => "[Ll]library/":true, to "[Ll]ibrary/[^P]*":true,

    You Should be able to see package source in vscode and F12 jump into it.