assignmnet to int form float / double [英] assignmnet to int form float/double

查看:114
本文介绍了assignmnet to int form float / double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的编译器警告关于int形式的赋值

float / double。

它是否会按照我的预期行事?

是否有洞穴/坑?

(当然除了范围和精度)

最好的方法是什么?

谢谢,

marc

My compiler warns about assignment to int form
float/double.
Will it nevertheless do what I expect?
Are there caveeats/pits?
(Apart from the range and the precision of course)
What would be the best way to do it?
thanks,
marc

推荐答案

Marc Schellens写道:
Marc Schellens wrote:
我的编译器警告关于int形式的赋值
float / double。
它是否仍然按照我的期望行事?


取决于您的期望。

是否有洞穴/坑?
(除了范围和精度当然)


再次,这取决于你的期望。

最好的方法是什么?
My compiler warns about assignment to int form
float/double.
Will it nevertheless do what I expect?
Depends on what you expect.
Are there caveeats/pits?
(Apart from the range and the precision of course)
Again, that depends on what you expect.
What would be the best way to do it?




如果你想删除警告,请做一个演员:


int main()

{

double d( 3); int i;


i = static_cast< int>(d);

}



If you want to remove the warning, do a cast:

int main()
{
double d(3); int i;

i = static_cast<int>(d);
}


Rolf Magnus写道:
Rolf Magnus wrote:
Marc Schellens写道:

Marc Schellens wrote:

我的编译器警告分配给int表格
float / double。
它会不会做我期望的呢?

取决于你的期望。
My compiler warns about assignment to int form
float/double.
Will it nevertheless do what I expect?

Depends on what you expect.




来吧,那很明显:

我想要一个接近浮点值的整数值(如果它在范围内)。

如果超出范围。那它会做什么? (最小的int,随机...)



Come on, thats obvious:
I want an integer value close to the float value (if its in the range).
If its out of range. Well what will it do? (largest-smallest int, random...)

是否有洞穴/坑?
(除了范围和精度当然)
Are there caveeats/pits?
(Apart from the range and the precision of course)



再次,这取决于你的期望。


Again, that depends on what you expect.




转换完成并且整个价值均匀

范围的int。



That the conversion is done and that homogenously over the whole value
range of the int.

最好的方法是什么?



如果你想要删除警告,做一个演员:

int main()
{
双d(3); int i;

i = static_cast< int>(d);
}


If you want to remove the warning, do a cast:

int main()
{
double d(3); int i;

i = static_cast<int>(d);
}




取决于它会做什么。也就是说,没有任何洞穴。


marc



Depends on what it will do. Ie there isn''t any caveeat.

marc


Marc Schellens写道:
Marc Schellens wrote:
Rolf Magnus写道:
Rolf Magnus wrote:
Marc Schellens写道:

Marc Schellens wrote:

我的编译器警告有关赋值给int形式
float / double 。
它会不会做我期望的事情?
My compiler warns about assignment to int form
float/double.
Will it nevertheless do what I expect?



取决于你的期望。


Depends on what you expect.



来吧,那显而易见:
我想要一个接近浮点值的整数值(如果它在
范围内)。
如果它超出范围。那它会做什么? (最小的int,
随机......)



Come on, thats obvious:
I want an integer value close to the float value (if its in the
range).
If its out of range. Well what will it do? (largest-smallest int,
random...)




明显的麻烦显而易见的是,对于其他人来说,这可能并不明显。人们倾向于对明显的东西有不同的想法,而不是什么。


要回答你的问题,转换会截断;小数部分是被丢弃的
。如果截断的值不能用整数类型表示

(超出范围),则结果是未定义的。即你将不得不自行完成

范围。


-

Peter van Merkerk

peter.van.merkerk(at)dse.nl



The trouble with "obvious" is what is obvious to you may not be obvious to
other people. People tend to have different ideas about what is obvious and
what is not.

To answer your question, the conversion truncates; the fractional part is
discarded. If the truncated value cannot be represented in the integer type
("out of range"), the result is undefined. I.e. you will have to do the
range checking yourself.

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl


这篇关于assignmnet to int form float / double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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