How to transfer force NAN * zero gives zero in a specific expression without branching?

(I feel that this should be a duplicate question, but I could not find the correct search terms when searching.)

In general, (silent) NAN times zero should give NAN - and it does.

However, in one specific performance-critical part of my code, I want a null value to be null.

What is a quick way to do this in C ++?

+7
c ++ nan
source share
2 answers
double mymult(double a, double b){ double result[]={a*b,0.}; return result[(a==0.)|(b==0.)]; } 

branches should be avoided: double-check the generated assembly.

Not all bool calculations imply a branch.

+5
source share

You cannot do this with arithmetic. You must check for zero.

+2
source share

All Articles