开发者

How can I deal with lots of precision warning flags?

The following simple code

int generated;
generated = (random() % 100) + 1;

gives a warning flag for loss of precision, 'long' to 'int', so I have been correcting it by rewriting the assignment code as

generated = ((int)random() % 100) + 开发者_C百科1;

Is this a valid way of correcting the problem or am I just covering up errors elsewhere?


You can also use long for your constants:

generated = (random() % 100L) + 1L;

Note that this assume that generated is long.

EDIT: Since generated is an int, you just need to cast it after you are done:

generated = (int)((random() % 100L) + 1L);


In your example you will be truncating the random() result too early. You need to cast the mod operation.

int generated;
generated = (int)(random() % 100) + 1;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜