私が持っていると言う
SDL_Rect rect;
rect.x = 5; // rect.x is of type "Uint16"
int y = 11;
と私は操作を実行したいrect.x/y
。
結果を浮動小数点数として取得し、最も近いint
.
このようなもの:
float result = rect.x/y;
int rounded = ceil(result);
どうすればいいですか?
rect.x
orをfloatにキャストy
してから割り算をします。これにより、除算操作全体が浮動小数点で行われます。
float result = rect.x/(float)y;
int rounded = ceil(result);
これは、浮動小数点型がなくても実行できます。x
とが正の整数の場合y
、x を y で割った値を切り上げて です(x+y-1)/y
。
変数をキャストする必要があります。
float result = (float)rect.x / (float)y;
int rounded = ceil(result);