2015-12-02 42 views
5

我有一個寬度888px和高度592px,寬高比爲3:2的圖像。Visual Studio 2015說'演員陣容是多餘的'。爲什麼?

下產生的1一個錯誤的值,因爲整數計算/截斷的作爲BitmapDecoder.PixelWidth和BitmapDecoder.PixelHeight均爲uint(無符號整數),和下面decoder是一個BitmapDecoder對象。

double aspectRatio = decoder.PixelWidth/decoder.PixelHeight;

下面給出了1.5的預期正確的值,但Visual Studio中說:「演員是多餘的」,但爲什麼呢?

double aspectRatio = (double)decoder.PixelWidth/(double)decoder.PixelHeight;

+1

你只需要一個(雙)鑄 - double/int = double。或者int/double = double。 – Dmitriy

+0

如果您用任何數字類型劃分雙精度型,結果將始終爲雙精度型。 –

+0

'double aspectRatio = static_cast (decoder.PixelWidth)/ decoder.PixelHeight;'應該足以使編譯器使用'PixelHeight'作爲double。 – Pixelchemist

回答

12

你只需要投一個的的uint的加倍給力的浮點運算,從而可以:

double aspectRatio = decoder.PixelWidth/(double)decoder.PixelHeight; 

或:

double aspectRatio = (double)decoder.PixelWidth/decoder.PixelHeight; 

就個人而言,我與後者一起去吧,但這是一個意見問題。

+1

得到它!一次演員就足夠了,但Visual Studio讓我脫離了軌道。我希望Visual Studio獨立完成第一個演員,並且只將第二個演員渲染爲絕對準確的不必要的。從這個意義上講,Visual Studio有點誤導,但可以解釋爲什麼。 – user2921851

2

只是爲了補充@ ChrisF的回答,您可以在IL代碼,其中一個投地double將產生一個轉換的兩個值很好地看到這一點:

IL_0013: stloc.0  // decoder 
IL_0014: ldloc.0  // decoder 
IL_0015: callvirt UserQuery+Decoder.get_PixelHeight 
IL_001A: conv.r.un // convert uint to float32 
IL_001B: conv.r8  // convert to float64 (double) 
IL_001C: ldloc.0  // decoder 
IL_001D: callvirt UserQuery+Decoder.get_PixelWidth 
IL_0022: conv.r.un // convert uint to float32 
IL_0023: conv.r8  // convert to float64 (double) 
相關問題