Reputation: 65
So I defined..
#define RADIAN(x) x*3.14159265f/180.0f
and then used it like this:
RADIAN(theta-90)
My program constantly gave me incorrect results, it took me a couple hours to realize that there was a huge difference between the above statement and the statement below.
RADIAN((theta-90))
Now my program is running perfectly fine. Why is the first statement incorrect?
Upvotes: 2
Views: 955
Reputation: 3338
The answers above are all correct. However, one point has not yet been made...
This is C++, not C. Stop using preprocessor macros.
Upvotes: 2
Reputation: 22104
This is because, in the first case X will be replaced by (theta-90) so your function would evaluate to:
theta - 90* 3.14159265f/180.0f
Upvotes: 0
Reputation: 4153
#define
makes just text substitution, so RADIAN(theta-90)
was really theta-90*3.14159265f/180.0f
, what obviously wasn't what you meant. Try
#define RADIAN(x) ((x)*3.14159265f/180.0f)
instead.
Upvotes: 7
Reputation: 10839
Macro's largley do text based replacement so
RADIAN(theta-90)
expands to:
theta - 90* 3.14159265f/180.0f
which because of operator precedence, evaluates as:
theta - (90* 3.14159265f/180.0f)
Upvotes: 4