DEFINE() 宏在表达式中的用法
所以我定义了..
#define RADIAN(x) x*3.14159265f/180.0f
然后像这样使用它:
RADIAN(theta-90)
我的程序不断地给我错误的结果,我花了几个小时才意识到上面的语句和下面的语句之间存在巨大的差异。
RADIAN((theta-90))
现在我的程序运行得很好。为什么第一个说法不正确?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
#define
仅进行文本替换,因此RADIAN(theta-90)
实际上是theta-90*3.14159265f/180.0f
,显然不是不是你的意思。尝试一下。
#define
makes just text substitution, soRADIAN(theta-90)
was reallytheta-90*3.14159265f/180.0f
, what obviously wasn't what you meant. Tryinstead.
宏的 largley 进行基于文本的替换,因此
扩展为:
由于运算符优先级,其计算结果为:
Macro's largley do text based replacement so
expands to:
which because of operator precedence, evaluates as:
上面的答案都是正确的。然而,还有一点尚未明确……
这是 C++,不是 C。停止使用预处理器宏。
The answers above are all correct. However, one point has not yet been made...
This is C++, not C. Stop using preprocessor macros.
这是因为,在第一种情况下 X 将被替换为 (theta-90),因此您的函数将计算为:
This is because, in the first case X will be replaced by (theta-90) so your function would evaluate to: