Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2015 says the 'cast is redundant'. Why?

I have an image with width 888px and height 592px, with aspect ratio of width:height as 3:2.

The following produces a wrong value of 1, because of integer calculation/truncation as BitmapDecoder.PixelWidth and BitmapDecoder.PixelHeight are both uint (unsigned integer), and decoder below being a BitmapDecoder object.

double aspectRatio = decoder.PixelWidth / decoder.PixelHeight;

The following gives the expected correct value of 1.5, but Visual Studio says 'Cast is redundant', but why?

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

like image 787
user2921851 Avatar asked Dec 02 '15 10:12

user2921851


2 Answers

You only need to cast one of the uints to double to force the floating point arithmetic so either:

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

or:

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

Personally, I'd go with the latter, but it is a matter of opinion.

like image 143
ChrisF Avatar answered Oct 24 '22 02:10

ChrisF


Just to complement @ChrisF's answer, you can see this nicely in the IL code, where a single cast to double will yield a conversion for both values:

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)
like image 4
Yuval Itzchakov Avatar answered Oct 24 '22 02:10

Yuval Itzchakov