Numerical Recipes Forum  

Go Back   Numerical Recipes Forum > Obsolete Editions Forum > General Hints, Tips, and Tricks for Using NR

Reply
 
Thread Tools Display Modes
  #1  
Old 10-07-2002, 02:52 PM
junderwood junderwood is offline
Member
 
Join Date: Jun 2002
Posts: 5
nrutil.h

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.
Reply With Quote
  #2  
Old 10-09-2002, 11:36 PM
Bill Press Bill Press is offline
Numerical Recipes Author
 
Join Date: Dec 2001
Posts: 225
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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:52 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.