cuda - division by 0 or multiplication by Nan must give zero

I basically want to some how enable a flag (if there is in Cuda) such that dividing a float value by zero or multiplying a float value by Nan gives result zero.
This will save me a lot of if(a != 0) statements and my program will run much faster!!

Any one please reply if you know how to enable it (if any)


There is no such compiler flag. The general idea for floating-point computations in CUDA is that all basic math operations deliver special case results as specified in IEEE-754 for the case that exceptions are masked. So division by zero delivers either infinity or NaN depending on what the dividend is, and multiplying by a NaN delivers a NaN.

Instead of doing individual checks on each floating-point operation, could you just screen out infinities and NaNs at the end of your computation and treat them as zero there? Or screen your inputs for NaNs, and convert zero inputs to a very small non-zero number.

No i cant screen them out. What I am doing is something like:

evaluate ‘a’


sum += 1/a;


if i remove the condition check, Nan will be added to sum ( i wanted it to add zero) when a=0

Do you have an alternative solution. May be I did not understand what you meant.



If x == 0.0, then 1.0 / x should return infinity, not NaN. I am not sure what the goal of treating 1.0 / 0.0 as 0.0 is, but if there is no way to avoid this, I would recommend to keep doing what you are doing.

Okay. :)
I will try to figure out another workaround in logic if possible. Thanks.

Also, you (probably) don’t want to directly check (a!=0) because in floating-point math, there are two representations of zero (positive and negative) and it’ll only match one of them. The standard practice is to take the absolute value of the number in question (e.g., “a”) then, test to see if it’s less than some allowable, fixed error amount; for example: if (abs(a) < 0.001) { /* do stuff */ }

good suggestion. but probably i can put that in my debug build. Thing is, I am running sort of n body evaluation with 10million bodies, so this code will be executed by 10^14 times, and using abs() will use an additional cycle and slow down my program. thats the reason why i wanted to get rid of if() statement which was slowing it down.