“在更广泛的背景下降低精度”是什么意思?警告实际上意味着什么?
对于嵌入式平台,我有以下代码,其中 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于表达式 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 twoint
s and that means you will have a truncated result, that will afterwards be converted tolong 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.
我认为该警告意味着您可能需要 32 位结果,但由于
0x1000
的类型为int
且i
的类型为int16_t
,表达式只是int
类型,很可能会溢出并且不会给出您想要的结果。I think the warning means that you probably want a 32-bit result, but since
0x1000
has typeint
andi
has typeint16_t
, the expression is only typeint
and likely to overflow and not give you the results you want.它警告您乘法将使用 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