“在更广泛的背景下降低精度”是什么意思?警告实际上意味着什么?

发布于 2024-09-11 19:19:47 字数 439 浏览 1 评论 0原文

对于嵌入式平台,我有以下代码,其中 int 为 16 位,long int 为 32 位:

#define MULTIPLIER 0x1000

static void my_function(uint16_t i, void *p)
{
    uint32_t start = MULTIPLIER * i;
    ...
}

我的编译器向我发出警告:

Warning 1 : lower precision in wider context: '*'

对于这一行。

这究竟意味着什么?我可以通过将 #define 更改为(明确使其成为无符号长整型)来使警告消失,

#define MULTIPLER 0x1000ul

但我想了解该警告。

I have the following code, for an embedded platform where an int is 16 bits and a long int is 32 bits:

#define MULTIPLIER 0x1000

static void my_function(uint16_t i, void *p)
{
    uint32_t start = MULTIPLIER * i;
    ...
}

My compiler gives me the warning:

Warning 1 : lower precision in wider context: '*'

for this line.

What does this really mean? I can make the warning go away by changing the #define to

#define MULTIPLER 0x1000ul

(explicitly making it an unsigned long) but I would like to understand the warning.

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

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

发布评论

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

评论(3

请帮我爱他 2024-09-18 19:19:52

对于表达式 MULTIPLIER * i,编译器从操作数类型中选择更高精度的类型。在第一种情况下,您给它两个 int,这意味着您将得到一个截断的结果,之后将被转换为 long int,但精度仍为 16 位。

在后一种情况下,您显式地使一个操作数变长,然后将结果写入具有相同精度的变量。

For the expression MULTIPLIER * i the compiler chooses a type of greater precision from types of operands. In the first case you give it two ints and that means you will have a truncated result, that will afterwards be converted to long int, but the precision will be still 16 bits.

in the latter case you explicitly make one of your operands long, and the result is then written to a variable of the same precision.

旧故 2024-09-18 19:19:50

我认为该警告意味着您可能需要 32 位结果,但由于 0x1000 的类型为 inti 的类型为 int16_t,表达式只是 int 类型,很可能会溢出并且不会给出您想要的结果。

I think the warning means that you probably want a 32-bit result, but since 0x1000 has type int and i has type int16_t, the expression is only type int and likely to overflow and not give you the results you want.

陪你搞怪i 2024-09-18 19:19:49

它警告您乘法将使用 16 位值进行,然后 16 位结果将转换为 32 位结果。这可能不是您所期望的(16 位乘法可能会溢出),因此会出现警告。

此论坛帖子涵盖了该问题

It's warning you that the multiplication will take place using 16-bit values, and the 16-bit result will then be converted to a 32-bit result. This might not be what you expect (the 16-bit multiplication might overflow), hence the warning.

This forum posting covers the issue

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