Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by xandermacleod · May 24, 2013 at 06:31 PM · rotationquaternioneuleranglesnan

How to check for NaN

Hi there,

I'm using C# and am using some quaternions in one of my scripts. Every so often I receive a debug message saying that some of one of my objects rotation values are Nan, or 'Not a Number' (likely because under certain circumstances the square root of a negative number is being calculated / something is being divided by 0).

In any event, I want to perform a check to see if the value will be NaN before altering the object's rotation. Something like:

if(rotation == Nan)...

or

if(rotation == error)...

Is there such a way to do this? All I could find was this link, but I can't find a C# equivalent: http://answers.unity3d.com/questions/182209/Checking-for-quaternion-values-to-not-be-NaN.html

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DMGregory · May 09, 2014 at 05:48 PM 0
Share

There's an older Quaternion-specific question here. I just added an answer with an extension method so you don't have to check every component of the Quaternion explicitly every time.

4 Replies

· Add your reply
  • Sort: 
avatar image
24
Best Answer

Answer by Stormizin · May 24, 2013 at 06:44 PM

The .NET framework already provides simple ways to achieve this. You can use the constant float.NaN if you need to actually generate a NaN in code, and float.IsNaN(value) returns true if the value is a NaN.

There are some related things, such as constants and comparisons for infinity.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xandermacleod · May 24, 2013 at 06:48 PM 0
Share

perfect. thankyou

avatar image
8

Answer by Paulo33 · May 09, 2014 at 05:31 PM

Well, i know this is a old post but, i will help others with simple soluction for check if "float variable" is a NaN or not.

 if(float.IsNaN("YOURFLOATVARIABLE")){
       //Your condition...
 }
 else{
       //Your "else" condition
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KarlKarl2000 · Dec 29, 2019 at 11:20 PM 0
Share

This worked for me. Thank u!

avatar image
6

Answer by dentedpixel · Jun 02, 2013 at 08:42 PM

 if(!System.Single.IsNaN( someVariable ))
   // assign value
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Varaughe · Aug 18, 2014 at 03:16 PM

can someone explain me ,why (float.NaN == float.NaN) is false ???

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DMGregory · Aug 18, 2014 at 04:52 PM 2
Share
  1. Please use answers for answers, not new questions.

  2. To borrow from a StackExchange answer:

    There was no isnan( ) predicate at the time that NaN was formalized in the 8087 arithmetic; it was necessary to provide programmers with a convenient and efficient means of detecting NaN values that didn’t depend on program$$anonymous$$g languages providing something like isnan( ) which could take many years. I’ll quote $$anonymous$$ahan’s own writing on the subject:

    "Were there no way to get rid of NaNs, they would be as useless as Indefinites on CRAYs; as soon as one were encountered, computation would be best stopped rather than continued for an indefinite time to an Indefinite conclusion. That is why some operations upon NaNs must deliver non-NaN results. Which operations? … The exceptions are C predicates “ x == x ” and “ x != x ”, which are respectively 1 and 0 for every infinite or finite number x but reverse if x is Not a Number ( NaN ); these provide the only simple unexceptional distinction between NaNs and numbers in languages that lack a word for NaN and a predicate IsNaN(x)."

So, short answer: it was important to be that way when the IEEE 754 spec was written, so we're still stuck with that convention today. Fortunately, we have IsNaN() now to check for this case explicitly.

avatar image Dino-Dini · Nov 12, 2019 at 04:24 PM 0
Share

$$anonymous$$y view is that equivalency is a mathematical concept that in this context only applies to numbers. If the thing is not a number then logically all numerical mathematical operations are not defined. This includes equal. Thus the answer to Nan == Nan should be undefined, but since the operation must return either true or false and cannot return "not a boolean", false has been chosen. Although either are incorrect, false is probably 'less wrong' overall.

For example, if two calculations are performed and the results would be very different, but due to some precision or other error both get turned into Nan, then suddenly they become equal. It is better to treat both as 'unknown' and if unknown, not treat them as unequal.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Clarifying the ambiguities of rotation in Unity 1 Answer

Rotating local transform not rotating fully if tranform.up is not exactly at (0,1,0) 1 Answer

How to handler rotation of a Gameobject while aligning it with normals 1 Answer

Object makes random jumps in rotation 1 Answer

Match Y axis rotation. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges