Int或Unsigned Int浮动而不收到警告 [英] Int or Unsigned Int to float without getting a warning

查看:240
本文介绍了Int或Unsigned Int浮动而不收到警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我必须从无符号整数值转换为浮点数。例如,我的图形引擎接受一个 SetScale(float x,float y,float z)与浮动,我有一个对象,有一定的大小作为 unsigned int 。我想将unsigned int转换为一个float来正确缩放一个实体(这个例子非常具体,但我希望你得到的点)。

Sometimes I have to convert from an unsigned integer value to a float. For example, my graphics engine takes in a SetScale(float x, float y, float z) with floats and I have an object that has a certain size as an unsigned int. I want to convert the unsigned int to a float to properly scale an entity (the example is very specific but I hope you get the point).

现在,我通常做的是:

unsigned int size = 5;
float scale = float(size);
My3DObject->SetScale(scale , scale , scale);



。是否有更好的方法比使用 float()


注意:我无法触摸图形API。我必须使用SetScale()函数,它接受浮点数。此外,我也不能碰大小,它必须是一个unsigned int。我相信有很多其他的例子,同样的问题。

Notes: I cannot touch the graphics API. I have to use the SetScale() function which takes in floats. Moreover, I also cannot touch the size, it has to be an unsigned int. I am sure there are plenty of other examples with the same 'problem'. The above can be applied to any conversion that needs to be done and you as a programmer have little choice in the matter.


推荐答案

有一个从 unsigned int float 的隐式转换,不必要。

There is an implicit conversion from unsigned int to float, so the cast is strictly unnecessary.

如果您的编译器发出警告,那么使用转换来停止警告并没有什么问题。请注意,如果 size 非常大,它可能不能由 float 完全表示。

If your compiler issues a warning, then there isn't really anything wrong with using a cast to silence the warning. Just be aware that if size is very large it may not be representable exactly by a float.

这篇关于Int或Unsigned Int浮动而不收到警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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