| title | lnt-integer-float-division | |
|---|---|---|
| description | Reference for Visual Studio C++ IntelliSense Linter check lnt-integer-float-division. | |
| ms.date | 09/29/2021 | |
| f1_keywords |
|
|
| helpviewer_keywords |
|
|
| monikerRange | >=msvc-160 |
An integer division expression was implicitly cast to a floating-point type.
The division is carried out using integer operations, which truncates the fractional part before it's assigned to the floating-point result type. This check doesn't always indicate a bug, because sometimes the truncation is intentional.
::: moniker range=">=msvc-170"
In Visual Studio 2022, the lnt-integer-float-division check is controlled by the Truncated Division Result setting in the C/C++ Code Style options. For information on how to change this setting, see Configure the linter.
::: moniker-end ::: moniker range="msvc-160"
In Visual Studio 2019, the lnt-integer-float-division check is controlled by the Integer division converted to floating point setting in the C/C++ Code Style options. For information on how to change this setting, see Configure the linter.
::: moniker-end
float divide(int i, int j) {
return i / j; // Flagged: The integer division result is implicitly cast to float.
}
float half(int i) {
return i / 2; // Flagged: An integer literal is used.
}The fix the linter suggests is to explicitly cast one of the division operands to a floating-point type, so the division result isn't truncated. You can also use a floating-point literal instead of a cast.
float divide(int i, int j) {
return static_cast<float>(i) / j;
}
float half(int i) {
return i / 2.0;
}If the truncation is intentional, you can add an explicit cast to prevent the warning.
float int_divide(int i, int j) {
return static_cast<float>(i / j); // Not flagged because of the explicit cast.
}