Jump to content

Checking for NaN's in script


photo

Recommended Posts

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)

Link to comment

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.

Link to comment

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 :(

Link to comment

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

Link to comment
×
×
  • Create New...