我的计算功能没有返回任何东西?

时间:2013-04-15 22:58:59

标签: c++

我的功能是0?

int ResizeLockRatio( int width, int height, int desired )
{
    int returnValue = ( height / width ) * desired;
    return returnValue; // returns 0?
}

int lockedRatioHeight = ResizeLockRatio( 1920, 1200, 1440 );

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

 int returnValue = ( height / width ) * desired;

你正在进行整数除法,有时会截断为0。

您正在传递width = 1920, height = 1200,因此在这种情况下,height/widht =1200/1920整数除法将截断为0。

编辑:您可以尝试先进行乘法,然后根据“Caption Obvlious”进行划分:

int returnValue = ( height * desired ) /width ;