V3041. The expression was implicitly cast from integer type to real type. Consider utilizing an explicit type cast to avoid the loss of a fractional part.

The analyzer detected a possible error that has to do with a result of integer division being implicitly cast to type float. Such cast may lead to inaccurate result.

Consider the following example:

int totalTime = 1700;
int operationNum = 900;
double averageTime = totalTime / operationNum;

The programmer expects the 'averageTime' variable to refer to value '1.888(8)', but because the division operation is applied to integer values and only then is the resulting value cast to type float, the variable will actually refer to '1.0'.

As in the previous case, there are two ways to fix the error.

One way is to change the variables' types:

double totalTime = 1700;
double operationNum = 900;
double averageTime = totalTime / operationNum;

Another way is to use explicit type cast.

int totalTime = 1700;
int operationNum = 900;
double averageTime = (double)(totalTime) / operationNum;

This diagnostic is classified as:

You can look at examples of errors detected by the V3041 diagnostic.


Bugs Found

Checked Projects
411
Collected Errors
14 123
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site. Learn More →
Accept