• 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
1
Question by Fattie · Aug 17, 2012 at 03:13 PM · javascriptinfinity

Is IsNan not IsInfinity ??

Say you have some code like this:

 {
 ....
 vx = ( blah ) / ( blah );
 }

obviously, in real production code you have to check somehow that you';re not creating an infinity. I have used the following code to check for such a problem:

 {
 ....
 vx = ( blah ) / ( blah );
 if ( float.IsInfinity(vx) || float.IsNegativeInfinity(vx) )
   {
   vx = 12345.6;
   Debug.Log("Saved an infinity situation!  Whoo!! We rock!");
   }
 }

However! I have recently seen cases where that still produces a NaN error, at least in the editor. So today I do this

 {
 ....
 vx = ( blah ) / ( blah );
 if ( float.IsInfinity(vx)
      || float.IsNegativeInfinity(vx)
      || float.IsNan(vx) )
   {
   vx = 12345.6;
   Debug.Log("New technology saved an infinity!  Whoox2!");
   }
 }

So, does anyone decisively understand, exactly, what the difference is between IsNan, IsInfinity, and so on? What's the best idiom to use in this situation?

If so, thank you!

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 Steazy · Jul 28, 2015 at 08:33 PM 0
Share

https://en.wikipedia.org/wiki/NaN

4 Replies

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

Answer by Bunny83 · Aug 17, 2012 at 03:33 PM

Exactly, NaN(Not a Number) is not the same as infinity. floats have actually a lot different "error types", but they all result in NaN. Infinity is an actual number, at least the representation. NaN actually says something is wrong because the result is not a number. The squareroot of a negative value will produce NaN since the result it's not defined within the real numbers.

NaN also "infects" calculations and always produce NaN as result.

 // C#
 float A = 2.0f;
 float B = 7.0f;
 float C = float.NaN;
 
 float result = A * B + C;

This will result in NaN because C is NaN. When ever you use a NaN value in a calculation the calculation will also result in NaN.

Furthermore NaN can't be compared to any value, even to itself.

 bool its_A_Number = (C == C);

This will result in "false" because you can't compare NaN to anything.

So if your operands don't contain a NaN value, something went wrong in the calculation.

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 Bunny83 · Aug 17, 2012 at 03:37 PM 1
Share

Some more concrete examples:

  1.0 /  0.0  --> +infinity
 -1.0 /  0.0  --> -infinity
  0.0 /  1.0  --> 0.0f
  0.0 /  0.0  --> NaN
 +inf * +inf  --> +infinity
 -inf * +inf  --> -infinity
 -inf * -inf  --> +infinity
 +inf + +inf  --> +infinity
 -inf + +inf  --> NaN
 +inf +  5.0  --> +infinity
 ATan(+inf)   --> PI/2 aka 90°
 sqrt( -1 )   --> NaN
 log( 0 )     --> -infinity
 log( -1 )    --> NaN
avatar image Bunny83 · Aug 17, 2012 at 04:02 PM 0
Share

:D Well infinity is a special value, not just the highest / lowest possible value. They are treated mathematically correctly, so when you divide a number by infinity it becomes 0.0f and so on. NaN is actually the result of an error. It depends on your formula for which of those values you want to check and what you do in those cases.

avatar image
0

Answer by IndieScapeGames · Aug 17, 2012 at 03:18 PM

NAN as far as I know is 'Not A Number' and I suppose if the number got too large, or too small, it could result in an NAN exception.

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 Fattie · Aug 17, 2012 at 03:21 PM 0
Share

chad, I totally hear you, but what we need here is EXTRE$$anonymous$$ELY DECISIVE INFOR$$anonymous$$ATION

I found it hard to find any exact, compelling doco on this online....

avatar image IndieScapeGames · Aug 17, 2012 at 03:38 PM 0
Share

Use IsNaN to deter$$anonymous$$e whether a value is not a number. It is not possible to deter$$anonymous$$e whether a value is not a number by comparing it to another value equal to NaN.

avatar image
0

Answer by Weitzel · Aug 17, 2012 at 03:36 PM

When compiled, the code will be attempted to execute a potential divide by either zero, or a very near zero number, which will result in vx being set to NAN. Typically if you end up doing something like this, it is unintentional and therefore does not default to setting it to infinity.

You can always check the precondition and postcondition of your variables and deliberately set to infinity if you'd like/need, which can be represended by the mathematical representation of infinity, and can be helpful. I believe the isInfinity or isNegativeInfinity checks deal check for and compare against these.

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 Fattie · Aug 17, 2012 at 03:46 PM 0
Share

$$anonymous$$att - in computing, be careful about comparing your divisor to zero !!!

the problem is not zero, but any relatively very small number. Checking divisor == 0.0 never works. You should edit your answer in case someone gets the wrong info in the future !

avatar image Weitzel · Aug 17, 2012 at 03:55 PM 0
Share

fixed, cheers

avatar image
0

Answer by Steazy · Jul 28, 2015 at 09:52 PM

[Edited: Sarcasm and jackassery removed per Bunny83's comment - Also my answer got approved so I was 100% wrong in my assertion. Sorry!]

Useful links w/out any sarcasm:

IEEE 754-1985 IEEE 754 NaN

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 Bunny83 · Jul 28, 2015 at 10:48 PM 0
Share

@S$$anonymous$$zy:
Well, most people don't even know what the standard that defines the floating point format we're all using is actually called and i can't remember the IEEE number as well. Once you know that it has been specified by the IEEE it's quite easy to find more information about that.

The dedicated NaN wiki page was created 2012 so it's relatively new if you actually look at the questions date.

And yes, we don't need sarcastic posts that revives a 3 years old question. However adding those links is always appreciated. I usually add a lot links to my answer if i have them at hand. However i often write an answer on my tablet and it's a pain to search and add links there. At the moment on my PC i have about 30 UA tabs, 10 wiki pages, 10 google tabs, and about 15 other pages open.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

How to make a List Constructor? 1 Answer

Can I convert unityscript to boo? 0 Answers

Dynamically load UnityScript file 1 Answer

If you destroy a List of Class objects... 1 Answer

Monodevelop autocomplete on private variables 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