2015-12-02 38 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) 
相关问题