分割两个数字始终为零? [英] Dividing two numbers always equals zero?

查看:173
本文介绍了分割两个数字始终为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的XNA游戏,我想有我的普雷规模,它运行在屏幕上。要做到这一点,我使用的比例,以发现,真正的窗口相对于我的操场上传缩放的百分比。要做到这一点,我由虚拟宽度除以实际宽度:

In my Xna game, I am trying to have my playfield scale to the screen that it is running on. To do this, I use proportions to find the percent that the real window is scaled relative to my playfield. To do this, I divide the real width by the virtual width:

float _percent = _realViewport.Width / this._viewport.Width;



我总是得到0,不过,在_percent变量。我成立了一个调试停止点在我的该行代码,并进行分析的变量。 this._viewport.Width等于640,和_realViewport.Width等于1280。所以,用我的计算器,一千二百八分之六百四十○应该等于0.5,但是在我的代码我总是得到0出来,而不是0.5。为什么要这样做呢?

I always get 0, though, in the _percent variable. I set up a debug stop point at that line in my code, and analyzed the variables. this._viewport.Width equals 640, and _realViewport.Width equals 1280. So, using my calculator, 640 / 1280 SHOULD equal 0.5, but in my code I always get 0 out, instead of 0.5. Why is it doing this?

推荐答案

由于整数除法截断(的XNA's 视窗 类型有整数宽度和高度属性),和640 。/ 1280是0.5,它截断为零

Because integer division truncates (XNA's Viewport type has integer width and height properties), and 640 / 1280 is 0.5, which truncates to zero.

你要把你的价值观之一为float如果你想浮点除法:

Cast one of your values to a float if you want floating point division:

float _percent = _realViewport.Width / (float)this._viewport.Width;

这篇关于分割两个数字始终为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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