x


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

more ▼

asked Jul 09 '10 at 07:12 PM

jjobby gravatar image

jjobby
417 27 34 46

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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...

more ▼

answered Jul 09 '10 at 07:42 PM

Cyclops gravatar image

Cyclops
7.1k 33 63 115

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.

Jul 09 '10 at 09:02 PM qJake
(comments are locked)
10|3000 characters needed characters left

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)
more ▼

answered Jul 09 '10 at 07:17 PM

qJake gravatar image

qJake
11.6k 43 78 161

Disregard this, it's incorrect. See Cyclops' answer for the correct way.

Jul 09 '10 at 08:55 PM qJake
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jul 09 '10 at 08:13 PM

Mike 3 gravatar image

Mike 3
30.7k 10 67 255

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x249
x71

asked: Jul 09 '10 at 07:12 PM

Seen: 1813 times

Last Updated: Jul 09 '10 at 07:12 PM