MACROS - 如何实现除法然后舍入

发布于 2024-10-10 03:00:11 字数 277 浏览 2 评论 0原文

对宏执行除法和舍入到较小数字的正确方法是什么?

我正在尝试这样做:

#define TOTAL_NUM_FFTS  (int) NO_SAMPLES / FFT_SIZE

但我收到了该宏的不兼容重新定义的警告,并且编译器将该行重述为: #define TOTAL_NUM_FFTS(int) NO_SAMPLES / FFT_SIZE TOTAL_NUM_FFTS 和 (int) 之间没有空格。

感谢您的帮助!

What is the proper way to perform a divide and round to lower number for a macro?

I am trying to do this:

#define TOTAL_NUM_FFTS  (int) NO_SAMPLES / FFT_SIZE

but I am getting a warning of incompaitible redefinition of that macro and the compiler restates the line as:
#define TOTAL_NUM_FFTS(int) NO_SAMPLES / FFT_SIZE without the space between TOTAL_NUM_FFTS and (int).

Thanks for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月静好 2024-10-17 03:00:11
#define TOTAL_NUM_FFTS  ((int) (NO_SAMPLES) / (FFT_SIZE))

预处理器认为 (int) 是宏的参数。

定义宏时,请尽可能多地使用括号。例如,想象一下如果有人将 FFT_SIZE 定义为 2+3 会发生什么。您可以先除以 2,然后再加 3,而不是除以 5。

#define TOTAL_NUM_FFTS  ((int) (NO_SAMPLES) / (FFT_SIZE))

The preprocessor thinks (int) is a parameter to the macro.

When defining macros, use as many parentheses as you can. For example, think what will happen if someone defines FFT_SIZE as 2+3. Instead of dividing by 5, you'd be dividing by 2 and then adding 3.

执着的年纪 2024-10-17 03:00:11

需要检查的几件事:

  • 始终正确地将宏(和宏参数)括起来,如 Ilya 提到
  • 确保其他地方没有重复(或接近重复)的宏定义。错误消息应该告诉您确切的位置,但如果没有,grep 或类似的命令会有所帮助(也许您的标头的旧版本隐藏在包含路径的其他目录中?)。
  • 确保您的头文件受到包含防护的多重包含保护。我不认为这是发生在你身上的事情,因为 C 或 C++ 编译器应该接受相同的宏重新定义,但你仍然应该确保你的标头有这个。

Several things to check:

  • always properly parenthesize your macros (and macro arguments), as Ilya mentioned
  • make sure there isn't a duplicate (or near duplicate) definition of the macro somewhere else. The error message should tell you exactly where, but if it doesn't, grep or similar will help (maybe there's an older version of your header hiding in some other directory f the include path?).
  • make sure your header file is protected against multiple inclusion with include guards. I don't think this is what's happening to you since since identical macro redefinition is supposed to be accepted by a C or C++ compiler, but you should still make sure your header has this.
芸娘子的小脾气 2024-10-17 03:00:11

对于除法,您可以使用 math.h 库中提供的 pow(base, exponent) 函数。
我想你必须实现类似的东西,

#define C A/B    //B!=0

然后使用以下代码代替它:

#define CA*pow(B,-1)

For dividing, you can use the pow(base, exponent) function available in math.h library.
I suppose you have to implement something like,

#define C A/B    //B!=0

then instead of it use the following:

#define C A*pow(B,-1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文