nrutil.h


junderwood
10-07-2002, 02:52 PM
In nrutil.h of the C version of NR there are a number of macro declarations of the type:

#define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)

which seem to declare unecessary variables - in this case sqrarg. I am wondering why this is so (I'm sure I'm missing something subtle).

jonathan.

Bill Press
10-09-2002, 11:36 PM
Mainly, it's because the argument "a" might be a complicated expression, with function calls, for example. Because these functions might have side effects, the compiler would be required to evaluate "a" twice when it computes a*a. The intermediate variable is to avoid this potential inefficiency.

Cheers,
Bill P.