C 中模数(数学函数)的等价物?

发布于 2024-12-28 07:55:29 字数 542 浏览 2 评论 0原文

我有一段代码,其中显示一条警告:

我正在对有符号数字和无符号数字进行比较。
像 int <= CONSTANT/sizeof(expression)
之类的东西

纠正这个问题的最佳方法是什么?我相信取有符号数的模然后进行比较,对吧?我的意思是我在表达式上除以 sizeof 运算符后得到无符号数。所以另一种方法可能是让这个 rhs 签名

如果是的话,c 中有一个函数可以让我这样做吗?我进行了快速搜索,他们说 % 表示模数,这显然不是我要找的。

这是实际的警告

警告:有符号和无符号整数表达式之间的比较

,这是实际的代码行

函数A(......, int num, ....) {

assert( num <= MAX_SIZE/sizeof(int));//其中 MAX_SIZE 为 #define MAX_SIZE 1000

}

I have a piece of code where I see a warning saying

I am doing a comparison between signed and unsigned number .
Something like int <= CONSTANT/sizeof(expression)

What is the best way to correct this? I believe to take the modulus of signed number and then do the comparison, right? I mean I get the unsigned number after division by sizeof operator on an expression. So the other way could be to make this rhs signed

If so is there a function in c that would let me do this? I did a quick search and they say % for modulo which obviously is not what I am looking for.

This is the actual warning

warning: comparison between signed and unsigned integer expressions

and this is the actual line of code

functionA( ......, int num, .....) {

assert( num <= MAX_SIZE/sizeof(int));//where MAX_SIZE is #define
MAX_SIZE 1000

}

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

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

发布评论

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

评论(3

私野 2025-01-04 07:55:29

如果您知道正确的操作数是 <= INT_MAX,则可以将其强制转换为 int

int bla;

...

if (bla < (int) sizeof expr) {
    ...
}

但是,如果您可以将对象 bla 的类型更改为 size_t (这是 sizeof 生成的值的类型),那就更好了比铸造。

If you know the right operand is <= INT_MAX, you can cast it to int.

int bla;

...

if (bla < (int) sizeof expr) {
    ...
}

But if you can change the type of object bla to size_t (which is the type of the value that sizeof yields) it would be even better than to cast.

红颜悴 2025-01-04 07:55:29

只需将一侧投射到另一侧即可。如果将其转换为无符号数,则必须确保有符号数不是负数 - 否则 -1 -1 -1 -1 -1 -1 100 将不会得到所需的结果,因为 (unsigned)(-1) == UINT_MAX -,或者如果将其转换为有符号,则无符号数不会溢出。在这些情况下,添加额外的条件来治疗它们。

对于上述特殊情况,我将使用

assert(num <= (int)(MAX_SIZE/sizeof(int)));
// num <= MAX_SIZE/(int)sizeof(int) if I'm in a mean mood

if num 可能为负数并且

assert((unsigned)num <= MAX_SIZE/sizeof(int));

if num 保证为非负数。

Just cast one side to the other signedness. You have to make sure that the signed number is not negative if you cast that to unsigned - otherwise a comparison of -1 < 100 will not have the desired outcome, since (unsigned)(-1) == UINT_MAX -, or that the unsigned number doesn't overflow if you cast that to signed. In those cases, add an additional condition to treat them.

For the above particular case, I would use

assert(num <= (int)(MAX_SIZE/sizeof(int)));
// num <= MAX_SIZE/(int)sizeof(int) if I'm in a mean mood

if num might be negative and

assert((unsigned)num <= MAX_SIZE/sizeof(int));

if num is guaranteed to be nonnegative.

久随 2025-01-04 07:55:29

正如我所看到的,您的 int num 允许采用任何负值和正值,最大为 MAX_SIZE/sizeof(int) 。否则,您肯定会将 num 声明为无符号整数...

通过此附加条件扩展您的断言语句对您的情况有帮助吗?

assert( num < 0 || /* make the signed check */
  (unsigned int)num <= MAX_SIZE/sizeof(int) /* the unsigned remainder */
  );

As I see this, your int num is allowed to take any negative value and positive values up to MAX_SIZE/sizeof(int). Otherwise you would have declared num as an unsigned integer for sure...

Would extending your assertion statement by this additional condition help in your case?

assert( num < 0 || /* make the signed check */
  (unsigned int)num <= MAX_SIZE/sizeof(int) /* the unsigned remainder */
  );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文