Version Used:
Tested in Visual Studio 2017 Professional and Visual Studio 2019 Professional
Steps to Reproduce:
Write the following code:
float f1 = 0.00000000002f;
float f2 = 1 / f1;
double d = (float)f2;
Console.WriteLine(d);
Expected Behavior:
The final explicit cast is not considered redundant: As I'm reading this StackOverflow answer from Eric Lippert, the explicit cast ensures that the operation is carried out at lower precision, so that removing the cast will allow the optimizer the freedom to potentially change results. Indeed, I see the following results when compiling with x86 as my target platform:
With the cast | Without the cast
Debug 49999998976 | 49999998976
Release 49999998976 | 50000000199,7901
Now, according to the StackOverflow answer, this behavior is not part of the spec, so I can see how IDE0004 might rightfully pick it up, even if it's de facto wrong. When I'm still not entirely convinced, it's because the relevant part of the C# spec (ignoring the CLR spec) seems to only be concerned with individual floating point operations where in the example above, we have a few different ones.
Actual Behavior:
The cast is considered redundant.
Version Used:
Tested in Visual Studio 2017 Professional and Visual Studio 2019 Professional
Steps to Reproduce:
Write the following code:
Expected Behavior:
The final explicit cast is not considered redundant: As I'm reading this StackOverflow answer from Eric Lippert, the explicit cast ensures that the operation is carried out at lower precision, so that removing the cast will allow the optimizer the freedom to potentially change results. Indeed, I see the following results when compiling with x86 as my target platform:
Now, according to the StackOverflow answer, this behavior is not part of the spec, so I can see how IDE0004 might rightfully pick it up, even if it's de facto wrong. When I'm still not entirely convinced, it's because the relevant part of the C# spec (ignoring the CLR spec) seems to only be concerned with individual floating point operations where in the example above, we have a few different ones.
Actual Behavior:
The cast is considered redundant.