C ++整数除法 [英] c++ integer division
本文介绍了C ++整数除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有
SDL_Rect rect;
rect.x = 5; // rect.x is of type "Uint16"
int y = 11;
我想执行 rect.x/y
.
我想将结果作为浮点数,然后四舍五入到最接近的 int
.
I want to get the result as a floating point number and then round up to the nearest int
.
类似这样的东西:
float result = rect.x/y;
int rounded = ceil(result);
我应该怎么做?
推荐答案
投射 rect.x
或 y
浮动,然后进行除法.这将迫使整个除法运算在浮点中进行.
Cast either rect.x
or y
to float, and then do the division. This will force the entire division operation to take place in floating point.
float result = rect.x/(float)y;
int rounded = ceil(result);
这篇关于C ++整数除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文