Jump to content

Checking for NaN's in script


photo

Recommended Posts

Posted

How do we check if a float is a NaN, denormal etc. in UnigineScript?

A quick search of the docs didn't throw up anything...

 

-a.

Posted

Don't think there are any functions for checking invalid float/double values. Use proper pre-condition checks to avoid such conditions (not sure if Unigine would throw runtime exceptions in case of invalid results)

Posted

IMO any floating point library should have these, it's much easier to put an assert( !isnan( f ) ) than sift through a changing codebase and make sure all cases are handled well.

Posted

I would always prefer safe coding techniques because asserts on NaN / denormal are just indications of a deeper algorithmic problem. But of course this is purely a matter of taste

Posted

No, I completely agree, the point of the assert is that we *realize* that there's a deeper algorithmic problem and fix it. The assert is of course, never the solution :(

Posted

Does UnigineScript set any signaling NaN's or are they all quiet?

Posted

You can use the following function to detect "wrong" fpu numbers:

int isnan(int v) {
 return (v != v);
}

 

This is an example:

void test(float f) {
 printf("%s: %d\n",typeinfo(f),isnan(f));
}

test(1.0f);
test(1.0);
test(vec3_one);
test(dvec3_one);

test(1.0f / 0.0f);
test(0.0f / 0.0f);
test(1.0 / 0.0);
test(0.0 / 0.0);
test(vec3_one / 0.0f);
test(vec3_zero / 0.0f);
test(dvec3_one / 0.0);
test(dvec3_zero / 0.0);

The result under win32 is:

float: 1: 0
double: 1: 0
vec3: 1 1 1: 0
dvec3: 1 1 1: 0
float: 1.#INF: 1
float: -1.#IND: 1
double: 1.#INF: 1
double: -1.#IND: 1
vec3: 1.#INF 1.#INF 1.#INF: 1
vec3: -1.#IND -1.#IND -1.#IND: 1
dvec3: 1.#INF 1.#INF 1.#INF: 1
dvec3: -1.#IND -1.#IND -1.#IND: 1

×
×
  • Create New...