从int类型的函数调用转换为非匹配类型double [英] cast from function call of type int to non-matching type double

查看:111
本文介绍了从int类型的函数调用转换为非匹配类型double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




简短的问题:


为什么我会在这里收到警告的任何特殊原因:

(从int类型的函数调用转换为非匹配类型double)

xdouble =(double)rand()/(double)RAND_MAX;

xdouble属于双重类型。所以一切都应该投入到
类型double。然后我不明白为什么编译器(gcc)会抱怨。线路

应该创建一个介于0和1之间的(类型为双)随机数,并且这个警告会消失,这将是很好的...

最好的问候

Martin J?rgensen


-

----------- -------------------------------------------------- --------------

Martin J?rgensen的家 - http://www.martinjoergensen.dk

推荐答案

Martin J?rgensen< ho **** ******@hotmail.com>写道:
Martin J?rgensen <ho**********@hotmail.com> writes:


简短的问题:

为什么我在这里收到警告的任何特殊原因:
(从int类型的函数调用转换为非匹配类型double)

xdouble =(double)rand()/(double)RAND_MAX;

xdouble是当然是双重的。因此,所有内容都应该转换为
类型double。然后我不明白为什么编译器(gcc)会抱怨。
行应该创建一个介于0和1之间的(类型双)随机数。
这个警告消失会很好...
Hi,

Short question:

Any particular reason for why I''m getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MAX;
xdouble is ofcourse of type double. So everything should be casted to
type double. Then I don''t see why the compiler (gcc) complains. The
line should create a (type double) random number between 0 and 1 and
it would be nice to have this warning go away...




只有在设置了特定的编辑

选项时,我才会在gcc上显示此警告(我不记得了,-W ......)。如果你将一个函数调用转换为一个不同的类型,比如

double和int,它会告诉你
。我认为它可以防止int malloc()[它的空白*

malloc()在正确的标题中包含]

被强制转换为指针。 />

-

Ioan - Ciprian Tandau

tandau _at_ freeshell _dot_ org(希望现在还不晚)

(......它仍然有效......)



This warning should show up on gcc only when a specific compilation
option is set (I don''t remember it, something with -W...). It tells
you if you cast a function call to a type that''s different, like
double and int. I think it protects against int malloc() [it''s void *
malloc() in the proper header is included]
being cast to a pointer.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it''s not too late)
(... and that it still works...)


Martin J?rgensen写道:
Martin J?rgensen wrote:
<简短的问题:

为什么我在这里收到警告的任何特殊原因:
(从int类型的函数调用转换为非匹配类型double )

xdouble =(double)rand()/(double)RAND_MAX;

xdouble是double类型的当然。因此,所有内容都应该转换为
类型double。然后我不明白为什么编译器(gcc)会抱怨。该行
应该创建一个介于0和1之间的(类型为双)随机数,并且这个警告消失后会很好......
Hi,

Short question:

Any particular reason for why I''m getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MAX;
xdouble is ofcourse of type double. So everything should be casted to
type double. Then I don''t see why the compiler (gcc) complains. The line
should create a (type double) random number between 0 and 1 and it would
be nice to have this warning go away...




gcc有一个选项-Wbad-function-cast,只要

函数调用被强制转换为非匹配类型,它就会发出警告。要么不使用这个

选项,显式禁用是使用-Wno-bad-function-cast,要么更好

,丢失多余的演员:


xdouble = rand()/(double)RAND_MAX;


Robert Gamble



gcc has an option, -Wbad-function-cast, that will warn "whenever a
function call is cast to a non-matching type". Either don''t use this
option, explicitly disable is using -Wno-bad-function-cast, or better
yet, lose the superfluous cast:

xdouble = rand()/(double)RAND_MAX;

Robert Gamble


Robert Gamble写道:
Robert Gamble wrote:
Martin J?rgensen写道:
Martin J?rgensen wrote:


简短的问题:

为什么我在这里得到警告的任何特殊原因:
(从int类型的函数调用转换为非匹配类型double)

xdouble =(double) rand()/(double)RAND_MAX;

xdouble是double类型的。因此,所有内容都应该转换为
类型double。然后我不明白为什么编译器(gcc)会抱怨。该行
应该创建一个介于0和1之间的(类型为双)随机数,并且这个警告消失后会很好...

gcc有一个选项, -Wbad-function-cast,每当
函数调用被强制转换为非匹配类型时,它将发出警告。不要使用这个
选项,显式禁用是使用-Wno-bad-function-cast,或者更好
Hi,

Short question:

Any particular reason for why I''m getting a warning here:
(cast from function call of type int to non-matching type double)
xdouble = (double)rand()/(double)RAND_MAX;
xdouble is ofcourse of type double. So everything should be casted to
type double. Then I don''t see why the compiler (gcc) complains. The line
should create a (type double) random number between 0 and 1 and it would
be nice to have this warning go away...

gcc has an option, -Wbad-function-cast, that will warn "whenever a
function call is cast to a non-matching type". Either don''t use this
option, explicitly disable is using -Wno-bad-function-cast, or better




坏主意。我希望它能够捕捉到真正的错误,但不是那些我故意施展的b
.
但是,失去了多余的演员阵容:

xdouble = rand()/(double)RAND_MAX;



Bad idea. I want it to catch "real errors", but not those intentionally
casts I make.
yet, lose the superfluous cast:

xdouble = rand()/(double)RAND_MAX;




很棒(问题解决了)!我只是不明白:我不会抱怨如果我将
分成两个类型的double并将结果存储为double类型

- >演员。因此,rand()返回一个整数,它由double类型划分为
。这没有任何警告......?


究竟什么是非匹配类型?

祝你好运

Martin J?rgensen


-

------------------- -------------------------------------------------- ------

Martin J?rgensen的家 - http:/ /www.martinjoergensen.dk


这篇关于从int类型的函数调用转换为非匹配类型double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆