- Home /
Is there a way to set Unity to write message log when the calculation has Infinity or NaN?
I've written a simple soccer simulation. It works without any error log from Unity. But I saw that the AI didn't have good decision behavior. So, I decided to check some variables with Debug.Log I found that some of them are infinity or NaN due to variable error or math error in my math method. What surprised me was that the simulation was still working and Unity didn't show any error message at all. Even if those variables were often used (comparing or dividing). o_O
I've already fixed the problem I found. But I'm worry that there may be more math error like this in my code which I still can't find. Is there a way to tell Unity to notify the user when the calculation has infinity or NaN?
Thank You
Answer by Cyclops · Jul 09, 2010 at 07:42 PM
Actually, it isn't float.Infinity, it's Mathf.Infinity. And unfortunately, that only represents positive Infinite values, you would also need to compare to Mathf.NegativeInfinity.
A slightly better way would be:
if (float.IsInfinity(myFloat) || float.IsNaN(myFloat))
Debug.Log("wups");
float.IsInfinity() checks for both positive and negative values of Infinity. Negative Infinity - what a concept...
Oh, you're right... I didn't have access to a compiler when I wrote my answer, and I wasn't sure where NaN and Infinity were stored, but you're right.
Answer by qJake · Jul 09, 2010 at 07:17 PM
No, you can't just notify randomly when any variable is equal to something, but you can do this to check each individual float:
if(myFloat == float.Infinity)
or
if(myFloat == float.NaN)
Disregard this, it's incorrect. See Cyclops' answer for the correct way.
Answer by Mike 3 · Jul 09, 2010 at 08:13 PM
Most of the time you should be checking if you're dividing things by zero - that's when things really go wrong. It'll save you time later if you check if divisors are zero before using them, and throw errors or modify the values if they are
Your answer
Follow this Question
Related Questions
Have my debug log saved to a .log file in build. 2 Answers
Debug.Log Override? 3 Answers
Reading the profiler log 0 Answers
Debug.Log is causing an Assert 2 Answers